1 19 20 package org.netbeans.editor; 21 22 23 import java.awt.Component ; 24 import java.awt.Dimension ; 25 import java.awt.Insets ; 26 import java.awt.Dialog ; 27 import java.awt.event.ActionListener ; 28 import java.awt.event.ActionEvent ; 29 import java.beans.PropertyChangeEvent ; 30 import java.beans.PropertyChangeSupport ; 31 import java.beans.PropertyChangeListener ; 32 import java.text.MessageFormat ; 33 34 import javax.swing.event.DocumentListener ; 35 import javax.swing.event.DocumentEvent ; 36 import javax.swing.JLabel ; 37 import javax.swing.JList ; 38 import javax.swing.JButton ; 39 import javax.swing.JDialog ; 40 import javax.swing.ListCellRenderer ; 41 import javax.swing.KeyStroke ; 42 import javax.swing.border.*; 43 import java.util.*; 44 import org.openide.util.NbBundle; 45 46 52 public class MacroSavePanel extends javax.swing.JPanel { 53 54 private final ResourceBundle bundle = NbBundle.getBundle(BaseKit.class); 55 private Vector bindings = new Vector(); 56 private Class kitClass; 57 58 59 public MacroSavePanel( Class kitClass ) { 60 this.kitClass = kitClass; 61 initComponents (); 62 63 nameLabel.setDisplayedMnemonic(bundle.getString("MSP_Name_Mnemonic").charAt(0)); macroLabel.setDisplayedMnemonic(bundle.getString("MSP_Macro_Mnemonic").charAt(0)); bindingLabel.setDisplayedMnemonic(bundle.getString("MSP_Keys_Mnemonic").charAt(0)); nameField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MSP_Name")); macroField.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MSP_Macro")); bindingList.getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MSP_Keys")); getAccessibleContext().setAccessibleName(bundle.getString("MDS_title")); getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MSP")); 72 setMaximumSize( new Dimension ( 400, 200 ) ); 74 } 75 76 public Dimension getPreferredSize() { 77 Dimension pref = super.getPreferredSize(); 78 Dimension max = getMaximumSize(); 79 if( pref.width > max.width ) pref.width = max.width; 80 if( pref.height > max.height ) pref.height = max.height; 81 return pref; 82 } 83 84 private void initComponents() { 86 java.awt.GridBagConstraints gridBagConstraints; 87 88 macroPanel = new javax.swing.JPanel (); 89 nameLabel = new javax.swing.JLabel (); 90 macroLabel = new javax.swing.JLabel (); 91 nameField = new javax.swing.JTextField (); 92 macroField = new javax.swing.JTextField (); 93 bindingPanel = new javax.swing.JPanel (); 94 bindingLabel = new javax.swing.JLabel (); 95 bindingScrollPane = new javax.swing.JScrollPane (); 96 bindingList = new javax.swing.JList (); 97 addButton = new javax.swing.JButton (); 98 removeButton = new javax.swing.JButton (); 99 100 setLayout(new java.awt.GridBagLayout ()); 101 102 setBorder(javax.swing.BorderFactory.createEmptyBorder(12, 12, 11, 11)); 103 macroPanel.setLayout(new java.awt.GridBagLayout ()); 104 105 nameLabel.setLabelFor(nameField); 106 nameLabel.setText(bundle.getString( "MSP_Name")); 107 gridBagConstraints = new java.awt.GridBagConstraints (); 108 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 109 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 110 macroPanel.add(nameLabel, gridBagConstraints); 111 112 macroLabel.setLabelFor(macroField); 113 macroLabel.setText(bundle.getString( "MSP_Macro")); 114 gridBagConstraints = new java.awt.GridBagConstraints (); 115 gridBagConstraints.gridx = 0; 116 gridBagConstraints.gridy = 1; 117 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 118 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 12); 119 macroPanel.add(macroLabel, gridBagConstraints); 120 121 gridBagConstraints = new java.awt.GridBagConstraints (); 122 gridBagConstraints.gridx = 1; 123 gridBagConstraints.gridy = 0; 124 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 125 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 126 gridBagConstraints.weightx = 1.0; 127 macroPanel.add(nameField, gridBagConstraints); 128 129 gridBagConstraints = new java.awt.GridBagConstraints (); 130 gridBagConstraints.gridx = 1; 131 gridBagConstraints.gridy = 1; 132 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 133 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 134 gridBagConstraints.weightx = 1.0; 135 gridBagConstraints.insets = new java.awt.Insets (5, 0, 0, 0); 136 macroPanel.add(macroField, gridBagConstraints); 137 138 gridBagConstraints = new java.awt.GridBagConstraints (); 139 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 140 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 141 add(macroPanel, gridBagConstraints); 142 143 bindingPanel.setLayout(new java.awt.GridBagLayout ()); 144 145 bindingLabel.setLabelFor(bindingList); 146 bindingLabel.setText(bundle.getString("MSP_Keys")); 147 gridBagConstraints = new java.awt.GridBagConstraints (); 148 gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST; 149 gridBagConstraints.insets = new java.awt.Insets (0, 0, 2, 0); 150 bindingPanel.add(bindingLabel, gridBagConstraints); 151 152 bindingList.setCellRenderer(new KeySequenceCellRenderer()); 153 bindingScrollPane.setViewportView(bindingList); 154 155 gridBagConstraints = new java.awt.GridBagConstraints (); 156 gridBagConstraints.gridx = 0; 157 gridBagConstraints.gridy = 1; 158 gridBagConstraints.gridheight = 3; 159 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 160 gridBagConstraints.weightx = 1.0; 161 gridBagConstraints.weighty = 1.0; 162 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 163 bindingPanel.add(bindingScrollPane, gridBagConstraints); 164 165 addButton.setMnemonic(bundle.getString("MSP_Add_Mnemonic").charAt(0)); 166 addButton.setText(bundle.getString("MSP_Add")); 167 addButton.setToolTipText(bundle.getString("MSP_AddToolTip")); 168 addButton.addActionListener(new java.awt.event.ActionListener () { 169 public void actionPerformed(java.awt.event.ActionEvent evt) { 170 addBindingActionPerformed(evt); 171 } 172 }); 173 174 gridBagConstraints = new java.awt.GridBagConstraints (); 175 gridBagConstraints.gridx = 1; 176 gridBagConstraints.gridy = 1; 177 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 178 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 179 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 180 bindingPanel.add(addButton, gridBagConstraints); 181 182 removeButton.setMnemonic(bundle.getString("MSP_Remove_Mnemonic").charAt(0)); 183 removeButton.setText(bundle.getString("MSP_Remove")); 184 removeButton.setToolTipText(bundle.getString( "MSP_RemoveToolTip")); 185 removeButton.setEnabled(false); 186 removeButton.addActionListener(new java.awt.event.ActionListener () { 187 public void actionPerformed(java.awt.event.ActionEvent evt) { 188 removeBindingActionPerformed(evt); 189 } 190 }); 191 192 gridBagConstraints = new java.awt.GridBagConstraints (); 193 gridBagConstraints.gridx = 1; 194 gridBagConstraints.gridy = 2; 195 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 196 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 197 bindingPanel.add(removeButton, gridBagConstraints); 198 199 gridBagConstraints = new java.awt.GridBagConstraints (); 200 gridBagConstraints.gridx = 0; 201 gridBagConstraints.gridy = 1; 202 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 203 gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTHWEST; 204 gridBagConstraints.weightx = 1.0; 205 gridBagConstraints.weighty = 1.0; 206 gridBagConstraints.insets = new java.awt.Insets (12, 0, 0, 0); 207 add(bindingPanel, gridBagConstraints); 208 209 } 211 private void removeBindingActionPerformed(java.awt.event.ActionEvent evt) { int index = bindingList.getSelectedIndex(); 213 if( index >= 0 ) { 214 bindings.remove(index); 215 bindingList.setListData(bindings); 216 } 217 if (bindingList.getModel().getSize() <= 0) 218 removeButton.setEnabled(false); 219 else 220 bindingList.setSelectedIndex(0); 221 } 223 private void addBindingActionPerformed(java.awt.event.ActionEvent evt) { KeyStroke [] newKeyStrokes = new KeySequenceRequester().getKeySequence(); 225 226 if (newKeyStrokes != null) 227 { 228 bindings.add(newKeyStrokes); 229 bindingList.setListData(bindings); 230 bindingList.setSelectedIndex(0); 231 removeButton.setEnabled(true); 232 } 233 } 235 public String getName() { 236 return nameField.getText(); 237 } 238 239 public void setName( String name ) { 240 nameField.setText( name ); 241 } 242 243 public String getBody() { 244 return macroField.getText(); 245 } 246 247 public void setBody( String body ) { 248 macroField.setText( body ); 249 } 250 251 252 public List getKeySequences() { 253 return new ArrayList( bindings ); 254 } 255 256 257 public void setKeySequences( List sequences ) { 258 bindings = new Vector( sequences ); 259 bindingList.setListData( bindings ); 260 } 261 262 public javax.swing.JButton addButton; 264 public javax.swing.JLabel bindingLabel; 265 public javax.swing.JList bindingList; 266 public javax.swing.JPanel bindingPanel; 267 public javax.swing.JScrollPane bindingScrollPane; 268 public javax.swing.JTextField macroField; 269 public javax.swing.JLabel macroLabel; 270 public javax.swing.JPanel macroPanel; 271 public javax.swing.JTextField nameField; 272 public javax.swing.JLabel nameLabel; 273 public javax.swing.JButton removeButton; 274 276 277 public void popupNotify() { 278 nameField.requestFocus(); 279 } 280 281 282 private static class KeySequenceCellRenderer extends JLabel implements ListCellRenderer { 283 public KeySequenceCellRenderer() { 284 setOpaque(true); 285 } 286 287 public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { 288 setText( Utilities.keySequenceToString( (KeyStroke [])value ) ); 289 setBackground(isSelected ? list.getSelectionBackground() : list.getBackground() ); 290 setForeground(isSelected ? list.getSelectionForeground() : list.getForeground() ); 291 return this; 292 } 293 } 294 295 296 299 class KeySequenceRequester { 300 301 KeySequenceInputPanel panel; 302 Dialog dial; 303 304 JButton [] buttons = { new JButton (bundle.getString("MSP_ok")), new JButton (bundle.getString("MSP_clear")), new JButton (bundle.getString("MSP_cancel"))}; 308 KeyStroke [] retVal = null; 309 310 311 KeySequenceRequester() { 312 ((JButton )buttons[0]).getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MSP_ok")); ((JButton )buttons[1]).getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MSP_clear")); ((JButton )buttons[2]).getAccessibleContext().setAccessibleDescription(bundle.getString("ACSD_MSP_cancel")); ((JButton )buttons[1]).setMnemonic(bundle.getString("MSP_clear_Mnemonic").charAt (0)); ((JButton )buttons[0]).setEnabled( false ); 318 panel = new KeySequenceInputPanel(); 320 panel.addPropertyChangeListener( new PropertyChangeListener () { 321 public void propertyChange( PropertyChangeEvent evt ) { 322 if( KeySequenceInputPanel.PROP_KEYSEQUENCE != evt.getPropertyName() ) return; 323 KeyStroke [] seq = panel.getKeySequence(); 324 String warn = isAlreadyBounded( seq ); 325 if (warn == null) 326 warn = getCollisionString( seq ); 327 ((JButton )buttons[0]).setEnabled( seq.length > 0 && warn == null ); 328 panel.setInfoText( warn == null ? "" : warn ); } 330 } ); 331 332 dial = DialogSupport.createDialog( 333 bundle.getString("MSP_AddTitle"), panel, true, buttons, false, -1, 2, new ActionListener (){ 335 public void actionPerformed( ActionEvent evt ) { 336 if( evt.getSource() == buttons[1] ) { panel.clear(); panel.requestFocus(); } else if( evt.getSource() == buttons[0] ) { retVal = panel.getKeySequence(); 341 dial.dispose(); } else if( evt.getSource() == buttons[2] ) { retVal = null; 344 dial.dispose(); } 346 } 347 }); 348 349 } 350 351 KeyStroke [] getKeySequence() { 352 dial.pack(); 353 panel.requestFocus(); 354 dial.show(); 355 return retVal; 356 } 357 358 361 String isAlreadyBounded( KeyStroke [] seq ) { 362 if( seq.length == 0 ) return null; 364 Iterator it = bindings.iterator(); 365 while( it.hasNext() ) { 366 if( isOverlapingSequence( (KeyStroke [])it.next(), seq ) ) { 367 return bundle.getString( "MSP_Collision" ); } 369 } 370 return null; } 372 373 String getCollisionString( KeyStroke [] seq ) { 374 if( seq.length == 0 ) return null; 376 Settings.KitAndValue[] kv = Settings.getValueHierarchy( kitClass, SettingsNames.KEY_BINDING_LIST ); 377 for (int i = 0; i < kv.length; i++) 378 { 379 Iterator iter = ((List )kv[i].value).iterator(); 380 while( iter.hasNext() ) { 381 MultiKeyBinding b = (MultiKeyBinding)iter.next(); 382 KeyStroke [] ks = b.keys; 383 if (ks == null && b.key != null) 384 { 385 ks = new KeyStroke [1]; 386 ks[0] = b.key; 387 } 388 if( ks != null && isOverlapingSequence( ks, seq ) ) { 389 Object [] values = { Utilities.keySequenceToString( ks ), b.actionName }; 390 return MessageFormat.format( bundle.getString( "MSP_FMT_Collision" ), values ); } 392 } 393 } 394 return null; } 396 397 private boolean isOverlapingSequence( KeyStroke [] s1, KeyStroke [] s2 ) { 398 int l = Math.min( s1.length, s2.length ); 399 if (l == 0) 400 return false; 401 while( l-- > 0 ) if( !s1[l].equals( s2[l] ) ) return false; 402 return true; 403 } 404 } 405 406 } 407 | Popular Tags |