PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/options/AbbrevsOptionPane.java

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