1 33 34 package edu.rice.cs.drjava.ui.config; 35 36 import edu.rice.cs.drjava.ui.MainFrame; 37 import javax.swing.*; 38 import edu.rice.cs.drjava.config.*; 39 import edu.rice.cs.drjava.*; 40 import java.awt.*; 41 import java.awt.event.*; 42 import java.util.Hashtable ; 43 44 48 public class KeyStrokeOptionComponent extends OptionComponent<KeyStroke> implements Comparable { 49 private static final int DIALOG_HEIGHT = 185; 50 53 public static final Hashtable <KeyStroke, KeyStrokeOptionComponent> _keyToKSOC = 54 new Hashtable <KeyStroke, KeyStrokeOptionComponent>(); 55 private JButton _button; 56 private JTextField _keyField; 57 private JPanel _panel; 58 private static GetKeyDialog _getKeyDialog = null; 59 60 private KeyStroke _key; 61 62 public KeyStrokeOptionComponent(KeyStrokeOption opt, 63 String text, 64 final Frame parent) { 65 super(opt, text, parent); 66 67 _key = DrJava.getConfig().getSetting(opt); 68 69 _button = new JButton(); 70 _button.addActionListener(new ActionListener() { 71 public void actionPerformed(ActionEvent ae) { 72 73 if (_getKeyDialog == null) { 74 _getKeyDialog = 75 new GetKeyDialog(parent, 76 "Specify Shortcut", 77 true); 78 } 79 80 String oldText = _keyField.getText(); 81 _getKeyDialog.promptKey(KeyStrokeOptionComponent.this); 82 if (!_keyField.getText().equals(oldText)) { 83 notifyChangeListeners(); 84 } 85 } 86 }); 87 _button.setText("..."); 88 _button.setMaximumSize(new Dimension(10,10)); 89 _button.setMinimumSize(new Dimension(10,10)); 90 91 _keyField = new JTextField(); 92 _keyField.setEditable(false); 93 _keyField.setBackground(Color.white); 94 _keyField.setHorizontalAlignment(JTextField.CENTER); 95 _keyField.setText(_option.format(_key)); 96 _panel = new JPanel(new BorderLayout()); 97 _panel.add(_keyField, BorderLayout.CENTER); 98 _panel.add(_button, BorderLayout.EAST); 99 100 GridLayout gl = new GridLayout(1,0); 101 gl.setHgap(15); 102 _keyToKSOC.put(_key, this); 103 } 104 105 108 public KeyStrokeOptionComponent(KeyStrokeOption opt, String text, 109 Frame parent, String description) { 110 this(opt, text, parent); 111 setDescription(description); 112 } 113 114 118 public void setDescription(String description) { 119 _panel.setToolTipText(description); 120 _button.setToolTipText(description); 121 _keyField.setToolTipText(description); 122 _label.setToolTipText(description); 123 } 124 125 128 public String toString() { 129 return "<KSOC>label:" + getLabelText() + "ks: " + 130 getKeyStroke() + "jb: " + _button.getText() + "</KSOC>\n"; 131 } 132 133 137 public boolean updateConfig() { 138 if (!_key.equals(getConfigKeyStroke())) { 139 DrJava.getConfig().setSetting(_option, _key); 140 setValue(_key); 141 } 142 return true; 143 } 144 145 148 public void setValue(KeyStroke value) { 149 _key = value; 150 _keyField.setText(_option.format(value)); 151 } 152 153 157 public int compareTo(Object o) { 158 if (o instanceof KeyStrokeOptionComponent) { 159 KeyStrokeOptionComponent other = (KeyStrokeOptionComponent)o; 160 return this.getLabelText().compareTo(other.getLabelText()); 161 } 162 else return 1; 163 } 164 165 168 public KeyStroke getKeyStroke() { 169 return _key; 170 } 171 172 175 public KeyStroke getConfigKeyStroke() { 176 return DrJava.getConfig().getSetting(_option); 177 } 178 179 182 public JComponent getComponent() { return _panel; } 183 184 189 private class GetKeyDialog extends JDialog { 190 private InputField _inputField; 191 private JButton _clearButton; 192 private JButton _cancelButton; 193 private JButton _okButton; 194 private JLabel _instructionLabel; 195 private JLabel _currentLabel; 196 private JLabel _actionLabel; 197 private JPanel _inputAndClearPanel; 198 private JPanel _cancelAndOKPanel; 200 private KeyStroke _currentKeyStroke; 201 private KeyStrokeOptionComponent _ksoc; 202 204 public GetKeyDialog(Frame f, String title, boolean modal) { 205 super(f, title, modal); 206 208 _inputField = new InputField(); 209 _clearButton = new JButton("Clear"); 210 _clearButton.addActionListener(new ActionListener() { 211 public void actionPerformed(ActionEvent ae) { 212 _inputField.setText(""); 213 _actionLabel.setText("<none>"); 214 _currentKeyStroke = KeyStrokeOption.NULL_KEYSTROKE; 215 _inputField.requestFocusInWindow(); 216 } 217 }); 218 _cancelButton = new JButton("Cancel"); 219 _cancelButton.addActionListener(new ActionListener() { 220 public void actionPerformed(ActionEvent ae) { 221 _inputField.requestFocusInWindow(); 222 GetKeyDialog.this.dispose(); 223 } 224 }); 225 _okButton = new JButton("OK"); 226 _okButton.addActionListener(new ActionListener() { 227 public void actionPerformed(ActionEvent ae) { 228 if (!_ksoc.getKeyStroke().equals(_currentKeyStroke)) { 229 _keyToKSOC.remove(_ksoc.getKeyStroke()); 230 231 KeyStrokeOptionComponent conflict = _keyToKSOC.get(_currentKeyStroke); 232 233 if (conflict != null) { 234 _keyToKSOC.remove(_currentKeyStroke); 235 conflict.setValue(KeyStrokeOption.NULL_KEYSTROKE); 236 } 237 _keyToKSOC.put(_currentKeyStroke, _ksoc); 238 _ksoc.setValue(_currentKeyStroke); 239 } 240 _inputField.requestFocusInWindow(); 241 GetKeyDialog.this.dispose(); 242 } 243 }); 244 _instructionLabel = new JLabel("Type in the keystroke you want to use " + 245 "and click \"OK\""); 246 _currentLabel = new JLabel("Current action bound to the keystroke:"); 247 _actionLabel = new JLabel("<none>"); 248 249 _inputAndClearPanel = new JPanel(new BorderLayout()); 250 _inputAndClearPanel.add(_inputField, BorderLayout.CENTER); 251 _inputAndClearPanel.add(_clearButton, BorderLayout.EAST); 252 253 257 _cancelAndOKPanel = new JPanel(new GridLayout(1,0)); 258 _cancelAndOKPanel.add(_okButton); 259 _cancelAndOKPanel.add(_cancelButton); 260 261 JPanel panel = (JPanel)this.getContentPane(); 262 263 panel.setLayout(new GridLayout(0, 1)); 264 panel.add(_instructionLabel); 265 panel.add(_inputAndClearPanel); 266 panel.add(_currentLabel); 268 panel.add(_actionLabel); 269 panel.add(_cancelAndOKPanel); 270 this.setSize((int)_instructionLabel.getPreferredSize().getWidth() + 30, DIALOG_HEIGHT); 271 this.pack(); 273 } 274 275 public void promptKey(KeyStrokeOptionComponent k) { 276 _ksoc = k; 277 _instructionLabel.setText("Type in the keystroke you want to use for \"" + 278 k.getLabelText() + 279 "\" and click \"OK\""); 280 _currentKeyStroke = k.getKeyStroke(); 281 _actionLabel.setText(k.getLabelText()); 282 _inputField.setText(_option.format(_currentKeyStroke)); 283 this.setSize((int)_instructionLabel.getPreferredSize().getWidth() + 30, DIALOG_HEIGHT); 285 MainFrame.setPopupLoc(this, getOwner()); 286 this.setVisible(true); 287 } 288 289 294 private class InputField extends JTextField { 295 298 299 public void processKeyEvent(KeyEvent e) { 300 KeyStroke ks = KeyStroke.getKeyStrokeForEvent(e); 301 if (e.getID() == KeyEvent.KEY_PRESSED) { 302 this.setText(_option.format(ks)); 303 KeyStrokeOptionComponent configKs = _keyToKSOC.get(ks); 304 if (configKs == null) 305 _actionLabel.setText("<none>"); 306 else { 307 String name = configKs.getLabelText(); _actionLabel.setText(name); 309 } 310 _currentKeyStroke = ks; 311 } 312 } 313 } 314 } 315 316 } 317 | Popular Tags |