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

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

#
Java | 484 lines | 382 code | 54 blank | 48 comment | 44 complexity | 56c91bed05b0933d263788f91e7b1266 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. * ShortcutsOptionPane.java - Shortcuts options panel
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 1999, 2000, 2001 Slava Pestov
  7. * Copyright (C) 2001 Dirk Moebius
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version 2
  12. * of the License, or any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. package org.gjt.sp.jedit.options;
  24. //{{{ Imports
  25. import org.gjt.sp.jedit.*;
  26. import org.gjt.sp.jedit.gui.FilteredTableModel;
  27. import org.gjt.sp.jedit.gui.GrabKeyDialog;
  28. import org.gjt.sp.jedit.gui.GrabKeyDialog.KeyBinding;
  29. import org.gjt.sp.util.Log;
  30. import org.gjt.sp.util.StandardUtilities;
  31. import javax.swing.*;
  32. import javax.swing.event.DocumentEvent;
  33. import javax.swing.event.DocumentListener;
  34. import javax.swing.table.AbstractTableModel;
  35. import java.awt.*;
  36. import java.awt.event.ActionEvent;
  37. import java.awt.event.ActionListener;
  38. import java.awt.event.MouseAdapter;
  39. import java.awt.event.MouseEvent;
  40. import java.util.*;
  41. import java.util.List;
  42. //}}}
  43. /**
  44. * Key binding editor.
  45. * @author Slava Pestov
  46. * @version $Id: ShortcutsOptionPane.java 19268 2011-01-25 14:16:18Z kpouer $
  47. */
  48. public class ShortcutsOptionPane extends AbstractOptionPane
  49. {
  50. //{{{ ShortcutsOptionPane constructor
  51. public ShortcutsOptionPane()
  52. {
  53. super("shortcuts");
  54. } //}}}
  55. //{{{ _init() method
  56. @Override
  57. protected void _init()
  58. {
  59. allBindings = new Vector<KeyBinding>();
  60. setLayout(new BorderLayout(12,12));
  61. initModels();
  62. selectModel = new JComboBox(models);
  63. selectModel.addActionListener(new ActionHandler());
  64. selectModel.setToolTipText(jEdit.getProperty("options.shortcuts.select.tooltip"));
  65. Box north = Box.createHorizontalBox();
  66. north.add(new JLabel(jEdit.getProperty(
  67. "options.shortcuts.select.label")));
  68. north.add(Box.createHorizontalStrut(6));
  69. north.add(selectModel);
  70. filterTF = new JTextField(40);
  71. filterTF.setToolTipText(jEdit.getProperty("options.shortcuts.filter.tooltip"));
  72. filterTF.getDocument().addDocumentListener(new DocumentListener()
  73. {
  74. @Override
  75. public void changedUpdate(DocumentEvent e)
  76. {
  77. setFilter();
  78. }
  79. @Override
  80. public void insertUpdate(DocumentEvent e)
  81. {
  82. setFilter();
  83. }
  84. @Override
  85. public void removeUpdate(DocumentEvent e)
  86. {
  87. setFilter();
  88. }
  89. });
  90. JButton clearButton = new JButton(jEdit.getProperty(
  91. "options.shortcuts.clear.label"));
  92. clearButton.addActionListener(new ActionListener()
  93. {
  94. @Override
  95. public void actionPerformed(ActionEvent arg0)
  96. {
  97. filterTF.setText("");
  98. filterTF.requestFocus();
  99. }
  100. });
  101. JPanel filterPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
  102. filterPanel.add(new JLabel(jEdit.getProperty("options.shortcuts.filter.label")));
  103. filterPanel.add(filterTF);
  104. filterPanel.add(clearButton);
  105. keyTable = new JTable(filteredModel);
  106. filteredModel.setTable(keyTable);
  107. keyTable.getTableHeader().setReorderingAllowed(false);
  108. keyTable.getTableHeader().addMouseListener(new HeaderMouseHandler());
  109. keyTable.addMouseListener(new TableMouseHandler());
  110. Dimension d = keyTable.getPreferredSize();
  111. d.height = Math.min(d.height,200);
  112. JScrollPane scroller = new JScrollPane(keyTable);
  113. scroller.setPreferredSize(d);
  114. JPanel tableFilterPanel = new JPanel(new BorderLayout());
  115. tableFilterPanel.add(BorderLayout.NORTH,filterPanel);
  116. tableFilterPanel.add(BorderLayout.CENTER,scroller);
  117. add(BorderLayout.NORTH,north);
  118. add(BorderLayout.CENTER,tableFilterPanel);
  119. try
  120. {
  121. selectModel.setSelectedIndex(jEdit.getIntegerProperty("options.shortcuts.select.index", 0));
  122. }
  123. catch (IllegalArgumentException eae) {}
  124. } //}}}
  125. //{{{ _save() method
  126. @Override
  127. protected void _save()
  128. {
  129. if(keyTable.getCellEditor() != null)
  130. keyTable.getCellEditor().stopCellEditing();
  131. for (ShortcutsModel model : models)
  132. model.save();
  133. Macros.loadMacros();
  134. } //}}}
  135. //{{{ Private members
  136. private JTable keyTable;
  137. private Vector<ShortcutsModel> models;
  138. private FilteredTableModel<ShortcutsModel> filteredModel;
  139. private JComboBox selectModel;
  140. private List<KeyBinding> allBindings;
  141. private JTextField filterTF;
  142. //{{{ setFilter() method
  143. private void setFilter()
  144. {
  145. filteredModel.setFilter(filterTF.getText());
  146. } //}}}
  147. //{{{ initModels() method
  148. private void initModels()
  149. {
  150. List<KeyBinding[]> allBindings = new ArrayList<KeyBinding[]>();
  151. Set<String> knownBindings = new HashSet<String>();
  152. models = new Vector<ShortcutsModel>();
  153. ActionSet[] actionSets = jEdit.getActionSets();
  154. for(int i = 0; i < actionSets.length; i++)
  155. {
  156. ActionSet actionSet = actionSets[i];
  157. if(actionSet.getActionCount() != 0)
  158. {
  159. String modelLabel = actionSet.getLabel();
  160. if(modelLabel == null)
  161. {
  162. Log.log(Log.ERROR,this,"Empty action set: "
  163. + actionSet.getPluginJAR());
  164. }
  165. ShortcutsModel model = createModel(modelLabel,
  166. actionSet.getActionNames());
  167. models.addElement(model);
  168. List<KeyBinding[]> bindings = model.getBindings();
  169. for (KeyBinding[] binding : bindings)
  170. {
  171. String name = binding[0].name;
  172. if (!knownBindings.contains(name))
  173. {
  174. knownBindings.add(name);
  175. allBindings.add(binding);
  176. }
  177. }
  178. }
  179. }
  180. if (models.size() > 1)
  181. models.addElement(new ShortcutsModel("All", allBindings));
  182. Collections.sort(models,new StandardUtilities.StringCompare<ShortcutsModel>(true));
  183. ShortcutsModel currentModel = models.elementAt(0);
  184. filteredModel = new FilteredTableModel<ShortcutsModel>(currentModel)
  185. {
  186. @Override
  187. public String prepareFilter(String filter)
  188. {
  189. return filter.toLowerCase();
  190. }
  191. @Override
  192. public boolean passFilter(int row, String filter)
  193. {
  194. String name = delegated.getBindingAt(row, 0).label.toLowerCase();
  195. return name.contains(filter);
  196. }
  197. };
  198. } //}}}
  199. //{{{ createModel() method
  200. private ShortcutsModel createModel(String modelLabel, String[] actions)
  201. {
  202. List<GrabKeyDialog.KeyBinding[]> bindings = new ArrayList<GrabKeyDialog.KeyBinding[]>(actions.length);
  203. for(int i = 0; i < actions.length; i++)
  204. {
  205. String name = actions[i];
  206. EditAction ea = jEdit.getAction(name);
  207. String label = ea.getLabel();
  208. // Skip certain actions this way
  209. if(label == null)
  210. continue;
  211. label = GUIUtilities.prettifyMenuLabel(label);
  212. addBindings(name,label,bindings);
  213. }
  214. return new ShortcutsModel(modelLabel,bindings);
  215. } //}}}
  216. //{{{ addBindings() method
  217. private void addBindings(String name, String label, Collection<KeyBinding[]> bindings)
  218. {
  219. GrabKeyDialog.KeyBinding[] b = new GrabKeyDialog.KeyBinding[2];
  220. b[0] = createBinding(name,label,
  221. jEdit.getProperty(name + ".shortcut"));
  222. b[1] = createBinding(name,label,
  223. jEdit.getProperty(name + ".shortcut2"));
  224. bindings.add(b);
  225. } //}}}
  226. //{{{ createBinding() method
  227. private GrabKeyDialog.KeyBinding createBinding(String name,
  228. String label, String shortcut)
  229. {
  230. if(shortcut != null && shortcut.length() == 0)
  231. shortcut = null;
  232. GrabKeyDialog.KeyBinding binding
  233. = new GrabKeyDialog.KeyBinding(name,label,shortcut,false);
  234. allBindings.add(binding);
  235. return binding;
  236. } //}}}
  237. //{{{ Inner classes
  238. //{{{ HeaderMouseHandler class
  239. private class HeaderMouseHandler extends MouseAdapter
  240. {
  241. @Override
  242. public void mouseClicked(MouseEvent evt)
  243. {
  244. ShortcutsModel shortcutsModel = filteredModel.getDelegated();
  245. switch(keyTable.getTableHeader().columnAtPoint(evt.getPoint()))
  246. {
  247. case 0:
  248. shortcutsModel.sort(0);
  249. break;
  250. case 1:
  251. shortcutsModel.sort(1);
  252. break;
  253. case 2:
  254. shortcutsModel.sort(2);
  255. break;
  256. }
  257. setFilter();
  258. }
  259. } //}}}
  260. //{{{ TableMouseHandler class
  261. private class TableMouseHandler extends MouseAdapter
  262. {
  263. @Override
  264. public void mouseClicked(MouseEvent evt)
  265. {
  266. int row = keyTable.getSelectedRow();
  267. int col = keyTable.getSelectedColumn();
  268. if(col != 0 && row != -1)
  269. {
  270. GrabKeyDialog gkd = new GrabKeyDialog(
  271. GUIUtilities.getParentDialog(
  272. ShortcutsOptionPane.this),
  273. filteredModel.getDelegated().getBindingAt(filteredModel.getTrueRow(row), col - 1),
  274. allBindings,null);
  275. if(gkd.isOK())
  276. filteredModel.setValueAt(
  277. gkd.getShortcut(),row,col);
  278. }
  279. }
  280. } //}}}
  281. //{{{ ActionHandler class
  282. private class ActionHandler implements ActionListener
  283. {
  284. @Override
  285. public void actionPerformed(ActionEvent evt)
  286. {
  287. ShortcutsModel newModel
  288. = (ShortcutsModel)selectModel.getSelectedItem();
  289. if(filteredModel.getDelegated() != newModel)
  290. {
  291. jEdit.setIntegerProperty("options.shortcuts.select.index", selectModel.getSelectedIndex());
  292. filteredModel.setDelegated(newModel);
  293. setFilter();
  294. }
  295. }
  296. } //}}}
  297. //{{{ ShortcutsModel class
  298. private static class ShortcutsModel extends AbstractTableModel
  299. {
  300. private final List<GrabKeyDialog.KeyBinding[]> bindings;
  301. private final String name;
  302. ShortcutsModel(String name, List<GrabKeyDialog.KeyBinding[]> bindings)
  303. {
  304. this.name = name;
  305. this.bindings = bindings;
  306. sort(0);
  307. }
  308. public List<GrabKeyDialog.KeyBinding[]> getBindings()
  309. {
  310. return bindings;
  311. }
  312. public void sort(int col)
  313. {
  314. Collections.sort(bindings,new KeyCompare(col));
  315. }
  316. @Override
  317. public int getColumnCount()
  318. {
  319. return 3;
  320. }
  321. @Override
  322. public int getRowCount()
  323. {
  324. return bindings.size();
  325. }
  326. @Override
  327. public Object getValueAt(int row, int col)
  328. {
  329. switch(col)
  330. {
  331. case 0:
  332. return getBindingAt(row,0).label;
  333. case 1:
  334. return getBindingAt(row,0).shortcut;
  335. case 2:
  336. return getBindingAt(row,1).shortcut;
  337. default:
  338. return null;
  339. }
  340. }
  341. @Override
  342. public void setValueAt(Object value, int row, int col)
  343. {
  344. if(col == 0)
  345. return;
  346. getBindingAt(row,col-1).shortcut = (String)value;
  347. // redraw the whole table because a second shortcut
  348. // might have changed, too
  349. fireTableDataChanged();
  350. }
  351. @Override
  352. public String getColumnName(int index)
  353. {
  354. switch(index)
  355. {
  356. case 0:
  357. return jEdit.getProperty("options.shortcuts.name");
  358. case 1:
  359. return jEdit.getProperty("options.shortcuts.shortcut1");
  360. case 2:
  361. return jEdit.getProperty("options.shortcuts.shortcut2");
  362. default:
  363. return null;
  364. }
  365. }
  366. public void save()
  367. {
  368. for (GrabKeyDialog.KeyBinding[] binding : bindings)
  369. {
  370. jEdit.setProperty(
  371. binding[0].name + ".shortcut",
  372. binding[0].shortcut);
  373. jEdit.setProperty(
  374. binding[1].name + ".shortcut2",
  375. binding[1].shortcut);
  376. }
  377. }
  378. public GrabKeyDialog.KeyBinding getBindingAt(int row, int nr)
  379. {
  380. GrabKeyDialog.KeyBinding[] binding = bindings.get(row);
  381. return binding[nr];
  382. }
  383. @Override
  384. public String toString()
  385. {
  386. return name;
  387. }
  388. private static class KeyCompare implements Comparator<GrabKeyDialog.KeyBinding[]>
  389. {
  390. private final int col;
  391. KeyCompare(int col)
  392. {
  393. this.col = col;
  394. }
  395. @Override
  396. public int compare(GrabKeyDialog.KeyBinding[] k1, GrabKeyDialog.KeyBinding[] k2)
  397. {
  398. String label1 = k1[0].label.toLowerCase();
  399. String label2 = k2[0].label.toLowerCase();
  400. if(col == 0)
  401. return StandardUtilities.compareStrings(
  402. label1,label2,true);
  403. else
  404. {
  405. String shortcut1, shortcut2;
  406. if(col == 1)
  407. {
  408. shortcut1 = k1[0].shortcut;
  409. shortcut2 = k2[0].shortcut;
  410. }
  411. else
  412. {
  413. shortcut1 = k1[1].shortcut;
  414. shortcut2 = k2[1].shortcut;
  415. }
  416. if(shortcut1 == null && shortcut2 != null)
  417. return 1;
  418. else if(shortcut2 == null && shortcut1 != null)
  419. return -1;
  420. else if(shortcut1 == null)
  421. return StandardUtilities.compareStrings(label1,label2,true);
  422. else
  423. return StandardUtilities.compareStrings(shortcut1,shortcut2,true);
  424. }
  425. }
  426. }
  427. } //}}}
  428. //}}}
  429. //}}}
  430. }