1 19 20 package org.netbeans.modules.options.macros; 21 22 import java.awt.Component ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ActionListener ; 25 import java.util.Iterator ; 26 import javax.swing.AbstractButton ; 27 import javax.swing.JEditorPane ; 28 import javax.swing.JLabel ; 29 import javax.swing.JPanel ; 30 import javax.swing.ListSelectionModel ; 31 import javax.swing.SwingUtilities ; 32 import javax.swing.event.ListSelectionEvent ; 33 import javax.swing.event.ListSelectionListener ; 34 import javax.swing.table.DefaultTableModel ; 35 import org.netbeans.modules.options.keymap.KeymapViewModel; 36 import org.netbeans.modules.options.keymap.ShortcutsDialog; 37 import org.netbeans.spi.options.OptionsPanelController; 38 import org.openide.DialogDisplayer; 39 import org.openide.NotifyDescriptor; 40 import org.openide.NotifyDescriptor.InputLine; 41 import org.openide.awt.Mnemonics; 42 import org.openide.util.Lookup; 43 import org.openide.util.NbBundle; 44 45 46 51 public class MacrosPanel extends JPanel implements ActionListener , 52 ListSelectionListener { 53 54 private MacrosModel model; 55 private boolean listen = true; 56 private String currentMacro; 57 58 59 62 public MacrosPanel () { 63 initComponents (); 64 65 tMacros.getAccessibleContext ().setAccessibleName (loc ("AN_Macros_Table")); 67 tMacros.getAccessibleContext ().setAccessibleDescription (loc ("AD_Macros_Table")); 68 epMacroCode.getAccessibleContext ().setAccessibleName (loc ("AN_Macro")); 69 epMacroCode.getAccessibleContext ().setAccessibleDescription (loc ("AD_Macro")); 70 bRemove.setEnabled (false); 71 bSetShortcut.setEnabled (false); 72 loc (bNew, "New_Macro"); 73 loc (bRemove, "Remove_Macro"); 74 loc (bSetShortcut, "Shortcut"); 75 bNew.addActionListener (this); 76 bRemove.addActionListener (this); 77 bSetShortcut.addActionListener (this); 78 tMacros.getSelectionModel ().setSelectionMode 79 (ListSelectionModel.SINGLE_SELECTION); 80 tMacros.getSelectionModel ().addListSelectionListener (this); 81 tMacros.getTableHeader ().setReorderingAllowed (false); 82 epMacroCode.setEditorKit (JEditorPane.createEditorKitForContentType("text/plain")); 83 loc (lMacros, "Macro_List"); 84 lMacros.setLabelFor (tMacros); 85 loc (lMacroCode, "Macro_Code"); 86 lMacroCode.setLabelFor (epMacroCode); 87 } 88 89 90 92 97 private void initComponents() { 99 lMacros = new javax.swing.JLabel (); 100 spMacros = new javax.swing.JScrollPane (); 101 tMacros = new javax.swing.JTable (); 102 bNew = new javax.swing.JButton (); 103 bSetShortcut = new javax.swing.JButton (); 104 bRemove = new javax.swing.JButton (); 105 lMacroCode = new javax.swing.JLabel (); 106 sMacroCode = new javax.swing.JScrollPane (); 107 epMacroCode = new javax.swing.JEditorPane (); 108 109 lMacros.setText("Macros:"); 110 111 tMacros.setModel(new javax.swing.table.DefaultTableModel ( 112 new Object [][] { 113 {null, null, null, null}, 114 {null, null, null, null}, 115 {null, null, null, null}, 116 {null, null, null, null} 117 }, 118 new String [] { 119 "Title 1", "Title 2", "Title 3", "Title 4" 120 } 121 )); 122 spMacros.setViewportView(tMacros); 123 124 bNew.setText("New"); 125 126 bSetShortcut.setText("Set Shortcut..."); 127 128 bRemove.setText("Remove"); 129 130 lMacroCode.setText("Macro Code:"); 131 132 sMacroCode.setViewportView(epMacroCode); 133 134 org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this); 135 this.setLayout(layout); 136 layout.setHorizontalGroup( 137 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 138 .add(layout.createSequentialGroup() 139 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 140 .add(lMacros) 141 .add(lMacroCode) 142 .add(layout.createSequentialGroup() 143 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) 144 .add(org.jdesktop.layout.GroupLayout.LEADING, sMacroCode, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE) 145 .add(org.jdesktop.layout.GroupLayout.LEADING, spMacros, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 326, Short.MAX_VALUE)) 146 .add(18, 18, 18) 147 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING) 148 .add(bNew, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 149 .add(bSetShortcut, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE) 150 .add(org.jdesktop.layout.GroupLayout.LEADING, bRemove, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 151 .add(13, 13, 13))) 152 .addContainerGap()) 153 ); 154 layout.setVerticalGroup( 155 layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 156 .add(layout.createSequentialGroup() 157 .add(lMacros) 158 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 159 .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING) 160 .add(layout.createSequentialGroup() 161 .add(bNew) 162 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 163 .add(bSetShortcut) 164 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 165 .add(bRemove)) 166 .add(spMacros, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 113, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)) 167 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 168 .add(lMacroCode) 169 .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED) 170 .add(sMacroCode, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 147, Short.MAX_VALUE)) 171 ); 172 } 174 175 private javax.swing.JButton bNew; 177 private javax.swing.JButton bRemove; 178 private javax.swing.JButton bSetShortcut; 179 private javax.swing.JEditorPane epMacroCode; 180 private javax.swing.JLabel lMacroCode; 181 private javax.swing.JLabel lMacros; 182 private javax.swing.JScrollPane sMacroCode; 183 private javax.swing.JScrollPane spMacros; 184 private javax.swing.JTable tMacros; 185 187 188 private static String loc (String key) { 189 return NbBundle.getMessage (MacrosPanel.class, key); 190 } 191 192 private static void loc (Component c, String key) { 193 if (!(c instanceof JLabel )) { 194 c.getAccessibleContext ().setAccessibleName (loc ("AN_" + key)); 195 c.getAccessibleContext ().setAccessibleDescription (loc ("AD_" + key)); 196 } 197 if (c instanceof AbstractButton ) { 198 Mnemonics.setLocalizedText ( 199 (AbstractButton ) c, 200 loc ("CTL_" + key) 201 ); 202 } else { 203 Mnemonics.setLocalizedText ( 204 (JLabel ) c, 205 loc ("CTL_" + key) 206 ); 207 } 208 } 209 210 public void update () { 211 model = new MacrosModel (lookup); 212 listen = false; 213 DefaultTableModel tableModel = model.getShortcutsTableModel (); 214 currentMacro = null; 215 tableModel.setColumnIdentifiers (new String [] { 216 loc ("Macro_Name_Title"), 217 loc ("Macro_Code_Title") 218 }); 219 tMacros.setModel (tableModel); 220 listen = true; 221 if (model.getMacroNames().size () > 0) 222 tMacros.getSelectionModel ().setSelectionInterval (0, 0); 223 } 224 225 public void applyChanges () { 226 saveCurrentMacro (); 227 if (model != null) 228 model.applyChanges (); 229 } 230 231 public void cancel () { 232 if (model != null) 233 model.cancel (); 234 } 235 236 public boolean dataValid () { 237 return true; 238 } 239 240 public boolean isChanged () { 241 if (model == null) return false; 242 return model.isChanged (); 243 } 244 245 private Lookup lookup; 246 247 void setLookup (Lookup lookup) { 248 this.lookup = lookup; 249 } 250 251 252 254 public void actionPerformed (ActionEvent e) { 255 if (!listen) return; 256 if (e.getSource () == bNew) { 257 InputLine descriptor = new InputLine ( 258 loc ("CTL_Enter_macro_name"), 259 loc ("CTL_New_macro_dialog_title") 260 ); 261 if (DialogDisplayer.getDefault ().notify (descriptor) == 262 InputLine.OK_OPTION 263 ) { 264 String macroName = descriptor.getInputText ().trim (); 265 if (macroName.length () == 0) { 266 DialogDisplayer.getDefault ().notify ( 267 new NotifyDescriptor.Message ( 268 loc ("CTL_Empty_Macro_Name"), 269 NotifyDescriptor.ERROR_MESSAGE 270 ) 271 ); 272 } else { 273 Iterator it = model.getMacroNames ().iterator (); 274 boolean duplicate = false; 275 while (it.hasNext ()) { 276 String name = (String ) it.next (); 277 if (macroName.equals (name)) { 278 DialogDisplayer.getDefault ().notify ( 279 new NotifyDescriptor.Message ( 280 loc ("CTL_Duplicate_Macro_Name"), 281 NotifyDescriptor.ERROR_MESSAGE 282 ) 283 ); 284 duplicate = true; 285 break; 286 } 287 } 288 if (!duplicate) { 289 listen = false; 290 model.addMacro (macroName, ""); 291 listen = true; 292 tMacros.getSelectionModel ().setSelectionInterval (0, 0); 293 spMacros.getVerticalScrollBar ().setValue (0); 294 } 295 } 296 } 297 SwingUtilities.invokeLater (new Runnable () { 298 public void run () { 299 epMacroCode.requestFocus (); 300 } 301 }); 302 } else 303 if (e.getSource () == bRemove) { 304 int index = tMacros.getSelectedRow (); 305 listen = false; 306 model.removeMacro (index); 307 listen = true; 308 currentMacro = null; 309 if (index < tMacros.getModel ().getRowCount ()) 310 tMacros.getSelectionModel ().setSelectionInterval 311 (index, index); 312 else 313 if (tMacros.getModel ().getRowCount () > 0) 314 tMacros.getSelectionModel ().setSelectionInterval ( 315 tMacros.getModel ().getRowCount () - 1, 316 tMacros.getModel ().getRowCount () - 1 317 ); 318 else { 319 bRemove.setEnabled (false); 320 bSetShortcut.setEnabled (false); 321 } 322 } else 323 if (e.getSource () == bSetShortcut) { 324 int index = tMacros.getSelectedRow (); 325 String shortcut = ShortcutsDialog.getShortcut ( 326 (KeymapViewModel) lookup.lookup (KeymapViewModel.class) 327 ); 328 saveCurrentMacro (); 329 if (shortcut != null) 330 model.setShortcut (index, shortcut); 331 SwingUtilities.invokeLater (new Runnable () { 332 public void run () { 333 epMacroCode.requestFocus (); 334 } 335 }); 336 } 337 } 338 339 public void valueChanged (ListSelectionEvent e) { 340 int index = tMacros.getSelectedRow (); 341 if (index < 0) { 342 epMacroCode.setText (""); 343 bRemove.setEnabled (false); 344 bSetShortcut.setEnabled (false); 345 return; 346 } 347 saveCurrentMacro (); 348 349 bRemove.setEnabled (true); 350 bSetShortcut.setEnabled (true); 351 String name = getCurrentMacro (); 352 String text = model.getMacroText (name); 353 epMacroCode.setText (text); 354 currentMacro = name; 355 } 356 357 private String getCurrentMacro () { 358 int index = tMacros.getSelectedRow (); 359 if (index < 0) return null; 360 return (String ) tMacros.getModel ().getValueAt (index, 0); 361 } 362 363 private void saveCurrentMacro () { 364 if (currentMacro == null) return; 365 if (epMacroCode.getText ().equals (model.getMacroText (currentMacro))) 366 return; 367 model.setMacroText (currentMacro, epMacroCode.getText ()); 368 firePropertyChange (OptionsPanelController.PROP_CHANGED, null, null); 369 } 370 } 371 | Popular Tags |