/plugins/SuperAbbrevs/tags/SuperAbbrevs-1.0/superabbrevs/gui/AbbrevsOptionPane.java

# · Java · 678 lines · 492 code · 106 blank · 80 comment · 76 complexity · 450b78a501d355f880dc52033b1c2252 MD5 · raw file

  1. /*
  2. * AbbrevsOptionPane.java - Abbrevs options panel
  3. * :tabSize=4:indentSize=4:noTabs=false:
  4. * :folding=explicit:collapseFolds=2:
  5. *
  6. * Copyright (C) 1999, 2000, 2001, 2002 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package superabbrevs.gui;
  23. //{{{ Imports
  24. import java.awt.*;
  25. import java.awt.event.*;
  26. import java.io.*;
  27. import java.util.*;
  28. import javax.swing.*;
  29. import javax.swing.border.EmptyBorder;
  30. import javax.swing.event.*;
  31. import javax.swing.table.*;
  32. import org.gjt.sp.jedit.*;
  33. import org.gjt.sp.jedit.gui.RolloverButton;
  34. import org.gjt.sp.util.Log;
  35. import org.gjt.sp.util.StandardUtilities;
  36. import superabbrevs.SuperAbbrevs;
  37. import superabbrevs.SuperAbbrevsIO;
  38. //}}}
  39. //{{{ AbbrevsOptionPane class
  40. /**
  41. * I modified Slava Pestov code
  42. * @author Sune Simonsen
  43. */
  44. public class AbbrevsOptionPane extends AbstractOptionPane
  45. {
  46. //{{{ AbbrevsOptionPane constructor
  47. public AbbrevsOptionPane(View view)
  48. {
  49. super("superabbrevs");
  50. this.view = view;
  51. } //}}}
  52. //{{{ _init() method
  53. protected void _init()
  54. {
  55. setLayout(new BorderLayout());
  56. JPanel abbrevsSetPanel = new JPanel();
  57. JLabel label = new JLabel(jEdit.getProperty("options.superabbrevs.mode"));
  58. label.setDisplayedMnemonic('S');
  59. label.setBorder(new EmptyBorder(0,0,0,12));
  60. abbrevsSetPanel.add(label);
  61. modeAbbrevs = new Hashtable();
  62. Mode[] modes = jEdit.getModes();
  63. Arrays.sort(modes,new StandardUtilities.StringCompare<Mode>(true));
  64. String[] sets = new String[modes.length + 1];
  65. sets[0] = "global";
  66. modeAbbrevs.put(sets[0],
  67. new AbbrevsModel(SuperAbbrevs.loadAbbrevs(sets[0])));
  68. int selectedIndex = 0;
  69. String mode = view.getBuffer().getMode().getName();
  70. for(int i = 0; i < modes.length; i++)
  71. {
  72. String name = modes[i].getName();
  73. sets[i+1] = name;
  74. // maybe load abbrevs on demand
  75. modeAbbrevs.put(name,
  76. new AbbrevsModel(SuperAbbrevs.loadAbbrevs(name)));
  77. if(name.equals(mode)){
  78. selectedIndex = i+1;
  79. }
  80. }
  81. setsComboBox = new JComboBox(sets);
  82. label.setLabelFor(setsComboBox);
  83. ActionHandler actionHandler = new ActionHandler();
  84. setsComboBox.addActionListener(actionHandler);
  85. abbrevsSetPanel.add(setsComboBox);
  86. add(BorderLayout.NORTH,abbrevsSetPanel);
  87. abbrevsTable = new JTable((AbbrevsModel)modeAbbrevs.get("global"));
  88. abbrevsTable.getColumnModel().getColumn(1).setCellRenderer(
  89. new Renderer());
  90. //abbrevsTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
  91. abbrevsTable.getTableHeader().setReorderingAllowed(false);
  92. abbrevsTable.getTableHeader().addMouseListener(new HeaderMouseHandler());
  93. abbrevsTable.getSelectionModel().addListSelectionListener(
  94. new SelectionHandler());
  95. abbrevsTable.getSelectionModel().setSelectionMode(
  96. ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  97. //ListSelectionModel.SINGLE_SELECTION);
  98. abbrevsTable.addMouseListener(new TableMouseHandler());
  99. Dimension d = abbrevsTable.getPreferredSize();
  100. d.height = Math.min(d.height,200);
  101. JScrollPane scroller = new JScrollPane(abbrevsTable);
  102. scroller.setPreferredSize(d);
  103. add(BorderLayout.CENTER,scroller);
  104. JPanel buttons = new JPanel();
  105. buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
  106. buttons.setBorder(new EmptyBorder(6,0,0,0));
  107. add = new RolloverButton(GUIUtilities.loadIcon("Plus.png"));
  108. add.setToolTipText(jEdit.getProperty("options.abbrevs.add"));
  109. add.addActionListener(actionHandler);
  110. buttons.add(add);
  111. remove = new RolloverButton(GUIUtilities.loadIcon("Minus.png"));
  112. remove.setToolTipText(jEdit.getProperty("options.abbrevs.remove"));
  113. remove.addActionListener(actionHandler);
  114. buttons.add(remove);
  115. edit = new RolloverButton(GUIUtilities.loadIcon("ButtonProperties.png"));
  116. edit.setToolTipText(jEdit.getProperty("options.abbrevs.edit"));
  117. edit.addActionListener(actionHandler);
  118. buttons.add(edit);
  119. buttons.add(Box.createGlue());
  120. importFromFile = new RolloverButton(GUIUtilities.loadIcon("PreviousFile.png"));
  121. importFromFile.setToolTipText("Import from file");
  122. importFromFile.addActionListener(actionHandler);
  123. buttons.add(importFromFile);
  124. exportToFile = new RolloverButton(GUIUtilities.loadIcon("NextFile.png"));
  125. exportToFile.setToolTipText("Export to file");
  126. exportToFile.addActionListener(actionHandler);
  127. buttons.add(exportToFile);
  128. importAbbrevs = new JButton("Import normal abbrevs");
  129. importAbbrevs.addActionListener(actionHandler);
  130. JPanel bottomPanel = new JPanel(new BorderLayout());
  131. bottomPanel.add(BorderLayout.WEST, buttons);
  132. bottomPanel.add(BorderLayout.EAST, new JPanel().add(importAbbrevs));
  133. add(BorderLayout.SOUTH,bottomPanel);
  134. setsComboBox.setSelectedIndex(selectedIndex);
  135. // Set the width of the columns
  136. abbrevsTable.getColumnModel().getColumn(0).setMinWidth(100);
  137. abbrevsTable.getColumnModel().getColumn(1).setPreferredWidth(550);
  138. updateEnabled();
  139. } //}}}
  140. //{{{ _save() method
  141. protected void _save()
  142. {
  143. if(abbrevsTable.getCellEditor() != null)
  144. abbrevsTable.getCellEditor().stopCellEditing();
  145. Enumeration keys = modeAbbrevs.keys();
  146. Enumeration values = modeAbbrevs.elements();
  147. while(keys.hasMoreElements())
  148. {
  149. SuperAbbrevs.saveAbbrevs(
  150. (String)keys.nextElement(),
  151. ((AbbrevsModel)values.nextElement()).toHashtable());
  152. }
  153. } //}}}
  154. //{{{ Private members
  155. //{{{ Instance variables
  156. private JComboBox setsComboBox;
  157. //private JCheckBox expandOnInput;
  158. private JTable abbrevsTable;
  159. private Hashtable modeAbbrevs;
  160. private JButton add;
  161. private JButton edit;
  162. private JButton remove;
  163. private JButton importFromFile;
  164. private JButton exportToFile;
  165. private JButton importAbbrevs;
  166. private View view;
  167. //}}}
  168. //{{{ updateEnabled() method
  169. private void updateEnabled()
  170. {
  171. int selectedRow = abbrevsTable.getSelectedRow();
  172. edit.setEnabled(selectedRow != -1);
  173. remove.setEnabled(selectedRow != -1);
  174. exportToFile.setEnabled(selectedRow != -1);
  175. } //}}}
  176. //{{{ edit() method
  177. private void edit()
  178. {
  179. AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel();
  180. int row = abbrevsTable.getSelectedRow();
  181. String abbrev = (String)abbrevsModel.getValueAt(row,0);
  182. String expansion = (String)abbrevsModel.getValueAt(row,1);
  183. String oldAbbrev = abbrev;
  184. EditAbbrevDialog dialog = new EditAbbrevDialog(
  185. GUIUtilities.getParentDialog(AbbrevsOptionPane.this),
  186. abbrev,expansion,abbrevsModel.toHashtable());
  187. abbrev = dialog.getAbbrev();
  188. expansion = dialog.getExpansion();
  189. if(abbrev != null && expansion != null)
  190. {
  191. for(int i = 0; i < abbrevsModel.getRowCount(); i++)
  192. {
  193. if(abbrevsModel.getValueAt(i,0).equals(oldAbbrev))
  194. {
  195. abbrevsModel.remove(i);
  196. break;
  197. }
  198. }
  199. add(abbrevsModel,abbrev,expansion);
  200. }
  201. } //}}}
  202. //{{{ add() method
  203. private void add(AbbrevsModel abbrevsModel, String abbrev,
  204. String expansion)
  205. {
  206. for(int i = 0; i < abbrevsModel.getRowCount(); i++)
  207. {
  208. if(abbrevsModel.getValueAt(i,0).equals(abbrev))
  209. {
  210. abbrevsModel.remove(i);
  211. break;
  212. }
  213. }
  214. abbrevsModel.add(abbrev,expansion);
  215. updateEnabled();
  216. } //}}}
  217. //{{{ importAbbrevs method
  218. private void importAbbrevs() {
  219. if(abbrevsTable.getCellEditor() != null)
  220. abbrevsTable.getCellEditor().stopCellEditing();
  221. Enumeration modes = Abbrevs.getModeAbbrevs().keys();
  222. while(modes.hasMoreElements()){
  223. String mode = (String)modes.nextElement();
  224. importAbbrevs(mode);
  225. }
  226. //update global
  227. importAbbrevs("global");
  228. //update model
  229. String selectedMode = (String)setsComboBox.getSelectedItem();
  230. abbrevsTable.setModel((AbbrevsModel)modeAbbrevs.get(selectedMode));
  231. }
  232. private void importAbbrevs(String mode) {
  233. //get the superAbbrevs hashtable for the specific mode
  234. Hashtable superModeAbbrevs =
  235. ((AbbrevsModel)modeAbbrevs.get(mode)).toHashtable();
  236. if (superModeAbbrevs == null){
  237. superModeAbbrevs = new Hashtable();
  238. }
  239. Hashtable normalModeAbbrevs;
  240. if (mode.equals("global")){
  241. normalModeAbbrevs = Abbrevs.getGlobalAbbrevs();
  242. } else {
  243. normalModeAbbrevs = (Hashtable)Abbrevs.getModeAbbrevs().get(mode);
  244. }
  245. //add normalAbbrevs to superAbbrevs
  246. Enumeration abbrevs = normalModeAbbrevs.keys();
  247. while (abbrevs.hasMoreElements()) {
  248. String abbrev = (String)abbrevs.nextElement();
  249. //only import the abbrev if it doesn't exists
  250. if (!superModeAbbrevs.containsKey(abbrev)) {
  251. String abbrevExpand = (String)normalModeAbbrevs.get(abbrev);
  252. abbrevExpand = abbrevExpand.replaceFirst("\\\\[|]","\\$end");
  253. abbrevExpand = abbrevExpand.replaceAll("\\\\n","\n");
  254. abbrevExpand = abbrevExpand.replaceAll("\\\\t","\t");
  255. superModeAbbrevs.put(abbrev,abbrevExpand);
  256. }
  257. }
  258. AbbrevsModel model = new AbbrevsModel(superModeAbbrevs);
  259. modeAbbrevs.put(mode,model);
  260. }
  261. //}}}
  262. //{{{ HeaderMouseHandler class
  263. class HeaderMouseHandler extends MouseAdapter
  264. {
  265. public void mouseClicked(MouseEvent evt)
  266. {
  267. switch(abbrevsTable.getTableHeader().columnAtPoint(evt.getPoint()))
  268. {
  269. case 0:
  270. ((AbbrevsModel)abbrevsTable.getModel()).sort(0);
  271. break;
  272. case 1:
  273. ((AbbrevsModel)abbrevsTable.getModel()).sort(1);
  274. break;
  275. }
  276. }
  277. } //}}}
  278. //{{{ TableMouseHandler class
  279. class TableMouseHandler extends MouseAdapter
  280. {
  281. public void mouseClicked(MouseEvent evt)
  282. {
  283. if(evt.getClickCount() == 2)
  284. edit();
  285. }
  286. } //}}}
  287. //{{{ SelectionHandler class
  288. class SelectionHandler implements ListSelectionListener
  289. {
  290. public void valueChanged(ListSelectionEvent evt)
  291. {
  292. updateEnabled();
  293. }
  294. } //}}}
  295. //{{{ ActionHandler class
  296. class ActionHandler implements ActionListener
  297. {
  298. public void actionPerformed(ActionEvent evt)
  299. {
  300. AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel();
  301. Object source = evt.getSource();
  302. if(source == setsComboBox)
  303. {
  304. String selected = (String)setsComboBox.getSelectedItem();
  305. abbrevsTable.setModel((AbbrevsModel)
  306. modeAbbrevs.get(selected));
  307. updateEnabled();
  308. }
  309. else if(source == add)
  310. {
  311. EditAbbrevDialog dialog = new EditAbbrevDialog(
  312. GUIUtilities.getParentDialog(AbbrevsOptionPane.this),
  313. null,null,abbrevsModel.toHashtable());
  314. String abbrev = dialog.getAbbrev();
  315. String expansion = dialog.getExpansion();
  316. if(abbrev != null && abbrev.length() != 0
  317. && expansion != null
  318. && expansion.length() != 0)
  319. {
  320. add(abbrevsModel,abbrev,expansion);
  321. }
  322. }
  323. else if(source == edit)
  324. {
  325. edit();
  326. }
  327. else if(source == remove)
  328. {
  329. abbrevsModel.remove(abbrevsTable.getSelectedRows());
  330. updateEnabled();
  331. }
  332. else if (source == importAbbrevs){
  333. //import normal abbreviations
  334. importAbbrevs();
  335. }
  336. else if (source == importFromFile){
  337. importFromFile();
  338. updateEnabled();
  339. }
  340. else if (source == exportToFile) {
  341. exportToFile(abbrevsTable.getSelectedRows());
  342. updateEnabled();
  343. }
  344. }
  345. } //}}}
  346. //{{{ importFromFile() method
  347. private void importFromFile() {
  348. JFileChooser fileChooser = new JFileChooser();
  349. fileChooser.setFileHidingEnabled(false);
  350. int returnValue = fileChooser.showOpenDialog(jEdit.getActiveView());
  351. if(returnValue == JFileChooser.APPROVE_OPTION) {
  352. File file = fileChooser.getSelectedFile();
  353. Hashtable importedAbbrevs = SuperAbbrevsIO.readObjectFile(file);
  354. AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel();
  355. Iterator iter = importedAbbrevs.keySet().iterator();
  356. while (iter.hasNext()){
  357. String abbrev = iter.next().toString();
  358. String expansion = (String)importedAbbrevs.get(abbrev);
  359. add(abbrevsModel, abbrev, expansion);
  360. }
  361. }
  362. }//}}}
  363. //{{{ exportToFile() method
  364. private void exportToFile(int[] index) {
  365. Log.log(Log.DEBUG, this, "Exporting Abbrevs");
  366. JFileChooser fileChooser = new JFileChooser();
  367. fileChooser.setFileHidingEnabled(false);
  368. int returnValue = fileChooser.showSaveDialog(jEdit.getActiveView());
  369. if(returnValue == JFileChooser.APPROVE_OPTION) {
  370. Hashtable hashTable = new Hashtable();
  371. AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel();
  372. for (int i=0; i<index.length; i++){
  373. Abbrev abbrev = (Abbrev)abbrevsModel.abbrevs.get(i);
  374. hashTable.put(abbrev.abbrev, abbrev.expand);
  375. }
  376. File file = fileChooser.getSelectedFile();
  377. SuperAbbrevsIO.writeObjectFile(file,hashTable);
  378. }
  379. }//}}}
  380. //}}}
  381. //{{{ Renderer class
  382. static class Renderer extends DefaultTableCellRenderer
  383. {
  384. public Component getTableCellRendererComponent(
  385. JTable table,
  386. Object value,
  387. boolean isSelected,
  388. boolean cellHasFocus,
  389. int row,
  390. int col)
  391. {
  392. String valueStr = value.toString();
  393. // workaround for Swing's annoying processing of
  394. // labels starting with <html>, which often breaks
  395. if(valueStr.toLowerCase().startsWith("<html>"))
  396. valueStr = " " + valueStr;
  397. return super.getTableCellRendererComponent(table,valueStr,
  398. isSelected,cellHasFocus,row,col);
  399. }
  400. } //}}}
  401. } //}}}
  402. //{{{ AbbrevsModel class
  403. class AbbrevsModel extends AbstractTableModel
  404. {
  405. Vector abbrevs;
  406. int lastSort;
  407. //{{{ AbbrevsModel constructor
  408. AbbrevsModel(Hashtable abbrevHash)
  409. {
  410. abbrevs = new Vector();
  411. if(abbrevHash != null)
  412. {
  413. Enumeration abbrevEnum = abbrevHash.keys();
  414. Enumeration expandEnum = abbrevHash.elements();
  415. while(abbrevEnum.hasMoreElements())
  416. {
  417. abbrevs.addElement(new Abbrev((String)abbrevEnum.nextElement(),
  418. (String)expandEnum.nextElement()));
  419. }
  420. sort(0);
  421. }
  422. } //}}}
  423. //{{{ sort() method
  424. void sort(int col)
  425. {
  426. lastSort = col;
  427. Collections.sort(abbrevs,new AbbrevCompare(col));
  428. fireTableDataChanged();
  429. } //}}}
  430. //{{{ add() method
  431. void add(String abbrev, String expansion)
  432. {
  433. abbrevs.addElement(new Abbrev(abbrev,expansion));
  434. sort(lastSort);
  435. } //}}}
  436. //{{{ remove() method
  437. void remove(int[] index)
  438. {
  439. int shiftet = 0;
  440. for (int i=0; i<index.length; i++){
  441. abbrevs.removeElementAt(index[i]-shiftet);
  442. shiftet++;
  443. }
  444. fireTableStructureChanged();
  445. } //}}}
  446. //{{{ remove() method
  447. void remove(int index)
  448. {
  449. abbrevs.removeElementAt(index);
  450. fireTableStructureChanged();
  451. } //}}}
  452. //{{{ toHashtable() method
  453. public Hashtable toHashtable()
  454. {
  455. Hashtable hash = new Hashtable();
  456. for(int i = 0; i < abbrevs.size(); i++)
  457. {
  458. Abbrev abbrev = (Abbrev)abbrevs.elementAt(i);
  459. if(abbrev.abbrev.length() > 0
  460. && abbrev.expand.length() > 0)
  461. {
  462. hash.put(abbrev.abbrev,abbrev.expand);
  463. }
  464. }
  465. return hash;
  466. } //}}}
  467. //{{{ getColumnCount() method
  468. public int getColumnCount()
  469. {
  470. return 2;
  471. } //}}}
  472. //{{{ getRowCount() method
  473. public int getRowCount()
  474. {
  475. return abbrevs.size();
  476. } //}}}
  477. //{{{ getValueAt() method
  478. public Object getValueAt(int row, int col)
  479. {
  480. Abbrev abbrev = (Abbrev)abbrevs.elementAt(row);
  481. switch(col)
  482. {
  483. case 0:
  484. return abbrev.abbrev;
  485. case 1:
  486. return abbrev.expand;
  487. default:
  488. return null;
  489. }
  490. } //}}}
  491. //{{{ isCellEditable() method
  492. public boolean isCellEditable(int row, int col)
  493. {
  494. return false;
  495. } //}}}
  496. //{{{ setValueAt() method
  497. public void setValueAt(Object value, int row, int col)
  498. {
  499. if(value == null)
  500. value = "";
  501. Abbrev abbrev = (Abbrev)abbrevs.elementAt(row);
  502. if(col == 0)
  503. abbrev.abbrev = (String)value;
  504. else
  505. abbrev.expand = (String)value;
  506. fireTableRowsUpdated(row,row);
  507. } //}}}
  508. //{{{ getColumnName() method
  509. public String getColumnName(int index)
  510. {
  511. switch(index)
  512. {
  513. case 0:
  514. return jEdit.getProperty("options.superabbrevs.abbreviations.abbrev");
  515. case 1:
  516. return jEdit.getProperty("options.superabbrevs.abbreviations.expand");
  517. default:
  518. return null;
  519. }
  520. } //}}}
  521. //{{{ AbbrevCompare class
  522. class AbbrevCompare implements Comparator
  523. {
  524. //{{{ field int col
  525. private int col;
  526. /**
  527. * Getter function for the field col
  528. */
  529. public int getCol() {
  530. return col;
  531. }
  532. //}}}
  533. AbbrevCompare(int col)
  534. {
  535. this.col = col;
  536. }
  537. public int compare(Object obj1, Object obj2)
  538. {
  539. Abbrev a1 = (Abbrev)obj1;
  540. Abbrev a2 = (Abbrev)obj2;
  541. if(col == 0)
  542. {
  543. String abbrev1 = a1.abbrev.toLowerCase();
  544. String abbrev2 = a2.abbrev.toLowerCase();
  545. return StandardUtilities.compareStrings(abbrev1,abbrev2,true);
  546. }
  547. else
  548. {
  549. String expand1 = a1.expand.toLowerCase();
  550. String expand2 = a2.expand.toLowerCase();
  551. return StandardUtilities.compareStrings(expand1,expand2,true);
  552. }
  553. }
  554. public boolean equals(Object obj) {
  555. if (obj == null || !(obj instanceof AbbrevCompare)) return false;
  556. AbbrevCompare abbrevCompare = (AbbrevCompare)obj;
  557. return col == abbrevCompare.col;
  558. }
  559. } //}}}
  560. } //}}}
  561. //{{{ Abbrev class
  562. class Abbrev
  563. {
  564. Abbrev() {}
  565. Abbrev(String abbrev, String expand)
  566. {
  567. this.abbrev = abbrev;
  568. this.expand = expand;
  569. }
  570. String abbrev;
  571. String expand;
  572. } //}}}