1 6 7 package org.netbeans.modules.options.keymap; 8 9 import java.awt.Component ; 10 import java.awt.Dimension ; 11 import java.awt.event.ActionEvent ; 12 import java.awt.event.ActionListener ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import javax.swing.AbstractButton ; 16 import javax.swing.AbstractListModel ; 17 import javax.swing.DefaultListModel ; 18 import javax.swing.JLabel ; 19 import javax.swing.JPanel ; 20 import javax.swing.JScrollPane ; 21 import javax.swing.SwingUtilities ; 22 import javax.swing.event.ListSelectionEvent ; 23 import javax.swing.event.ListSelectionListener ; 24 import javax.swing.event.TreeSelectionEvent ; 25 import javax.swing.event.TreeSelectionListener ; 26 import org.openide.DialogDisplayer; 27 import org.openide.NotifyDescriptor; 28 import org.openide.NotifyDescriptor.InputLine; 29 import org.openide.NotifyDescriptor.Message; 30 import org.openide.awt.Mnemonics; 31 import org.openide.util.NbBundle; 32 33 37 public class KeymapPanel extends JPanel implements ActionListener , 38 TreeSelectionListener , ListSelectionListener { 39 40 41 private boolean listen = false; 42 43 44 public KeymapPanel() { 45 initComponents(); 46 loc (bDuplicate, "Duplicate"); 47 loc (bDelete, "Delete"); 48 liShortcuts.getAccessibleContext ().setAccessibleName (loc ("AN_Shortcuts")); 51 liShortcuts.getAccessibleContext ().setAccessibleDescription (loc ("AD_Shortcuts")); 52 tActions.getAccessibleContext ().setAccessibleName (loc ("AN_Actions")); 53 tActions.getAccessibleContext ().setAccessibleDescription (loc ("AD_Actions")); 54 cbProfile.getAccessibleContext ().setAccessibleName (loc ("AN_Profiles")); 55 cbProfile.getAccessibleContext ().setAccessibleDescription (loc ("AD_Profiles")); 56 bDuplicate.addActionListener (this); 59 bDelete.addActionListener (this); 60 tActions.setRootVisible (false); 64 tActions.setShowsRootHandles (true); 65 tActions.addTreeSelectionListener (this); 66 cbProfile.addActionListener (this); 67 loc (bAdd, "Add_Shortcut"); 68 loc (bRemove, "Remove_Shortcut"); 69 bAdd.addActionListener (this); 70 bRemove.addActionListener (this); 71 liShortcuts.addListSelectionListener (this); 72 bAdd.setEnabled (false); 73 bRemove.setEnabled (false); 74 loc (lShortcuts, "Shortcuts"); lShortcuts.setLabelFor (liShortcuts); 76 loc (lActions, "Actions"); 77 lActions.setLabelFor (tActions); 78 } 79 80 85 private void initComponents() { 87 lProfile = new javax.swing.JLabel (); 88 cbProfile = new javax.swing.JComboBox (); 89 bDuplicate = new javax.swing.JButton (); 90 bDelete = new javax.swing.JButton (); 91 lActions = new javax.swing.JLabel (); 92 spActions = new javax.swing.JScrollPane (); 93 tActions = new javax.swing.JTree (); 94 lShortcuts = new javax.swing.JLabel (); 95 spShortcuts = new javax.swing.JScrollPane (); 96 liShortcuts = new javax.swing.JList (); 97 bAdd = new javax.swing.JButton (); 98 bRemove = new javax.swing.JButton (); 99 100 lProfile.setText("Profile:"); 101 102 bDuplicate.setText("Duplicate..."); 103 104 bDelete.setText("Delete"); 105 106 lActions.setText("Actions:"); 107 108 spActions.setViewportView(tActions); 109 110 lShortcuts.setText("Shortcuts:"); 111 112 spShortcuts.setViewportView(liShortcuts); 113 114 bAdd.setText("Add..."); 115 116 bRemove.setText("Remove"); 117 118 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 119 this.setLayout(layout); 120 layout.setHorizontalGroup( 121 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 122 .add(layout.createSequentialGroup() 123 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 124 .add(layout.createSequentialGroup() 125 .add(lProfile) 126 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 127 .add(cbProfile, 0, 139, Short.MAX_VALUE) 128 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 129 .add(bDuplicate)) 130 .add(layout.createSequentialGroup() 131 .add(spShortcuts, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 175, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 132 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 133 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 134 .add(bRemove) 135 .add(bAdd)))) 136 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 137 .add(bDelete)) 138 .add(layout.createSequentialGroup() 139 .add(lActions) 140 .addContainerGap()) 141 .add(spActions, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 367, Short.MAX_VALUE) 142 .add(layout.createSequentialGroup() 143 .add(lShortcuts) 144 .addContainerGap()) 145 ); 146 147 layout.linkSize(new java.awt.Component [] {bAdd, bRemove}, org.jdesktop.layout.GroupLayout.HORIZONTAL); 148 149 layout.linkSize(new java.awt.Component [] {bDelete, bDuplicate}, org.jdesktop.layout.GroupLayout.HORIZONTAL); 150 151 layout.setVerticalGroup( 152 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 153 .add(layout.createSequentialGroup() 154 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE) 155 .add(bDelete) 156 .add(bDuplicate) 157 .add(lProfile) 158 .add(cbProfile, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 159 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 160 .add(lActions) 161 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 162 .add(spActions, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 168, Short.MAX_VALUE) 163 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 164 .add(lShortcuts) 165 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 166 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false) 167 .add(layout.createSequentialGroup() 168 .add(bAdd) 169 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 170 .add(bRemove)) 171 .add(spShortcuts, 0, 52, Short.MAX_VALUE))) 172 ); 173 } 175 176 private javax.swing.JButton bAdd; 178 private javax.swing.JButton bDelete; 179 private javax.swing.JButton bDuplicate; 180 private javax.swing.JButton bRemove; 181 private javax.swing.JComboBox cbProfile; 182 private javax.swing.JLabel lActions; 183 private javax.swing.JLabel lProfile; 184 private javax.swing.JLabel lShortcuts; 185 private javax.swing.JList liShortcuts; 186 private javax.swing.JScrollPane spActions; 187 private javax.swing.JScrollPane spShortcuts; 188 private javax.swing.JTree tActions; 189 191 192 public void actionPerformed (ActionEvent e) { 193 if (!listen) return; 194 Object source = e.getSource (); 195 if (source == bAdd) { 196 Object action = tActions.getSelectionPath ().getLastPathComponent (); 197 String shortcut = ShortcutsDialog.getShortcut (getModel ()); 198 if (shortcut == null) return; 199 getModel ().addShortcut ( 200 tActions.getSelectionPath (), shortcut 201 ); 202 selectAction (action); 203 if (liShortcuts.getModel ().getSize () > 0) 204 liShortcuts.setSelectedIndex (0); 205 SwingUtilities.invokeLater (new Runnable () { 206 public void run () { 207 liShortcuts.requestFocus (); 208 } 209 }); 210 } else 211 if (source == bRemove) { 212 int index = liShortcuts.getSelectedIndex (); 213 Object action = tActions.getSelectionPath ().getLastPathComponent (); 214 String shortcut = (String ) liShortcuts.getSelectedValue (); 215 getModel ().removeShortcut ( 216 tActions.getSelectionPath (), shortcut 217 ); 218 selectAction (action); 219 if (liShortcuts.getModel ().getSize () > index) 220 liShortcuts.setSelectedIndex (index); 221 else 222 if (liShortcuts.getModel ().getSize () > 0) 223 liShortcuts.setSelectedIndex (0); 224 } else 225 if (source == bDelete) { 226 deleteCurrentProfile (); 227 } else 228 if (source == cbProfile) { 229 String profile = (String ) cbProfile.getSelectedItem (); 230 getModel ().setCurrentProfile (profile); 231 if (getModel ().isCustomProfile (profile)) 232 loc (bDelete, "Delete"); else 234 loc (bDelete, "Restore"); refreshAction (); 236 } else 237 if (source == bDuplicate) { 238 InputLine il = new InputLine ( 239 loc ("CTL_Create_New_Profile_Message"), loc ("CTL_Create_New_Profile_Title") ); 242 il.setInputText ((String ) cbProfile. 243 getSelectedItem ()); 244 DialogDisplayer.getDefault ().notify (il); 245 if (il.getValue () == NotifyDescriptor.OK_OPTION) { 246 String newProfile = il.getInputText (); 247 Iterator it = getModel ().getProfiles ().iterator (); 248 while (it.hasNext ()) 249 if (newProfile.equals (it.next ())) { 250 Message md = new Message ( 251 loc ("CTL_Duplicate_Profile_Name"), Message.ERROR_MESSAGE 253 ); 254 DialogDisplayer.getDefault ().notify (md); 255 return; 256 } 257 getModel ().cloneProfile (newProfile); 258 cbProfile.addItem (il.getInputText ()); 259 cbProfile.setSelectedItem (il.getInputText ()); 260 } 261 return; 262 } 263 } 264 265 public void valueChanged (TreeSelectionEvent e) { 266 if (!listen) return; 267 refreshAction (); 268 } 269 270 public void valueChanged (ListSelectionEvent e) { 271 if (!listen) return; 272 int i = liShortcuts.getSelectedIndex (); 274 if (i < 0) { 275 bRemove.setEnabled (false); 276 return; 277 } 278 bRemove.setEnabled (true); 279 } 280 281 private boolean initialized = false; 282 void update () { 283 if (!initialized) { 284 initialized = true; 285 listen = false; 286 287 tActions.setCellRenderer (new KeymapListRenderer (getModel ())); 288 tActions.setModel (getModel ()); 289 290 List keymaps = getModel ().getProfiles (); 292 cbProfile.removeAllItems (); 293 int i, k = keymaps.size (); 294 for (i = 0; i < k; i++) 295 cbProfile.addItem (keymaps.get (i)); 296 listen = true; 297 } 298 cbProfile.setSelectedItem (getModel ().getCurrentProfile ()); 299 } 300 301 private void deleteCurrentProfile () { 302 String currentProfile = (String ) cbProfile.getSelectedItem (); 303 getModel ().deleteProfile (currentProfile); 304 if (getModel ().isCustomProfile (currentProfile)) { 305 cbProfile.removeItem (currentProfile); 306 cbProfile.setSelectedIndex (0); 307 } 308 } 309 310 void applyChanges () { 311 if (!initialized) return; getModel ().apply (); 313 } 314 315 void cancel () { 316 if (model == null) return; 317 model.cancel (); 318 } 319 320 boolean dataValid () { 321 return true; 322 } 323 324 boolean isChanged () { 325 return getModel ().isChanged (); 326 } 327 328 private KeymapViewModel model; 329 330 KeymapViewModel getModel () { 331 if (model == null) 332 model = new KeymapViewModel (); 333 return model; 334 } 335 336 337 339 private static String loc (String key) { 340 return NbBundle.getMessage (KeymapPanel.class, key); 341 } 342 343 private static void loc (Component c, String key) { 344 if (!(c instanceof JLabel )) { 345 c.getAccessibleContext ().setAccessibleName (loc ("AN_" + key)); 346 c.getAccessibleContext ().setAccessibleDescription (loc ("AD_" + key)); 347 } 348 if (c instanceof AbstractButton ) { 349 Mnemonics.setLocalizedText ( 350 (AbstractButton ) c, 351 loc ("CTL_" + key) 352 ); 353 } else { 354 Mnemonics.setLocalizedText ( 355 (JLabel ) c, 356 loc ("CTL_" + key) 357 ); 358 } 359 } 360 361 void refreshAction () { 362 Object action = tActions.getSelectionPath () == null ? 363 null : tActions.getSelectionPath ().getLastPathComponent (); 364 selectAction (action); 365 if (liShortcuts.getModel ().getSize () > 0) 366 liShortcuts.setSelectedIndex (0); 367 } 368 369 void selectAction (Object action) { 370 if (action == null || action instanceof String ) { 371 liShortcuts.setModel (new DefaultListModel ()); 372 bAdd.setEnabled (false); 373 bRemove.setEnabled (false); 374 return; 375 } 376 bAdd.setEnabled (true); 377 bRemove.setEnabled (false); 378 final String [] shortcuts = getModel ().getShortcuts ((ActionImpl) action); 379 liShortcuts.setModel (new AbstractListModel () { 380 public int getSize () { 381 return shortcuts.length; 382 } 383 public Object getElementAt (int i) { 384 return shortcuts [i]; 385 } 386 }); 387 } 388 } 389 | Popular Tags |