001: /*
002: * Sun Public License Notice
003: *
004: * The contents of this file are subject to the Sun Public License
005: * Version 1.0 (the "License"). You may not use this file except in
006: * compliance with the License. A copy of the License is available at
007: * http://www.sun.com/
008: *
009: * The Original Code is NetBeans. The Initial Developer of the Original
010: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
011: * Microsystems, Inc. All Rights Reserved.
012: */
013:
014: package org.netbeans.editor;
015:
016: import java.awt.Dimension;
017: import java.awt.event.KeyEvent;
018: import java.util.Vector;
019:
020: import javax.swing.KeyStroke;
021:
022: /**
023: * This class could be used as input of sequence of KeyStrokes.
024: * {@link #getKeySequence} One instance could be reused. {@link #clear} When
025: * actual keySequence changes, it fires PropertyChangeEvent of property
026: * {@link #PROP_KEYSEQUENCE}. There is additional label on the bottom, which
027: * could be set with {@link #setInfoText} to pass some information to user.
028: *
029: * @author David Konecny
030: */
031:
032: public class KeySequenceInputPanel extends javax.swing.JPanel {
033:
034: public static String PROP_KEYSEQUENCE = "keySequence"; // NOI18N
035: private Vector strokes = new Vector();
036: private StringBuffer text = new StringBuffer();
037:
038: /** Creates new form KeySequenceInputPanel with empty sequence */
039: public KeySequenceInputPanel() {
040: initComponents();
041:
042: keySequenceLabel.setDisplayedMnemonic(LocaleSupport.getString(
043: "LBL_KSIP_Sequence_Mnemonic").charAt(0)); // NOI18N
044: keySequenceInputField.getAccessibleContext()
045: .setAccessibleDescription(
046: LocaleSupport
047: .getString("ACSD_LBL_KSIP_Sequence")); // NOI18N
048: getAccessibleContext().setAccessibleName(
049: LocaleSupport.getString("MSP_AddTitle")); // NOI18N
050: getAccessibleContext().setAccessibleDescription(
051: LocaleSupport.getString("ACSD_KSIP")); // NOI18N
052: }
053:
054: /**
055: * Clears actual sequence of KeyStrokes
056: */
057: public void clear() {
058: strokes.clear();
059: text.setLength(0);
060: keySequenceInputField.setText(text.toString());
061: firePropertyChange(PROP_KEYSEQUENCE, null, null);
062: }
063:
064: /*
065: * Sets the text of JLabel locaten on the bottom of this panel
066: */
067: public void setInfoText(String s) {
068: collisionLabel.setText(s + ' '); // NOI18N
069: }
070:
071: /**
072: * Returns sequence of completed KeyStrokes as KeyStroke[]
073: */
074: public KeyStroke[] getKeySequence() {
075: return (KeyStroke[]) strokes.toArray(new KeyStroke[0]);
076: }
077:
078: /**
079: * Makes it trying to be bigger
080: */
081: public Dimension getPreferredSize() {
082: Dimension dim = super .getPreferredSize();
083:
084: if (dim.width < 400)
085: dim.width = 400;
086:
087: return dim;
088: }
089:
090: /**
091: * We're redirecting our focus to proper component.
092: */
093: public void requestFocus() {
094: keySequenceInputField.requestFocus();
095: }
096:
097: /**
098: * Visual part and event handling:
099: */
100: private void initComponents() {// GEN-BEGIN:initComponents
101: java.awt.GridBagConstraints gridBagConstraints;
102:
103: keySequenceLabel = new javax.swing.JLabel();
104: keySequenceInputField = new javax.swing.JTextField();
105: collisionLabel = new javax.swing.JTextArea();
106:
107: setLayout(new java.awt.GridBagLayout());
108:
109: setBorder(new javax.swing.border.EmptyBorder(
110: new java.awt.Insets(12, 12, 11, 11)));
111: keySequenceLabel.setText(LocaleSupport
112: .getString("LBL_KSIP_Sequence"));
113: keySequenceLabel.setBorder(new javax.swing.border.EmptyBorder(
114: new java.awt.Insets(0, 0, 0, 8)));
115: keySequenceLabel.setLabelFor(keySequenceInputField);
116: gridBagConstraints = new java.awt.GridBagConstraints();
117: gridBagConstraints.gridx = 0;
118: gridBagConstraints.gridy = 0;
119: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
120: gridBagConstraints.insets = new java.awt.Insets(0, 0, 0, 12);
121: add(keySequenceLabel, gridBagConstraints);
122:
123: keySequenceInputField
124: .addKeyListener(new java.awt.event.KeyAdapter() {
125: public void keyTyped(java.awt.event.KeyEvent evt) {
126: keySequenceInputFieldKeyTyped(evt);
127: }
128:
129: public void keyPressed(java.awt.event.KeyEvent evt) {
130: keySequenceInputFieldKeyPressed(evt);
131: }
132:
133: public void keyReleased(java.awt.event.KeyEvent evt) {
134: keySequenceInputFieldKeyReleased(evt);
135: }
136: });
137:
138: gridBagConstraints = new java.awt.GridBagConstraints();
139: gridBagConstraints.gridx = 1;
140: gridBagConstraints.gridy = 0;
141: gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
142: gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
143: gridBagConstraints.weightx = 1.0;
144: add(keySequenceInputField, gridBagConstraints);
145:
146: collisionLabel.setLineWrap(true);
147: collisionLabel.setEditable(false);
148: collisionLabel.setRows(2);
149: collisionLabel.setForeground(java.awt.Color.red);
150: collisionLabel.setBackground(getBackground());
151: collisionLabel.setDisabledTextColor(java.awt.Color.red);
152: collisionLabel.setEnabled(false);
153: gridBagConstraints = new java.awt.GridBagConstraints();
154: gridBagConstraints.gridx = 0;
155: gridBagConstraints.gridy = 1;
156: gridBagConstraints.gridwidth = 2;
157: gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
158: gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST;
159: gridBagConstraints.weightx = 1.0;
160: gridBagConstraints.weighty = 1.0;
161: gridBagConstraints.insets = new java.awt.Insets(12, 0, 0, 0);
162: add(collisionLabel, gridBagConstraints);
163:
164: }// GEN-END:initComponents
165:
166: private void keySequenceInputFieldKeyTyped(
167: java.awt.event.KeyEvent evt) {// GEN-FIRST:event_keySequenceInputFieldKeyTyped
168: evt.consume();
169: }// GEN-LAST:event_keySequenceInputFieldKeyTyped
170:
171: private void keySequenceInputFieldKeyReleased(
172: java.awt.event.KeyEvent evt) {// GEN-FIRST:event_keySequenceInputFieldKeyReleased
173: evt.consume();
174: keySequenceInputField.setText(text.toString());
175: }// GEN-LAST:event_keySequenceInputFieldKeyReleased
176:
177: private void keySequenceInputFieldKeyPressed(
178: java.awt.event.KeyEvent evt) {// GEN-FIRST:event_keySequenceInputFieldKeyPressed
179: evt.consume();
180:
181: String modif = KeyEvent.getKeyModifiersText(evt.getModifiers());
182: if (isModifier(evt.getKeyCode())) {
183: keySequenceInputField
184: .setText(text.toString() + modif + '+'); // NOI18N
185: } else {
186: KeyStroke stroke = KeyStroke.getKeyStrokeForEvent(evt);
187: strokes.add(stroke);
188: text.append(Utilities.keyStrokeToString(stroke));
189: text.append(' ');
190: keySequenceInputField.setText(text.toString());
191: firePropertyChange(PROP_KEYSEQUENCE, null, null);
192: }
193: }// GEN-LAST:event_keySequenceInputFieldKeyPressed
194:
195: private boolean isModifier(int keyCode) {
196: return (keyCode == KeyEvent.VK_ALT)
197: || (keyCode == KeyEvent.VK_ALT_GRAPH)
198: || (keyCode == KeyEvent.VK_CONTROL)
199: || (keyCode == KeyEvent.VK_SHIFT)
200: || (keyCode == KeyEvent.VK_META);
201: }
202:
203: // Variables declaration - do not modify//GEN-BEGIN:variables
204: private javax.swing.JLabel keySequenceLabel;
205: private javax.swing.JTextArea collisionLabel;
206: private javax.swing.JTextField keySequenceInputField;
207: // End of variables declaration//GEN-END:variables
208: }
|