altoslib: Bump library version
[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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altosuilib_1;
19
20 import java.io.File;
21 import java.util.prefs.*;
22 import org.altusmetrum.altoslib_3.*;
23 import javax.swing.filechooser.FileSystemView;
24
25 public class AltosUIPreferencesBackend implements AltosPreferencesBackend {
26
27         private Preferences _preferences = null;
28         
29         public AltosUIPreferencesBackend() {
30                 _preferences = Preferences.userRoot().node("/org/altusmetrum/altosui");
31         }
32
33         public AltosUIPreferencesBackend(Preferences in_preferences) {
34                 _preferences = in_preferences;
35         }
36
37         public String  getString(String key, String def) {
38                 return _preferences.get(key, def);
39         }
40         public void    putString(String key, String value) {
41                 _preferences.put(key, value);
42         }
43
44         public int     getInt(String key, int def) {
45                 return _preferences.getInt(key, def);
46         }
47         public void    putInt(String key, int value) {
48                 _preferences.putInt(key, value);
49         }
50
51         public double  getDouble(String key, double def) {
52                 return _preferences.getDouble(key, def);
53         }
54         public void    putDouble(String key, double value) {
55                 _preferences.putDouble(key, value);
56         }
57
58         public boolean getBoolean(String key, boolean def) {
59                 return _preferences.getBoolean(key, def);
60         }
61         public void    putBoolean(String key, boolean value) {
62                 _preferences.putBoolean(key, value);
63         }
64
65         public boolean nodeExists(String key) {
66                 try {
67                         return _preferences.nodeExists(key);
68                 } catch (BackingStoreException be) {
69                         return false;
70                 }
71         }
72
73         public AltosPreferencesBackend node(String key) {
74                 return new AltosUIPreferencesBackend(_preferences.node(key));
75         }
76
77         public String[] keys() {
78                 try {
79                         return _preferences.keys();
80                 } catch (BackingStoreException be) {
81                         return null;
82                 }
83         }
84
85         public void remove(String key) {
86                 _preferences.remove(key);
87         }
88
89         public void    flush() {
90                 try {
91                         _preferences.flush();
92                 } catch (BackingStoreException ee) {
93                         System.err.printf("Cannot save preferences\n");
94                 }
95         }
96
97         public File homeDirectory() {
98                 /* Use the file system view default directory */
99                 return FileSystemView.getFileSystemView().getDefaultDirectory();
100         }
101 }