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

# · Java · 503 lines · 384 code · 60 blank · 59 comment · 49 complexity · 0fbd7d40f6b0edf49b0ef15da43cfcd6 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 12504 2008-04-22 23:12:43Z ezust $
  40. */
  41. public class AbbrevsOptionPane extends AbstractOptionPane
  42. {
  43. //{{{ AbbrevsOptionPane constructor
  44. public AbbrevsOptionPane()
  45. {
  46. super("abbrevs");
  47. } //}}}
  48. //{{{ _init() method
  49. @Override
  50. protected void _init()
  51. {
  52. setLayout(new BorderLayout());
  53. JPanel panel = new JPanel(new BorderLayout(6,6));
  54. expandOnInput = new JCheckBox(jEdit.getProperty("options.abbrevs"
  55. + ".expandOnInput"),Abbrevs.getExpandOnInput());
  56. panel.add(expandOnInput,BorderLayout.NORTH);
  57. JPanel panel2 = new JPanel();
  58. panel2.setLayout(new BoxLayout(panel2,BoxLayout.X_AXIS));
  59. panel2.setBorder(new EmptyBorder(0,0,6,0));
  60. panel2.add(Box.createGlue());
  61. JLabel label = new JLabel(jEdit.getProperty("options.abbrevs.set"));
  62. label.setBorder(new EmptyBorder(0,0,0,12));
  63. panel2.add(label);
  64. Map<String,Hashtable<String,String>> _modeAbbrevs = Abbrevs.getModeAbbrevs();
  65. modeAbbrevs = new HashMap<String,AbbrevsModel>();
  66. Mode[] modes = jEdit.getModes();
  67. Arrays.sort(modes,new MiscUtilities.StringICaseCompare());
  68. String[] sets = new String[modes.length + 1];
  69. sets[0] = "global";
  70. for(int i = 0; i < modes.length; i++)
  71. {
  72. String name = modes[i].getName();
  73. sets[i+1] = name;
  74. modeAbbrevs.put(name,new AbbrevsModel(_modeAbbrevs.get(name)));
  75. }
  76. setsComboBox = new JComboBox(sets);
  77. ActionHandler actionHandler = new ActionHandler();
  78. setsComboBox.addActionListener(actionHandler);
  79. panel2.add(setsComboBox);
  80. panel2.add(Box.createGlue());
  81. panel.add(panel2,BorderLayout.SOUTH);
  82. add(BorderLayout.NORTH,panel);
  83. globalAbbrevs = new AbbrevsModel(Abbrevs.getGlobalAbbrevs());
  84. abbrevsTable = new JTable(globalAbbrevs);
  85. abbrevsTable.getColumnModel().getColumn(1).setCellRenderer(
  86. new Renderer());
  87. abbrevsTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
  88. abbrevsTable.getTableHeader().setReorderingAllowed(false);
  89. abbrevsTable.getTableHeader().addMouseListener(new HeaderMouseHandler());
  90. abbrevsTable.getSelectionModel().addListSelectionListener(
  91. new SelectionHandler());
  92. abbrevsTable.getSelectionModel().setSelectionMode(
  93. ListSelectionModel.SINGLE_SELECTION);
  94. abbrevsTable.addMouseListener(new TableMouseHandler());
  95. Dimension d = abbrevsTable.getPreferredSize();
  96. d.height = Math.min(d.height,200);
  97. JScrollPane scroller = new JScrollPane(abbrevsTable);
  98. scroller.setPreferredSize(d);
  99. add(BorderLayout.CENTER,scroller);
  100. JPanel buttons = new JPanel();
  101. buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
  102. buttons.setBorder(new EmptyBorder(6,0,0,0));
  103. add = new RolloverButton(GUIUtilities.loadIcon(jEdit.getProperty("options.abbrevs.add.icon")));
  104. add.setToolTipText(jEdit.getProperty("options.abbrevs.add"));
  105. add.addActionListener(actionHandler);
  106. buttons.add(add);
  107. remove = new RolloverButton(GUIUtilities.loadIcon(jEdit.getProperty("options.abbrevs.remove.icon")));
  108. remove.setToolTipText(jEdit.getProperty("options.abbrevs.remove"));
  109. remove.addActionListener(actionHandler);
  110. buttons.add(remove);
  111. edit = new RolloverButton(GUIUtilities.loadIcon(jEdit.getProperty("options.abbrevs.edit.icon")));
  112. edit.setToolTipText(jEdit.getProperty("options.abbrevs.edit"));
  113. edit.addActionListener(actionHandler);
  114. buttons.add(edit);
  115. buttons.add(Box.createGlue());
  116. add(BorderLayout.SOUTH,buttons);
  117. setsComboBox.setSelectedIndex(jEdit.getIntegerProperty("options.abbrevs.combobox.index", 0));
  118. updateEnabled();
  119. } //}}}
  120. //{{{ _save() method
  121. @Override
  122. protected void _save()
  123. {
  124. if(abbrevsTable.getCellEditor() != null)
  125. abbrevsTable.getCellEditor().stopCellEditing();
  126. Abbrevs.setExpandOnInput(expandOnInput.isSelected());
  127. Abbrevs.setGlobalAbbrevs(globalAbbrevs.toHashtable());
  128. Hashtable<String,Hashtable<String,String>> modeHash = new Hashtable<String,Hashtable<String,String>>();
  129. Set<Map.Entry<String,AbbrevsModel>> entrySet = modeAbbrevs.entrySet();
  130. for (Map.Entry<String,AbbrevsModel> entry : entrySet)
  131. {
  132. modeHash.put(entry.getKey(),entry.getValue().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 Map<String,AbbrevsModel> 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. @Override
  200. public void mouseClicked(MouseEvent evt)
  201. {
  202. switch(abbrevsTable.getTableHeader().columnAtPoint(evt.getPoint()))
  203. {
  204. case 0:
  205. ((AbbrevsModel)abbrevsTable.getModel()).sort(0);
  206. break;
  207. case 1:
  208. ((AbbrevsModel)abbrevsTable.getModel()).sort(1);
  209. break;
  210. }
  211. }
  212. } //}}}
  213. //{{{ TableMouseHandler class
  214. class TableMouseHandler extends MouseAdapter
  215. {
  216. @Override
  217. public void mouseClicked(MouseEvent evt)
  218. {
  219. if(evt.getClickCount() == 2)
  220. edit();
  221. }
  222. } //}}}
  223. //{{{ SelectionHandler class
  224. class SelectionHandler implements ListSelectionListener
  225. {
  226. public void valueChanged(ListSelectionEvent evt)
  227. {
  228. updateEnabled();
  229. }
  230. } //}}}
  231. //{{{ ActionHandler class
  232. class ActionHandler implements ActionListener
  233. {
  234. public void actionPerformed(ActionEvent evt)
  235. {
  236. AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel();
  237. Object source = evt.getSource();
  238. if(source == setsComboBox)
  239. {
  240. jEdit.setIntegerProperty("options.abbrevs.combobox.index", setsComboBox.getSelectedIndex());
  241. String selected = (String)setsComboBox.getSelectedItem();
  242. if(selected.equals("global"))
  243. {
  244. abbrevsTable.setModel(globalAbbrevs);
  245. }
  246. else
  247. {
  248. abbrevsTable.setModel(modeAbbrevs.get(selected));
  249. }
  250. updateEnabled();
  251. }
  252. else if(source == add)
  253. {
  254. EditAbbrevDialog dialog = new EditAbbrevDialog(
  255. GUIUtilities.getParentDialog(AbbrevsOptionPane.this),
  256. null,null,abbrevsModel.toHashtable());
  257. String abbrev = dialog.getAbbrev();
  258. String expansion = dialog.getExpansion();
  259. if(abbrev != null && abbrev.length() != 0
  260. && expansion != null
  261. && expansion.length() != 0)
  262. {
  263. add(abbrevsModel,abbrev,expansion);
  264. }
  265. }
  266. else if(source == edit)
  267. {
  268. edit();
  269. }
  270. else if(source == remove)
  271. {
  272. int selectedRow = abbrevsTable.getSelectedRow();
  273. abbrevsModel.remove(selectedRow);
  274. updateEnabled();
  275. }
  276. }
  277. } //}}}
  278. //{{{ Renderer class
  279. static class Renderer extends DefaultTableCellRenderer
  280. {
  281. @Override
  282. public Component getTableCellRendererComponent(
  283. JTable table,
  284. Object value,
  285. boolean isSelected,
  286. boolean cellHasFocus,
  287. int row,
  288. int col)
  289. {
  290. String valueStr = value.toString();
  291. // workaround for Swing's annoying processing of
  292. // labels starting with <html>, which often breaks
  293. if(valueStr.toLowerCase().startsWith("<html>"))
  294. valueStr = ' ' + valueStr;
  295. return super.getTableCellRendererComponent(table,valueStr,
  296. isSelected,cellHasFocus,row,col);
  297. }
  298. } //}}}
  299. } //}}}
  300. //{{{ AbbrevsModel class
  301. class AbbrevsModel extends AbstractTableModel
  302. {
  303. Vector<Abbrev> abbrevs;
  304. int lastSort;
  305. //{{{ AbbrevsModel constructor
  306. AbbrevsModel(Map<String,String> abbrevHash)
  307. {
  308. abbrevs = new Vector<Abbrev>();
  309. if(abbrevHash != null)
  310. {
  311. Set<Map.Entry<String,String>> entrySet = abbrevHash.entrySet();
  312. for (Map.Entry<String,String> entry : entrySet)
  313. {
  314. abbrevs.add(new Abbrev(entry.getKey(),
  315. entry.getValue()));
  316. }
  317. sort(0);
  318. }
  319. } //}}}
  320. //{{{ sort() method
  321. void sort(int col)
  322. {
  323. lastSort = col;
  324. Collections.sort(abbrevs,new AbbrevCompare(col));
  325. fireTableDataChanged();
  326. } //}}}
  327. //{{{ add() method
  328. void add(String abbrev, String expansion)
  329. {
  330. abbrevs.add(new Abbrev(abbrev,expansion));
  331. sort(lastSort);
  332. } //}}}
  333. //{{{ remove() method
  334. void remove(int index)
  335. {
  336. abbrevs.removeElementAt(index);
  337. fireTableStructureChanged();
  338. } //}}}
  339. //{{{ toHashtable() method
  340. public Hashtable<String,String> toHashtable()
  341. {
  342. Hashtable<String,String> hash = new Hashtable<String,String>();
  343. for(int i = 0; i < abbrevs.size(); i++)
  344. {
  345. Abbrev abbrev = abbrevs.get(i);
  346. if(abbrev.abbrev.length() > 0
  347. && abbrev.expand.length() > 0)
  348. {
  349. hash.put(abbrev.abbrev,abbrev.expand);
  350. }
  351. }
  352. return hash;
  353. } //}}}
  354. //{{{ getColumnCount() method
  355. public int getColumnCount()
  356. {
  357. return 2;
  358. } //}}}
  359. //{{{ getRowCount() method
  360. public int getRowCount()
  361. {
  362. return abbrevs.size();
  363. } //}}}
  364. //{{{ getValueAt() method
  365. public Object getValueAt(int row, int col)
  366. {
  367. Abbrev abbrev = abbrevs.get(row);
  368. switch(col)
  369. {
  370. case 0:
  371. return abbrev.abbrev;
  372. case 1:
  373. return abbrev.expand;
  374. default:
  375. return null;
  376. }
  377. } //}}}
  378. //{{{ setValueAt() method
  379. @Override
  380. public void setValueAt(Object value, int row, int col)
  381. {
  382. if(value == null)
  383. value = "";
  384. Abbrev abbrev = abbrevs.get(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. @Override
  393. public String getColumnName(int index)
  394. {
  395. switch(index)
  396. {
  397. case 0:
  398. return jEdit.getProperty("options.abbrevs.abbrev");
  399. case 1:
  400. return jEdit.getProperty("options.abbrevs.expand");
  401. default:
  402. return null;
  403. }
  404. } //}}}
  405. //{{{ AbbrevCompare class
  406. static class AbbrevCompare implements Comparator<Abbrev>
  407. {
  408. int col;
  409. AbbrevCompare(int col)
  410. {
  411. this.col = col;
  412. }
  413. public int compare(Abbrev a1, Abbrev a2)
  414. {
  415. if(col == 0)
  416. {
  417. String abbrev1 = a1.abbrev.toLowerCase();
  418. String abbrev2 = a2.abbrev.toLowerCase();
  419. return StandardUtilities.compareStrings(
  420. abbrev1,abbrev2,true);
  421. }
  422. else
  423. {
  424. String expand1 = a1.expand.toLowerCase();
  425. String expand2 = a2.expand.toLowerCase();
  426. return StandardUtilities.compareStrings(
  427. expand1,expand2,true);
  428. }
  429. }
  430. } //}}}
  431. } //}}}
  432. //{{{ Abbrev class
  433. class Abbrev
  434. {
  435. Abbrev() {}
  436. Abbrev(String abbrev, String expand)
  437. {
  438. this.abbrev = abbrev;
  439. this.expand = expand;
  440. }
  441. String abbrev;
  442. String expand;
  443. } //}}}