You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserPreferencesStore.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v 2.0
  7. * which accompanies this distribution and is available at
  8. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * Helen Hawkins Converted to new interface (bug 148190)
  13. * ******************************************************************/
  14. package org.aspectj.ajde.ui.internal;
  15. import java.io.File;
  16. import java.io.FileInputStream;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import java.util.ArrayList;
  20. import java.util.List;
  21. import java.util.Properties;
  22. import java.util.StringTokenizer;
  23. import org.aspectj.ajde.Ajde;
  24. import org.aspectj.ajde.ui.UserPreferencesAdapter;
  25. import org.aspectj.bridge.IMessage;
  26. import org.aspectj.bridge.Message;
  27. import org.aspectj.util.LangUtil;
  28. public class UserPreferencesStore implements UserPreferencesAdapter {
  29. public static final String FILE_NAME = "/.ajbrowser";
  30. private static final String VALUE_SEP = ";";
  31. private Properties properties = new Properties();
  32. private boolean persist = true;
  33. public UserPreferencesStore() {
  34. this(true);
  35. }
  36. public UserPreferencesStore(boolean loadDefault) {
  37. persist = loadDefault;
  38. if (persist) {
  39. loadProperties(getPropertiesFilePath());
  40. }
  41. }
  42. @Override
  43. public String getProjectPreference(String name) {
  44. return properties.getProperty(name);
  45. }
  46. @Override
  47. public List<String> getProjectMultivalPreference(String name) {
  48. List<String> values = new ArrayList<>();
  49. String valuesString = properties.getProperty(name);
  50. if (valuesString != null && !valuesString.trim().equals("")) {
  51. StringTokenizer st = new StringTokenizer(valuesString, VALUE_SEP);
  52. while (st.hasMoreTokens()) {
  53. values.add(st.nextToken());
  54. }
  55. }
  56. return values;
  57. }
  58. @Override
  59. public void setProjectPreference(String name, String value) {
  60. properties.setProperty(name, value);
  61. saveProperties();
  62. }
  63. @Override
  64. public void setProjectMultivalPreference(String name, List values) {
  65. String valuesString = "";
  66. for (Object value : values) {
  67. valuesString += (String) value + ';';
  68. }
  69. properties.setProperty(name, valuesString);
  70. saveProperties();
  71. }
  72. public static String getPropertiesFilePath() {
  73. String path = System.getProperty("user.home");
  74. if (path == null) {
  75. path = ".";
  76. }
  77. return path + FILE_NAME;
  78. }
  79. @Override
  80. public String getGlobalPreference(String name) {
  81. return getProjectPreference(name);
  82. }
  83. @Override
  84. public List getGlobalMultivalPreference(String name) {
  85. return getProjectMultivalPreference(name);
  86. }
  87. @Override
  88. public void setGlobalPreference(String name, String value) {
  89. setProjectPreference(name, value);
  90. }
  91. @Override
  92. public void setGlobalMultivalPreference(String name, List values) {
  93. setProjectMultivalPreference(name, values);
  94. }
  95. private void loadProperties(String path) {
  96. if (LangUtil.isEmpty(path)) {
  97. return;
  98. }
  99. File file = new File(path);
  100. if (!file.canRead()) {
  101. return;
  102. }
  103. FileInputStream in = null;
  104. try {
  105. path = getPropertiesFilePath();
  106. in = new FileInputStream(file);
  107. properties.load(in);
  108. } catch (IOException ioe) {
  109. Message msg = new Message("Error reading properties from " + path,IMessage.ERROR,ioe,null);
  110. Ajde.getDefault().getMessageHandler().handleMessage(msg);
  111. } finally {
  112. if (null != in) {
  113. try {
  114. in.close();
  115. } catch (IOException e) {
  116. // ignore
  117. }
  118. }
  119. }
  120. }
  121. public void saveProperties() {
  122. if (!persist) return;
  123. FileOutputStream out = null;
  124. String path = null;
  125. try {
  126. path = getPropertiesFilePath();
  127. out = new FileOutputStream(path);
  128. properties.store(out, "AJDE Settings");
  129. } catch (IOException ioe) {
  130. Message msg = new Message("Error writing properties to " + path,IMessage.ERROR,ioe,null);
  131. Ajde.getDefault().getMessageHandler().handleMessage(msg);
  132. } finally {
  133. if (null != out) {
  134. try {
  135. out.close();
  136. } catch (IOException e) {
  137. // ignore
  138. }
  139. }
  140. }
  141. }
  142. }