1 6 7 package org.netbeans.modules.options.keymap; 8 9 import java.awt.Color ; 10 import java.awt.Component ; 11 import java.awt.KeyboardFocusManager ; 12 import java.awt.event.ActionEvent ; 13 import java.awt.event.ActionListener ; 14 import java.awt.event.KeyEvent ; 15 import java.awt.event.KeyListener ; 16 import java.text.MessageFormat ; 17 import java.util.Collections ; 18 import javax.swing.AbstractButton ; 19 import javax.swing.JButton ; 20 import javax.swing.JLabel ; 21 import javax.swing.KeyStroke ; 22 import org.openide.DialogDescriptor; 23 import org.openide.DialogDisplayer; 24 import org.openide.awt.Mnemonics; 25 import org.openide.util.NbBundle; 26 27 31 public class ShortcutsDialog extends javax.swing.JPanel { 32 33 public static String getShortcut (final ShortcutsFinder shortcutsFinder) { 34 final ShortcutsDialog d = new ShortcutsDialog (); 35 loc (d.lShortcut, "Shortcut"); 36 d.lConflict.setForeground (Color.red); 37 final JButton bTab = new JButton (); 38 loc (bTab, "CTL_Tab"); 39 final JButton bClear = new JButton (); 40 loc (bClear, "CTL_Clear"); 41 d.tfShortcut.setFocusTraversalKeys ( 42 KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, 43 Collections.EMPTY_SET 44 ); 45 d.tfShortcut.setFocusTraversalKeys ( 46 KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS, 47 Collections.EMPTY_SET 48 ); 49 d.tfShortcut.setFocusTraversalKeys ( 54 KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, 55 Collections.EMPTY_SET 56 ); 57 class Listener implements ActionListener , KeyListener { 58 59 private KeyStroke backspaceKS = KeyStroke.getKeyStroke 60 (KeyEvent.VK_BACK_SPACE, 0); 61 private KeyStroke tabKS = KeyStroke.getKeyStroke 62 (KeyEvent.VK_TAB, 0); 63 64 private String key = ""; 65 66 public void keyTyped (KeyEvent e) { 67 e.consume (); 68 } 69 70 public void keyPressed (KeyEvent e) { 71 KeyStroke keyStroke = KeyStroke.getKeyStroke ( 72 e.getKeyCode (), 73 e.getModifiers () 74 ); 75 76 boolean add = e.getKeyCode () != e.VK_SHIFT && 77 e.getKeyCode () != e.VK_CONTROL && 78 e.getKeyCode () != e.VK_ALT && 79 e.getKeyCode () != e.VK_META && 80 e.getKeyCode () != e.VK_ALT_GRAPH; 81 82 if (keyStroke.equals (backspaceKS) && !key.equals ("")) { 83 int i = key.lastIndexOf (' '); 85 if (i < 0) 86 key = ""; 87 else 88 key = key.substring (0, i); 89 d.tfShortcut.setText (key); 90 } else 91 addKeyStroke (keyStroke, add); 93 94 if (add) updateWarning (); 95 e.consume (); 96 } 97 98 public void keyReleased (KeyEvent e) { 99 e.consume (); 100 } 101 102 public void actionPerformed (ActionEvent e) { 103 if (e.getSource () == bClear) { 104 key = ""; 105 d.tfShortcut.setText (key); 106 } else 107 if (e.getSource () == bTab) 108 addKeyStroke (tabKS, true); 109 } 110 111 private void updateWarning () { 112 ActionImpl action = shortcutsFinder.findActionForShortcut 113 (d.tfShortcut.getText ()); 114 if (action != null) { 115 d.lConflict.setText (MessageFormat.format ( 116 loc ("Shortcut_Conflict"), 117 new Object [] {action.getDisplayName ()} 118 )); 119 } else 120 d.lConflict.setText (""); 121 } 122 123 private void addKeyStroke (KeyStroke keyStroke, boolean add) { 124 String k = Utils.getKeyStrokeAsText (keyStroke); 125 if (key.equals ("")) { 126 d.tfShortcut.setText (k); 127 if (add) key = k; 128 } else { 129 d.tfShortcut.setText (key + " " + k); 130 if (add) key += " " + k; 131 } 132 } 133 } 134 Listener listener = new Listener (); 135 d.tfShortcut.addKeyListener (listener); 136 DialogDescriptor descriptor = new DialogDescriptor ( 137 d, 138 loc ("Add_Shortcut_Dialog"), 139 true, 140 new Object [] { 141 DialogDescriptor.OK_OPTION, 142 DialogDescriptor.CANCEL_OPTION 143 }, 144 DialogDescriptor.OK_OPTION, 145 DialogDescriptor.DEFAULT_ALIGN, 146 null, 147 listener 148 ); 149 descriptor.setClosingOptions (new Object [] { 150 DialogDescriptor.OK_OPTION, 151 DialogDescriptor.CANCEL_OPTION 152 }); 153 descriptor.setAdditionalOptions (new Object [] { 154 bClear, bTab 155 }); 156 DialogDisplayer.getDefault ().notify (descriptor); 157 if (descriptor.getValue () == DialogDescriptor.OK_OPTION) 158 return d.tfShortcut.getText (); 159 return null; 160 } 161 162 private static String loc (String key) { 163 return NbBundle.getMessage (ShortcutsDialog.class, key); 164 } 165 166 private static void loc (Component c, String key) { 167 if (c instanceof AbstractButton ) 168 Mnemonics.setLocalizedText ( 169 (AbstractButton ) c, 170 loc (key) 171 ); 172 else 173 Mnemonics.setLocalizedText ( 174 (JLabel ) c, 175 loc (key) 176 ); 177 } 178 179 public interface ShortcutsFinder { 180 ActionImpl findActionForShortcut (String shortcuts); 181 } 182 183 184 185 public ShortcutsDialog() { 186 initComponents(); 187 } 188 189 194 private void initComponents() { 196 lShortcut = new javax.swing.JLabel (); 197 tfShortcut = new javax.swing.JTextField (); 198 lConflict = new javax.swing.JLabel (); 199 200 lShortcut.setText("Shortcut:"); 201 202 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 203 this.setLayout(layout); 204 layout.setHorizontalGroup( 205 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 206 .add(layout.createSequentialGroup() 207 .addContainerGap() 208 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 209 .add(lConflict, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 449, Short.MAX_VALUE) 210 .add(layout.createSequentialGroup() 211 .add(lShortcut) 212 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 213 .add(tfShortcut, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE))) 214 .addContainerGap()) 215 ); 216 layout.setVerticalGroup( 217 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 218 .add(layout.createSequentialGroup() 219 .addContainerGap() 220 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 221 .add(lShortcut) 222 .add(tfShortcut, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 223 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 224 .add(lConflict) 225 .addContainerGap(25, Short.MAX_VALUE)) 226 ); 227 } 229 230 private javax.swing.JLabel lConflict; 232 private javax.swing.JLabel lShortcut; 233 private javax.swing.JTextField tfShortcut; 234 236 } 237 | Popular Tags |