163ba173f2076f1fac0386ea7ceb8ddaf023b5f1
[fw/altos] / altosuilib / AltosUIPreferencesBackend.java
1 /*
2  * Copyright © 2012 Mike Beattie <mike@ethernal.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altosuilib_12;
20
21 import java.io.File;
22 import java.util.prefs.*;
23 import org.altusmetrum.altoslib_12.*;
24 import javax.swing.filechooser.FileSystemView;
25
26 public class AltosUIPreferencesBackend extends AltosPreferencesBackend {
27
28         private Preferences _preferences = null;
29
30         public AltosUIPreferencesBackend() {
31                 _preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
32         }
33
34         public AltosUIPreferencesBackend(Preferences in_preferences) {
35                 _preferences = in_preferences;
36         }
37
38         public String  getString(String key, String def) {
39                 return _preferences.get(key, def);
40         }
41         public void    putString(String key, String value) {
42                 _preferences.put(key, value);
43         }
44
45         public int     getInt(String key, int def) {
46                 return _preferences.getInt(key, def);
47         }
48         public void    putInt(String key, int value) {
49                 _preferences.putInt(key, value);
50         }
51
52         public double  getDouble(String key, double def) {
53                 return _preferences.getDouble(key, def);
54         }
55         public void    putDouble(String key, double value) {
56                 _preferences.putDouble(key, value);
57         }
58
59         public boolean getBoolean(String key, boolean def) {
60                 return _preferences.getBoolean(key, def);
61         }
62         public void    putBoolean(String key, boolean value) {
63                 _preferences.putBoolean(key, value);
64         }
65
66         public byte[] getBytes(String key, byte[] def) {
67                 return _preferences.getByteArray(key, def);
68         }
69
70         public void putBytes(String key, byte[] value) {
71                 _preferences.putByteArray(key, value);
72         }
73
74         public boolean nodeExists(String key) {
75                 try {
76                         return _preferences.nodeExists(key);
77                 } catch (BackingStoreException be) {
78                         return false;
79                 }
80         }
81
82         public AltosPreferencesBackend node(String key) {
83                 return new AltosUIPreferencesBackend(_preferences.node(key));
84         }
85
86         public String[] keys() {
87                 try {
88                         return _preferences.keys();
89                 } catch (BackingStoreException be) {
90                         return null;
91                 }
92         }
93
94         public void remove(String key) {
95                 _preferences.remove(key);
96         }
97
98         public void    flush() {
99                 try {
100                         _preferences.flush();
101                 } catch (BackingStoreException ee) {
102                         System.err.printf("Cannot save preferences\n");
103                 }
104         }
105
106         public File homeDirectory() {
107                 /* Use the file system view default directory */
108                 return FileSystemView.getFileSystemView().getDefaultDirectory();
109         }
110
111         public void debug(String format, Object ... arguments) {
112                 System.out.printf(format, arguments);
113         }
114 }