PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/org/gjt/sp/jedit/options/AbbrevsOptionPane.java

#
Java | 425 lines | 341 code | 59 blank | 25 comment | 40 complexity | 9f4c7e1d2d90396b7bb54e305f4346ac 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. * Copyright (C) 1999, 2000, 2001 Slava Pestov
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package org.gjt.sp.jedit.options;
  20. import javax.swing.border.EmptyBorder;
  21. import javax.swing.event.*;
  22. import javax.swing.table.*;
  23. import javax.swing.*;
  24. import java.awt.event.*;
  25. import java.awt.*;
  26. import java.util.*;
  27. import org.gjt.sp.jedit.gui.EditAbbrevDialog;
  28. import org.gjt.sp.jedit.*;
  29. /**
  30. * Abbrev editor.
  31. * @author Slava Pestov
  32. * @version $Id: AbbrevsOptionPane.java 3828 2001-10-07 10:42:45Z spestov $
  33. */
  34. public class AbbrevsOptionPane extends AbstractOptionPane
  35. {
  36. public AbbrevsOptionPane()
  37. {
  38. super("abbrevs");
  39. }
  40. // protected members
  41. protected void _init()
  42. {
  43. setLayout(new BorderLayout());
  44. JPanel panel = new JPanel(new BorderLayout());
  45. JPanel panel2 = new JPanel();
  46. panel2.setLayout(new BoxLayout(panel2,BoxLayout.X_AXIS));
  47. panel2.setBorder(new EmptyBorder(0,0,6,0));
  48. panel2.add(Box.createGlue());
  49. expandOnInput = new JCheckBox(jEdit.getProperty("options.abbrevs"
  50. + ".expandOnInput"),Abbrevs.getExpandOnInput());
  51. panel2.add(expandOnInput);
  52. panel2.add(Box.createGlue());
  53. panel.add(panel2,BorderLayout.NORTH);
  54. JPanel panel3 = new JPanel();
  55. panel3.setLayout(new BoxLayout(panel3,BoxLayout.X_AXIS));
  56. panel3.setBorder(new EmptyBorder(0,0,6,0));
  57. panel3.add(Box.createGlue());
  58. JLabel label = new JLabel(jEdit.getProperty("options.abbrevs.set"));
  59. label.setBorder(new EmptyBorder(0,0,0,12));
  60. panel3.add(label);
  61. Hashtable _modeAbbrevs = Abbrevs.getModeAbbrevs();
  62. modeAbbrevs = new Hashtable();
  63. Mode[] modes = jEdit.getModes();
  64. String[] sets = new String[modes.length + 1];
  65. sets[0] = "global";
  66. for(int i = 0; i < modes.length; i++)
  67. {
  68. String name = modes[i].getName();
  69. sets[i+1] = name;
  70. modeAbbrevs.put(name,new AbbrevsModel((Hashtable)_modeAbbrevs.get(name)));
  71. }
  72. setsComboBox = new JComboBox(sets);
  73. ActionHandler actionHandler = new ActionHandler();
  74. setsComboBox.addActionListener(actionHandler);
  75. panel3.add(setsComboBox);
  76. panel3.add(Box.createGlue());
  77. panel.add(panel3,BorderLayout.SOUTH);
  78. add(BorderLayout.NORTH,panel);
  79. globalAbbrevs = new AbbrevsModel(Abbrevs.getGlobalAbbrevs());
  80. abbrevsTable = new JTable(globalAbbrevs);
  81. abbrevsTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
  82. abbrevsTable.getTableHeader().setReorderingAllowed(false);
  83. abbrevsTable.getTableHeader().addMouseListener(new HeaderMouseHandler());
  84. abbrevsTable.getSelectionModel().addListSelectionListener(
  85. new SelectionHandler());
  86. Dimension d = abbrevsTable.getPreferredSize();
  87. d.height = Math.min(d.height,200);
  88. JScrollPane scroller = new JScrollPane(abbrevsTable);
  89. scroller.setPreferredSize(d);
  90. add(BorderLayout.CENTER,scroller);
  91. JPanel buttons = new JPanel();
  92. buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
  93. buttons.setBorder(new EmptyBorder(6,0,0,0));
  94. buttons.add(Box.createGlue());
  95. add = new JButton(jEdit.getProperty("options.abbrevs.add"));
  96. add.addActionListener(actionHandler);
  97. buttons.add(add);
  98. buttons.add(Box.createHorizontalStrut(6));
  99. edit = new JButton(jEdit.getProperty("options.abbrevs.edit"));
  100. edit.addActionListener(actionHandler);
  101. buttons.add(edit);
  102. buttons.add(Box.createHorizontalStrut(6));
  103. remove = new JButton(jEdit.getProperty("options.abbrevs.remove"));
  104. remove.addActionListener(actionHandler);
  105. buttons.add(remove);
  106. buttons.add(Box.createGlue());
  107. add(BorderLayout.SOUTH,buttons);
  108. updateEnabled();
  109. }
  110. protected void _save()
  111. {
  112. if(abbrevsTable.getCellEditor() != null)
  113. abbrevsTable.getCellEditor().stopCellEditing();
  114. Abbrevs.setExpandOnInput(expandOnInput.isSelected());
  115. Abbrevs.setGlobalAbbrevs(globalAbbrevs.toHashtable());
  116. Hashtable modeHash = new Hashtable();
  117. Enumeration keys = modeAbbrevs.keys();
  118. Enumeration values = modeAbbrevs.elements();
  119. while(keys.hasMoreElements())
  120. {
  121. modeHash.put(keys.nextElement(),((AbbrevsModel)values.nextElement())
  122. .toHashtable());
  123. }
  124. Abbrevs.setModeAbbrevs(modeHash);
  125. }
  126. // private members
  127. private JComboBox setsComboBox;
  128. private JCheckBox expandOnInput;
  129. private JTable abbrevsTable;
  130. private AbbrevsModel globalAbbrevs;
  131. private Hashtable modeAbbrevs;
  132. private JButton add;
  133. private JButton edit;
  134. private JButton remove;
  135. private void updateEnabled()
  136. {
  137. int selectedRow = abbrevsTable.getSelectedRow();
  138. edit.setEnabled(selectedRow != -1);
  139. remove.setEnabled(selectedRow != -1);
  140. }
  141. class HeaderMouseHandler extends MouseAdapter
  142. {
  143. public void mouseClicked(MouseEvent evt)
  144. {
  145. switch(abbrevsTable.getTableHeader().columnAtPoint(evt.getPoint()))
  146. {
  147. case 0:
  148. ((AbbrevsModel)abbrevsTable.getModel()).sort(0);
  149. break;
  150. case 1:
  151. ((AbbrevsModel)abbrevsTable.getModel()).sort(1);
  152. break;
  153. }
  154. }
  155. }
  156. class SelectionHandler implements ListSelectionListener
  157. {
  158. public void valueChanged(ListSelectionEvent evt)
  159. {
  160. updateEnabled();
  161. }
  162. }
  163. class ActionHandler implements ActionListener
  164. {
  165. public void actionPerformed(ActionEvent evt)
  166. {
  167. AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel();
  168. Object source = evt.getSource();
  169. if(source == setsComboBox)
  170. {
  171. String selected = (String)setsComboBox.getSelectedItem();
  172. if(selected.equals("global"))
  173. {
  174. abbrevsTable.setModel(globalAbbrevs);
  175. }
  176. else
  177. {
  178. abbrevsTable.setModel((AbbrevsModel)
  179. modeAbbrevs.get(selected));
  180. }
  181. updateEnabled();
  182. }
  183. else if(source == add)
  184. {
  185. EditAbbrevDialog dialog = new EditAbbrevDialog(
  186. AbbrevsOptionPane.this,
  187. null,null);
  188. String abbrev = dialog.getAbbrev();
  189. String expansion = dialog.getExpansion();
  190. if(abbrev != null && abbrev.length() != 0
  191. && expansion != null
  192. && expansion.length() != 0)
  193. {
  194. abbrevsModel.add(abbrev,expansion);
  195. int index = abbrevsModel.getRowCount() - 1;
  196. abbrevsTable.getSelectionModel()
  197. .setSelectionInterval(index,index);
  198. Rectangle rect = abbrevsTable.getCellRect(
  199. index,0,true);
  200. abbrevsTable.scrollRectToVisible(rect);
  201. updateEnabled();
  202. }
  203. }
  204. else if(source == edit)
  205. {
  206. int row = abbrevsTable.getSelectedRow();
  207. String abbrev = (String)abbrevsModel.getValueAt(row,0);
  208. String expansion = (String)abbrevsModel.getValueAt(row,1);
  209. EditAbbrevDialog dialog = new EditAbbrevDialog(
  210. AbbrevsOptionPane.this,
  211. abbrev,expansion);
  212. abbrev = dialog.getAbbrev();
  213. expansion = dialog.getExpansion();
  214. if(abbrev != null && expansion != null)
  215. {
  216. abbrevsModel.setValueAt(abbrev,row,0);
  217. abbrevsModel.setValueAt(expansion,row,1);
  218. }
  219. }
  220. else if(source == remove)
  221. {
  222. int selectedRow = abbrevsTable.getSelectedRow();
  223. abbrevsModel.remove(selectedRow);
  224. updateEnabled();
  225. }
  226. }
  227. }
  228. }
  229. class AbbrevsModel extends AbstractTableModel
  230. {
  231. Vector abbrevs;
  232. AbbrevsModel()
  233. {
  234. abbrevs = new Vector();
  235. }
  236. AbbrevsModel(Hashtable abbrevHash)
  237. {
  238. this();
  239. if(abbrevHash != null)
  240. {
  241. Enumeration abbrevEnum = abbrevHash.keys();
  242. Enumeration expandEnum = abbrevHash.elements();
  243. while(abbrevEnum.hasMoreElements())
  244. {
  245. abbrevs.addElement(new Abbrev((String)abbrevEnum.nextElement(),
  246. (String)expandEnum.nextElement()));
  247. }
  248. sort(0);
  249. }
  250. }
  251. void sort(int col)
  252. {
  253. MiscUtilities.quicksort(abbrevs,new AbbrevCompare(col));
  254. fireTableDataChanged();
  255. }
  256. void add(String abbrev, String expansion)
  257. {
  258. abbrevs.addElement(new Abbrev(abbrev,expansion));
  259. fireTableStructureChanged();
  260. }
  261. void remove(int index)
  262. {
  263. abbrevs.removeElementAt(index);
  264. fireTableStructureChanged();
  265. }
  266. public Hashtable toHashtable()
  267. {
  268. Hashtable hash = new Hashtable();
  269. for(int i = 0; i < abbrevs.size(); i++)
  270. {
  271. Abbrev abbrev = (Abbrev)abbrevs.elementAt(i);
  272. if(abbrev.abbrev.length() > 0
  273. && abbrev.expand.length() > 0)
  274. {
  275. hash.put(abbrev.abbrev,abbrev.expand);
  276. }
  277. }
  278. return hash;
  279. }
  280. public int getColumnCount()
  281. {
  282. return 2;
  283. }
  284. public int getRowCount()
  285. {
  286. return abbrevs.size();
  287. }
  288. public Object getValueAt(int row, int col)
  289. {
  290. Abbrev abbrev = (Abbrev)abbrevs.elementAt(row);
  291. switch(col)
  292. {
  293. case 0:
  294. return abbrev.abbrev;
  295. case 1:
  296. return abbrev.expand;
  297. default:
  298. return null;
  299. }
  300. }
  301. public boolean isCellEditable(int row, int col)
  302. {
  303. return false;
  304. }
  305. public void setValueAt(Object value, int row, int col)
  306. {
  307. if(value == null)
  308. value = "";
  309. Abbrev abbrev = (Abbrev)abbrevs.elementAt(row);
  310. if(col == 0)
  311. abbrev.abbrev = (String)value;
  312. else
  313. abbrev.expand = (String)value;
  314. fireTableRowsUpdated(row,row);
  315. }
  316. public String getColumnName(int index)
  317. {
  318. switch(index)
  319. {
  320. case 0:
  321. return jEdit.getProperty("options.abbrevs.abbrev");
  322. case 1:
  323. return jEdit.getProperty("options.abbrevs.expand");
  324. default:
  325. return null;
  326. }
  327. }
  328. class AbbrevCompare implements MiscUtilities.Compare
  329. {
  330. int col;
  331. AbbrevCompare(int col)
  332. {
  333. this.col = col;
  334. }
  335. public int compare(Object obj1, Object obj2)
  336. {
  337. Abbrev a1 = (Abbrev)obj1;
  338. Abbrev a2 = (Abbrev)obj2;
  339. if(col == 0)
  340. {
  341. String abbrev1 = a1.abbrev.toLowerCase();
  342. String abbrev2 = a2.abbrev.toLowerCase();
  343. return MiscUtilities.compareStrings(
  344. abbrev1,abbrev2,true);
  345. }
  346. else
  347. {
  348. String expand1 = a1.expand.toLowerCase();
  349. String expand2 = a2.expand.toLowerCase();
  350. return MiscUtilities.compareStrings(
  351. expand1,expand2,true);
  352. }
  353. }
  354. }
  355. }
  356. class Abbrev
  357. {
  358. Abbrev() {}
  359. Abbrev(String abbrev, String expand)
  360. {
  361. this.abbrev = abbrev;
  362. this.expand = expand;
  363. }
  364. String abbrev;
  365. String expand;
  366. }