1 19 20 package org.netbeans.editor; 21 22 import java.util.ResourceBundle ; 23 import java.util.Vector ; 24 import java.awt.Dimension ; 25 import java.awt.event.*; 26 import javax.swing.KeyStroke ; 27 import org.openide.util.NbBundle; 28 29 41 42 public class KeySequenceInputPanel extends javax.swing.JPanel { 43 44 public final static String PROP_KEYSEQUENCE = "keySequence"; private Vector strokes = new Vector (); 46 private StringBuffer text = new StringBuffer (); 47 private final ResourceBundle bundle = NbBundle.getBundle(BaseKit.class); 48 49 50 public KeySequenceInputPanel() { 51 initComponents (); 52 53 54 keySequenceLabel.setDisplayedMnemonic(bundle.getString("LBL_KSIP_Sequence_Mnemonic").charAt(0)); keySequenceInputField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_LBL_KSIP_Sequence")); getAccessibleContext().setAccessibleName(bundle.getString("MSP_AddTitle")); getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_KSIP")); 59 keySequenceInputField.setFocusTraversalKeysEnabled(false); 60 } 61 62 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 75 public void setInfoText( String s ) { 76 collisionLabel.setText( s + ' ' ); } 78 79 82 public KeyStroke [] getKeySequence() { 83 return (KeyStroke [])strokes.toArray( new KeyStroke [0] ); 84 } 85 86 89 public Dimension getPreferredSize() { 90 Dimension dim = super.getPreferredSize(); 91 92 if (dim.width < 400) 93 dim.width = 400; 94 95 return dim; 96 } 97 98 101 public void requestFocus() { 102 keySequenceInputField.requestFocus(); 103 } 104 105 108 private void initComponents() { 110 java.awt.GridBagConstraints gridBagConstraints; 111 112 keySequenceLabel = new javax.swing.JLabel (); 113 keySequenceInputField = new javax.swing.JTextField (); 114 collisionLabel = new javax.swing.JTextArea (); 115 116 setLayout(new java.awt.GridBagLayout ()); 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 (); 123 gridBagConstraints.gridx = 0; 124 gridBagConstraints.gridy = 0; 125 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 126 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 127 add(keySequenceLabel, gridBagConstraints); 128 129 keySequenceInputField.addKeyListener(new java.awt.event.KeyAdapter () { 130 public void keyTyped(java.awt.event.KeyEvent evt) { 131 keySequenceInputFieldKeyTyped(evt); 132 } 133 public void keyPressed(java.awt.event.KeyEvent evt) { 134 keySequenceInputFieldKeyPressed(evt); 135 } 136 public void keyReleased(java.awt.event.KeyEvent evt) { 137 keySequenceInputFieldKeyReleased(evt); 138 } 139 }); 140 141 gridBagConstraints = new java.awt.GridBagConstraints (); 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 (); 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 (12, 0, 0, 0); 165 add(collisionLabel, gridBagConstraints); 166 167 } 169 private void keySequenceInputFieldKeyTyped (java.awt.event.KeyEvent evt) { evt.consume(); 171 } 173 private void keySequenceInputFieldKeyReleased (java.awt.event.KeyEvent evt) { evt.consume(); 175 keySequenceInputField.setText( text.toString() ); 176 } 178 private void keySequenceInputFieldKeyPressed (java.awt.event.KeyEvent evt) { 180 String 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 modif = KeyEvent.getKeyModifiersText( evt.getModifiers() ); 191 if( isModifier( evt.getKeyCode() ) ) { 192 keySequenceInputField.setText( text.toString() + modif + '+' ); } else { 194 KeyStroke 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 } 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 public javax.swing.JTextArea collisionLabel; 214 public javax.swing.JTextField keySequenceInputField; 215 public javax.swing.JLabel keySequenceLabel; 216 } 218 | Popular Tags |