Preferences.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. class Preferences {
  2. private static Ctor = (() => {
  3. new Preferences();
  4. })();
  5. private fileSystem: Atomic.FileSystem;
  6. private static instance: Preferences;
  7. private _prefs: PreferencesFormat;
  8. constructor() {
  9. this.fileSystem = Atomic.getFileSystem();
  10. Preferences.instance = this;
  11. }
  12. registerRecentProject(path: string): void {
  13. var index = this._prefs.recentProjects.indexOf(path);
  14. if (index >= 0) {
  15. this._prefs.recentProjects.splice(index, 1);
  16. }
  17. this._prefs.recentProjects.unshift(path);
  18. this.updateRecentProjects();
  19. }
  20. unRegisterRecentProject(path: string): void {
  21. var index = this._prefs.recentProjects.indexOf(path);
  22. if (index >= 0) {
  23. this._prefs.recentProjects.splice(index, 1);
  24. }
  25. this.updateRecentProjects();
  26. }
  27. updateRecentProjects(): void {
  28. for (var i in this._prefs.recentProjects) {
  29. var path = this._prefs.recentProjects[i];
  30. if (!this.fileSystem.exists(path)) {
  31. this._prefs.recentProjects.splice(i, 1);
  32. }
  33. }
  34. }
  35. deleteRecentProjects(): void {
  36. this._prefs.recentProjects.length = 0;
  37. }
  38. getPreferencesFullPath(): string {
  39. var filePath = this.fileSystem.getAppPreferencesDir("AtomicEditor", "Preferences");
  40. filePath += "prefs.json";
  41. return filePath;
  42. }
  43. read(): void {
  44. var filePath = this.getPreferencesFullPath();
  45. var jsonFile;
  46. //check if file doesn't exists, create an empty JSON file
  47. if (!this.fileSystem.fileExists(filePath)) {
  48. jsonFile = new Atomic.File(filePath, Atomic.FILE_WRITE);
  49. jsonFile.writeString("{}");
  50. jsonFile.close();
  51. }
  52. //Read file
  53. jsonFile = new Atomic.File(filePath, Atomic.FILE_READ);
  54. var prefs = <PreferencesFormat> JSON.parse(jsonFile.readText());
  55. if (prefs) {
  56. if(!prefs.recentProjects) prefs.recentProjects = [""];
  57. this._prefs = prefs;
  58. }
  59. }
  60. write(): void {
  61. var filePath = this.getPreferencesFullPath();
  62. var jsonFile = new Atomic.File(filePath, Atomic.FILE_WRITE);
  63. if (!jsonFile.isOpen()) return;
  64. var graphics = Atomic.getGraphics();
  65. var pos, width, height;
  66. if (graphics && !graphics.getFullscreen()) {
  67. pos = graphics.getWindowPosition();
  68. width = graphics.getWidth();
  69. height = graphics.getHeight();
  70. }
  71. this._prefs.window = { x: pos[0], y: pos[1], width: width, height: height };
  72. jsonFile.writeString(JSON.stringify(this._prefs, null, 2));
  73. }
  74. get recentProjects(): [string] {
  75. return this._prefs.recentProjects;
  76. }
  77. get androidSDKPath(): string {
  78. return Atomic.addTrailingSlash(this._prefs.androidSDKPath);
  79. }
  80. set androidSDKPath(path: string) {
  81. this._prefs.androidSDKPath = path;
  82. this.write()
  83. }
  84. get jdkRootPath(): string {
  85. return Atomic.addTrailingSlash(this._prefs.jdkRootPath);
  86. }
  87. set jdkRootPath(path: string) {
  88. this._prefs.jdkRootPath = path;
  89. this.write();
  90. }
  91. get antPath(): string {
  92. return Atomic.addTrailingSlash(this._prefs.antPath);
  93. }
  94. set antPath(path: string) {
  95. this._prefs.antPath = path;
  96. this.write();
  97. }
  98. static getInstance(): Preferences {
  99. return Preferences.instance;
  100. }
  101. }
  102. class PreferencesFormat {
  103. recentProjects: [string];
  104. androidSDKPath: string;
  105. jdkRootPath: string;
  106. antPath: string;
  107. window: { x: number, y: number, width: number, height: number };
  108. }
  109. export = Preferences;