PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/org/gjt/sp/jedit/options/AbbrevsOptionPane.java

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