/trunk/src/org/getopt/luke/Prefs.java

http://luke.googlecode.com/ · Java · 152 lines · 125 code · 22 blank · 5 comment · 26 complexity · ce192d6911f2b2a941214a2c219c2398 MD5 · raw file

  1. package org.getopt.luke;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.util.*;
  5. /**
  6. * @author abial
  7. */
  8. public class Prefs {
  9. public static final String LUKE_PREFS_FILE = ".luke";
  10. private static final String HOME_DIR = System.getProperty("user.home");
  11. public static final String P_LAST_PWD = "luke.last_pwd";
  12. public static final String P_MRU_ARRAY = "luke.mru";
  13. public static final String P_MRU_SIZE = "luke.mru_size";
  14. public static final String P_FONT_NAME = "luke.fontname";
  15. public static final String P_FONT_SIZE = "luke.fontsize";
  16. public static final String P_THEME = "luke.theme";
  17. public static final String P_ANALYZER = "luke.analyzer";
  18. public static final String P_FIELD = "luke.field";
  19. private static Properties props = new Properties();
  20. private static String[][] defaults = {
  21. {P_MRU_SIZE, "10"},
  22. {P_FONT_NAME, "sansserif.plain"},
  23. {P_FONT_SIZE, "12"}
  24. };
  25. private static List<String> mruList = new ArrayList<String>();
  26. private static int mruMaxSize = 10;
  27. private static String prefsFile = HOME_DIR + "/" + LUKE_PREFS_FILE;
  28. public static void load() {
  29. load(prefsFile);
  30. }
  31. public static void load(String filename) {
  32. for (int i = 0; i < defaults.length; i++) {
  33. props.setProperty(defaults[i][0], defaults[i][1]);
  34. }
  35. try {
  36. props.load(new FileInputStream(filename));
  37. initMruList();
  38. } catch (Exception e) {
  39. // not found or corrupted, keep defaults
  40. }
  41. }
  42. private static void initMruList() {
  43. mruMaxSize = getInteger(P_MRU_SIZE, 10);
  44. String[] mrus = getPropertyArray(P_MRU_ARRAY);
  45. if (mrus != null && mrus.length > 0) {
  46. for (int i = 0;
  47. i < Math.min(mrus.length, mruMaxSize); i++) {
  48. mruList.add(mrus[i].intern());
  49. }
  50. }
  51. }
  52. public static void addToMruList(String value) {
  53. if (value == null || value.trim().equals("") ||
  54. mruList.contains(value.intern())) return;
  55. if (mruList.size() >= mruMaxSize) {
  56. mruList.remove(mruList.size() - 1);
  57. }
  58. mruList.add(0, value);
  59. }
  60. public static List<String> getMruList() {
  61. return Collections.unmodifiableList(mruList);
  62. }
  63. public static void save() throws Exception {
  64. setPropertyArray(P_MRU_ARRAY,
  65. (String[])mruList.toArray(new String[0]));
  66. props.store(new FileOutputStream(prefsFile), null);
  67. }
  68. public static int getInteger(String key, int defVal) {
  69. String val = props.getProperty(key);
  70. int iVal = defVal;
  71. if (val != null) {
  72. try {
  73. iVal = Integer.parseInt(val);
  74. } catch (Exception e) {};
  75. }
  76. return iVal;
  77. }
  78. public static boolean getBoolean(String key, boolean defVal) {
  79. String val = props.getProperty(key);
  80. boolean iVal = defVal;
  81. if (val != null) {
  82. iVal = Boolean.valueOf(val).booleanValue();
  83. }
  84. return iVal;
  85. }
  86. public static String[] getPropertyArray(String key) {
  87. int i = 0;
  88. ArrayList<String> v = new ArrayList<String>();
  89. String val = null;
  90. do {
  91. String iKey = key + "." + i;
  92. val = props.getProperty(iKey);
  93. if (val != null) {
  94. v.add(val);
  95. }
  96. i++;
  97. } while (val != null);
  98. if (v.size() == 0) return null;
  99. String[] res = new String[v.size()];
  100. return (String[])v.toArray(res);
  101. }
  102. public static void deletePropertyArray(String key) {
  103. String key1 = key + ".";
  104. for (Enumeration e = props.propertyNames(); e.hasMoreElements(); ) {
  105. String k = (String)e.nextElement();
  106. if (k.startsWith(key1)) {
  107. // TODO: should check for integer value part
  108. props.remove(k);
  109. }
  110. }
  111. }
  112. public static void setPropertyArray(String key, String[] values) {
  113. deletePropertyArray(key);
  114. if (values == null || values.length == 0) {
  115. return;
  116. }
  117. for (int i = 0; i < values.length; i++) {
  118. props.setProperty(key + "." + i, values[i]);
  119. }
  120. }
  121. public static String getProperty(String key) {
  122. return getProperty(key, null);
  123. }
  124. public static String getProperty(String key, String defVal) {
  125. return props.getProperty(key, defVal);
  126. }
  127. public static void setProperty(String key, String val) {
  128. props.setProperty(key, val);
  129. }
  130. }