KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > KeySequenceInputPanel


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.editor;
21
22 import java.util.ResourceBundle JavaDoc;
23 import java.util.Vector JavaDoc;
24 import java.awt.Dimension JavaDoc;
25 import java.awt.event.*;
26 import javax.swing.KeyStroke JavaDoc;
27 import org.openide.util.NbBundle;
28
29 /**
30  * This class could be used as input of sequence of KeyStrokes.
31  * {@link #getKeySequence}
32  * One instance could be reused.
33  * {@link #clear}
34  * When actual keySequence changes, it fires PropertyChangeEvent
35  * of property {@link #PROP_KEYSEQUENCE}.
36  * There is additional label on the bottom, which could be set
37  * with {@link #setInfoText} to pass some information to user.
38  *
39  * @author David Konecny
40  */

41
42 public class KeySequenceInputPanel extends javax.swing.JPanel JavaDoc {
43
44     public final static String JavaDoc PROP_KEYSEQUENCE = "keySequence"; // NOI18N
45
private Vector JavaDoc strokes = new Vector JavaDoc();
46     private StringBuffer JavaDoc text = new StringBuffer JavaDoc();
47     private final ResourceBundle JavaDoc bundle = NbBundle.getBundle(BaseKit.class);
48
49     /** Creates new form KeySequenceInputPanel with empty sequence*/
50     public KeySequenceInputPanel() {
51         initComponents ();
52         
53         
54         keySequenceLabel.setDisplayedMnemonic(bundle.getString("LBL_KSIP_Sequence_Mnemonic").charAt(0)); // NOI18N
55
keySequenceInputField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_LBL_KSIP_Sequence")); // NOI18N
56
getAccessibleContext().setAccessibleName(bundle.getString("MSP_AddTitle")); // NOI18N
57
getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_KSIP")); // NOI18N
58

59         keySequenceInputField.setFocusTraversalKeysEnabled(false);
60     }
61
62     /**
63      * Clears actual sequence of KeyStrokes
64      */

65     public void clear() {
66         strokes.clear();
67         text.setLength( 0 );
68         keySequenceInputField.setText( text.toString() );
69         firePropertyChange( PROP_KEYSEQUENCE, null, null );
70     }
71
72     /*
73      * Sets the text of JLabel locaten on the bottom of this panel
74      */

75     public void setInfoText( String JavaDoc s ) {
76         collisionLabel.setText( s + ' ' ); // NOI18N
77
}
78
79     /**
80      * Returns sequence of completed KeyStrokes as KeyStroke[]
81      */

82     public KeyStroke JavaDoc[] getKeySequence() {
83         return (KeyStroke JavaDoc[])strokes.toArray( new KeyStroke JavaDoc[0] );
84     }
85
86     /**
87      * Makes it trying to be bigger
88      */

89     public Dimension JavaDoc getPreferredSize() {
90         Dimension JavaDoc dim = super.getPreferredSize();
91         
92         if (dim.width < 400)
93             dim.width = 400;
94         
95         return dim;
96     }
97
98     /**
99      * We're redirecting our focus to proper component.
100      */

101     public void requestFocus() {
102         keySequenceInputField.requestFocus();
103     }
104
105     /**
106      * Visual part and event handling:
107      */

108     // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
109
private void initComponents() {
110         java.awt.GridBagConstraints JavaDoc gridBagConstraints;
111
112         keySequenceLabel = new javax.swing.JLabel JavaDoc();
113         keySequenceInputField = new javax.swing.JTextField JavaDoc();
114         collisionLabel = new javax.swing.JTextArea JavaDoc();
115
116         setLayout(new java.awt.GridBagLayout JavaDoc());
117
118         setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 11, 11));
119         keySequenceLabel.setLabelFor(keySequenceInputField);
120         keySequenceLabel.setText(bundle.getString( "LBL_KSIP_Sequence" ));
121         keySequenceLabel.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 8));
122         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
123         gridBagConstraints.gridx = 0;
124         gridBagConstraints.gridy = 0;
125         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
126         gridBagConstraints.insets = new java.awt.Insets JavaDoc(0, 0, 0, 12);
127         add(keySequenceLabel, gridBagConstraints);
128
129         keySequenceInputField.addKeyListener(new java.awt.event.KeyAdapter JavaDoc() {
130             public void keyTyped(java.awt.event.KeyEvent JavaDoc evt) {
131                 keySequenceInputFieldKeyTyped(evt);
132             }
133             public void keyPressed(java.awt.event.KeyEvent JavaDoc evt) {
134                 keySequenceInputFieldKeyPressed(evt);
135             }
136             public void keyReleased(java.awt.event.KeyEvent JavaDoc evt) {
137                 keySequenceInputFieldKeyReleased(evt);
138             }
139         });
140
141         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
142         gridBagConstraints.gridx = 1;
143         gridBagConstraints.gridy = 0;
144         gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
145         gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
146         gridBagConstraints.weightx = 1.0;
147         add(keySequenceInputField, gridBagConstraints);
148
149         collisionLabel.setLineWrap(true);
150         collisionLabel.setEditable(false);
151         collisionLabel.setRows(2);
152         collisionLabel.setForeground(java.awt.Color.red);
153         collisionLabel.setBackground(getBackground());
154         collisionLabel.setDisabledTextColor(java.awt.Color.red);
155         collisionLabel.setEnabled(false);
156         gridBagConstraints = new java.awt.GridBagConstraints JavaDoc();
157         gridBagConstraints.gridx = 0;
158         gridBagConstraints.gridy = 1;
159         gridBagConstraints.gridwidth = 2;
160         gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
161         gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
162         gridBagConstraints.weightx = 1.0;
163         gridBagConstraints.weighty = 1.0;
164         gridBagConstraints.insets = new java.awt.Insets JavaDoc(12, 0, 0, 0);
165         add(collisionLabel, gridBagConstraints);
166
167     }// </editor-fold>//GEN-END:initComponents
168

169     private void keySequenceInputFieldKeyTyped (java.awt.event.KeyEvent JavaDoc evt) {//GEN-FIRST:event_keySequenceInputFieldKeyTyped
170
evt.consume();
171     }//GEN-LAST:event_keySequenceInputFieldKeyTyped
172

173     private void keySequenceInputFieldKeyReleased (java.awt.event.KeyEvent JavaDoc evt) {//GEN-FIRST:event_keySequenceInputFieldKeyReleased
174
evt.consume();
175         keySequenceInputField.setText( text.toString() );
176     }//GEN-LAST:event_keySequenceInputFieldKeyReleased
177

178     private void keySequenceInputFieldKeyPressed (java.awt.event.KeyEvent JavaDoc evt) {//GEN-FIRST:event_keySequenceInputFieldKeyPressed
179

180         String JavaDoc inputText = keySequenceInputField.getText();
181         if (evt.getModifiers() == 0 &&
182                 KeyStroke.getKeyStroke(KeyEvent.VK_TAB,0).equals(KeyStroke.getKeyStrokeForEvent( evt )) &&
183                 inputText!=null && inputText.length()>0){
184             keySequenceInputField.transferFocus();
185             return;
186         }
187         
188         evt.consume();
189
190         String JavaDoc modif = KeyEvent.getKeyModifiersText( evt.getModifiers() );
191         if( isModifier( evt.getKeyCode() ) ) {
192             keySequenceInputField.setText( text.toString() + modif + '+' ); //NOI18N
193
} else {
194             KeyStroke JavaDoc stroke = KeyStroke.getKeyStrokeForEvent( evt );
195             strokes.add( stroke );
196             text.append( Utilities.keyStrokeToString( stroke ) );
197             text.append( ' ' );
198             keySequenceInputField.setText( text.toString() );
199             firePropertyChange( PROP_KEYSEQUENCE, null, null );
200         }
201     }//GEN-LAST:event_keySequenceInputFieldKeyPressed
202

203     private boolean isModifier( int keyCode ) {
204         return (keyCode == KeyEvent.VK_ALT) ||
205                (keyCode == KeyEvent.VK_ALT_GRAPH) ||
206                (keyCode == KeyEvent.VK_CONTROL) ||
207                (keyCode == KeyEvent.VK_SHIFT) ||
208                (keyCode == KeyEvent.VK_META);
209     }
210
211
212     // Variables declaration - do not modify//GEN-BEGIN:variables
213
public javax.swing.JTextArea JavaDoc collisionLabel;
214     public javax.swing.JTextField JavaDoc keySequenceInputField;
215     public javax.swing.JLabel JavaDoc keySequenceLabel;
216     // End of variables declaration//GEN-END:variables
217
}
218
Popular Tags