altoslib: Add getBytes/putBytes interface to AltosPreferencesBackend
[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_3;
19
20 import java.io.File;
21 import java.util.prefs.*;
22 import org.altusmetrum.altoslib_5.*;
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 byte[] getBytes(String key, byte[] def) {
66                 return _preferences.getByteArray(key, def);
67         }
68
69         public void putBytes(String key, byte[] value) {
70                 _preferences.putByteArray(key, value);
71         }
72
73         public boolean nodeExists(String key) {
74                 try {
75                         return _preferences.nodeExists(key);
76                 } catch (BackingStoreException be) {
77                         return false;
78                 }
79         }
80
81         public AltosPreferencesBackend node(String key) {
82                 return new AltosUIPreferencesBackend(_preferences.node(key));
83         }
84
85         public String[] keys() {
86                 try {
87                         return _preferences.keys();
88                 } catch (BackingStoreException be) {
89                         return null;
90                 }
91         }
92
93         public void remove(String key) {
94                 _preferences.remove(key);
95         }
96
97         public void    flush() {
98                 try {
99                         _preferences.flush();
100                 } catch (BackingStoreException ee) {
101                         System.err.printf("Cannot save preferences\n");
102                 }
103         }
104
105         public File homeDirectory() {
106                 /* Use the file system view default directory */
107                 return FileSystemView.getFileSystemView().getDefaultDirectory();
108         }
109 }