1 /*
2 * PreferencesModel
3 *
4 * $RCSfile: PreferencesModel.java,v $
5 * $Revision: 1.3 $
6 * $Date: 2004/01/10 20:10:46 $
7 * $Source: /cvsroot/jpui/jpui/src/PreferencesModel.java,v $
8 *
9 * JPUI - Java Preferences User Interface
10 * Copyright (C) 2003
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
20 * more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
24 * Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 * Author: macksold@users.sourceforge.net
27 */
28
29 import java.util.Observable;
30 import java.util.prefs.BackingStoreException;
31 import java.util.prefs.NodeChangeEvent;
32 import java.util.prefs.NodeChangeListener;
33 import java.util.prefs.PreferenceChangeEvent;
34 import java.util.prefs.PreferenceChangeListener;
35 import java.util.prefs.Preferences;
36
37 /***
38 * The model where all modifications to the preferences store are
39 * done. Uses the Observer pattern to notify the views when the
40 * model changes.
41 * This singleton class effectively adds the idea of a 'current node' to the
42 * java preferences node heirarchy.
43 */
44 public class PreferencesModel
45 extends Observable
46 implements NodeChangeListener, PreferenceChangeListener {
47 // singleton instance
48 private static PreferencesModel moInstance = new PreferencesModel();
49 // reference to the current preferences node
50 Preferences moCurrentNode = null;
51
52 /***
53 * Private ctor, clients use Instance()
54 */
55 private PreferencesModel() {
56 Preferences oSystem = PreferencesNode.systemRoot();
57 Preferences oUser = PreferencesNode.userRoot();
58
59 // listen for preference chance events
60 oSystem.addNodeChangeListener(this);
61 oSystem.addPreferenceChangeListener(this);
62 oUser.addNodeChangeListener(this);
63 oUser.addPreferenceChangeListener(this);
64
65 // the current node defaults to the system root
66 moCurrentNode = oSystem;
67 }
68
69 /***
70 * Singleton accessor
71 * @return PreferencesModel
72 */
73 public static PreferencesModel Instance() {
74 return moInstance;
75 }
76
77 /***
78 * Gets the current preferences node
79 * @return java.util.prefs.Preferences
80 */
81 public Preferences getCurrentNode() {
82 return moCurrentNode;
83 }
84
85 /***
86 * Sets the current preferences node and notifies
87 * observers if the new node is not the same as the
88 * previous current node.
89 * @param oNode new current node
90 */
91 public void setCurrentNode(Preferences oNode) {
92 if (!oNode.equals(moCurrentNode)) {
93 moCurrentNode = oNode;
94 setChanged();
95 notifyObservers();
96 }
97 }
98
99 /***
100 * Sets the attribute sKey of the current node
101 * @param sKey node key name
102 * @param sValue node key value
103 */
104 public void setAttribute(String sKey, String sValue) {
105 Preferences oCurrentNode = getCurrentNode();
106 oCurrentNode.put(sKey, sValue);
107 sync(oCurrentNode);
108 setChanged();
109 notifyObservers();
110 }
111
112 /***
113 * Removes the attribute sKey of the current node
114 * @param sKey node key to remove
115 */
116 public void removeAttribute(String sKey) {
117 Preferences oCurrentNode = getCurrentNode();
118 if (oCurrentNode.get(sKey, null) != null) {
119 oCurrentNode.remove(sKey);
120 sync(oCurrentNode);
121 setChanged();
122 notifyObservers();
123 }
124 }
125
126 /***
127 * Renames the attribute sOldKey of the current node
128 * @param sOldKey node key old name
129 * @param sNewKey node key new name
130 */
131 public void renameAttribute(String sOldKey, String sNewKey) {
132 Preferences oCurrentNode = getCurrentNode();
133 if (oCurrentNode.get(sNewKey, null) == null) {
134 String sValue = oCurrentNode.get(sOldKey, null);
135 oCurrentNode.put(sNewKey, sValue);
136 oCurrentNode.remove(sOldKey);
137 sync(oCurrentNode);
138 setChanged();
139 notifyObservers();
140 }
141 else {
142 // TODO: a key by this name already exists
143 }
144
145 }
146
147 /***
148 * Creates a new node as a child of the current node
149 * @param sNodeName new node name
150 * @return java.util.prefs.Preferences the new node
151 */
152 public Preferences newNode(String sNodeName) {
153 Preferences oCurrentNode = getCurrentNode();
154 Preferences oNewNode = oCurrentNode.node(sNodeName);
155 sync(oCurrentNode);
156 setCurrentNode(oNewNode);
157 return oNewNode;
158 }
159
160 /***
161 * Deletes the current node and its children
162 * @return java.util.prefs.Preferences the parent of the deleted node
163 */
164 public Preferences deleteNode() {
165 Preferences oCurrentNode = getCurrentNode();
166 Preferences oParentNode = oCurrentNode.parent();
167 if (oParentNode != null) {
168 try {
169 oCurrentNode.removeNode();
170 sync(oParentNode);
171 setCurrentNode(oParentNode);
172 }
173 catch (BackingStoreException oEx) {
174 oEx.printStackTrace();
175 }
176 }
177 return oParentNode;
178 }
179
180 /***
181 * @see java.util.prefs.NodeChangeListener#childAdded(java.util.prefs.NodeChangeEvent)
182 */
183 public void childAdded(NodeChangeEvent evt) {
184 // stub
185 }
186
187 /***
188 * @see java.util.prefs.NodeChangeListener#childRemoved(java.util.prefs.NodeChangeEvent)
189 */
190 public void childRemoved(NodeChangeEvent evt) {
191 // stub
192 }
193
194 /***
195 * @see java.util.prefs.PreferenceChangeListener#preferenceChange(java.util.prefs.PreferenceChangeEvent)
196 */
197 public void preferenceChange(PreferenceChangeEvent evt) {
198 // stub
199 }
200
201 /***
202 * Utility method to persist the preferences store after a change
203 * @param oPref preferences node to sync from
204 */
205 private void sync(Preferences oPref) {
206 try {
207 oPref.sync();
208 }
209 catch (BackingStoreException oEx) {
210 oEx.printStackTrace();
211 }
212 }
213 }
This page was automatically generated by Maven