1 19 20 package org.netbeans.modules.editor.options; 21 22 import java.awt.Dialog ; 23 import java.awt.Font ; 24 import java.awt.FontMetrics ; 25 import java.awt.Graphics ; 26 import java.awt.Window ; 27 import java.awt.event.*; 28 import java.util.*; 29 import javax.swing.*; 30 import javax.swing.event.ListSelectionEvent ; 31 import javax.swing.event.ListSelectionListener ; 32 import javax.swing.table.*; 33 34 import org.openide.*; 35 import org.openide.util.NbBundle; 36 import org.openide.util.HelpCtx; 37 38 39 47 48 public class MacrosEditorPanel extends javax.swing.JPanel { 49 50 PairStringModel model; 51 52 private MacrosEditor editor; 54 55 private FontSizeTable macrosTable; 56 57 58 public MacrosEditorPanel(MacrosEditor editor) { 59 this.editor = editor; 60 model = new PairStringModel(); 61 initComponents (); 62 63 macrosTable = new FontSizeTable(); 64 macrosTable.setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (8, 8, 8, 8))); 65 macrosTable.setModel(model); 66 macrosTable.setShowVerticalLines(false); 67 macrosTable.setShowHorizontalLines(false); 68 macrosTable.setSelectionMode( DefaultListSelectionModel.SINGLE_SELECTION ); 69 TableColumnModel col = macrosTable.getColumnModel(); 71 col.getColumn( 0 ).setMaxWidth( 3000 ); 72 col.getColumn( 0 ).setPreferredWidth( 30 ); 73 col.getColumn( 1 ).setMaxWidth( 7000 ); 74 col.getColumn( 1 ).setPreferredWidth( 70 ); 75 macrosPane.setViewportView(macrosTable); 76 77 getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_MEP")); macrosTable.getAccessibleContext().setAccessibleName(getBundleString("ACSN_MEP_Table")); macrosTable.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_MEP_Table")); addButton.setMnemonic(getBundleString("MEP_Add_Mnemonic").charAt(0)); editButton.setMnemonic(getBundleString("MEP_Edit_Mnemonic").charAt(0)); removeButton.setMnemonic(getBundleString("MEP_Remove_Mnemonic").charAt(0)); addButton.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_MEP_Add")); editButton.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_MEP_Edit")); removeButton.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_MEP_Remove")); enableButtons(false); 87 88 macrosTable.registerKeyboardAction(new ActionListener() { 89 public void actionPerformed(ActionEvent evt) { 90 SwingUtilities.getAncestorOfClass(Window .class, MacrosEditorPanel.this).setVisible(false); 91 }}, 92 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), 93 JComponent.WHEN_FOCUSED 94 ); 95 96 macrosTable.getSelectionModel().addListSelectionListener(new ListSelectionListener () { 97 public void valueChanged(ListSelectionEvent e) { 98 if (e.getValueIsAdjusting()) return; 99 100 if (macrosTable.getRowCount() == 0){ 102 enableButtons(false); 103 return; 104 } 105 106 ListSelectionModel lsm = (ListSelectionModel)e.getSource(); 108 enableButtons(!lsm.isSelectionEmpty()); 109 } 110 }); 111 112 } 113 114 private void enableButtons(boolean enable){ 115 editButton.setEnabled(enable); 116 removeButton.setEnabled(enable); 117 } 118 119 private String getBundleString(String s) { 120 return NbBundle.getMessage(MacrosEditorPanel.class, s); 121 } 122 123 126 public void setValue( Map m ) { 127 HashMap hm; 128 if (m != null) 129 hm = new HashMap(m); 130 else 131 hm = new HashMap(); 132 if (hm.containsKey(null)) { 133 hm.remove(null); 134 } 135 model.setData( new TreeMap( hm ) ); 137 if( model.getRowCount() > 0 ) macrosTable.setRowSelectionInterval( 0, 0 ); 139 } 140 141 144 public Map getValue() { 145 return model.getData(); 146 } 147 148 152 private void notifyEditor() { 153 if( editor != null ) editor.customEditorChange(); 154 } 155 156 157 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 159 160 macrosPane = new javax.swing.JScrollPane (); 161 addButton = new javax.swing.JButton (); 162 editButton = new javax.swing.JButton (); 163 removeButton = new javax.swing.JButton (); 164 165 setLayout(new java.awt.GridBagLayout ()); 166 167 setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (12, 12, 11, 11))); 168 gridBagConstraints = new java.awt.GridBagConstraints (); 169 gridBagConstraints.gridheight = 4; 170 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 171 gridBagConstraints.weightx = 1.0; 172 gridBagConstraints.weighty = 1.0; 173 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 174 add(macrosPane, gridBagConstraints); 175 176 addButton.setText(getBundleString( "MEP_Add" )); 177 addButton.addActionListener(new java.awt.event.ActionListener () { 178 public void actionPerformed(java.awt.event.ActionEvent evt) { 179 addButtonActionPerformed(evt); 180 } 181 }); 182 183 gridBagConstraints = new java.awt.GridBagConstraints (); 184 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 185 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 186 add(addButton, gridBagConstraints); 187 188 editButton.setText(getBundleString( "MEP_Edit" )); 189 editButton.addActionListener(new java.awt.event.ActionListener () { 190 public void actionPerformed(java.awt.event.ActionEvent evt) { 191 editButtonActionPerformed(evt); 192 } 193 }); 194 195 gridBagConstraints = new java.awt.GridBagConstraints (); 196 gridBagConstraints.gridx = 1; 197 gridBagConstraints.gridy = 1; 198 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 199 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 200 add(editButton, gridBagConstraints); 201 202 removeButton.setText(getBundleString( "MEP_Remove" )); 203 removeButton.addActionListener(new java.awt.event.ActionListener () { 204 public void actionPerformed(java.awt.event.ActionEvent evt) { 205 removeButtonActionPerformed(evt); 206 } 207 }); 208 209 gridBagConstraints = new java.awt.GridBagConstraints (); 210 gridBagConstraints.gridx = 1; 211 gridBagConstraints.gridy = 2; 212 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 213 add(removeButton, gridBagConstraints); 214 215 } 217 private void addButtonActionPerformed (java.awt.event.ActionEvent evt) { String [] macro = getMacro( null ); 219 if( macro == null ) return; 221 int index = model.putPair( macro ); macrosTable.setRowSelectionInterval( index, index ); 223 notifyEditor(); 224 } 226 private void editButtonActionPerformed (java.awt.event.ActionEvent evt) { int index = macrosTable.getSelectedRow(); 228 if( index != -1 ) { String [] pair = model.getPair( index ); 230 pair = getMacro( pair ); 231 if( pair != null ) { 232 model.removePair( index ); 233 index = model.putPair( pair ); 234 macrosTable.setRowSelectionInterval( index, index ); 235 notifyEditor(); 236 } 237 } 238 } 240 private void removeButtonActionPerformed (java.awt.event.ActionEvent evt) { int index = macrosTable.getSelectedRow(); 242 if( index != -1 ) { model.removePair( index ); 244 if( index >= model.getRowCount() ) index--; 245 if( index >= 0 ) macrosTable.setRowSelectionInterval( index, index ); 246 notifyEditor(); 247 } 248 } 250 256 private String [] getMacro( String [] macro ) { 257 MacroInputPanel input = new MacroInputPanel(); 258 HelpCtx.setHelpIDString( input, (HelpCtx.findHelp(this) != null ? HelpCtx.findHelp(this).getHelpID() : null) ); 260 if( macro != null ) input.setMacro( macro ); 262 DialogDescriptor dd = new DialogDescriptor ( input, getBundleString( "MEP_EnterMacro" ) ); Dialog dial = org.openide.DialogDisplayer.getDefault().createDialog(dd); 264 input.requestFocus(); dial.setVisible(true); 267 if( dd.getValue() == DialogDescriptor.OK_OPTION ) { 268 String [] retVal = input.getMacro(); 269 if( ! "".equals( retVal[0] ) ) { int existingKeyPosition = model.containsKey(retVal[0]); 271 272 if (existingKeyPosition >= 0){ 273 if ( macro!=null && macro[0].equals(retVal[0]) ) return retVal; 275 276 String [] existingPair = model.getPair(existingKeyPosition); 277 NotifyDescriptor NDConfirm = new NotifyDescriptor.Confirmation( 278 NbBundle.getMessage(MacrosEditorPanel.class, "MEP_Overwrite", retVal[0] ), 279 NotifyDescriptor.YES_NO_OPTION, 280 NotifyDescriptor.WARNING_MESSAGE 281 ); 282 283 org.openide.DialogDisplayer.getDefault().notify(NDConfirm); 284 if (NDConfirm.getValue()!=NDConfirm.YES_OPTION){ 285 return null; 286 } 287 } 288 return retVal; 289 } 290 } 291 return null; } 293 294 295 private javax.swing.JButton addButton; 297 private javax.swing.JButton editButton; 298 private javax.swing.JScrollPane macrosPane; 299 private javax.swing.JButton removeButton; 300 302 303 307 private class PairStringModel extends javax.swing.table.AbstractTableModel { 308 309 String [] columns = { getBundleString( "MEP_MacroTitle" ), getBundleString( "MEP_ExpandTitle" ) }; 312 TreeMap data; 313 String [] keys; 314 315 public PairStringModel() { 316 data = new TreeMap(); 317 keys = new String [0]; 318 } 319 320 public void setData( TreeMap data ) { 321 this.data = data; 322 updateKeys(); 323 } 324 325 private void updateKeys() { 326 keys = (String [])data.keySet().toArray( new String [0] ); 327 fireTableDataChanged(); } 329 330 public TreeMap getData() { 331 return data; 332 } 333 334 public int getRowCount() { 335 return keys.length; 336 } 337 338 public int getColumnCount() { 339 return 2; 340 } 341 342 public String getColumnName(int column) { 343 return columns[column]; 344 } 345 346 public Object getValueAt(int row, int column) { 347 if( column == 0 ) return keys[row]; 348 else return data.get( keys[row] ); 349 } 350 351 public int putPair( String [] pair ) { 352 data.put( pair[0], pair[1] ); 353 updateKeys(); 354 return Arrays.binarySearch( keys, pair[0] ); } 356 357 public void removePair( int row ) { 358 data.remove( getValueAt( row, 0 ) ); 359 updateKeys(); 360 } 361 362 public String [] getPair( int row ) { 363 String key = (String )getValueAt( row, 0 ); 364 String [] retVal = { key, (String )data.get( key ) }; 365 return retVal; 366 } 367 368 public int containsKey( String key ){ 369 return Arrays.binarySearch( keys, key ); 370 } 371 372 } 373 374 private final class FontSizeTable extends JTable{ 375 376 private boolean needCalcRowHeight = true; 377 378 public void updateUI() { 379 super.updateUI(); 380 needCalcRowHeight = true; 381 } 382 383 public void paint(Graphics g) { 384 if (needCalcRowHeight) { 385 calcRowHeight(g); 386 } 387 super.paint(g); 388 } 389 390 394 private void calcRowHeight(Graphics g) { 395 Font f = getFont(); 396 FontMetrics fm = g.getFontMetrics(f); 397 int rowHeight = fm.getHeight(); 398 needCalcRowHeight = false; 399 setRowHeight(rowHeight); 400 } 401 402 } 403 404 } 405 | Popular Tags |