/jEdit/tags/jedit-4-3-pre11/org/gjt/sp/jedit/options/AbbrevsOptionPane.java

# · Java · 511 lines · 387 code · 64 blank · 60 comment · 47 complexity · 7b37dd66c715de4af2aea80908d742c1 MD5 · raw file

  1. /*
  2. * AbbrevsOptionPane.java - Abbrevs options panel
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  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 org.gjt.sp.jedit.options;
  23. //{{{ Imports
  24. import javax.swing.border.EmptyBorder;
  25. import javax.swing.event.*;
  26. import javax.swing.table.*;
  27. import javax.swing.*;
  28. import java.awt.event.*;
  29. import java.awt.*;
  30. import java.util.*;
  31. import org.gjt.sp.jedit.gui.*;
  32. import org.gjt.sp.jedit.*;
  33. import org.gjt.sp.util.StandardUtilities;
  34. //}}}
  35. //{{{ AbbrevsOptionPane class
  36. /**
  37. * Abbrev editor.
  38. * @author Slava Pestov
  39. * @version $Id: AbbrevsOptionPane.java 5570 2006-07-11 09:27:07Z kpouer $
  40. */
  41. public class AbbrevsOptionPane extends AbstractOptionPane
  42. {
  43. //{{{ AbbrevsOptionPane constructor
  44. public AbbrevsOptionPane()
  45. {
  46. super("abbrevs");
  47. } //}}}
  48. //{{{ _init() method
  49. protected void _init()
  50. {
  51. setLayout(new BorderLayout());
  52. JPanel panel = new JPanel(new BorderLayout(6,6));
  53. expandOnInput = new JCheckBox(jEdit.getProperty("options.abbrevs"
  54. + ".expandOnInput"),Abbrevs.getExpandOnInput());
  55. panel.add(expandOnInput,BorderLayout.NORTH);
  56. JPanel panel2 = new JPanel();
  57. panel2.setLayout(new BoxLayout(panel2,BoxLayout.X_AXIS));
  58. panel2.setBorder(new EmptyBorder(0,0,6,0));
  59. panel2.add(Box.createGlue());
  60. JLabel label = new JLabel(jEdit.getProperty("options.abbrevs.set"));
  61. label.setBorder(new EmptyBorder(0,0,0,12));
  62. panel2.add(label);
  63. Hashtable _modeAbbrevs = Abbrevs.getModeAbbrevs();
  64. modeAbbrevs = new Hashtable();
  65. Mode[] modes = jEdit.getModes();
  66. Arrays.sort(modes,new MiscUtilities.StringICaseCompare());
  67. String[] sets = new String[modes.length + 1];
  68. sets[0] = "global";
  69. for(int i = 0; i < modes.length; i++)
  70. {
  71. String name = modes[i].getName();
  72. sets[i+1] = name;
  73. modeAbbrevs.put(name,new AbbrevsModel((Hashtable)_modeAbbrevs.get(name)));
  74. }
  75. setsComboBox = new JComboBox(sets);
  76. ActionHandler actionHandler = new ActionHandler();
  77. setsComboBox.addActionListener(actionHandler);
  78. panel2.add(setsComboBox);
  79. panel2.add(Box.createGlue());
  80. panel.add(panel2,BorderLayout.SOUTH);
  81. add(BorderLayout.NORTH,panel);
  82. globalAbbrevs = new AbbrevsModel(Abbrevs.getGlobalAbbrevs());
  83. abbrevsTable = new JTable(globalAbbrevs);
  84. abbrevsTable.getColumnModel().getColumn(1).setCellRenderer(
  85. new Renderer());
  86. abbrevsTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
  87. abbrevsTable.getTableHeader().setReorderingAllowed(false);
  88. abbrevsTable.getTableHeader().addMouseListener(new HeaderMouseHandler());
  89. abbrevsTable.getSelectionModel().addListSelectionListener(
  90. new SelectionHandler());
  91. abbrevsTable.getSelectionModel().setSelectionMode(
  92. ListSelectionModel.SINGLE_SELECTION);
  93. abbrevsTable.addMouseListener(new TableMouseHandler());
  94. Dimension d = abbrevsTable.getPreferredSize();
  95. d.height = Math.min(d.height,200);
  96. JScrollPane scroller = new JScrollPane(abbrevsTable);
  97. scroller.setPreferredSize(d);
  98. add(BorderLayout.CENTER,scroller);
  99. JPanel buttons = new JPanel();
  100. buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
  101. buttons.setBorder(new EmptyBorder(6,0,0,0));
  102. add = new RolloverButton(GUIUtilities.loadIcon("Plus.png"));
  103. add.setToolTipText(jEdit.getProperty("options.abbrevs.add"));
  104. add.addActionListener(actionHandler);
  105. buttons.add(add);
  106. remove = new RolloverButton(GUIUtilities.loadIcon("Minus.png"));
  107. remove.setToolTipText(jEdit.getProperty("options.abbrevs.remove"));
  108. remove.addActionListener(actionHandler);
  109. buttons.add(remove);
  110. edit = new RolloverButton(GUIUtilities.loadIcon("ButtonProperties.png"));
  111. edit.setToolTipText(jEdit.getProperty("options.abbrevs.edit"));
  112. edit.addActionListener(actionHandler);
  113. buttons.add(edit);
  114. buttons.add(Box.createGlue());
  115. add(BorderLayout.SOUTH,buttons);
  116. setsComboBox.setSelectedIndex(jEdit.getIntegerProperty("options.abbrevs.combobox.index", 0));
  117. updateEnabled();
  118. } //}}}
  119. //{{{ _save() method
  120. protected void _save()
  121. {
  122. if(abbrevsTable.getCellEditor() != null)
  123. abbrevsTable.getCellEditor().stopCellEditing();
  124. Abbrevs.setExpandOnInput(expandOnInput.isSelected());
  125. Abbrevs.setGlobalAbbrevs(globalAbbrevs.toHashtable());
  126. Hashtable modeHash = new Hashtable();
  127. Enumeration keys = modeAbbrevs.keys();
  128. Enumeration values = modeAbbrevs.elements();
  129. while(keys.hasMoreElements())
  130. {
  131. modeHash.put(keys.nextElement(),((AbbrevsModel)values.nextElement())
  132. .toHashtable());
  133. }
  134. Abbrevs.setModeAbbrevs(modeHash);
  135. } //}}}
  136. //{{{ Private members
  137. //{{{ Instance variables
  138. private JComboBox setsComboBox;
  139. private JCheckBox expandOnInput;
  140. private JTable abbrevsTable;
  141. private AbbrevsModel globalAbbrevs;
  142. private Hashtable modeAbbrevs;
  143. private JButton add;
  144. private JButton edit;
  145. private JButton remove;
  146. //}}}
  147. //{{{ updateEnabled() method
  148. private void updateEnabled()
  149. {
  150. int selectedRow = abbrevsTable.getSelectedRow();
  151. edit.setEnabled(selectedRow != -1);
  152. remove.setEnabled(selectedRow != -1);
  153. } //}}}
  154. //{{{ edit() method
  155. private void edit()
  156. {
  157. AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel();
  158. int row = abbrevsTable.getSelectedRow();
  159. String abbrev = (String)abbrevsModel.getValueAt(row,0);
  160. String expansion = (String)abbrevsModel.getValueAt(row,1);
  161. String oldAbbrev = abbrev;
  162. EditAbbrevDialog dialog = new EditAbbrevDialog(
  163. GUIUtilities.getParentDialog(AbbrevsOptionPane.this),
  164. abbrev,expansion,abbrevsModel.toHashtable());
  165. abbrev = dialog.getAbbrev();
  166. expansion = dialog.getExpansion();
  167. if(abbrev != null && expansion != null)
  168. {
  169. for(int i = 0; i < abbrevsModel.getRowCount(); i++)
  170. {
  171. if(abbrevsModel.getValueAt(i,0).equals(oldAbbrev))
  172. {
  173. abbrevsModel.remove(i);
  174. break;
  175. }
  176. }
  177. add(abbrevsModel,abbrev,expansion);
  178. }
  179. } //}}}
  180. //{{{ add() method
  181. private void add(AbbrevsModel abbrevsModel, String abbrev,
  182. String expansion)
  183. {
  184. for(int i = 0; i < abbrevsModel.getRowCount(); i++)
  185. {
  186. if(abbrevsModel.getValueAt(i,0).equals(abbrev))
  187. {
  188. abbrevsModel.remove(i);
  189. break;
  190. }
  191. }
  192. abbrevsModel.add(abbrev,expansion);
  193. updateEnabled();
  194. } //}}}
  195. //}}}
  196. //{{{ HeaderMouseHandler class
  197. class HeaderMouseHandler extends MouseAdapter
  198. {
  199. public void mouseClicked(MouseEvent evt)
  200. {
  201. switch(abbrevsTable.getTableHeader().columnAtPoint(evt.getPoint()))
  202. {
  203. case 0:
  204. ((AbbrevsModel)abbrevsTable.getModel()).sort(0);
  205. break;
  206. case 1:
  207. ((AbbrevsModel)abbrevsTable.getModel()).sort(1);
  208. break;
  209. }
  210. }
  211. } //}}}
  212. //{{{ TableMouseHandler class
  213. class TableMouseHandler extends MouseAdapter
  214. {
  215. public void mouseClicked(MouseEvent evt)
  216. {
  217. if(evt.getClickCount() == 2)
  218. edit();
  219. }
  220. } //}}}
  221. //{{{ SelectionHandler class
  222. class SelectionHandler implements ListSelectionListener
  223. {
  224. public void valueChanged(ListSelectionEvent evt)
  225. {
  226. updateEnabled();
  227. }
  228. } //}}}
  229. //{{{ ActionHandler class
  230. class ActionHandler implements ActionListener
  231. {
  232. public void actionPerformed(ActionEvent evt)
  233. {
  234. AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel();
  235. Object source = evt.getSource();
  236. if(source == setsComboBox)
  237. {
  238. jEdit.setIntegerProperty("options.abbrevs.combobox.index", setsComboBox.getSelectedIndex());
  239. String selected = (String)setsComboBox.getSelectedItem();
  240. if(selected.equals("global"))
  241. {
  242. abbrevsTable.setModel(globalAbbrevs);
  243. }
  244. else
  245. {
  246. abbrevsTable.setModel((AbbrevsModel)
  247. modeAbbrevs.get(selected));
  248. }
  249. updateEnabled();
  250. }
  251. else if(source == add)
  252. {
  253. EditAbbrevDialog dialog = new EditAbbrevDialog(
  254. GUIUtilities.getParentDialog(AbbrevsOptionPane.this),
  255. null,null,abbrevsModel.toHashtable());
  256. String abbrev = dialog.getAbbrev();
  257. String expansion = dialog.getExpansion();
  258. if(abbrev != null && abbrev.length() != 0
  259. && expansion != null
  260. && expansion.length() != 0)
  261. {
  262. add(abbrevsModel,abbrev,expansion);
  263. }
  264. }
  265. else if(source == edit)
  266. {
  267. edit();
  268. }
  269. else if(source == remove)
  270. {
  271. int selectedRow = abbrevsTable.getSelectedRow();
  272. abbrevsModel.remove(selectedRow);
  273. updateEnabled();
  274. }
  275. }
  276. } //}}}
  277. //{{{ Renderer class
  278. static class Renderer extends DefaultTableCellRenderer
  279. {
  280. public Component getTableCellRendererComponent(
  281. JTable table,
  282. Object value,
  283. boolean isSelected,
  284. boolean cellHasFocus,
  285. int row,
  286. int col)
  287. {
  288. String valueStr = value.toString();
  289. // workaround for Swing's annoying processing of
  290. // labels starting with <html>, which often breaks
  291. if(valueStr.toLowerCase().startsWith("<html>"))
  292. valueStr = ' ' + valueStr;
  293. return super.getTableCellRendererComponent(table,valueStr,
  294. isSelected,cellHasFocus,row,col);
  295. }
  296. } //}}}
  297. } //}}}
  298. //{{{ AbbrevsModel class
  299. class AbbrevsModel extends AbstractTableModel
  300. {
  301. Vector abbrevs;
  302. int lastSort;
  303. //{{{ AbbrevsModel constructor
  304. AbbrevsModel(Hashtable abbrevHash)
  305. {
  306. abbrevs = new Vector();
  307. if(abbrevHash != null)
  308. {
  309. Enumeration abbrevEnum = abbrevHash.keys();
  310. Enumeration expandEnum = abbrevHash.elements();
  311. while(abbrevEnum.hasMoreElements())
  312. {
  313. abbrevs.addElement(new Abbrev((String)abbrevEnum.nextElement(),
  314. (String)expandEnum.nextElement()));
  315. }
  316. sort(0);
  317. }
  318. } //}}}
  319. //{{{ sort() method
  320. void sort(int col)
  321. {
  322. lastSort = col;
  323. Collections.sort(abbrevs,new AbbrevCompare(col));
  324. fireTableDataChanged();
  325. } //}}}
  326. //{{{ add() method
  327. void add(String abbrev, String expansion)
  328. {
  329. abbrevs.addElement(new Abbrev(abbrev,expansion));
  330. sort(lastSort);
  331. } //}}}
  332. //{{{ remove() method
  333. void remove(int index)
  334. {
  335. abbrevs.removeElementAt(index);
  336. fireTableStructureChanged();
  337. } //}}}
  338. //{{{ toHashtable() method
  339. public Hashtable toHashtable()
  340. {
  341. Hashtable hash = new Hashtable();
  342. for(int i = 0; i < abbrevs.size(); i++)
  343. {
  344. Abbrev abbrev = (Abbrev)abbrevs.elementAt(i);
  345. if(abbrev.abbrev.length() > 0
  346. && abbrev.expand.length() > 0)
  347. {
  348. hash.put(abbrev.abbrev,abbrev.expand);
  349. }
  350. }
  351. return hash;
  352. } //}}}
  353. //{{{ getColumnCount() method
  354. public int getColumnCount()
  355. {
  356. return 2;
  357. } //}}}
  358. //{{{ getRowCount() method
  359. public int getRowCount()
  360. {
  361. return abbrevs.size();
  362. } //}}}
  363. //{{{ getValueAt() method
  364. public Object getValueAt(int row, int col)
  365. {
  366. Abbrev abbrev = (Abbrev)abbrevs.elementAt(row);
  367. switch(col)
  368. {
  369. case 0:
  370. return abbrev.abbrev;
  371. case 1:
  372. return abbrev.expand;
  373. default:
  374. return null;
  375. }
  376. } //}}}
  377. //{{{ isCellEditable() method
  378. public boolean isCellEditable(int row, int col)
  379. {
  380. return false;
  381. } //}}}
  382. //{{{ setValueAt() method
  383. public void setValueAt(Object value, int row, int col)
  384. {
  385. if(value == null)
  386. value = "";
  387. Abbrev abbrev = (Abbrev)abbrevs.elementAt(row);
  388. if(col == 0)
  389. abbrev.abbrev = (String)value;
  390. else
  391. abbrev.expand = (String)value;
  392. fireTableRowsUpdated(row,row);
  393. } //}}}
  394. //{{{ getColumnName() method
  395. public String getColumnName(int index)
  396. {
  397. switch(index)
  398. {
  399. case 0:
  400. return jEdit.getProperty("options.abbrevs.abbrev");
  401. case 1:
  402. return jEdit.getProperty("options.abbrevs.expand");
  403. default:
  404. return null;
  405. }
  406. } //}}}
  407. //{{{ AbbrevCompare class
  408. static class AbbrevCompare implements Comparator
  409. {
  410. int col;
  411. AbbrevCompare(int col)
  412. {
  413. this.col = col;
  414. }
  415. public int compare(Object obj1, Object obj2)
  416. {
  417. Abbrev a1 = (Abbrev)obj1;
  418. Abbrev a2 = (Abbrev)obj2;
  419. if(col == 0)
  420. {
  421. String abbrev1 = a1.abbrev.toLowerCase();
  422. String abbrev2 = a2.abbrev.toLowerCase();
  423. return StandardUtilities.compareStrings(
  424. abbrev1,abbrev2,true);
  425. }
  426. else
  427. {
  428. String expand1 = a1.expand.toLowerCase();
  429. String expand2 = a2.expand.toLowerCase();
  430. return StandardUtilities.compareStrings(
  431. expand1,expand2,true);
  432. }
  433. }
  434. } //}}}
  435. } //}}}
  436. //{{{ Abbrev class
  437. class Abbrev
  438. {
  439. Abbrev() {}
  440. Abbrev(String abbrev, String expand)
  441. {
  442. this.abbrev = abbrev;
  443. this.expand = expand;
  444. }
  445. String abbrev;
  446. String expand;
  447. } //}}}