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

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/options/AbbrevsOptionPane.java

#
Java | 507 lines | 385 code | 63 blank | 59 comment | 49 complexity | 3aede0a01df69f350ad09a5478332722 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 java.util.List;
  32. import org.gjt.sp.jedit.gui.*;
  33. import org.gjt.sp.jedit.*;
  34. import org.gjt.sp.util.StandardUtilities;
  35. //}}}
  36. //{{{ AbbrevsOptionPane class
  37. /**
  38. * Abbrev editor.
  39. * @author Slava Pestov
  40. * @version $Id: AbbrevsOptionPane.java 14126 2008-12-01 10:09:52Z kpouer $
  41. */
  42. public class AbbrevsOptionPane extends AbstractOptionPane
  43. {
  44. //{{{ AbbrevsOptionPane constructor
  45. public AbbrevsOptionPane()
  46. {
  47. super("abbrevs");
  48. } //}}}
  49. //{{{ _init() method
  50. @Override
  51. protected void _init()
  52. {
  53. setLayout(new BorderLayout());
  54. JPanel panel = new JPanel(new BorderLayout(6,6));
  55. expandOnInput = new JCheckBox(jEdit.getProperty("options.abbrevs"
  56. + ".expandOnInput"),Abbrevs.getExpandOnInput());
  57. panel.add(expandOnInput,BorderLayout.NORTH);
  58. JPanel panel2 = new JPanel();
  59. panel2.setLayout(new BoxLayout(panel2,BoxLayout.X_AXIS));
  60. panel2.setBorder(new EmptyBorder(0,0,6,0));
  61. panel2.add(Box.createGlue());
  62. JLabel label = new JLabel(jEdit.getProperty("options.abbrevs.set"));
  63. label.setBorder(new EmptyBorder(0,0,0,12));
  64. panel2.add(label);
  65. Map<String,Hashtable<String,String>> _modeAbbrevs = Abbrevs.getModeAbbrevs();
  66. modeAbbrevs = new HashMap<String,AbbrevsModel>();
  67. Mode[] modes = jEdit.getModes();
  68. Arrays.sort(modes,new StandardUtilities.StringCompare<Mode>(true));
  69. String[] sets = new String[modes.length + 1];
  70. sets[0] = "global";
  71. for(int i = 0; i < modes.length; i++)
  72. {
  73. String name = modes[i].getName();
  74. sets[i+1] = name;
  75. modeAbbrevs.put(name,new AbbrevsModel(_modeAbbrevs.get(name)));
  76. }
  77. setsComboBox = new JComboBox(sets);
  78. ActionHandler actionHandler = new ActionHandler();
  79. setsComboBox.addActionListener(actionHandler);
  80. panel2.add(setsComboBox);
  81. panel2.add(Box.createGlue());
  82. panel.add(panel2,BorderLayout.SOUTH);
  83. add(BorderLayout.NORTH,panel);
  84. globalAbbrevs = new AbbrevsModel(Abbrevs.getGlobalAbbrevs());
  85. abbrevsTable = new JTable(globalAbbrevs);
  86. abbrevsTable.getColumnModel().getColumn(1).setCellRenderer(
  87. new Renderer());
  88. abbrevsTable.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
  89. abbrevsTable.getTableHeader().setReorderingAllowed(false);
  90. abbrevsTable.getTableHeader().addMouseListener(new HeaderMouseHandler());
  91. abbrevsTable.getSelectionModel().addListSelectionListener(
  92. new SelectionHandler());
  93. abbrevsTable.getSelectionModel().setSelectionMode(
  94. ListSelectionModel.SINGLE_SELECTION);
  95. abbrevsTable.addMouseListener(new TableMouseHandler());
  96. Dimension d = abbrevsTable.getPreferredSize();
  97. d.height = Math.min(d.height,200);
  98. JScrollPane scroller = new JScrollPane(abbrevsTable);
  99. scroller.setPreferredSize(d);
  100. add(BorderLayout.CENTER,scroller);
  101. JPanel buttons = new JPanel();
  102. buttons.setLayout(new BoxLayout(buttons,BoxLayout.X_AXIS));
  103. buttons.setBorder(new EmptyBorder(6,0,0,0));
  104. add = new RolloverButton(GUIUtilities.loadIcon(jEdit.getProperty("options.abbrevs.add.icon")));
  105. add.setToolTipText(jEdit.getProperty("options.abbrevs.add"));
  106. add.addActionListener(actionHandler);
  107. buttons.add(add);
  108. remove = new RolloverButton(GUIUtilities.loadIcon(jEdit.getProperty("options.abbrevs.remove.icon")));
  109. remove.setToolTipText(jEdit.getProperty("options.abbrevs.remove"));
  110. remove.addActionListener(actionHandler);
  111. buttons.add(remove);
  112. edit = new RolloverButton(GUIUtilities.loadIcon(jEdit.getProperty("options.abbrevs.edit.icon")));
  113. edit.setToolTipText(jEdit.getProperty("options.abbrevs.edit"));
  114. edit.addActionListener(actionHandler);
  115. buttons.add(edit);
  116. buttons.add(Box.createGlue());
  117. add(BorderLayout.SOUTH,buttons);
  118. setsComboBox.setSelectedIndex(jEdit.getIntegerProperty("options.abbrevs.combobox.index", 0));
  119. updateEnabled();
  120. } //}}}
  121. //{{{ _save() method
  122. @Override
  123. protected void _save()
  124. {
  125. if(abbrevsTable.getCellEditor() != null)
  126. abbrevsTable.getCellEditor().stopCellEditing();
  127. Abbrevs.setExpandOnInput(expandOnInput.isSelected());
  128. Abbrevs.setGlobalAbbrevs(globalAbbrevs.toHashtable());
  129. Hashtable<String,Hashtable<String,String>> modeHash = new Hashtable<String,Hashtable<String,String>>();
  130. Set<Map.Entry<String,AbbrevsModel>> entrySet = modeAbbrevs.entrySet();
  131. for (Map.Entry<String,AbbrevsModel> entry : entrySet)
  132. {
  133. modeHash.put(entry.getKey(),entry.getValue().toHashtable());
  134. }
  135. Abbrevs.setModeAbbrevs(modeHash);
  136. } //}}}
  137. //{{{ Private members
  138. //{{{ Instance variables
  139. private JComboBox setsComboBox;
  140. private JCheckBox expandOnInput;
  141. private JTable abbrevsTable;
  142. private AbbrevsModel globalAbbrevs;
  143. private Map<String,AbbrevsModel> modeAbbrevs;
  144. private JButton add;
  145. private JButton edit;
  146. private JButton remove;
  147. //}}}
  148. //{{{ updateEnabled() method
  149. private void updateEnabled()
  150. {
  151. int selectedRow = abbrevsTable.getSelectedRow();
  152. edit.setEnabled(selectedRow != -1);
  153. remove.setEnabled(selectedRow != -1);
  154. } //}}}
  155. //{{{ edit() method
  156. private void edit()
  157. {
  158. AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel();
  159. int row = abbrevsTable.getSelectedRow();
  160. String abbrev = (String)abbrevsModel.getValueAt(row,0);
  161. String expansion = (String)abbrevsModel.getValueAt(row,1);
  162. String oldAbbrev = abbrev;
  163. EditAbbrevDialog dialog = new EditAbbrevDialog(
  164. GUIUtilities.getParentDialog(AbbrevsOptionPane.this),
  165. abbrev,expansion,abbrevsModel.toHashtable());
  166. abbrev = dialog.getAbbrev();
  167. expansion = dialog.getExpansion();
  168. if(abbrev != null && expansion != null)
  169. {
  170. for(int i = 0; i < abbrevsModel.getRowCount(); i++)
  171. {
  172. if(abbrevsModel.getValueAt(i,0).equals(oldAbbrev))
  173. {
  174. abbrevsModel.remove(i);
  175. break;
  176. }
  177. }
  178. add(abbrevsModel,abbrev,expansion);
  179. }
  180. } //}}}
  181. //{{{ add() method
  182. private void add(AbbrevsModel abbrevsModel, String abbrev,
  183. String expansion)
  184. {
  185. for(int i = 0; i < abbrevsModel.getRowCount(); i++)
  186. {
  187. if(abbrevsModel.getValueAt(i,0).equals(abbrev))
  188. {
  189. abbrevsModel.remove(i);
  190. break;
  191. }
  192. }
  193. abbrevsModel.add(abbrev,expansion);
  194. updateEnabled();
  195. } //}}}
  196. //}}}
  197. //{{{ HeaderMouseHandler class
  198. private class HeaderMouseHandler extends MouseAdapter
  199. {
  200. @Override
  201. public void mouseClicked(MouseEvent evt)
  202. {
  203. switch(abbrevsTable.getTableHeader().columnAtPoint(evt.getPoint()))
  204. {
  205. case 0:
  206. ((AbbrevsModel)abbrevsTable.getModel()).sort(0);
  207. break;
  208. case 1:
  209. ((AbbrevsModel)abbrevsTable.getModel()).sort(1);
  210. break;
  211. }
  212. }
  213. } //}}}
  214. //{{{ TableMouseHandler class
  215. private class TableMouseHandler extends MouseAdapter
  216. {
  217. @Override
  218. public void mouseClicked(MouseEvent evt)
  219. {
  220. if(evt.getClickCount() == 2)
  221. edit();
  222. }
  223. } //}}}
  224. //{{{ SelectionHandler class
  225. private class SelectionHandler implements ListSelectionListener
  226. {
  227. public void valueChanged(ListSelectionEvent evt)
  228. {
  229. updateEnabled();
  230. }
  231. } //}}}
  232. //{{{ ActionHandler class
  233. private class ActionHandler implements ActionListener
  234. {
  235. public void actionPerformed(ActionEvent evt)
  236. {
  237. AbbrevsModel abbrevsModel = (AbbrevsModel)abbrevsTable.getModel();
  238. Object source = evt.getSource();
  239. if(source == setsComboBox)
  240. {
  241. jEdit.setIntegerProperty("options.abbrevs.combobox.index", setsComboBox.getSelectedIndex());
  242. String selected = (String)setsComboBox.getSelectedItem();
  243. if(selected.equals("global"))
  244. {
  245. abbrevsTable.setModel(globalAbbrevs);
  246. }
  247. else
  248. {
  249. abbrevsTable.setModel(modeAbbrevs.get(selected));
  250. }
  251. updateEnabled();
  252. }
  253. else if(source == add)
  254. {
  255. EditAbbrevDialog dialog = new EditAbbrevDialog(
  256. GUIUtilities.getParentDialog(AbbrevsOptionPane.this),
  257. null,null,abbrevsModel.toHashtable());
  258. String abbrev = dialog.getAbbrev();
  259. String expansion = dialog.getExpansion();
  260. if(abbrev != null && abbrev.length() != 0
  261. && expansion != null
  262. && expansion.length() != 0)
  263. {
  264. add(abbrevsModel,abbrev,expansion);
  265. }
  266. }
  267. else if(source == edit)
  268. {
  269. edit();
  270. }
  271. else if(source == remove)
  272. {
  273. int selectedRow = abbrevsTable.getSelectedRow();
  274. abbrevsModel.remove(selectedRow);
  275. updateEnabled();
  276. }
  277. }
  278. } //}}}
  279. //{{{ Renderer class
  280. private static class Renderer extends DefaultTableCellRenderer
  281. {
  282. @Override
  283. public Component getTableCellRendererComponent(
  284. JTable table,
  285. Object value,
  286. boolean isSelected,
  287. boolean cellHasFocus,
  288. int row,
  289. int col)
  290. {
  291. String valueStr = value.toString();
  292. // workaround for Swing's annoying processing of
  293. // labels starting with <html>, which often breaks
  294. if(valueStr.toLowerCase().startsWith("<html>"))
  295. valueStr = ' ' + valueStr;
  296. return super.getTableCellRendererComponent(table,valueStr,
  297. isSelected,cellHasFocus,row,col);
  298. }
  299. } //}}}
  300. //{{{ AbbrevsModel class
  301. private static class AbbrevsModel extends AbstractTableModel
  302. {
  303. List<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.remove(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. private static class AbbrevCompare implements Comparator<Abbrev>
  407. {
  408. private 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. //{{{ Abbrev class
  432. private static class Abbrev
  433. {
  434. Abbrev() {}
  435. Abbrev(String abbrev, String expand)
  436. {
  437. this.abbrev = abbrev;
  438. this.expand = expand;
  439. }
  440. String abbrev;
  441. String expand;
  442. } //}}}
  443. } //}}}
  444. } //}}}