001    package org.shiftone.jrat.desktop;
002    
003    import org.shiftone.jrat.util.StringUtil;
004    
005    import java.awt.*;
006    import java.io.File;
007    import java.util.prefs.Preferences;
008    
009    /**
010     * @author jeff@shiftone.org (Jeff Drost)
011     */
012    public class DesktopPreferences {
013        private static final Preferences PREFS = Preferences.userNodeForPackage(DesktopPreferences.class);
014        private static final String RUN_COUNT = "run_count";
015        private static final String TIPS_ON_START = "show_tips_on_startup";
016        private static final String LAST_RUN_TIME = "last_run_time";
017        private static final String LAST_OPENED_FILE = "last_opened_file";
018        private static final String WINDOW_BOUNDS = "window_bounds";
019        private static final String LAST_INJECTED_FILE = "last_injected_file";
020        private static final String LAST_INJECTED_DIR = "last_injected_dir";
021    
022    
023        public static int getRunCount() {
024            return PREFS.getInt(RUN_COUNT, 0);
025        }
026    
027        public static void setRunCount(int runCount) {
028            PREFS.putInt(RUN_COUNT, runCount);
029        }
030    
031        public static void incrementRunCount() {
032            setRunCount(getRunCount() + 1);
033        }
034    
035        public static boolean isShowTipsOnStartup() {
036            return PREFS.getBoolean(TIPS_ON_START, true);
037        }
038    
039        public static void setShowTipsOnStartup(boolean showTipsOnStartup) {
040            PREFS.putBoolean(TIPS_ON_START, showTipsOnStartup);
041        }
042    
043        public static long getLastRunTime() {
044            return PREFS.getLong(LAST_RUN_TIME, 0);
045        }
046    
047        public static void setLastRunTime(long lastRunTime) {
048            PREFS.putLong(LAST_RUN_TIME, lastRunTime);
049        }
050    
051        public static Rectangle getWindowBounds() {
052    
053            String text = PREFS.get(WINDOW_BOUNDS, null);
054            if (text != null) {
055                String[] values = StringUtil.tokenize(text, ",", false);
056                // x, y, width, height
057                return new Rectangle(
058                        Integer.parseInt(values[0]),
059                        Integer.parseInt(values[1]),
060                        Integer.parseInt(values[2]),
061                        Integer.parseInt(values[3]));
062            }
063            return null;
064        }
065    
066        public static void setWindowBounds(Rectangle bounds) {
067            PREFS.put(WINDOW_BOUNDS, bounds.x + "," + bounds.y + "," + bounds.width + "," + bounds.height);
068        }
069    
070        public static File getLastOpenedFile() {
071            return getFile(LAST_OPENED_FILE);
072        }
073    
074        public static void setLastOpenedFile(File file) {
075            setFile(LAST_OPENED_FILE, file);
076        }
077    
078        public static File getLastInjectedFile() {
079            return getFile(LAST_INJECTED_FILE);
080        }
081    
082        public static void setLastInjectedFile(File file) {
083            setFile(LAST_INJECTED_FILE, file);
084        }
085    
086        public static File getLastInjectedDir() {
087            return getFile(LAST_INJECTED_DIR);
088        }
089    
090        public static void setLastInjectedDir(File file) {
091            setFile(LAST_INJECTED_DIR, file);
092        }
093    
094    
095        private static File getFile(String key) {
096            String name = PREFS.get(key, null);
097            return name == null ? null : new File(name);
098        }
099    
100        private static void setFile(String key, File value) {
101            PREFS.put(key, value == null ? null : value.getAbsolutePath());
102        }
103    }