KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > xjlib > appkit > app > XJPreferences


1 /*
2
3 [The "BSD licence"]
4 Copyright (c) 2005 Jean Bovet
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. The name of the author may not be used to endorse or promote products
17 derived from this software without specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 */

31
32 package org.antlr.xjlib.appkit.app;
33
34 import org.antlr.xjlib.appkit.frame.XJDialog;
35 import org.antlr.xjlib.appkit.swing.XJColorChooser;
36
37 import javax.swing.*;
38 import javax.swing.event.ChangeEvent JavaDoc;
39 import javax.swing.event.ChangeListener JavaDoc;
40 import java.awt.*;
41 import java.awt.event.*;
42 import java.io.*;
43 import java.util.*;
44 import java.util.List JavaDoc;
45 import java.util.prefs.Preferences JavaDoc;
46
47 public class XJPreferences {
48
49     protected Preferences JavaDoc prefs = null;
50     protected Map<String JavaDoc,EventListener> bindings = new HashMap<String JavaDoc, EventListener>();
51
52     public XJPreferences(Class JavaDoc c) {
53         this.prefs = Preferences.userNodeForPackage(c);
54     }
55
56     public void setString(String JavaDoc key, String JavaDoc value) {
57         prefs.put(key, value);
58     }
59
60     public String JavaDoc getString(String JavaDoc key, String JavaDoc def) {
61         return prefs.get(key, def);
62     }
63
64     public void setInt(String JavaDoc key, int value) {
65         prefs.putInt(key, value);
66     }
67
68     public void setInt(String JavaDoc key, Integer JavaDoc value) {
69         prefs.putInt(key, value);
70     }
71
72     public int getInt(String JavaDoc key, int def) {
73         return prefs.getInt(key, def);
74     }
75
76     public void setBoolean(String JavaDoc key, boolean value) {
77         prefs.putBoolean(key, value);
78     }
79
80     public boolean getBoolean(String JavaDoc key, boolean def) {
81         return prefs.getBoolean(key, def);
82     }
83
84     public void setColor(String JavaDoc key, Color value) {
85         setObject(key, value);
86     }
87
88     public Color getColor(String JavaDoc key, Color def) {
89         return (Color) getObject(key, def);
90     }
91
92     public void setList(String JavaDoc key, List JavaDoc<String JavaDoc> array) {
93         setObject(key, array);
94     }
95
96     public List JavaDoc<String JavaDoc> getList(String JavaDoc key) {
97         return (List JavaDoc<String JavaDoc>)getObject(key, null);
98     }
99
100     public void setObject(String JavaDoc key, Object JavaDoc obj) {
101         try {
102             ByteArrayOutputStream bos = new ByteArrayOutputStream();
103             ObjectOutput out = new ObjectOutputStream(bos);
104             out.writeObject(obj);
105             out.close();
106             prefs.putByteArray(key, bos.toByteArray());
107         } catch(Exception JavaDoc e) {
108             System.err.println("Cannot set the object associated with key "+key+": "+e);
109         }
110     }
111
112     public Object JavaDoc getObject(String JavaDoc key, Object JavaDoc defaultObject) {
113         try {
114             byte[] bytes = prefs.getByteArray(key, null);
115             if(bytes == null)
116                 return defaultObject;
117
118             ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
119             Object JavaDoc o = in.readObject();
120             in.close();
121             return o;
122         } catch(Exception JavaDoc e) {
123             System.err.println("Cannot get the object associated with key "+key+": "+e);
124         }
125         return defaultObject;
126     }
127
128     public void remove(String JavaDoc key) {
129         prefs.remove(key);
130     }
131
132     public Preferences JavaDoc getPreferences() {
133         return prefs;
134     }
135
136     // *** Bindings
137

138     public void bindToPreferences(JComboBox component, String JavaDoc key, int defaultValue) {
139         try {
140             component.setSelectedIndex(getInt(key, defaultValue));
141         } catch(IllegalArgumentException JavaDoc e) {
142             e.printStackTrace();
143         }
144         setInt(key, component.getSelectedIndex());
145
146         JComboBoxBindingAction action = new JComboBoxBindingAction(component, key, true);
147         bindings.put(key, action);
148         component.addActionListener(action);
149     }
150
151     public void bindToPreferences(JComboBox component, String JavaDoc key, String JavaDoc defaultValue) {
152         component.setSelectedItem(getString(key, defaultValue));
153         setString(key, (String JavaDoc)component.getSelectedItem());
154
155         JComboBoxBindingAction action = new JComboBoxBindingAction(component, key, false);
156         bindings.put(key, action);
157         component.addActionListener(action);
158     }
159
160     public void bindToPreferences(JSpinner component, String JavaDoc key, int defaultValue) {
161         component.setValue(getInt(key, defaultValue));
162         setInt(key, (Integer JavaDoc)component.getValue());
163
164         JSpinnerBindingAction action = new JSpinnerBindingAction(component, key);
165         bindings.put(key, action);
166         component.addChangeListener(action);
167     }
168
169     public void bindToPreferences(JTextField component, String JavaDoc key, int defaultValue) {
170         bindToPreferences(component, key, String.valueOf(defaultValue));
171     }
172
173     public void bindToPreferences(JTextField component, String JavaDoc key, String JavaDoc defaultValue) {
174         component.setText(getString(key, defaultValue));
175         setString(key, component.getText());
176
177         JTextFieldBindingAction action = new JTextFieldBindingAction(component, key);
178         bindings.put(key, action);
179         component.addActionListener(action);
180     }
181
182     public void defaultPreference(JCheckBox component, String JavaDoc key, boolean defaultValue) {
183         component.setSelected(defaultValue);
184         setBoolean(key, component.isSelected());
185     }
186
187     public void bindToPreferences(JCheckBox component, String JavaDoc key, boolean defaultValue) {
188         component.setSelected(getBoolean(key, defaultValue));
189         setBoolean(key, component.isSelected());
190
191         JCheckBoxBindingAction action = new JCheckBoxBindingAction(component, key);
192         bindings.put(key, action);
193         component.addActionListener(action);
194     }
195
196     public void bindToPreferences(JToggleButton component, String JavaDoc key, boolean defaultValue) {
197         component.setSelected(getBoolean(key, defaultValue));
198         setBoolean(key, component.isSelected());
199
200         JToggleButtonBindingAction action = new JToggleButtonBindingAction(component, key);
201         bindings.put(key, action);
202         component.addActionListener(action);
203     }
204
205     public void bindToPreferences(ButtonGroup component, String JavaDoc key, String JavaDoc defaultValue) {
206         component.setSelected(getButtonWithActionCommand(component, getString(key, defaultValue)).getModel(), true);
207         setString(key, component.getSelection().getActionCommand());
208
209         ButtonGroupBindingAction action = new ButtonGroupBindingAction(component, key);
210         bindings.put(key, action);
211
212         Enumeration<AbstractButton> elements = component.getElements();
213         while (elements.hasMoreElements()) {
214             AbstractButton button = elements.nextElement();
215             button.addActionListener(action);
216         }
217     }
218
219     /* Used to select a color using a JPanel as visual feedback */
220
221     public void defaultPreference(JPanel component, String JavaDoc key, Color defaultValue) {
222         component.setBackground(defaultValue);
223         setColor(key, component.getBackground());
224     }
225
226     public void bindToPreferences(JPanel component, String JavaDoc key, Color defaultValue) {
227         component.setBackground(getColor(key, defaultValue));
228         setColor(key, component.getBackground());
229
230         ColorChooserBindingMouseListener listener = new ColorChooserBindingMouseListener(component, key);
231         bindings.put(key, listener);
232         component.addMouseListener(listener);
233     }
234
235     public void applyPreferences() {
236         for (String JavaDoc s : bindings.keySet()) {
237             applyPreference(s);
238         }
239     }
240
241     public void applyPreference(String JavaDoc key) {
242         Object JavaDoc o = bindings.get(key);
243         if(o instanceof ActionListener) {
244             ActionListener action = (ActionListener)o;
245             action.actionPerformed(null);
246         } else if(o instanceof ChangeListener JavaDoc) {
247             ChangeListener JavaDoc action = (ChangeListener JavaDoc)o;
248             action.stateChanged(null);
249         } else if(o instanceof MouseListener) {
250             MouseListener listener = (MouseListener)o;
251             listener.mousePressed(null);
252         }
253     }
254
255     protected AbstractButton getButtonWithActionCommand(ButtonGroup group, String JavaDoc actionCommand) {
256         Enumeration<AbstractButton> elements = group.getElements();
257         while (elements.hasMoreElements()) {
258             AbstractButton button = elements.nextElement();
259             if(button.getActionCommand().equalsIgnoreCase(actionCommand))
260                 return button;
261         }
262         return null;
263     }
264
265     protected class JComboBoxBindingAction implements ActionListener {
266
267         JComboBox component = null;
268         String JavaDoc key = null;
269         boolean index = false;
270
271         public JComboBoxBindingAction(JComboBox component, String JavaDoc key, boolean index) {
272             this.component = component;
273             this.key = key;
274             this.index = index;
275         }
276
277         public void actionPerformed(ActionEvent e) {
278             if(index)
279                 setInt(key, component.getSelectedIndex());
280             else
281                 setString(key, (String JavaDoc)component.getSelectedItem());
282         }
283     }
284
285     protected class JTextFieldBindingAction implements ActionListener {
286
287         JTextField component = null;
288         String JavaDoc key = null;
289
290         public JTextFieldBindingAction(JTextField component, String JavaDoc key) {
291             this.component = component;
292             this.key = key;
293         }
294
295         public void actionPerformed(ActionEvent e) {
296             setString(key, component.getText());
297         }
298     }
299
300     protected class JCheckBoxBindingAction implements ActionListener {
301
302         JCheckBox component = null;
303         String JavaDoc key = null;
304
305         public JCheckBoxBindingAction(JCheckBox component, String JavaDoc key) {
306             this.component = component;
307             this.key = key;
308         }
309
310         public void actionPerformed(ActionEvent e) {
311             setBoolean(key, component.isSelected());
312         }
313     }
314
315     protected class JToggleButtonBindingAction implements ActionListener {
316
317         JToggleButton component = null;
318         String JavaDoc key = null;
319
320         public JToggleButtonBindingAction(JToggleButton component, String JavaDoc key) {
321             this.component = component;
322             this.key = key;
323         }
324
325         public void actionPerformed(ActionEvent e) {
326             setBoolean(key, component.isSelected());
327         }
328     }
329
330     protected class ButtonGroupBindingAction implements ActionListener {
331
332         ButtonGroup component = null;
333         String JavaDoc key = null;
334
335         public ButtonGroupBindingAction(ButtonGroup component, String JavaDoc key) {
336             this.component = component;
337             this.key = key;
338         }
339
340         public void actionPerformed(ActionEvent e) {
341             setString(key, component.getSelection().getActionCommand());
342         }
343     }
344
345     protected class JSpinnerBindingAction implements ChangeListener JavaDoc {
346
347         JSpinner component = null;
348         String JavaDoc key = null;
349
350         public JSpinnerBindingAction(JSpinner component, String JavaDoc key) {
351             this.component = component;
352             this.key = key;
353         }
354
355         public void stateChanged(ChangeEvent JavaDoc e) {
356             setInt(key, (Integer JavaDoc)component.getValue());
357         }
358     }
359
360     protected class ColorChooserBindingMouseListener extends MouseAdapter {
361
362         JPanel component = null;
363         String JavaDoc key = null;
364
365         public ColorChooserBindingMouseListener(JPanel component, String JavaDoc key) {
366             this.component = component;
367             this.key = key;
368         }
369
370         public void mousePressed(MouseEvent e) {
371             if(e == null) {
372                 setColor(key, component.getBackground());
373             } else {
374                 XJColorChooser cc = new XJColorChooser(component.getParent(), true, component);
375                 if(cc.runModal() == XJDialog.BUTTON_OK) {
376                     setColor(key, cc.getColor());
377                 }
378             }
379         }
380     }
381
382 }
383
Popular Tags