1 33 34 package edu.rice.cs.drjava.ui.config; 35 36 import javax.swing.*; 37 import java.awt.*; 38 39 import java.io.Serializable ; 40 import java.util.ArrayList ; 41 import java.util.Vector ; 42 43 import edu.rice.cs.drjava.config.*; 44 import edu.rice.cs.drjava.DrJava; 45 import edu.rice.cs.util.Lambda; 46 import edu.rice.cs.util.swing.Utilities; 47 48 52 public abstract class OptionComponent<T> implements Serializable { 53 protected final Option<T> _option; 54 protected final JLabel _label; 55 protected final Frame _parent; 56 57 public OptionComponent(Option<T> option, String labelText, Frame parent) { 58 _option = option; 59 _label = new JLabel(labelText); 60 _label.setHorizontalAlignment(JLabel.RIGHT); 61 _parent = parent; 62 if (option != null) { 63 DrJava.getConfig().addOptionListener(option, new OptionListener<T>() { 64 public void optionChanged(OptionEvent<T> oe) { 65 resetToCurrent(); 66 } 67 }); 68 } 69 } 70 71 75 public OptionComponent (String labelText, Frame parent) { this(null, labelText, parent); } 76 77 public Option<T> getOption() { return _option; } 78 79 public String getLabelText() { return _label.getText(); } 80 81 public JLabel getLabel() { return _label; } 82 83 84 public abstract JComponent getComponent(); 85 86 90 public abstract void setDescription(String description); 91 92 99 public abstract boolean updateConfig(); 100 101 102 public void resetToCurrent() { 103 if (_option != null) { 104 setValue(DrJava.getConfig().getSetting(_option)); 105 } 106 } 107 108 111 public void resetToDefault() { 112 if (_option != null) { 113 setValue(_option.getDefault()); 114 notifyChangeListeners(); 115 } 116 } 117 118 119 public abstract void setValue(T value); 120 121 public void showErrorMessage(String title, OptionParseException e) { 122 showErrorMessage(title, e.value, e.message); 123 } 124 125 public void showErrorMessage(String title, String value, String message) { 126 JOptionPane.showMessageDialog(_parent, 127 "There was an error in one of the options that you entered.\n" + 128 "Option: '" + getLabelText() + "'\n" + 129 "Your value: '" + value + "'\n" + 130 "Error: "+ message, 131 title, 132 JOptionPane.WARNING_MESSAGE); 133 } 134 135 136 public static interface ChangeListener extends Lambda<Object , Object > { 137 public abstract Object apply(Object c); 138 } 139 140 143 public void addChangeListener(ChangeListener listener) { _changeListeners.add(listener); } 144 145 148 public void removeChangeListener(ChangeListener listener) { _changeListeners.remove(listener); } 149 150 151 protected void notifyChangeListeners() { 152 Utilities.invokeLater(new Runnable () { 153 public void run() { 154 ChangeListener[] listeners = _changeListeners.toArray(new ChangeListener[_changeListeners.size()]); 156 for (ChangeListener l: listeners) l.apply(this); } 157 }); 158 } 159 160 162 private volatile Vector <ChangeListener> _changeListeners = new Vector <ChangeListener>(); 163 } 164 165 166 | Popular Tags |