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

/jEdit/tags/jedit-4-3-pre5/org/gjt/sp/jedit/options/ShortcutsOptionPane.java

#
Java | 367 lines | 288 code | 50 blank | 29 comment | 37 complexity | adf3b36ea2f6069376949cf6523e758f 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. * Copyright (C) 1999, 2000, 2001 Slava Pestov
  4. * Copyright (C) 2001 Dirk Moebius
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. */
  20. package org.gjt.sp.jedit.options;
  21. import javax.swing.table.*;
  22. import javax.swing.*;
  23. import java.awt.event.*;
  24. import java.awt.*;
  25. import java.util.*;
  26. import org.gjt.sp.jedit.gui.GrabKeyDialog;
  27. import org.gjt.sp.jedit.*;
  28. import org.gjt.sp.util.Log;
  29. /**
  30. * Key binding editor.
  31. * @author Slava Pestov
  32. * @version $Id: ShortcutsOptionPane.java 5527 2006-07-04 18:49:40Z ezust $
  33. */
  34. public class ShortcutsOptionPane extends AbstractOptionPane
  35. {
  36. public ShortcutsOptionPane()
  37. {
  38. super("shortcuts");
  39. }
  40. // protected members
  41. protected void _init()
  42. {
  43. allBindings = new Vector();
  44. setLayout(new BorderLayout(12,12));
  45. initModels();
  46. selectModel = new JComboBox(models);
  47. selectModel.addActionListener(new ActionHandler());
  48. selectModel.setToolTipText(jEdit.getProperty("options.shortcuts.select.tooltip"));
  49. Box north = Box.createHorizontalBox();
  50. north.add(new JLabel(jEdit.getProperty(
  51. "options.shortcuts.select.label")));
  52. north.add(Box.createHorizontalStrut(6));
  53. north.add(selectModel);
  54. keyTable = new JTable(currentModel);
  55. keyTable.getTableHeader().setReorderingAllowed(false);
  56. keyTable.getTableHeader().addMouseListener(new HeaderMouseHandler());
  57. keyTable.addMouseListener(new TableMouseHandler());
  58. Dimension d = keyTable.getPreferredSize();
  59. d.height = Math.min(d.height,200);
  60. JScrollPane scroller = new JScrollPane(keyTable);
  61. scroller.setPreferredSize(d);
  62. add(BorderLayout.NORTH,north);
  63. add(BorderLayout.CENTER,scroller);
  64. selectModel.setSelectedIndex(jEdit.getIntegerProperty("options.shortcuts.select.index", 0));
  65. }
  66. protected void _save()
  67. {
  68. if(keyTable.getCellEditor() != null)
  69. keyTable.getCellEditor().stopCellEditing();
  70. Enumeration e = models.elements();
  71. while(e.hasMoreElements())
  72. ((ShortcutsModel)e.nextElement()).save();
  73. Macros.loadMacros();
  74. }
  75. private void initModels()
  76. {
  77. models = new Vector();
  78. ActionSet[] actionSets = jEdit.getActionSets();
  79. for(int i = 0; i < actionSets.length; i++)
  80. {
  81. ActionSet actionSet = actionSets[i];
  82. if(actionSet.getActionCount() != 0)
  83. {
  84. String modelLabel = actionSet.getLabel();
  85. if(modelLabel == null)
  86. {
  87. Log.log(Log.ERROR,this,"Empty action set: "
  88. + actionSet.getPluginJAR());
  89. }
  90. models.addElement(createModel(modelLabel,
  91. actionSet.getActionNames()));
  92. }
  93. }
  94. Collections.sort(models,new MiscUtilities.StringICaseCompare());
  95. currentModel = (ShortcutsModel)models.elementAt(0);
  96. }
  97. private ShortcutsModel createModel(String modelLabel, String[] actions)
  98. {
  99. Vector bindings = new Vector(actions.length);
  100. for(int i = 0; i < actions.length; i++)
  101. {
  102. String name = actions[i];
  103. String label = jEdit.getProperty(actions[i] + ".label");
  104. // Skip certain actions this way
  105. if(label == null)
  106. continue;
  107. label = GUIUtilities.prettifyMenuLabel(label);
  108. addBindings(name,label,bindings);
  109. }
  110. return new ShortcutsModel(modelLabel,bindings);
  111. }
  112. private void addBindings(String name, String label, Vector bindings)
  113. {
  114. GrabKeyDialog.KeyBinding b[] = new GrabKeyDialog.KeyBinding[2];
  115. b[0] = createBinding(name,label,
  116. jEdit.getProperty(name + ".shortcut"));
  117. b[1] = createBinding(name,label,
  118. jEdit.getProperty(name + ".shortcut2"));
  119. bindings.addElement(b);
  120. }
  121. private GrabKeyDialog.KeyBinding createBinding(String name,
  122. String label, String shortcut)
  123. {
  124. if(shortcut != null && shortcut.length() == 0)
  125. shortcut = null;
  126. GrabKeyDialog.KeyBinding binding
  127. = new GrabKeyDialog.KeyBinding(name,label,shortcut,false);
  128. allBindings.addElement(binding);
  129. return binding;
  130. }
  131. // private members
  132. private JTable keyTable;
  133. private Vector models;
  134. private ShortcutsModel currentModel;
  135. private JComboBox selectModel;
  136. private Vector allBindings;
  137. class HeaderMouseHandler extends MouseAdapter
  138. {
  139. public void mouseClicked(MouseEvent evt)
  140. {
  141. switch(keyTable.getTableHeader().columnAtPoint(evt.getPoint()))
  142. {
  143. case 0:
  144. currentModel.sort(0);
  145. break;
  146. case 1:
  147. currentModel.sort(1);
  148. break;
  149. case 2:
  150. currentModel.sort(2);
  151. break;
  152. }
  153. }
  154. }
  155. class TableMouseHandler extends MouseAdapter
  156. {
  157. public void mouseClicked(MouseEvent evt)
  158. {
  159. int row = keyTable.getSelectedRow();
  160. int col = keyTable.getSelectedColumn();
  161. if(col != 0 && row != -1)
  162. {
  163. GrabKeyDialog gkd = new GrabKeyDialog(
  164. GUIUtilities.getParentDialog(
  165. ShortcutsOptionPane.this),
  166. currentModel.getBindingAt(row,col-1),
  167. allBindings,null);
  168. if(gkd.isOK())
  169. currentModel.setValueAt(
  170. gkd.getShortcut(),row,col);
  171. }
  172. }
  173. }
  174. class ActionHandler implements ActionListener
  175. {
  176. public void actionPerformed(ActionEvent evt)
  177. {
  178. ShortcutsModel newModel
  179. = (ShortcutsModel)selectModel.getSelectedItem();
  180. if(currentModel != newModel)
  181. {
  182. jEdit.setIntegerProperty("options.shortcuts.select.index", selectModel.getSelectedIndex());
  183. currentModel = newModel;
  184. keyTable.setModel(currentModel);
  185. }
  186. }
  187. }
  188. class ShortcutsModel extends AbstractTableModel
  189. {
  190. private Vector bindings;
  191. private String name;
  192. ShortcutsModel(String name, Vector bindings)
  193. {
  194. this.name = name;
  195. this.bindings = bindings;
  196. sort(0);
  197. }
  198. public void sort(int col)
  199. {
  200. Collections.sort(bindings,new KeyCompare(col));
  201. fireTableDataChanged();
  202. }
  203. public int getColumnCount()
  204. {
  205. return 3;
  206. }
  207. public int getRowCount()
  208. {
  209. return bindings.size();
  210. }
  211. public Object getValueAt(int row, int col)
  212. {
  213. switch(col)
  214. {
  215. case 0:
  216. return getBindingAt(row,0).label;
  217. case 1:
  218. return getBindingAt(row,0).shortcut;
  219. case 2:
  220. return getBindingAt(row,1).shortcut;
  221. default:
  222. return null;
  223. }
  224. }
  225. public void setValueAt(Object value, int row, int col)
  226. {
  227. if(col == 0)
  228. return;
  229. getBindingAt(row,col-1).shortcut = (String)value;
  230. // redraw the whole table because a second shortcut
  231. // might have changed, too
  232. fireTableDataChanged();
  233. }
  234. public String getColumnName(int index)
  235. {
  236. switch(index)
  237. {
  238. case 0:
  239. return jEdit.getProperty("options.shortcuts.name");
  240. case 1:
  241. return jEdit.getProperty("options.shortcuts.shortcut1");
  242. case 2:
  243. return jEdit.getProperty("options.shortcuts.shortcut2");
  244. default:
  245. return null;
  246. }
  247. }
  248. public void save()
  249. {
  250. Enumeration e = bindings.elements();
  251. while(e.hasMoreElements())
  252. {
  253. GrabKeyDialog.KeyBinding binding[]
  254. = (GrabKeyDialog.KeyBinding[])
  255. e.nextElement();
  256. jEdit.setProperty(
  257. binding[0].name + ".shortcut",
  258. binding[0].shortcut);
  259. jEdit.setProperty(
  260. binding[1].name + ".shortcut2",
  261. binding[1].shortcut);
  262. }
  263. }
  264. public GrabKeyDialog.KeyBinding getBindingAt(int row, int nr)
  265. {
  266. GrabKeyDialog.KeyBinding binding[]
  267. = (GrabKeyDialog.KeyBinding[])
  268. bindings.elementAt(row);
  269. return binding[nr];
  270. }
  271. public String toString()
  272. {
  273. return name;
  274. }
  275. class KeyCompare implements Comparator
  276. {
  277. int col;
  278. KeyCompare(int col)
  279. {
  280. this.col = col;
  281. }
  282. public int compare(Object obj1, Object obj2)
  283. {
  284. GrabKeyDialog.KeyBinding[] k1
  285. = (GrabKeyDialog.KeyBinding[])obj1;
  286. GrabKeyDialog.KeyBinding[] k2
  287. = (GrabKeyDialog.KeyBinding[])obj2;
  288. String label1 = k1[0].label.toLowerCase();
  289. String label2 = k2[0].label.toLowerCase();
  290. if(col == 0)
  291. return MiscUtilities.compareStrings(
  292. label1,label2,true);
  293. else
  294. {
  295. String shortcut1, shortcut2;
  296. if(col == 1)
  297. {
  298. shortcut1 = k1[0].shortcut;
  299. shortcut2 = k2[0].shortcut;
  300. }
  301. else
  302. {
  303. shortcut1 = k1[1].shortcut;
  304. shortcut2 = k2[1].shortcut;
  305. }
  306. if(shortcut1 == null && shortcut2 != null)
  307. return 1;
  308. if(shortcut2 == null && shortcut1 != null)
  309. return -1;
  310. if(shortcut1 == null)
  311. return MiscUtilities.compareStrings(label1,label2,true);
  312. return MiscUtilities.compareStrings(shortcut1,shortcut2,true);
  313. }
  314. }
  315. }
  316. }
  317. }