KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > ui > wizard > action > ShortcutEnterPanel


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.apisupport.project.ui.wizard.action;
21 import java.awt.Component JavaDoc;
22 import java.awt.KeyboardFocusManager JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.awt.event.ActionListener JavaDoc;
25 import java.awt.event.KeyEvent JavaDoc;
26 import java.awt.event.KeyListener JavaDoc;
27 import java.util.Collections JavaDoc;
28 import javax.swing.AbstractButton JavaDoc;
29 import javax.swing.JButton JavaDoc;
30 import javax.swing.JLabel JavaDoc;
31 import javax.swing.KeyStroke JavaDoc;
32 import org.netbeans.modules.apisupport.project.ui.UIUtil;
33 import org.openide.DialogDescriptor;
34 import org.openide.DialogDisplayer;
35 import org.openide.awt.Mnemonics;
36 import org.openide.util.NbBundle;
37
38 /**
39  *
40  * @author Radek Matous
41  */

42 public class ShortcutEnterPanel extends javax.swing.JPanel JavaDoc {
43     private final Listener JavaDoc listener = new Listener JavaDoc();
44     private final JButton JavaDoc bTab;
45     private final JButton JavaDoc bClear;
46     
47     
48     /** Creates new form ShortcutCustomizerPanel */
49     public ShortcutEnterPanel() {
50         initComponents();
51         bTab = new JButton JavaDoc();
52         bClear = new JButton JavaDoc();
53         loc(bTab, "CTL_Tab");
54         loc(bClear, "CTL_Clear");
55         tfShortcut.setFocusTraversalKeys(
56                 KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS,
57                 Collections.EMPTY_SET
58                 );
59         tfShortcut.setFocusTraversalKeys(
60                 KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS,
61                 Collections.EMPTY_SET
62                 );
63         tfShortcut.setFocusTraversalKeys(
64                 KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS,
65                 Collections.EMPTY_SET
66                 );
67         
68         tfShortcut.addKeyListener(listener);
69     }
70     
71     private String JavaDoc getTitle() {
72         return loc("LBL_AddShortcutTitle");
73     }
74     
75     private Object JavaDoc[] getAdditionalOptions() {
76         return new Object JavaDoc[] {bClear, bTab};
77     }
78     
79     private String JavaDoc getShortcutText() {
80         return tfShortcut.getText();
81     }
82     
83     
84     static KeyStroke JavaDoc[] showDialog() {
85         Object JavaDoc[] buttons = new Object JavaDoc[] {
86             DialogDescriptor.OK_OPTION,
87             DialogDescriptor.CANCEL_OPTION
88         };
89         final ShortcutEnterPanel sepPanel = new ShortcutEnterPanel();
90         
91         DialogDescriptor descriptor = new DialogDescriptor(sepPanel,sepPanel.getTitle(),
92                 true,buttons,DialogDescriptor.OK_OPTION,DialogDescriptor.DEFAULT_ALIGN, null,sepPanel.listener);
93         descriptor.setClosingOptions(new Object JavaDoc[] {
94             DialogDescriptor.OK_OPTION,
95             DialogDescriptor.CANCEL_OPTION
96         });
97         descriptor.setAdditionalOptions(sepPanel.getAdditionalOptions());
98         
99         DialogDisplayer.getDefault().notify(descriptor);
100         String JavaDoc shortcut = sepPanel.getShortcutText();
101         if (descriptor.getValue() == DialogDescriptor.OK_OPTION && shortcut != null && shortcut.trim().length() > 0) {
102             return UIUtil.stringToKeyStrokes(shortcut);//NOI18N
103

104         } else {
105             return null;
106         }
107     }
108     
109     private static void loc(Component JavaDoc c, String JavaDoc key) {
110         if (c instanceof AbstractButton JavaDoc)
111             Mnemonics.setLocalizedText(
112                     (AbstractButton JavaDoc) c,
113                     loc(key)
114                     );
115         else
116             Mnemonics.setLocalizedText(
117                     (JLabel JavaDoc) c,
118                     loc(key)
119                     );
120     }
121     
122     private static String JavaDoc loc(String JavaDoc key) {
123         return NbBundle.getMessage(ShortcutEnterPanel.class, key);
124     }
125     
126     private class Listener implements ActionListener JavaDoc, KeyListener JavaDoc {
127         
128         private KeyStroke JavaDoc backspaceKS = KeyStroke.getKeyStroke
129                 (KeyEvent.VK_BACK_SPACE, 0);
130         private KeyStroke JavaDoc tabKS = KeyStroke.getKeyStroke
131                 (KeyEvent.VK_TAB, 0);
132         
133         private String JavaDoc key = "";
134         
135         public void keyTyped(KeyEvent JavaDoc e) {
136             e.consume();
137         }
138         
139         public void keyPressed(KeyEvent JavaDoc e) {
140             KeyStroke JavaDoc keyStroke = KeyStroke.getKeyStroke(
141                     e.getKeyCode(),
142                     e.getModifiers()
143                     );
144             
145             boolean add = e.getKeyCode() != e.VK_SHIFT &&
146                     e.getKeyCode() != e.VK_CONTROL &&
147                     e.getKeyCode() != e.VK_ALT &&
148                     e.getKeyCode() != e.VK_META &&
149                     e.getKeyCode() != e.VK_ALT_GRAPH;
150             
151             if (keyStroke.equals(backspaceKS) && !key.equals("")) {
152                 // delete last key
153
int i = key.lastIndexOf(' ');
154                 if (i < 0)
155                     key = "";
156                 else
157                     key = key.substring(0, i);
158                 tfShortcut.setText(key);
159             } else
160                 // add key
161
addKeyStroke(keyStroke, add);
162             
163             e.consume();
164         }
165         
166         public void keyReleased(KeyEvent JavaDoc e) {
167             e.consume();
168         }
169         
170         public void actionPerformed(ActionEvent JavaDoc e) {
171             if (e.getSource() == bClear) {
172                 key = "";
173                 tfShortcut.setText(key);
174                 tfShortcut.requestFocusInWindow();
175             } else if (e.getSource() == bTab) {
176                 addKeyStroke(tabKS, true);
177                 tfShortcut.requestFocusInWindow();
178             }
179         }
180         
181         
182         private void addKeyStroke(KeyStroke JavaDoc keyStroke, boolean add) {
183             String JavaDoc k = UIUtil.keyStrokeToString(keyStroke);
184             if (key.equals("")) {
185                 tfShortcut.setText(k);
186                 if (add) key = k;
187             } else {
188                 tfShortcut.setText(key + " " + k);
189                 if (add) key += " " + k;
190             }
191         }
192     }
193     /** This method is called from within the constructor to
194      * initialize the form.
195      * WARNING: Do NOT modify this code. The content of this method is
196      * always regenerated by the Form Editor.
197      */

198     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
199
private void initComponents() {
200         tfShortcut = new javax.swing.JTextField JavaDoc();
201
202         org.openide.awt.Mnemonics.setLocalizedText(tfShortcutLabel, java.util.ResourceBundle.getBundle("org/netbeans/modules/apisupport/project/ui/wizard/action/Bundle").getString("LBL_Shortcut"));
203
204         org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
205         this.setLayout(layout);
206         layout.setHorizontalGroup(
207             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
208             .add(layout.createSequentialGroup()
209                 .addContainerGap()
210                 .add(tfShortcutLabel)
211                 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
212                 .add(tfShortcut, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
213                 .addContainerGap())
214         );
215         layout.setVerticalGroup(
216             layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
217             .add(layout.createSequentialGroup()
218                 .addContainerGap()
219                 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
220                     .add(tfShortcutLabel)
221                     .add(tfShortcut, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
222                 .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
223         );
224     }// </editor-fold>//GEN-END:initComponents
225

226     
227     // Variables declaration - do not modify//GEN-BEGIN:variables
228
private javax.swing.JTextField JavaDoc tfShortcut;
229     private final javax.swing.JLabel JavaDoc tfShortcutLabel = new javax.swing.JLabel JavaDoc();
230     // End of variables declaration//GEN-END:variables
231

232 }
233
Popular Tags