/platform/platform-api/src/com/intellij/ide/GeneralSettings.java

https://bitbucket.org/nbargnesi/idea · Java · 542 lines · 424 code · 82 blank · 36 comment · 35 complexity · afe876a44db793375a40a2b8462c53e4 MD5 · raw file

  1. /*
  2. * Copyright 2000-2011 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.intellij.ide;
  17. import com.intellij.openapi.application.ApplicationManager;
  18. import com.intellij.openapi.application.PathManager;
  19. import com.intellij.openapi.components.ExportableApplicationComponent;
  20. import com.intellij.openapi.util.NamedJDOMExternalizable;
  21. import com.intellij.openapi.util.SystemInfo;
  22. import com.intellij.openapi.vfs.CharsetToolkit;
  23. import com.intellij.openapi.vfs.encoding.EncodingManager;
  24. import org.intellij.lang.annotations.MagicConstant;
  25. import org.jdom.Element;
  26. import org.jetbrains.annotations.NonNls;
  27. import org.jetbrains.annotations.NotNull;
  28. import java.beans.PropertyChangeListener;
  29. import java.beans.PropertyChangeSupport;
  30. import java.io.File;
  31. import java.nio.charset.Charset;
  32. import java.util.List;
  33. public class GeneralSettings implements NamedJDOMExternalizable, ExportableApplicationComponent {
  34. @NonNls private static final String OPTION_INACTIVE_TIMEOUT = "inactiveTimeout";
  35. @NonNls public static final String PROP_INACTIVE_TIMEOUT = OPTION_INACTIVE_TIMEOUT;
  36. private static final int DEFAULT_INACTIVE_TIMEOUT = 15;
  37. @NonNls private String myBrowserPath;
  38. private boolean myShowTipsOnStartup = true;
  39. private int myLastTip = 0;
  40. private boolean myShowOccupiedMemory = false;
  41. private boolean myReopenLastProject = true;
  42. private boolean mySyncOnFrameActivation = true;
  43. private boolean mySaveOnFrameDeactivation = true;
  44. private boolean myAutoSaveIfInactive = false; // If true the IDEA automatically saves files if it is inactive for some seconds
  45. private int myInactiveTimeout; // Number of seconds of inactivity after which IDEA automatically saves all files
  46. private boolean myUseSafeWrite = true;
  47. private final PropertyChangeSupport myPropertyChangeSupport;
  48. private boolean myUseDefaultBrowser = true;
  49. private boolean myConfirmExtractFiles = true;
  50. private String myLastProjectLocation;
  51. private boolean mySearchInBackground;
  52. private boolean myConfirmExit = true;
  53. private int myConfirmOpenNewProject = OPEN_PROJECT_ASK;
  54. @NonNls private static final String ELEMENT_OPTION = "option";
  55. @NonNls private static final String ATTRIBUTE_NAME = "name";
  56. @NonNls private static final String ATTRIBUTE_VALUE = "value";
  57. @NonNls private static final String OPTION_BROWSER_PATH = "browserPath";
  58. @NonNls private static final String OPTION_LAST_TIP = "lastTip";
  59. @NonNls private static final String OPTION_SHOW_TIPS_ON_STARTUP = "showTipsOnStartup";
  60. @NonNls private static final String OPTION_SHOW_OCCUPIED_MEMORY = "showOccupiedMemory";
  61. @NonNls private static final String OPTION_REOPEN_LAST_PROJECT = "reopenLastProject";
  62. @NonNls private static final String OPTION_AUTO_SYNC_FILES = "autoSyncFiles";
  63. @NonNls private static final String OPTION_AUTO_SAVE_FILES = "autoSaveFiles";
  64. @NonNls private static final String OPTION_AUTO_SAVE_IF_INACTIVE = "autoSaveIfInactive";
  65. @NonNls private static final String OPTION_USE_SAFE_WRITE = "useSafeWrite";
  66. @Deprecated
  67. @NonNls private static final String OPTION_CHARSET = "charset";
  68. @Deprecated
  69. @NonNls private static final String OPTION_UTFGUESSING = "UTFGuessing";
  70. @NonNls private static final String OPTION_USE_DEFAULT_BROWSER = "useDefaultBrowser";
  71. @NonNls private static final String OPTION_CONFIRM_EXTRACT_FILES = "confirmExtractFiles";
  72. @NonNls private static final String OPTION_USE_CYCLIC_BUFFER = "useCyclicBuffer";
  73. @NonNls private static final String OPTION_SEARCH_IN_BACKGROUND = "searchInBackground";
  74. @NonNls private static final String OPTION_CONFIRM_EXIT = "confirmExit";
  75. @NonNls private static final String OPTION_CONFIRM_OPEN_NEW_PROJECT = "confirmOpenNewProject2";
  76. @NonNls private static final String OPTION_CYCLIC_BUFFER_SIZE = "cyclicBufferSize";
  77. @NonNls private static final String OPTION_LAST_PROJECT_LOCATION = "lastProjectLocation";
  78. @Deprecated
  79. private Charset myCharset;
  80. @Deprecated
  81. private boolean myUseUTFGuessing;
  82. @Deprecated
  83. private boolean oldCharsetSettingsHaveBeenRead;
  84. public static GeneralSettings getInstance(){
  85. return ApplicationManager.getApplication().getComponent(GeneralSettings.class);
  86. }
  87. public GeneralSettings() {
  88. myInactiveTimeout=DEFAULT_INACTIVE_TIMEOUT;
  89. if (SystemInfo.isWindows) {
  90. myBrowserPath = "C:\\Program Files\\Internet Explorer\\IExplore.exe";
  91. }
  92. else if (SystemInfo.isMac) {
  93. myBrowserPath = "open";
  94. }
  95. else {
  96. myBrowserPath = "";
  97. }
  98. myPropertyChangeSupport = new PropertyChangeSupport(this);
  99. }
  100. public void addPropertyChangeListener(PropertyChangeListener listener){
  101. myPropertyChangeSupport.addPropertyChangeListener(listener);
  102. }
  103. public void removePropertyChangeListener(PropertyChangeListener listener){
  104. myPropertyChangeSupport.removePropertyChangeListener(listener);
  105. }
  106. public void initComponent() { }
  107. public void disposeComponent() { }
  108. public String getBrowserPath() {
  109. return myBrowserPath;
  110. }
  111. /**
  112. * @return a path pointing to a directory where the last project was created or null if not available
  113. */
  114. public String getLastProjectLocation() {
  115. return myLastProjectLocation;
  116. }
  117. public void setLastProjectLocation(String lastProjectLocation) {
  118. myLastProjectLocation = lastProjectLocation;
  119. }
  120. public void setBrowserPath(String browserPath) {
  121. myBrowserPath = browserPath;
  122. }
  123. public boolean showTipsOnStartup() {
  124. return myShowTipsOnStartup;
  125. }
  126. public void setShowTipsOnStartup(boolean b) {
  127. myShowTipsOnStartup = b;
  128. }
  129. public int getLastTip() {
  130. return myLastTip;
  131. }
  132. public void setLastTip(int i) {
  133. myLastTip = i;
  134. }
  135. public boolean isShowOccupiedMemory() {
  136. return myShowOccupiedMemory;
  137. }
  138. public boolean isReopenLastProject() {
  139. return myReopenLastProject;
  140. }
  141. public void setReopenLastProject(boolean reopenLastProject) {
  142. myReopenLastProject = reopenLastProject;
  143. }
  144. public boolean isSyncOnFrameActivation() {
  145. return mySyncOnFrameActivation;
  146. }
  147. public void setSyncOnFrameActivation(boolean syncOnFrameActivation) {
  148. mySyncOnFrameActivation = syncOnFrameActivation;
  149. }
  150. public boolean isSaveOnFrameDeactivation() {
  151. return mySaveOnFrameDeactivation;
  152. }
  153. public void setSaveOnFrameDeactivation(boolean saveOnFrameDeactivation) {
  154. mySaveOnFrameDeactivation = saveOnFrameDeactivation;
  155. }
  156. /**
  157. * @return <code>true</code> if IDEA saves all files after "idle" timeout.
  158. */
  159. public boolean isAutoSaveIfInactive(){
  160. return myAutoSaveIfInactive;
  161. }
  162. public void setAutoSaveIfInactive(boolean autoSaveIfInactive) {
  163. myAutoSaveIfInactive = autoSaveIfInactive;
  164. }
  165. /**
  166. * @return timeout in seconds after which IDEA saves all files if there was no user activity.
  167. * The method always return non positive (more then zero) value.
  168. */
  169. public int getInactiveTimeout(){
  170. return myInactiveTimeout;
  171. }
  172. public void setInactiveTimeout(int inactiveTimeout) {
  173. int oldInactiveTimeout = myInactiveTimeout;
  174. myInactiveTimeout = inactiveTimeout;
  175. myPropertyChangeSupport.firePropertyChange(
  176. PROP_INACTIVE_TIMEOUT, Integer.valueOf(oldInactiveTimeout), Integer.valueOf(inactiveTimeout)
  177. );
  178. }
  179. public boolean isUseSafeWrite() {
  180. return myUseSafeWrite;
  181. }
  182. public void setUseSafeWrite(final boolean useSafeWrite) {
  183. myUseSafeWrite = useSafeWrite;
  184. }
  185. //todo use DefaultExternalizer
  186. public void readExternal(Element parentNode) {
  187. boolean safeWriteSettingRead = false;
  188. List children = parentNode.getChildren(ELEMENT_OPTION);
  189. for (final Object aChildren : children) {
  190. Element element = (Element)aChildren;
  191. String name = element.getAttributeValue(ATTRIBUTE_NAME);
  192. String value = element.getAttributeValue(ATTRIBUTE_VALUE);
  193. if (OPTION_BROWSER_PATH.equals(name)) {
  194. myBrowserPath = value;
  195. }
  196. if (OPTION_LAST_TIP.equals(name)) {
  197. try {
  198. myLastTip = Integer.parseInt(value);
  199. }
  200. catch (NumberFormatException ex) {
  201. myLastTip = 0;
  202. }
  203. }
  204. if (OPTION_SHOW_TIPS_ON_STARTUP.equals(name)) {
  205. try {
  206. myShowTipsOnStartup = Boolean.valueOf(value).booleanValue();
  207. }
  208. catch (Exception ex) {
  209. myShowTipsOnStartup = true;
  210. }
  211. }
  212. if (OPTION_SHOW_OCCUPIED_MEMORY.equals(name)) {
  213. try {
  214. myShowOccupiedMemory = Boolean.valueOf(value).booleanValue();
  215. }
  216. catch (Exception ex) {
  217. myShowOccupiedMemory = false;
  218. }
  219. }
  220. if (OPTION_REOPEN_LAST_PROJECT.equals(name)) {
  221. try {
  222. myReopenLastProject = Boolean.valueOf(value).booleanValue();
  223. }
  224. catch (Exception ex) {
  225. myReopenLastProject = true;
  226. }
  227. }
  228. if (OPTION_AUTO_SYNC_FILES.equals(name)) {
  229. try {
  230. mySyncOnFrameActivation = Boolean.valueOf(value).booleanValue();
  231. }
  232. catch (Exception ex) {
  233. mySyncOnFrameActivation = true;
  234. }
  235. }
  236. if (OPTION_AUTO_SAVE_FILES.equals(name)) {
  237. try {
  238. mySaveOnFrameDeactivation = Boolean.valueOf(value).booleanValue();
  239. }
  240. catch (Exception ex) {
  241. mySaveOnFrameDeactivation = true;
  242. }
  243. }
  244. if (OPTION_AUTO_SAVE_IF_INACTIVE.equals(name) && value != null) {
  245. myAutoSaveIfInactive = Boolean.valueOf(value).booleanValue();
  246. }
  247. if (OPTION_INACTIVE_TIMEOUT.equals(name)) {
  248. try {
  249. int inactiveTimeout = Integer.parseInt(value);
  250. if (inactiveTimeout > 0) {
  251. myInactiveTimeout = inactiveTimeout;
  252. }
  253. }
  254. catch (Exception ignored) {
  255. }
  256. }
  257. if (OPTION_USE_SAFE_WRITE.equals(name) && value != null) {
  258. myUseSafeWrite = Boolean.valueOf(value).booleanValue();
  259. safeWriteSettingRead = true;
  260. }
  261. if (OPTION_CHARSET.equals(name)) {
  262. //for migration
  263. myCharset = CharsetToolkit.forName(value);
  264. oldCharsetSettingsHaveBeenRead = true;
  265. }
  266. if (OPTION_UTFGUESSING.equals(name)) {
  267. myUseUTFGuessing = Boolean.valueOf(value).booleanValue();
  268. oldCharsetSettingsHaveBeenRead = true;
  269. }
  270. if (OPTION_USE_DEFAULT_BROWSER.equals(name)) {
  271. try {
  272. myUseDefaultBrowser = Boolean.valueOf(value).booleanValue();
  273. }
  274. catch (Exception ex) {
  275. myUseDefaultBrowser = true;
  276. }
  277. }
  278. if (OPTION_CONFIRM_EXTRACT_FILES.equals(name)) {
  279. try {
  280. myConfirmExtractFiles = Boolean.valueOf(value).booleanValue();
  281. }
  282. catch (Exception ex) {
  283. myConfirmExtractFiles = true;
  284. }
  285. }
  286. if (OPTION_SEARCH_IN_BACKGROUND.equals(name)) {
  287. try {
  288. mySearchInBackground = Boolean.valueOf(value).booleanValue();
  289. }
  290. catch (Exception ex) {
  291. mySearchInBackground = false;
  292. }
  293. }
  294. if (OPTION_CONFIRM_EXIT.equals(name)) {
  295. try {
  296. myConfirmExit = Boolean.valueOf(value).booleanValue();
  297. }
  298. catch (Exception ex) {
  299. myConfirmExit = false;
  300. }
  301. }
  302. if (OPTION_CONFIRM_OPEN_NEW_PROJECT.equals(name)) {
  303. try {
  304. myConfirmOpenNewProject = Integer.valueOf(value).intValue();
  305. }
  306. catch (Exception ex) {
  307. myConfirmOpenNewProject = OPEN_PROJECT_ASK;
  308. }
  309. }
  310. if (OPTION_LAST_PROJECT_LOCATION.equals(name)) {
  311. try {
  312. myLastProjectLocation = value;
  313. }
  314. catch (Exception ex) {
  315. myLastProjectLocation = null;
  316. }
  317. }
  318. }
  319. if (!safeWriteSettingRead && "true".equals(System.getProperty("idea.no.safe.write"))) {
  320. myUseSafeWrite = false;
  321. }
  322. }
  323. public void writeExternal(Element parentNode) {
  324. if (myBrowserPath != null) {
  325. Element element = new Element(ELEMENT_OPTION);
  326. element.setAttribute(ATTRIBUTE_NAME, OPTION_BROWSER_PATH);
  327. element.setAttribute(ATTRIBUTE_VALUE, myBrowserPath);
  328. parentNode.addContent(element);
  329. }
  330. Element optionElement = new Element(ELEMENT_OPTION);
  331. optionElement.setAttribute(ATTRIBUTE_NAME, OPTION_LAST_TIP);
  332. optionElement.setAttribute(ATTRIBUTE_VALUE, Integer.toString(myLastTip));
  333. parentNode.addContent(optionElement);
  334. optionElement = new Element(ELEMENT_OPTION);
  335. optionElement.setAttribute(ATTRIBUTE_NAME, OPTION_SHOW_TIPS_ON_STARTUP);
  336. optionElement.setAttribute(ATTRIBUTE_VALUE, Boolean.toString(myShowTipsOnStartup));
  337. parentNode.addContent(optionElement);
  338. optionElement = new Element(ELEMENT_OPTION);
  339. optionElement.setAttribute(ATTRIBUTE_NAME, OPTION_SHOW_OCCUPIED_MEMORY);
  340. optionElement.setAttribute(ATTRIBUTE_VALUE, Boolean.toString(myShowOccupiedMemory));
  341. parentNode.addContent(optionElement);
  342. optionElement = new Element(ELEMENT_OPTION);
  343. optionElement.setAttribute(ATTRIBUTE_NAME, OPTION_REOPEN_LAST_PROJECT);
  344. optionElement.setAttribute(ATTRIBUTE_VALUE, Boolean.toString(myReopenLastProject));
  345. parentNode.addContent(optionElement);
  346. optionElement = new Element(ELEMENT_OPTION);
  347. optionElement.setAttribute(ATTRIBUTE_NAME, OPTION_AUTO_SYNC_FILES);
  348. optionElement.setAttribute(ATTRIBUTE_VALUE, Boolean.toString(mySyncOnFrameActivation));
  349. parentNode.addContent(optionElement);
  350. optionElement = new Element(ELEMENT_OPTION);
  351. optionElement.setAttribute(ATTRIBUTE_NAME, OPTION_AUTO_SAVE_FILES);
  352. optionElement.setAttribute(ATTRIBUTE_VALUE, Boolean.toString(mySaveOnFrameDeactivation));
  353. parentNode.addContent(optionElement);
  354. optionElement = new Element(ELEMENT_OPTION);
  355. optionElement.setAttribute(ATTRIBUTE_NAME,OPTION_AUTO_SAVE_IF_INACTIVE);
  356. optionElement.setAttribute(ATTRIBUTE_VALUE,(myAutoSaveIfInactive?Boolean.TRUE:Boolean.FALSE).toString());
  357. parentNode.addContent(optionElement);
  358. optionElement = new Element(ELEMENT_OPTION);
  359. optionElement.setAttribute(ATTRIBUTE_NAME,OPTION_INACTIVE_TIMEOUT);
  360. optionElement.setAttribute(ATTRIBUTE_VALUE,Integer.toString(myInactiveTimeout));
  361. parentNode.addContent(optionElement);
  362. optionElement = new Element(ELEMENT_OPTION);
  363. optionElement.setAttribute(ATTRIBUTE_NAME, OPTION_USE_SAFE_WRITE);
  364. optionElement.setAttribute(ATTRIBUTE_VALUE, (myUseSafeWrite ? Boolean.TRUE : Boolean.FALSE).toString());
  365. parentNode.addContent(optionElement);
  366. optionElement = new Element(ELEMENT_OPTION);
  367. optionElement.setAttribute(ATTRIBUTE_NAME, OPTION_USE_DEFAULT_BROWSER);
  368. optionElement.setAttribute(ATTRIBUTE_VALUE, Boolean.toString(myUseDefaultBrowser));
  369. parentNode.addContent(optionElement);
  370. optionElement = new Element(ELEMENT_OPTION);
  371. optionElement.setAttribute(ATTRIBUTE_NAME, OPTION_CONFIRM_EXTRACT_FILES);
  372. optionElement.setAttribute(ATTRIBUTE_VALUE, Boolean.toString(myConfirmExtractFiles));
  373. parentNode.addContent(optionElement);
  374. optionElement = new Element(ELEMENT_OPTION);
  375. optionElement.setAttribute(ATTRIBUTE_NAME, OPTION_SEARCH_IN_BACKGROUND);
  376. optionElement.setAttribute(ATTRIBUTE_VALUE, Boolean.toString(mySearchInBackground));
  377. parentNode.addContent(optionElement);
  378. optionElement = new Element(ELEMENT_OPTION);
  379. optionElement.setAttribute(ATTRIBUTE_NAME, OPTION_CONFIRM_EXIT);
  380. optionElement.setAttribute(ATTRIBUTE_VALUE, Boolean.toString(myConfirmExit));
  381. parentNode.addContent(optionElement);
  382. optionElement = new Element(ELEMENT_OPTION);
  383. optionElement.setAttribute(ATTRIBUTE_NAME, OPTION_CONFIRM_OPEN_NEW_PROJECT);
  384. optionElement.setAttribute(ATTRIBUTE_VALUE, Integer.toString(myConfirmOpenNewProject));
  385. parentNode.addContent(optionElement);
  386. if (myLastProjectLocation != null) {
  387. optionElement = new Element(ELEMENT_OPTION);
  388. optionElement.setAttribute(ATTRIBUTE_NAME, OPTION_LAST_PROJECT_LOCATION);
  389. optionElement.setAttribute(ATTRIBUTE_VALUE, myLastProjectLocation);
  390. parentNode.addContent(optionElement);
  391. }
  392. }
  393. public String getExternalFileName() {
  394. return "ide.general";
  395. }
  396. @NotNull
  397. public File[] getExportFiles() {
  398. return new File[]{PathManager.getOptionsFile(this)};
  399. }
  400. @NotNull
  401. public String getPresentableName() {
  402. return IdeBundle.message("general.settings");
  403. }
  404. @NotNull
  405. public String getComponentName() {
  406. return "GeneralSettings";
  407. }
  408. public boolean isUseDefaultBrowser() {
  409. return myUseDefaultBrowser;
  410. }
  411. public void setUseDefaultBrowser(boolean value) {
  412. myUseDefaultBrowser = value;
  413. }
  414. public boolean isConfirmExtractFiles() {
  415. return myConfirmExtractFiles;
  416. }
  417. public void setConfirmExtractFiles(boolean value) {
  418. myConfirmExtractFiles = value;
  419. }
  420. public boolean isConfirmExit() {
  421. return myConfirmExit;
  422. }
  423. public void setConfirmExit(boolean confirmExit) {
  424. myConfirmExit = confirmExit;
  425. }
  426. @MagicConstant(intValues = {OPEN_PROJECT_ASK, OPEN_PROJECT_NEW_WINDOW, OPEN_PROJECT_SAME_WINDOW})
  427. @interface OpenNewProjectOption {}
  428. /**
  429. * @return
  430. * <ul>
  431. * <li>{@link GeneralSettings#OPEN_PROJECT_NEW_WINDOW} if new project should be opened in new window
  432. * <li>{@link GeneralSettings#OPEN_PROJECT_SAME_WINDOW} if new project should be opened in same window
  433. * <li>{@link GeneralSettings#OPEN_PROJECT_ASK} if a confirmation dialog should be shown
  434. * </ul>
  435. */
  436. @OpenNewProjectOption
  437. public int getConfirmOpenNewProject() {
  438. return myConfirmOpenNewProject;
  439. }
  440. public void setConfirmOpenNewProject(@OpenNewProjectOption int confirmOpenNewProject) {
  441. myConfirmOpenNewProject = confirmOpenNewProject;
  442. }
  443. public static final int OPEN_PROJECT_ASK = -1;
  444. public static final int OPEN_PROJECT_NEW_WINDOW = 0;
  445. public static final int OPEN_PROJECT_SAME_WINDOW = 1;
  446. public boolean isSearchInBackground() {
  447. return mySearchInBackground;
  448. }
  449. public void setSearchInBackground(final boolean searchInBackground) {
  450. mySearchInBackground = searchInBackground;
  451. }
  452. // returns true if something has been migrated
  453. public boolean migrateCharsetSettingsTo(EncodingManager encodingProjectManager) {
  454. if (oldCharsetSettingsHaveBeenRead) {
  455. encodingProjectManager.setEncoding(null, myCharset);
  456. encodingProjectManager.setUseUTFGuessing(null, myUseUTFGuessing);
  457. }
  458. return oldCharsetSettingsHaveBeenRead;
  459. }
  460. }