KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > ui > config > KeyStrokeOptionComponent


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

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 JavaDoc;
43
44 /**
45  * Graphical form of a KeyStrokeOption.
46  * @version $Id: KeyStrokeOptionComponent.java 3869 2006-05-27 04:24:47Z mgricken $
47  */

48 public class KeyStrokeOptionComponent extends OptionComponent<KeyStroke> implements Comparable JavaDoc {
49   private static final int DIALOG_HEIGHT = 185;
50   /**
51    * TODO: should this be synchronized?
52    */

53   public static final Hashtable JavaDoc<KeyStroke, KeyStrokeOptionComponent> _keyToKSOC =
54     new Hashtable JavaDoc<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 JavaDoc 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 JavaDoc 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   /**
106    * Constructor that allows for a tooltip description.
107    */

108   public KeyStrokeOptionComponent(KeyStrokeOption opt, String JavaDoc text,
109                                   Frame parent, String JavaDoc description) {
110     this(opt, text, parent);
111     setDescription(description);
112   }
113
114   /**
115    * Sets the tooltip description text for this option.
116    * @param description the tooltip text
117    */

118   public void setDescription(String JavaDoc description) {
119     _panel.setToolTipText(description);
120     _button.setToolTipText(description);
121     _keyField.setToolTipText(description);
122     _label.setToolTipText(description);
123   }
124
125   /**
126    * Returns a custom string representation of this option component.
127    */

128   public String JavaDoc toString() {
129     return "<KSOC>label:" + getLabelText() + "ks: " +
130       getKeyStroke() + "jb: " + _button.getText() + "</KSOC>\n";
131   }
132
133   /**
134    * Updates the config object with the new setting.
135    * @return true if the new value is set successfully
136    */

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   /**
146    * Displays the given value.
147    */

148   public void setValue(KeyStroke value) {
149     _key = value;
150     _keyField.setText(_option.format(value));
151   }
152
153   /**
154    * Compares two KeyStrokeOptionComponents based on the text of their labels.
155    * @return Comparison based on labels, or 1 if o is not a KeyStrokeOptionComponent
156    */

157   public int compareTo(Object JavaDoc 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   /**
166    * Returns the currently selected KeyStroke.
167    */

168   public KeyStroke getKeyStroke() {
169     return _key;
170   }
171
172   /**
173    * Returns the KeyStroke current set in the Config settings.
174    */

175   public KeyStroke getConfigKeyStroke() {
176     return DrJava.getConfig().getSetting(_option);
177   }
178
179   /**
180    * Return's this OptionComponent's configurable component.
181    */

182   public JComponent getComponent() { return _panel; }
183
184   /**
185    * A dialog that allows the user to type in a keystroke to be bound
186    * to the action that was clicked. If the user types a keystroke that
187    * is bound to another action, the dialog will display that information.
188    */

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 _labelsPanel;
199
private JPanel _cancelAndOKPanel;
200     private KeyStroke _currentKeyStroke;
201     private KeyStrokeOptionComponent _ksoc;
202 // private Frame frame;
203

204     public GetKeyDialog(Frame f, String JavaDoc title, boolean modal) {
205       super(f, title, modal);
206 // frame = f;
207

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       //_labelsPanel = new JPanel();
254
//_labelsPanel.add(_currentLabel);
255
//_labelsPanel.add(_actionLabel);
256

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(_labelsPanel);
267
panel.add(_currentLabel);
268       panel.add(_actionLabel);
269       panel.add(_cancelAndOKPanel);
270       this.setSize((int)_instructionLabel.getPreferredSize().getWidth() + 30, DIALOG_HEIGHT);
271       //centerOnScreen();
272
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.setLocation(frame.getLocation());
284
this.setSize((int)_instructionLabel.getPreferredSize().getWidth() + 30, DIALOG_HEIGHT);
285       MainFrame.setPopupLoc(this, getOwner());
286       this.setVisible(true);
287     }
288
289     /**
290      * A textfield that takes in one keystroke at a time and displays
291      * its formatted String version. It updates the label that displays
292      * what action the currently displayed keystroke is bound to.
293      */

294     private class InputField extends JTextField {
295       /*public boolean getFocusTraversalKeysEnabled() {
296         return false;
297       }*/

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 JavaDoc name = configKs.getLabelText();//KeyBindingManager.Singleton.getName(configKs.getConfigKeyStroke());
308
_actionLabel.setText(name);
309           }
310           _currentKeyStroke = ks;
311         }
312       }
313     }
314   }
315
316 }
317
Popular Tags