KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > wizards > shortcut > SelectKeyboardShortcutPanel


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.apache.tools.ant.module.wizards.shortcut;
21
22 import java.awt.Component JavaDoc;
23 import java.awt.event.KeyEvent JavaDoc;
24 import java.awt.event.KeyListener JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.swing.KeyStroke JavaDoc;
28 import javax.swing.event.ChangeEvent JavaDoc;
29 import javax.swing.event.ChangeListener JavaDoc;
30 import org.openide.WizardDescriptor;
31 import org.openide.filesystems.FileObject;
32 import org.openide.filesystems.Repository;
33 import org.openide.util.HelpCtx;
34 import org.openide.util.NbBundle;
35 import org.openide.util.Utilities;
36
37 final class SelectKeyboardShortcutPanel extends javax.swing.JPanel JavaDoc implements KeyListener JavaDoc {
38
39     private KeyStroke JavaDoc stroke = null;
40     
41     private SelectKeyboardShortcutWizardPanel wiz;
42     
43     /** Create the wizard panel component and set up some basic properties. */
44     public SelectKeyboardShortcutPanel (SelectKeyboardShortcutWizardPanel wiz) {
45         this.wiz = wiz;
46         initComponents ();
47     initAccessibility ();
48         // Provide a name in the title bar.
49
setName (NbBundle.getMessage (SelectKeyboardShortcutPanel.class, "SKSP_LBL_select_shortcut_to_add"));
50         testField.addKeyListener (this);
51     }
52
53     
54     private void initAccessibility () {
55         testField.getAccessibleContext().setAccessibleName(NbBundle.getMessage (SelectKeyboardShortcutPanel.class, "ACSN_LBL_type_here"));
56         testField.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage (SelectKeyboardShortcutPanel.class, "ACSD_LBL_type_here"));
57         this.getAccessibleContext().setAccessibleDescription(NbBundle.getMessage(SelectKeyboardShortcutPanel.class, "SKSP_TEXT_press_any_key_seq"));
58     }
59     
60     // --- VISUAL DESIGN OF PANEL ---
61

62     /** This method is called from within the constructor to
63      * initialize the form.
64      * WARNING: Do NOT modify this code. The content of this method is
65      * always regenerated by the Form Editor.
66      */

67     private void initComponents() {//GEN-BEGIN:initComponents
68
hintsArea = new javax.swing.JTextArea JavaDoc();
69         mainPanel = new javax.swing.JPanel JavaDoc();
70         testField = new javax.swing.JTextField JavaDoc();
71
72         setLayout(new java.awt.BorderLayout JavaDoc());
73
74         hintsArea.setBackground(new java.awt.Color JavaDoc(204, 204, 204));
75         hintsArea.setEditable(false);
76         hintsArea.setFont(javax.swing.UIManager.getFont ("Label.font"));
77         hintsArea.setForeground(new java.awt.Color JavaDoc(102, 102, 153));
78         hintsArea.setLineWrap(true);
79         hintsArea.setText(NbBundle.getMessage(SelectKeyboardShortcutPanel.class, "SKSP_TEXT_press_any_key_seq"));
80         hintsArea.setWrapStyleWord(true);
81         hintsArea.setDisabledTextColor(javax.swing.UIManager.getColor ("Label.foreground"));
82         hintsArea.setEnabled(false);
83         hintsArea.setOpaque(false);
84         add(hintsArea, java.awt.BorderLayout.NORTH);
85
86         testField.setColumns(15);
87         testField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
88         testField.setText(NbBundle.getMessage(SelectKeyboardShortcutPanel.class, "SKSP_LBL_type_here"));
89         mainPanel.add(testField);
90
91         add(mainPanel, java.awt.BorderLayout.CENTER);
92
93     }//GEN-END:initComponents
94

95     // Variables declaration - do not modify//GEN-BEGIN:variables
96
private javax.swing.JTextArea JavaDoc hintsArea;
97     private javax.swing.JPanel JavaDoc mainPanel;
98     private javax.swing.JTextField JavaDoc testField;
99     // End of variables declaration//GEN-END:variables
100

101     // KeyListener:
102

103     public void keyPressed (KeyEvent JavaDoc e) {
104         // XXX ideally make TAB switch focus, rather than be handled...
105
stroke = KeyStroke.getKeyStroke (e.getKeyCode (), e.getModifiers ());
106         testField.setText (Utilities.keyToString (stroke));
107         wiz.fireChangeEvent ();
108         e.consume ();
109     }
110     public void keyReleased (KeyEvent JavaDoc e) {
111         e.consume ();
112     }
113     public void keyTyped (KeyEvent JavaDoc e) {
114         e.consume ();
115     }
116     
117     public static class SelectKeyboardShortcutWizardPanel implements WizardDescriptor.Panel {
118
119         private SelectKeyboardShortcutPanel panel = null;
120         private FileObject shortcutsFolder = null;
121         
122         public Component JavaDoc getComponent () {
123             return getPanel();
124         }
125         
126         private SelectKeyboardShortcutPanel getPanel() {
127             if (panel == null) {
128                 panel = new SelectKeyboardShortcutPanel(this);
129             }
130             return panel;
131         }
132
133         public HelpCtx getHelp () {
134             return HelpCtx.DEFAULT_HELP;
135         }
136
137         public boolean isValid () {
138             if (shortcutsFolder == null)
139                 shortcutsFolder = Repository.getDefault ().getDefaultFileSystem ().findResource ("Shortcuts"); // NOI18N
140
return (getPanel().stroke != null) &&
141                    (shortcutsFolder.getFileObject(Utilities.keyToString(getPanel().stroke), "instance") == null) && // NOI18N
142
(shortcutsFolder.getFileObject(Utilities.keyToString(getPanel().stroke), "xml") == null); // NOI18N
143
}
144
145         private final Set JavaDoc<ChangeListener JavaDoc> listeners = new HashSet JavaDoc<ChangeListener JavaDoc>(1);
146         public final void addChangeListener (ChangeListener JavaDoc l) {
147             synchronized (listeners) {
148                 listeners.add (l);
149             }
150         }
151         public final void removeChangeListener (ChangeListener JavaDoc l) {
152             synchronized (listeners) {
153                 listeners.remove (l);
154             }
155         }
156         protected final void fireChangeEvent () {
157             ChangeListener JavaDoc[] ls;
158             synchronized (listeners) {
159                 ls = listeners.toArray(new ChangeListener JavaDoc[listeners.size()]);
160             }
161             ChangeEvent JavaDoc ev = new ChangeEvent JavaDoc (this);
162             for (ChangeListener JavaDoc l : ls) {
163                 l.stateChanged(ev);
164             }
165         }
166
167         public void readSettings (Object JavaDoc settings) {
168             // XXX later...
169
}
170         public void storeSettings (Object JavaDoc settings) {
171             WizardDescriptor wiz = (WizardDescriptor) settings;
172             wiz.putProperty(ShortcutWizard.PROP_STROKE, getPanel().stroke);
173         }
174     }
175     
176 }
177
Popular Tags