1 19 20 package org.netbeans.modules.apisupport.project.ui.wizard.action; 21 import java.awt.Component ; 22 import java.awt.KeyboardFocusManager ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.awt.event.KeyEvent ; 26 import java.awt.event.KeyListener ; 27 import java.util.Collections ; 28 import javax.swing.AbstractButton ; 29 import javax.swing.JButton ; 30 import javax.swing.JLabel ; 31 import javax.swing.KeyStroke ; 32 import org.netbeans.modules.apisupport.project.ui.UIUtil; 33 import org.openide.DialogDescriptor; 34 import org.openide.DialogDisplayer; 35 import org.openide.awt.Mnemonics; 36 import org.openide.util.NbBundle; 37 38 42 public class ShortcutEnterPanel extends javax.swing.JPanel { 43 private final Listener listener = new Listener (); 44 private final JButton bTab; 45 private final JButton bClear; 46 47 48 49 public ShortcutEnterPanel() { 50 initComponents(); 51 bTab = new JButton (); 52 bClear = new JButton (); 53 loc(bTab, "CTL_Tab"); 54 loc(bClear, "CTL_Clear"); 55 tfShortcut.setFocusTraversalKeys( 56 KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, 57 Collections.EMPTY_SET 58 ); 59 tfShortcut.setFocusTraversalKeys( 60 KeyboardFocusManager.DOWN_CYCLE_TRAVERSAL_KEYS, 61 Collections.EMPTY_SET 62 ); 63 tfShortcut.setFocusTraversalKeys( 64 KeyboardFocusManager.UP_CYCLE_TRAVERSAL_KEYS, 65 Collections.EMPTY_SET 66 ); 67 68 tfShortcut.addKeyListener(listener); 69 } 70 71 private String getTitle() { 72 return loc("LBL_AddShortcutTitle"); 73 } 74 75 private Object [] getAdditionalOptions() { 76 return new Object [] {bClear, bTab}; 77 } 78 79 private String getShortcutText() { 80 return tfShortcut.getText(); 81 } 82 83 84 static KeyStroke [] showDialog() { 85 Object [] buttons = new Object [] { 86 DialogDescriptor.OK_OPTION, 87 DialogDescriptor.CANCEL_OPTION 88 }; 89 final ShortcutEnterPanel sepPanel = new ShortcutEnterPanel(); 90 91 DialogDescriptor descriptor = new DialogDescriptor(sepPanel,sepPanel.getTitle(), 92 true,buttons,DialogDescriptor.OK_OPTION,DialogDescriptor.DEFAULT_ALIGN, null,sepPanel.listener); 93 descriptor.setClosingOptions(new Object [] { 94 DialogDescriptor.OK_OPTION, 95 DialogDescriptor.CANCEL_OPTION 96 }); 97 descriptor.setAdditionalOptions(sepPanel.getAdditionalOptions()); 98 99 DialogDisplayer.getDefault().notify(descriptor); 100 String shortcut = sepPanel.getShortcutText(); 101 if (descriptor.getValue() == DialogDescriptor.OK_OPTION && shortcut != null && shortcut.trim().length() > 0) { 102 return UIUtil.stringToKeyStrokes(shortcut); 104 } else { 105 return null; 106 } 107 } 108 109 private static void loc(Component c, String key) { 110 if (c instanceof AbstractButton ) 111 Mnemonics.setLocalizedText( 112 (AbstractButton ) c, 113 loc(key) 114 ); 115 else 116 Mnemonics.setLocalizedText( 117 (JLabel ) c, 118 loc(key) 119 ); 120 } 121 122 private static String loc(String key) { 123 return NbBundle.getMessage(ShortcutEnterPanel.class, key); 124 } 125 126 private class Listener implements ActionListener , KeyListener { 127 128 private KeyStroke backspaceKS = KeyStroke.getKeyStroke 129 (KeyEvent.VK_BACK_SPACE, 0); 130 private KeyStroke tabKS = KeyStroke.getKeyStroke 131 (KeyEvent.VK_TAB, 0); 132 133 private String key = ""; 134 135 public void keyTyped(KeyEvent e) { 136 e.consume(); 137 } 138 139 public void keyPressed(KeyEvent e) { 140 KeyStroke keyStroke = KeyStroke.getKeyStroke( 141 e.getKeyCode(), 142 e.getModifiers() 143 ); 144 145 boolean add = e.getKeyCode() != e.VK_SHIFT && 146 e.getKeyCode() != e.VK_CONTROL && 147 e.getKeyCode() != e.VK_ALT && 148 e.getKeyCode() != e.VK_META && 149 e.getKeyCode() != e.VK_ALT_GRAPH; 150 151 if (keyStroke.equals(backspaceKS) && !key.equals("")) { 152 int i = key.lastIndexOf(' '); 154 if (i < 0) 155 key = ""; 156 else 157 key = key.substring(0, i); 158 tfShortcut.setText(key); 159 } else 160 addKeyStroke(keyStroke, add); 162 163 e.consume(); 164 } 165 166 public void keyReleased(KeyEvent e) { 167 e.consume(); 168 } 169 170 public void actionPerformed(ActionEvent e) { 171 if (e.getSource() == bClear) { 172 key = ""; 173 tfShortcut.setText(key); 174 tfShortcut.requestFocusInWindow(); 175 } else if (e.getSource() == bTab) { 176 addKeyStroke(tabKS, true); 177 tfShortcut.requestFocusInWindow(); 178 } 179 } 180 181 182 private void addKeyStroke(KeyStroke keyStroke, boolean add) { 183 String k = UIUtil.keyStrokeToString(keyStroke); 184 if (key.equals("")) { 185 tfShortcut.setText(k); 186 if (add) key = k; 187 } else { 188 tfShortcut.setText(key + " " + k); 189 if (add) key += " " + k; 190 } 191 } 192 } 193 198 private void initComponents() { 200 tfShortcut = new javax.swing.JTextField (); 201 202 org.openide.awt.Mnemonics.setLocalizedText(tfShortcutLabel, java.util.ResourceBundle.getBundle("org/netbeans/modules/apisupport/project/ui/wizard/action/Bundle").getString("LBL_Shortcut")); 203 204 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 205 this.setLayout(layout); 206 layout.setHorizontalGroup( 207 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 208 .add(layout.createSequentialGroup() 209 .addContainerGap() 210 .add(tfShortcutLabel) 211 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 212 .add(tfShortcut, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE) 213 .addContainerGap()) 214 ); 215 layout.setVerticalGroup( 216 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 217 .add(layout.createSequentialGroup() 218 .addContainerGap() 219 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 220 .add(tfShortcutLabel) 221 .add(tfShortcut, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 222 .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) 223 ); 224 } 226 227 private javax.swing.JTextField tfShortcut; 229 private final javax.swing.JLabel tfShortcutLabel = new javax.swing.JLabel (); 230 232 } 233 | Popular Tags |