1 22 23 package org.gjt.sp.jedit.options; 24 25 import javax.swing.border.EmptyBorder ; 27 import javax.swing.event.*; 28 import javax.swing.table.*; 29 import javax.swing.*; 30 import java.awt.event.*; 31 import java.awt.*; 32 import java.util.*; 33 import org.gjt.sp.jedit.gui.*; 34 import org.gjt.sp.jedit.*; 35 import org.gjt.sp.util.StandardUtilities; 36 38 44 public class AbbrevsOptionPane extends AbstractOptionPane 45 { 46 public AbbrevsOptionPane() 48 { 49 super("abbrevs"); 50 } 52 protected void _init() 54 { 55 setLayout(new BorderLayout()); 56 57 JPanel panel = new JPanel(new BorderLayout(6,6)); 58 59 expandOnInput = new JCheckBox(jEdit.getProperty("options.abbrevs" 60 + ".expandOnInput"),Abbrevs.getExpandOnInput()); 61 62 panel.add(expandOnInput,BorderLayout.NORTH); 63 64 JPanel panel2 = new JPanel(); 65 panel2.setLayout(new BoxLayout(panel2,BoxLayout.X_AXIS)); 66 panel2.setBorder(new EmptyBorder (0,0,6,0)); 67 panel2.add(Box.createGlue()); 68 JLabel label = new JLabel(jEdit.getProperty("options.abbrevs.set")); 69 label.setBorder(new EmptyBorder (0,0,0,12)); 70 panel2.add(label); 71 72 Hashtable _modeAbbrevs = Abbrevs.getModeAbbrevs(); 73 modeAbbrevs = new Hashtable(); 74 Mode[] modes = jEdit.getModes(); 75 Arrays.sort(modes,new MiscUtilities.StringICaseCompare()); 76 String [] sets = new String [modes.length + 1]; 77 sets[0] = "global"; 78 for(int i = 0; i < modes.length; i++) 79 { 80 String name = modes[i].getName(); 81 sets[i+1] = name; 82 modeAbbrevs.put(name,new AbbrevsModel((Hashtable)_modeAbbrevs.get(name))); 83 } 84 85 setsComboBox = new JComboBox(sets); 86 ActionHandler actionHandler = new ActionHandler(); 87 setsComboBox.addActionListener(actionHandler); 88 panel2.add(setsComboBox); 89 panel2.add(Box.createGlue()); 90 panel.add(panel2,BorderLayout.SOUTH); 91 92 add(BorderLayout.NORTH,panel); 93 94 globalAbbrevs = new AbbrevsModel(Abbrevs.getGlobalAbbrevs()); 95 abbrevsTable = new JTable(globalAbbrevs); 96 abbrevsTable.getColumnModel().getColumn(1).setCellRenderer( 97 new Renderer()); 98 abbrevsTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); 99 abbrevsTable.getTableHeader().setReorderingAllowed(false); 100 abbrevsTable.getTableHeader().addMouseListener(new HeaderMouseHandler()); 101 abbrevsTable.getSelectionModel().addListSelectionListener( 102 new SelectionHandler()); 103 abbrevsTable.getSelectionModel().setSelectionMode( 104 ListSelectionModel.SINGLE_SELECTION); 105 abbrevsTable.addMouseListener(new TableMouseHandler()); 106 Dimension d = abbrevsTable.getPreferredSize(); 107 d.height = Math.min(d.height,200); 108 JScrollPane scroller = new JScrollPane(abbrevsTable); 109 scroller.setPreferredSize(d); 110 add(BorderLayout.CENTER,scroller); 111 112 JPanel buttons = new JPanel(); 113 buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS)); 114 buttons.setBorder(new EmptyBorder (6,0,0,0)); 115 116 add = new RolloverButton(GUIUtilities.loadIcon("Plus.png")); 117 add.setToolTipText(jEdit.getProperty("options.abbrevs.add")); 118 add.addActionListener(actionHandler); 119 buttons.add(add); 120 remove = new RolloverButton(GUIUtilities.loadIcon("Minus.png")); 121 remove.setToolTipText(jEdit.getProperty("options.abbrevs.remove")); 122 remove.addActionListener(actionHandler); 123 buttons.add(remove); 124 edit = new RolloverButton(GUIUtilities.loadIcon("ButtonProperties.png")); 125 edit.setToolTipText(jEdit.getProperty("options.abbrevs.edit")); 126 edit.addActionListener(actionHandler); 127 buttons.add(edit); 128 buttons.add(Box.createGlue()); 129 130 add(BorderLayout.SOUTH,buttons); 131 setsComboBox.setSelectedIndex(jEdit.getIntegerProperty("options.abbrevs.combobox.index", 0)); 132 updateEnabled(); 133 } 135 protected void _save() 137 { 138 if(abbrevsTable.getCellEditor() != null) 139 abbrevsTable.getCellEditor().stopCellEditing(); 140 141 Abbrevs.setExpandOnInput(expandOnInput.isSelected()); 142 143 Abbrevs.setGlobalAbbrevs(globalAbbrevs.toHashtable()); 144 145 Hashtable modeHash = new Hashtable(); 146 Enumeration keys = modeAbbrevs.keys(); 147 Enumeration values = modeAbbrevs.elements(); 148 while(keys.hasMoreElements()) 149 { 150 modeHash.put(keys.nextElement(),((AbbrevsModel)values.nextElement()) 151 .toHashtable()); 152 } 153 Abbrevs.setModeAbbrevs(modeHash); 154 } 156 158 private JComboBox setsComboBox; 160 private JCheckBox expandOnInput; 161 private JTable abbrevsTable; 162 private AbbrevsModel globalAbbrevs; 163 private Hashtable modeAbbrevs; 164 private JButton add; 165 private JButton edit; 166 private JButton remove; 167 169 private void updateEnabled() 171 { 172 int selectedRow = abbrevsTable.getSelectedRow(); 173 edit.setEnabled(selectedRow != -1); 174 remove.setEnabled(selectedRow != -1); 175 } 177 private void edit() 179 { 180 AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel(); 181 182 int row = abbrevsTable.getSelectedRow(); 183 184 String abbrev = (String )abbrevsModel.getValueAt(row,0); 185 String expansion = (String )abbrevsModel.getValueAt(row,1); 186 String oldAbbrev = abbrev; 187 188 EditAbbrevDialog dialog = new EditAbbrevDialog( 189 GUIUtilities.getParentDialog(AbbrevsOptionPane.this), 190 abbrev,expansion,abbrevsModel.toHashtable()); 191 abbrev = dialog.getAbbrev(); 192 expansion = dialog.getExpansion(); 193 if(abbrev != null && expansion != null) 194 { 195 for(int i = 0; i < abbrevsModel.getRowCount(); i++) 196 { 197 if(abbrevsModel.getValueAt(i,0).equals(oldAbbrev)) 198 { 199 abbrevsModel.remove(i); 200 break; 201 } 202 } 203 204 add(abbrevsModel,abbrev,expansion); 205 } 206 } 208 private void add(AbbrevsModel abbrevsModel, String abbrev, 210 String expansion) 211 { 212 for(int i = 0; i < abbrevsModel.getRowCount(); i++) 213 { 214 if(abbrevsModel.getValueAt(i,0).equals(abbrev)) 215 { 216 abbrevsModel.remove(i); 217 break; 218 } 219 } 220 221 abbrevsModel.add(abbrev,expansion); 222 updateEnabled(); 223 } 225 227 class HeaderMouseHandler extends MouseAdapter 229 { 230 public void mouseClicked(MouseEvent evt) 231 { 232 switch(abbrevsTable.getTableHeader().columnAtPoint(evt.getPoint())) 233 { 234 case 0: 235 ((AbbrevsModel)abbrevsTable.getModel()).sort(0); 236 break; 237 case 1: 238 ((AbbrevsModel)abbrevsTable.getModel()).sort(1); 239 break; 240 } 241 } 242 } 244 class TableMouseHandler extends MouseAdapter 246 { 247 public void mouseClicked(MouseEvent evt) 248 { 249 if(evt.getClickCount() == 2) 250 edit(); 251 } 252 } 254 class SelectionHandler implements ListSelectionListener 256 { 257 public void valueChanged(ListSelectionEvent evt) 258 { 259 updateEnabled(); 260 } 261 } 263 class ActionHandler implements ActionListener 265 { 266 public void actionPerformed(ActionEvent evt) 267 { 268 AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel(); 269 270 Object source = evt.getSource(); 271 if(source == setsComboBox) 272 { 273 jEdit.setIntegerProperty("options.abbrevs.combobox.index", setsComboBox.getSelectedIndex()); 274 String selected = (String )setsComboBox.getSelectedItem(); 275 if(selected.equals("global")) 276 { 277 abbrevsTable.setModel(globalAbbrevs); 278 } 279 else 280 { 281 abbrevsTable.setModel((AbbrevsModel) 282 modeAbbrevs.get(selected)); 283 } 284 updateEnabled(); 285 } 286 else if(source == add) 287 { 288 EditAbbrevDialog dialog = new EditAbbrevDialog( 289 GUIUtilities.getParentDialog(AbbrevsOptionPane.this), 290 null,null,abbrevsModel.toHashtable()); 291 String abbrev = dialog.getAbbrev(); 292 String expansion = dialog.getExpansion(); 293 if(abbrev != null && abbrev.length() != 0 294 && expansion != null 295 && expansion.length() != 0) 296 { 297 add(abbrevsModel,abbrev,expansion); 298 } 299 } 300 else if(source == edit) 301 { 302 edit(); 303 } 304 else if(source == remove) 305 { 306 int selectedRow = abbrevsTable.getSelectedRow(); 307 abbrevsModel.remove(selectedRow); 308 updateEnabled(); 309 } 310 } 311 } 313 static class Renderer extends DefaultTableCellRenderer 315 { 316 public Component getTableCellRendererComponent( 317 JTable table, 318 Object value, 319 boolean isSelected, 320 boolean cellHasFocus, 321 int row, 322 int col) 323 { 324 String valueStr = value.toString(); 325 326 if(valueStr.toLowerCase().startsWith("<html>")) 329 valueStr = ' ' + valueStr; 330 return super.getTableCellRendererComponent(table,valueStr, 331 isSelected,cellHasFocus,row,col); 332 } 333 } } 336 class AbbrevsModel extends AbstractTableModel 338 { 339 Vector abbrevs; 340 int lastSort; 341 342 AbbrevsModel(Hashtable abbrevHash) 344 { 345 abbrevs = new Vector(); 346 347 if(abbrevHash != null) 348 { 349 Enumeration abbrevEnum = abbrevHash.keys(); 350 Enumeration expandEnum = abbrevHash.elements(); 351 352 while(abbrevEnum.hasMoreElements()) 353 { 354 abbrevs.addElement(new Abbrev((String )abbrevEnum.nextElement(), 355 (String )expandEnum.nextElement())); 356 } 357 358 sort(0); 359 } 360 } 362 void sort(int col) 364 { 365 lastSort = col; 366 Collections.sort(abbrevs,new AbbrevCompare(col)); 367 fireTableDataChanged(); 368 } 370 void add(String abbrev, String expansion) 372 { 373 abbrevs.addElement(new Abbrev(abbrev,expansion)); 374 sort(lastSort); 375 } 377 void remove(int index) 379 { 380 abbrevs.removeElementAt(index); 381 fireTableStructureChanged(); 382 } 384 public Hashtable toHashtable() 386 { 387 Hashtable hash = new Hashtable(); 388 for(int i = 0; i < abbrevs.size(); i++) 389 { 390 Abbrev abbrev = (Abbrev)abbrevs.elementAt(i); 391 if(abbrev.abbrev.length() > 0 392 && abbrev.expand.length() > 0) 393 { 394 hash.put(abbrev.abbrev,abbrev.expand); 395 } 396 } 397 return hash; 398 } 400 public int getColumnCount() 402 { 403 return 2; 404 } 406 public int getRowCount() 408 { 409 return abbrevs.size(); 410 } 412 public Object getValueAt(int row, int col) 414 { 415 Abbrev abbrev = (Abbrev)abbrevs.elementAt(row); 416 switch(col) 417 { 418 case 0: 419 return abbrev.abbrev; 420 case 1: 421 return abbrev.expand; 422 default: 423 return null; 424 } 425 } 427 public boolean isCellEditable(int row, int col) 429 { 430 return false; 431 } 433 public void setValueAt(Object value, int row, int col) 435 { 436 if(value == null) 437 value = ""; 438 439 Abbrev abbrev = (Abbrev)abbrevs.elementAt(row); 440 441 if(col == 0) 442 abbrev.abbrev = (String )value; 443 else 444 abbrev.expand = (String )value; 445 446 fireTableRowsUpdated(row,row); 447 } 449 public String getColumnName(int index) 451 { 452 switch(index) 453 { 454 case 0: 455 return jEdit.getProperty("options.abbrevs.abbrev"); 456 case 1: 457 return jEdit.getProperty("options.abbrevs.expand"); 458 default: 459 return null; 460 } 461 } 463 static class AbbrevCompare implements Comparator 465 { 466 int col; 467 468 AbbrevCompare(int col) 469 { 470 this.col = col; 471 } 472 473 public int compare(Object obj1, Object obj2) 474 { 475 Abbrev a1 = (Abbrev)obj1; 476 Abbrev a2 = (Abbrev)obj2; 477 478 if(col == 0) 479 { 480 String abbrev1 = a1.abbrev.toLowerCase(); 481 String abbrev2 = a2.abbrev.toLowerCase(); 482 483 return StandardUtilities.compareStrings( 484 abbrev1,abbrev2,true); 485 } 486 else 487 { 488 String expand1 = a1.expand.toLowerCase(); 489 String expand2 = a2.expand.toLowerCase(); 490 491 return StandardUtilities.compareStrings( 492 expand1,expand2,true); 493 } 494 } 495 } } 498 class Abbrev 500 { 501 Abbrev() {} 502 503 Abbrev(String abbrev, String expand) 504 { 505 this.abbrev = abbrev; 506 this.expand = expand; 507 } 508 509 String abbrev; 510 String expand; 511 } | Popular Tags |