| 1 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 ; 39 import javax.swing.event.ChangeListener ; 40 import java.awt.*; 41 import java.awt.event.*; 42 import java.io.*; 43 import java.util.*; 44 import java.util.List ; 45 import java.util.prefs.Preferences ; 46 47 public class XJPreferences { 48 49 protected Preferences prefs = null; 50 protected Map<String ,EventListener> bindings = new HashMap<String , EventListener>(); 51 52 public XJPreferences(Class c) { 53 this.prefs = Preferences.userNodeForPackage(c); 54 } 55 56 public void setString(String key, String value) { 57 prefs.put(key, value); 58 } 59 60 public String getString(String key, String def) { 61 return prefs.get(key, def); 62 } 63 64 public void setInt(String key, int value) { 65 prefs.putInt(key, value); 66 } 67 68 public void setInt(String key, Integer value) { 69 prefs.putInt(key, value); 70 } 71 72 public int getInt(String key, int def) { 73 return prefs.getInt(key, def); 74 } 75 76 public void setBoolean(String key, boolean value) { 77 prefs.putBoolean(key, value); 78 } 79 80 public boolean getBoolean(String key, boolean def) { 81 return prefs.getBoolean(key, def); 82 } 83 84 public void setColor(String key, Color value) { 85 setObject(key, value); 86 } 87 88 public Color getColor(String key, Color def) { 89 return (Color) getObject(key, def); 90 } 91 92 public void setList(String key, List <String > array) { 93 setObject(key, array); 94 } 95 96 public List <String > getList(String key) { 97 return (List <String >)getObject(key, null); 98 } 99 100 public void setObject(String key, Object 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 e) { 108 System.err.println("Cannot set the object associated with key "+key+": "+e); 109 } 110 } 111 112 public Object getObject(String key, Object 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 o = in.readObject(); 120 in.close(); 121 return o; 122 } catch(Exception 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 key) { 129 prefs.remove(key); 130 } 131 132 public Preferences getPreferences() { 133 return prefs; 134 } 135 136 138 public void bindToPreferences(JComboBox component, String key, int defaultValue) { 139 try { 140 component.setSelectedIndex(getInt(key, defaultValue)); 141 } catch(IllegalArgumentException 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 key, String defaultValue) { 152 component.setSelectedItem(getString(key, defaultValue)); 153 setString(key, (String )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 key, int defaultValue) { 161 component.setValue(getInt(key, defaultValue)); 162 setInt(key, (Integer )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 key, int defaultValue) { 170 bindToPreferences(component, key, String.valueOf(defaultValue)); 171 } 172 173 public void bindToPreferences(JTextField component, String key, String 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 key, boolean defaultValue) { 183 component.setSelected(defaultValue); 184 setBoolean(key, component.isSelected()); 185 } 186 187 public void bindToPreferences(JCheckBox component, String 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 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 key, String 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 220 221 public void defaultPreference(JPanel component, String key, Color defaultValue) { 222 component.setBackground(defaultValue); 223 setColor(key, component.getBackground()); 224 } 225 226 public void bindToPreferences(JPanel component, String 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 s : bindings.keySet()) { 237 applyPreference(s); 238 } 239 } 240 241 public void applyPreference(String key) { 242 Object o = bindings.get(key); 243 if(o instanceof ActionListener) { 244 ActionListener action = (ActionListener)o; 245 action.actionPerformed(null); 246 } else if(o instanceof ChangeListener ) { 247 ChangeListener action = (ChangeListener )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 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 key = null; 269 boolean index = false; 270 271 public JComboBoxBindingAction(JComboBox component, String 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 )component.getSelectedItem()); 282 } 283 } 284 285 protected class JTextFieldBindingAction implements ActionListener { 286 287 JTextField component = null; 288 String key = null; 289 290 public JTextFieldBindingAction(JTextField component, String 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 key = null; 304 305 public JCheckBoxBindingAction(JCheckBox component, String 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 key = null; 319 320 public JToggleButtonBindingAction(JToggleButton component, String 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 key = null; 334 335 public ButtonGroupBindingAction(ButtonGroup component, String 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 { 346 347 JSpinner component = null; 348 String key = null; 349 350 public JSpinnerBindingAction(JSpinner component, String key) { 351 this.component = component; 352 this.key = key; 353 } 354 355 public void stateChanged(ChangeEvent e) { 356 setInt(key, (Integer )component.getValue()); 357 } 358 } 359 360 protected class ColorChooserBindingMouseListener extends MouseAdapter { 361 362 JPanel component = null; 363 String key = null; 364 365 public ColorChooserBindingMouseListener(JPanel component, String 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 |