PageRenderTime 26ms CodeModel.GetById 17ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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