KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > options > keymap > ShortcutsDialog


1 /*
2  * ShortcutsDialog1.java
3  *
4  * Created on February 20, 2006, 2:51 PM
5  */

6
7 package org.netbeans.modules.options.keymap;
8
9 import java.awt.Color JavaDoc;
10 import java.awt.Component JavaDoc;
11 import java.awt.KeyboardFocusManager JavaDoc;
12 import java.awt.event.ActionEvent JavaDoc;
13 import java.awt.event.ActionListener JavaDoc;
14 import java.awt.event.KeyEvent JavaDoc;
15 import java.awt.event.KeyListener JavaDoc;
16 import java.text.MessageFormat JavaDoc;
17 import java.util.Collections JavaDoc;
18 import javax.swing.AbstractButton JavaDoc;
19 import javax.swing.JButton JavaDoc;
20 import javax.swing.JLabel JavaDoc;
21 import javax.swing.KeyStroke JavaDoc;
22 import org.openide.DialogDescriptor;
23 import org.openide.DialogDisplayer;
24 import org.openide.awt.Mnemonics;
25 import org.openide.util.NbBundle;
26
27 /**
28  *
29  * @author Jan Jancura
30  */

31 public class ShortcutsDialog extends javax.swing.JPanel JavaDoc {
32     
33     public static String JavaDoc getShortcut (final ShortcutsFinder shortcutsFinder) {
34         final ShortcutsDialog d = new ShortcutsDialog ();
35         loc (d.lShortcut, "Shortcut");
36         d.lConflict.setForeground (Color.red);
37         final JButton JavaDoc bTab = new JButton JavaDoc ();
38         loc (bTab, "CTL_Tab");
39         final JButton JavaDoc bClear = new JButton JavaDoc ();
40         loc (bClear, "CTL_Clear");
41         d.tfShortcut.setFocusTraversalKeys (
42             KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
43             Collections.EMPTY_SET
44         );
45         d.tfShortcut.setFocusTraversalKeys (
46             KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS,
47             Collections.EMPTY_SET
48         );
49 // tfShortcut.setFocusTraversalKeys (
50
// KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS,
51
// Collections.EMPTY_SET
52
// );
53
d.tfShortcut.setFocusTraversalKeys (
54             KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS,
55             Collections.EMPTY_SET
56         );
57         class Listener implements ActionListener JavaDoc, KeyListener JavaDoc {
58
59             private KeyStroke JavaDoc backspaceKS = KeyStroke.getKeyStroke
60                 (KeyEvent.VK_BACK_SPACE, 0);
61             private KeyStroke JavaDoc tabKS = KeyStroke.getKeyStroke
62                 (KeyEvent.VK_TAB, 0);
63             
64             private String JavaDoc key = "";
65
66             public void keyTyped (KeyEvent JavaDoc e) {
67                 e.consume ();
68             }
69
70             public void keyPressed (KeyEvent JavaDoc e) {
71                 KeyStroke JavaDoc keyStroke = KeyStroke.getKeyStroke (
72                     e.getKeyCode (),
73                     e.getModifiers ()
74                 );
75                 
76                 boolean add = e.getKeyCode () != e.VK_SHIFT &&
77                               e.getKeyCode () != e.VK_CONTROL &&
78                               e.getKeyCode () != e.VK_ALT &&
79                               e.getKeyCode () != e.VK_META &&
80                               e.getKeyCode () != e.VK_ALT_GRAPH;
81                 
82                 if (keyStroke.equals (backspaceKS) && !key.equals ("")) {
83                     // delete last key
84
int i = key.lastIndexOf (' ');
85                     if (i < 0)
86                         key = "";
87                     else
88                         key = key.substring (0, i);
89                     d.tfShortcut.setText (key);
90                 } else
91                     // add key
92
addKeyStroke (keyStroke, add);
93                 
94                 if (add) updateWarning ();
95                 e.consume ();
96             }
97
98             public void keyReleased (KeyEvent JavaDoc e) {
99                 e.consume ();
100             }
101             
102             public void actionPerformed (ActionEvent JavaDoc e) {
103                 if (e.getSource () == bClear) {
104                     key = "";
105                     d.tfShortcut.setText (key);
106                 } else
107                 if (e.getSource () == bTab)
108                     addKeyStroke (tabKS, true);
109             }
110             
111             private void updateWarning () {
112                 ActionImpl action = shortcutsFinder.findActionForShortcut
113                     (d.tfShortcut.getText ());
114                 if (action != null) {
115                     d.lConflict.setText (MessageFormat.format (
116                         loc ("Shortcut_Conflict"),
117                         new Object JavaDoc[] {action.getDisplayName ()}
118                     ));
119                 } else
120                     d.lConflict.setText ("");
121             }
122             
123             private void addKeyStroke (KeyStroke JavaDoc keyStroke, boolean add) {
124                 String JavaDoc k = Utils.getKeyStrokeAsText (keyStroke);
125                 if (key.equals ("")) {
126                     d.tfShortcut.setText (k);
127                     if (add) key = k;
128                 } else {
129                     d.tfShortcut.setText (key + " " + k);
130                     if (add) key += " " + k;
131                 }
132             }
133         }
134         Listener JavaDoc listener = new Listener JavaDoc ();
135         d.tfShortcut.addKeyListener (listener);
136         DialogDescriptor descriptor = new DialogDescriptor (
137             d,
138             loc ("Add_Shortcut_Dialog"),
139             true,
140             new Object JavaDoc[] {
141                 DialogDescriptor.OK_OPTION,
142                 DialogDescriptor.CANCEL_OPTION
143             },
144             DialogDescriptor.OK_OPTION,
145             DialogDescriptor.DEFAULT_ALIGN,
146             null,
147             listener
148         );
149         descriptor.setClosingOptions (new Object JavaDoc[] {
150             DialogDescriptor.OK_OPTION,
151             DialogDescriptor.CANCEL_OPTION
152         });
153         descriptor.setAdditionalOptions (new Object JavaDoc [] {
154             bClear, bTab
155         });
156         DialogDisplayer.getDefault ().notify (descriptor);
157         if (descriptor.getValue () == DialogDescriptor.OK_OPTION)
158             return d.tfShortcut.getText ();
159         return null;
160     }
161     
162     private static String JavaDoc loc (String JavaDoc key) {
163         return NbBundle.getMessage (ShortcutsDialog.class, key);
164     }
165     
166     private static void loc (Component JavaDoc c, String JavaDoc key) {
167         if (c instanceof AbstractButton JavaDoc)
168             Mnemonics.setLocalizedText (
169                 (AbstractButton JavaDoc) c,
170                 loc (key)
171             );
172         else
173             Mnemonics.setLocalizedText (
174                 (JLabel JavaDoc) c,
175                 loc (key)
176             );
177     }
178     
179     public interface ShortcutsFinder {
180         ActionImpl findActionForShortcut (String JavaDoc shortcuts);
181     }
182     
183     
184     /** Creates new form ShortcutsDialog1 */
185     public ShortcutsDialog() {
186         initComponents();
187     }
188     
189     /** This method is called from within the constructor to
190      * initialize the form.
191      * WARNING: Do NOT modify this code. The content of this method is
192      * always regenerated by the Form Editor.
193      */

194     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
195
private void initComponents() {
196         lShortcut = new javax.swing.JLabel JavaDoc();
197         tfShortcut = new javax.swing.JTextField JavaDoc();
198         lConflict = new javax.swing.JLabel JavaDoc();
199
200         lShortcut.setText("Shortcut:");
201
202         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
203         this.setLayout(layout);
204         layout.setHorizontalGroup(
205             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
206             .add(layout.createSequentialGroup()
207                 .addContainerGap()
208                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
209                     .add(lConflict, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE)
210                     .add(layout.createSequentialGroup()
211                         .add(lShortcut)
212                         .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
213                         .add(tfShortcut, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)))
214                 .addContainerGap())
215         );
216         layout.setVerticalGroup(
217             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
218             .add(layout.createSequentialGroup()
219                 .addContainerGap()
220                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
221                     .add(lShortcut)
222                     .add(tfShortcut, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
223                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
224                 .add(lConflict)
225                 .addContainerGap(25, Short.MAX_VALUE))
226         );
227     }// </editor-fold>//GEN-END:initComponents
228

229     
230     // Variables declaration - do not modify//GEN-BEGIN:variables
231
private javax.swing.JLabel JavaDoc lConflict;
232     private javax.swing.JLabel JavaDoc lShortcut;
233     private javax.swing.JTextField JavaDoc tfShortcut;
234     // End of variables declaration//GEN-END:variables
235

236 }
237
Popular Tags