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.table.*; 31 32 import org.openide.*; 33 import org.openide.util.NbBundle; 34 import org.openide.util.HelpCtx; 35 import javax.swing.event.ListSelectionEvent ; 36 import javax.swing.event.ListSelectionListener ; 37 38 39 47 48 public class AbbrevsEditorPanel extends javax.swing.JPanel { 49 50 PairStringModel model; 51 52 private AbbrevsEditor editor; 54 55 private FontSizeTable abbrevsTable; 56 57 58 public AbbrevsEditorPanel( AbbrevsEditor editor ) { 59 this.editor = editor; 60 model = new PairStringModel(); 61 initComponents (); 62 63 abbrevsTable = new FontSizeTable(); 64 abbrevsTable.setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (8, 8, 8, 8))); 65 abbrevsTable.setModel(model); 66 abbrevsTable.setShowHorizontalLines(false); 67 abbrevsTable.setShowVerticalLines(false); 68 abbrevsTable.setSelectionMode( DefaultListSelectionModel.SINGLE_SELECTION ); 69 TableColumnModel col = abbrevsTable.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 abbrevsPane.setViewportView(abbrevsTable); 76 77 78 getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_AEP")); abbrevsTable.getAccessibleContext().setAccessibleName(getBundleString("ACSN_AEP_Table")); abbrevsTable.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_AEP_Table")); addButton.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_AEP_Add")); editButton.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_AEP_Edit")); removeButton.getAccessibleContext().setAccessibleDescription(getBundleString("ACSD_AEP_Remove")); enableButtons(false); 85 86 abbrevsTable.registerKeyboardAction(new ActionListener() { 87 public void actionPerformed(ActionEvent evt) { 88 SwingUtilities.getAncestorOfClass(Window .class, AbbrevsEditorPanel.this).setVisible(false); 89 }}, 90 KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), 91 JComponent.WHEN_FOCUSED 92 ); 93 94 abbrevsTable.getSelectionModel().addListSelectionListener(new ListSelectionListener () { 95 public void valueChanged(ListSelectionEvent e) { 96 if (e.getValueIsAdjusting()) return; 97 98 if (abbrevsTable.getRowCount() == 0){ 100 enableButtons(false); 101 return; 102 } 103 104 ListSelectionModel lsm = (ListSelectionModel)e.getSource(); 106 enableButtons(!lsm.isSelectionEmpty()); 107 } 108 }); 109 110 } 111 112 private void enableButtons(boolean enable){ 113 editButton.setEnabled(enable); 114 removeButton.setEnabled(enable); 115 } 116 117 private String getBundleString(String s) { 118 return NbBundle.getMessage(AbbrevsEditorPanel.class, s); 119 } 120 121 122 125 public void setValue( Map m ) { 126 if (m != null) 128 model.setData( new TreeMap( m ) ); 129 else 130 model.setData( new TreeMap() ); 131 132 if( model.getRowCount() > 0 ) abbrevsTable.setRowSelectionInterval( 0, 0 ); 134 } 135 136 139 public Map getValue() { 140 return model.getData(); 141 } 142 143 147 private void notifyEditor() { 148 if( editor != null ) editor.customEditorChange(); 149 } 150 151 152 private void initComponents() { java.awt.GridBagConstraints gridBagConstraints; 154 155 abbrevsPane = new javax.swing.JScrollPane (); 156 addButton = new javax.swing.JButton (); 157 editButton = new javax.swing.JButton (); 158 removeButton = new javax.swing.JButton (); 159 160 setLayout(new java.awt.GridBagLayout ()); 161 162 setBorder(new javax.swing.border.EmptyBorder (new java.awt.Insets (12, 12, 11, 11))); 163 gridBagConstraints = new java.awt.GridBagConstraints (); 164 gridBagConstraints.gridheight = 4; 165 gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH; 166 gridBagConstraints.weightx = 1.0; 167 gridBagConstraints.weighty = 1.0; 168 gridBagConstraints.insets = new java.awt.Insets (0, 0, 0, 12); 169 add(abbrevsPane, gridBagConstraints); 170 171 addButton.setMnemonic(getBundleString("AEP_Add_Mnemonic").charAt (0)); 172 addButton.setText(getBundleString( "AEP_Add" )); 173 addButton.addActionListener(new java.awt.event.ActionListener () { 174 public void actionPerformed(java.awt.event.ActionEvent evt) { 175 addButtonActionPerformed(evt); 176 } 177 }); 178 179 gridBagConstraints = new java.awt.GridBagConstraints (); 180 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 181 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 182 add(addButton, gridBagConstraints); 183 184 editButton.setMnemonic(getBundleString("AEP_Edit_Mnemonic").charAt (0)); 185 editButton.setText(getBundleString( "AEP_Edit" )); 186 editButton.addActionListener(new java.awt.event.ActionListener () { 187 public void actionPerformed(java.awt.event.ActionEvent evt) { 188 editButtonActionPerformed(evt); 189 } 190 }); 191 192 gridBagConstraints = new java.awt.GridBagConstraints (); 193 gridBagConstraints.gridx = 1; 194 gridBagConstraints.gridy = 1; 195 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 196 gridBagConstraints.insets = new java.awt.Insets (0, 0, 5, 0); 197 add(editButton, gridBagConstraints); 198 199 removeButton.setMnemonic(getBundleString("AEP_Remove_Mnemonic").charAt (0)); 200 removeButton.setText(getBundleString( "AEP_Remove" )); 201 removeButton.addActionListener(new java.awt.event.ActionListener () { 202 public void actionPerformed(java.awt.event.ActionEvent evt) { 203 removeButtonActionPerformed(evt); 204 } 205 }); 206 207 gridBagConstraints = new java.awt.GridBagConstraints (); 208 gridBagConstraints.gridx = 1; 209 gridBagConstraints.gridy = 2; 210 gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL; 211 add(removeButton, gridBagConstraints); 212 213 } 215 private void addButtonActionPerformed (java.awt.event.ActionEvent evt) { String [] abbrev = getAbbrev( null ); 217 if( abbrev == null ) return; 219 int index = model.putPair( abbrev ); abbrevsTable.setRowSelectionInterval( index, index ); 221 notifyEditor(); 222 } 224 private void editButtonActionPerformed (java.awt.event.ActionEvent evt) { int index = abbrevsTable.getSelectedRow(); 226 if( index != -1 ) { String [] pair = model.getPair( index ); 228 pair = getAbbrev( pair ); 229 if( pair != null ) { 230 model.removePair( index ); 231 index = model.putPair( pair ); 232 abbrevsTable.setRowSelectionInterval( index, index ); 233 notifyEditor(); 234 } 235 } 236 } 238 private void removeButtonActionPerformed (java.awt.event.ActionEvent evt) { int index = abbrevsTable.getSelectedRow(); 240 if( index != -1 ) { model.removePair( index ); 242 if( index >= model.getRowCount() ) index--; 243 if( index >= 0 ) abbrevsTable.setRowSelectionInterval( index, index ); 244 notifyEditor(); 245 } 246 } 248 254 private String [] getAbbrev( String [] abbrev ) { 255 AbbrevInputPanel input = new AbbrevInputPanel(); 256 HelpCtx.setHelpIDString( input, (HelpCtx.findHelp(this) != null ? HelpCtx.findHelp(this).getHelpID() : null) ); 258 if( abbrev != null ) input.setAbbrev( abbrev ); 260 DialogDescriptor dd = new DialogDescriptor ( input, getBundleString("AEP_EnterAbbrev" ) ); Dialog dial = org.openide.DialogDisplayer.getDefault().createDialog(dd); 262 input.requestFocus(); dial.setVisible(true); 265 if( dd.getValue() == DialogDescriptor.OK_OPTION ) { 266 String [] retVal = input.getAbbrev(); 267 if( ! "".equals( retVal[0] ) ){ int existingKeyPosition = model.containsKey(retVal[0]); 269 270 if (existingKeyPosition >= 0){ 271 if ( abbrev!=null && abbrev[0].equals(retVal[0]) ) return retVal; 273 274 String [] existingPair = model.getPair(existingKeyPosition); 275 NotifyDescriptor NDConfirm = new NotifyDescriptor.Confirmation( 276 NbBundle.getMessage(AbbrevsEditorPanel.class, "AEP_Overwrite", new Object [] {retVal[0], existingPair[1], retVal[1]}), 277 NotifyDescriptor.YES_NO_OPTION, 278 NotifyDescriptor.WARNING_MESSAGE 279 ); 280 281 org.openide.DialogDisplayer.getDefault().notify(NDConfirm); 282 if (NDConfirm.getValue()!=NDConfirm.YES_OPTION){ 283 return null; 284 } 285 } 286 return retVal; 287 } 288 } 289 return null; } 291 292 293 private javax.swing.JScrollPane abbrevsPane; 295 private javax.swing.JButton addButton; 296 private javax.swing.JButton editButton; 297 private javax.swing.JButton removeButton; 298 300 301 305 private class PairStringModel extends javax.swing.table.AbstractTableModel { 306 307 String [] columns = { getBundleString( "AEP_AbbrevTitle" ), getBundleString( "AEP_ExpandTitle" ) }; 310 TreeMap data; 311 String [] keys; 312 313 public PairStringModel() { 314 data = new TreeMap(); 315 keys = new String [0]; 316 } 317 318 public void setData( TreeMap data ) { 319 this.data = data; 320 updateKeys(); 321 } 322 323 private void updateKeys() { 324 keys = (String [])data.keySet().toArray( new String [0] ); 325 fireTableDataChanged(); } 327 328 public TreeMap getData() { 329 return data; 330 } 331 332 public int getRowCount() { 333 return keys.length; 334 } 335 336 public int getColumnCount() { 337 return 2; 338 } 339 340 public String getColumnName(int column) { 341 return columns[column]; 342 } 343 344 public Object getValueAt(int row, int column) { 345 if( column == 0 ) return keys[row]; 346 else return data.get( keys[row] ); 347 } 348 349 public int putPair( String [] pair ) { 350 data.put( pair[0], pair[1] ); 351 updateKeys(); 352 return Arrays.binarySearch( keys, pair[0] ); } 354 355 public void removePair( int row ) { 356 data.remove( getValueAt( row, 0 ) ); 357 updateKeys(); 358 } 359 360 public String [] getPair( int row ) { 361 String key = (String )getValueAt( row, 0 ); 362 String [] retVal = { key, (String )data.get( key ) }; 363 return retVal; 364 } 365 366 public int containsKey( String key ){ 367 return Arrays.binarySearch( keys, key ); 368 } 369 } 370 371 372 private final class FontSizeTable extends JTable{ 373 374 private boolean needCalcRowHeight = true; 375 376 public FontSizeTable () {} 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 |