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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  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.Iterator;
  21. import java.util.List;
  22. import java.util.Properties;
  23. import java.util.StringTokenizer;
  24. import org.aspectj.ajde.Ajde;
  25. import org.aspectj.ajde.ui.UserPreferencesAdapter;
  26. import org.aspectj.bridge.IMessage;
  27. import org.aspectj.bridge.Message;
  28. import org.aspectj.util.LangUtil;
  29. public class UserPreferencesStore implements UserPreferencesAdapter {
  30. public static final String FILE_NAME = "/.ajbrowser";
  31. private static final String VALUE_SEP = ";";
  32. private Properties properties = new Properties();
  33. private boolean persist = true;
  34. public UserPreferencesStore() {
  35. this(true);
  36. }
  37. public UserPreferencesStore(boolean loadDefault) {
  38. persist = loadDefault;
  39. if (persist) {
  40. loadProperties(getPropertiesFilePath());
  41. }
  42. }
  43. @Override
  44. public String getProjectPreference(String name) {
  45. return properties.getProperty(name);
  46. }
  47. @Override
  48. public List<String> getProjectMultivalPreference(String name) {
  49. List<String> values = new ArrayList<>();
  50. String valuesString = properties.getProperty(name);
  51. if (valuesString != null && !valuesString.trim().equals("")) {
  52. StringTokenizer st = new StringTokenizer(valuesString, VALUE_SEP);
  53. while (st.hasMoreTokens()) {
  54. values.add(st.nextToken());
  55. }
  56. }
  57. return values;
  58. }
  59. @Override
  60. public void setProjectPreference(String name, String value) {
  61. properties.setProperty(name, value);
  62. saveProperties();
  63. }
  64. @Override
  65. public void setProjectMultivalPreference(String name, List values) {
  66. String valuesString = "";
  67. for (Iterator it = values.iterator(); it.hasNext(); ) {
  68. valuesString += (String)it.next() + ';';
  69. }
  70. properties.setProperty(name, valuesString);
  71. saveProperties();
  72. }
  73. public static String getPropertiesFilePath() {
  74. String path = System.getProperty("user.home");
  75. if (path == null) {
  76. path = ".";
  77. }
  78. return path + FILE_NAME;
  79. }
  80. @Override
  81. public String getGlobalPreference(String name) {
  82. return getProjectPreference(name);
  83. }
  84. @Override
  85. public List getGlobalMultivalPreference(String name) {
  86. return getProjectMultivalPreference(name);
  87. }
  88. @Override
  89. public void setGlobalPreference(String name, String value) {
  90. setProjectPreference(name, value);
  91. }
  92. @Override
  93. public void setGlobalMultivalPreference(String name, List values) {
  94. setProjectMultivalPreference(name, values);
  95. }
  96. private void loadProperties(String path) {
  97. if (LangUtil.isEmpty(path)) {
  98. return;
  99. }
  100. File file = new File(path);
  101. if (!file.canRead()) {
  102. return;
  103. }
  104. FileInputStream in = null;
  105. try {
  106. path = getPropertiesFilePath();
  107. in = new FileInputStream(file);
  108. properties.load(in);
  109. } catch (IOException ioe) {
  110. Message msg = new Message("Error reading properties from " + path,IMessage.ERROR,ioe,null);
  111. Ajde.getDefault().getMessageHandler().handleMessage(msg);
  112. } finally {
  113. if (null != in) {
  114. try {
  115. in.close();
  116. } catch (IOException e) {
  117. // ignore
  118. }
  119. }
  120. }
  121. }
  122. public void saveProperties() {
  123. if (!persist) return;
  124. FileOutputStream out = null;
  125. String path = null;
  126. try {
  127. path = getPropertiesFilePath();
  128. out = new FileOutputStream(path);
  129. properties.store(out, "AJDE Settings");
  130. } catch (IOException ioe) {
  131. Message msg = new Message("Error writing properties to " + path,IMessage.ERROR,ioe,null);
  132. Ajde.getDefault().getMessageHandler().handleMessage(msg);
  133. } finally {
  134. if (null != out) {
  135. try {
  136. out.close();
  137. } catch (IOException e) {
  138. // ignore
  139. }
  140. }
  141. }
  142. }
  143. }