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

/jEdit/tags/jedit-4-2-pre4/org/gjt/sp/jedit/options/ShortcutsOptionPane.java

#
Java | 357 lines | 279 code | 49 blank | 29 comment | 39 complexity | c52a806ec91965dbbb80f0bfee51ce5f 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. /**
  29. * Key binding editor.
  30. * @author Slava Pestov
  31. * @version $Id: ShortcutsOptionPane.java 4826 2003-07-14 23:00:54Z spestov $
  32. */
  33. public class ShortcutsOptionPane extends AbstractOptionPane
  34. {
  35. public ShortcutsOptionPane()
  36. {
  37. super("shortcuts");
  38. }
  39. // protected members
  40. protected void _init()
  41. {
  42. allBindings = new Vector();
  43. setLayout(new BorderLayout(12,12));
  44. initModels();
  45. selectModel = new JComboBox(models);
  46. selectModel.addActionListener(new ActionHandler());
  47. Box north = Box.createHorizontalBox();
  48. north.add(new JLabel(jEdit.getProperty(
  49. "options.shortcuts.select.label")));
  50. north.add(Box.createHorizontalStrut(6));
  51. north.add(selectModel);
  52. keyTable = new JTable(currentModel);
  53. keyTable.getTableHeader().setReorderingAllowed(false);
  54. keyTable.getTableHeader().addMouseListener(new HeaderMouseHandler());
  55. keyTable.addMouseListener(new TableMouseHandler());
  56. Dimension d = keyTable.getPreferredSize();
  57. d.height = Math.min(d.height,200);
  58. JScrollPane scroller = new JScrollPane(keyTable);
  59. scroller.setPreferredSize(d);
  60. add(BorderLayout.NORTH,north);
  61. add(BorderLayout.CENTER,scroller);
  62. }
  63. protected void _save()
  64. {
  65. if(keyTable.getCellEditor() != null)
  66. keyTable.getCellEditor().stopCellEditing();
  67. Enumeration e = models.elements();
  68. while(e.hasMoreElements())
  69. ((ShortcutsModel)e.nextElement()).save();
  70. Macros.loadMacros();
  71. }
  72. private void initModels()
  73. {
  74. models = new Vector();
  75. ActionSet[] actionSets = jEdit.getActionSets();
  76. for(int i = 0; i < actionSets.length; i++)
  77. {
  78. ActionSet actionSet = actionSets[i];
  79. if(actionSet.getActionCount() != 0)
  80. {
  81. models.addElement(createModel(actionSet.getLabel(),
  82. actionSet.getActionNames()));
  83. }
  84. }
  85. Collections.sort(models,new MiscUtilities.StringICaseCompare());
  86. currentModel = (ShortcutsModel)models.elementAt(0);
  87. }
  88. private ShortcutsModel createModel(String modelLabel, String[] actions)
  89. {
  90. Vector bindings = new Vector(actions.length);
  91. for(int i = 0; i < actions.length; i++)
  92. {
  93. String name = actions[i];
  94. String label = jEdit.getProperty(actions[i] + ".label");
  95. // Skip certain actions this way
  96. if(label == null)
  97. continue;
  98. label = GUIUtilities.prettifyMenuLabel(label);
  99. addBindings(name,label,bindings);
  100. }
  101. return new ShortcutsModel(modelLabel,bindings);
  102. }
  103. private void addBindings(String name, String label, Vector bindings)
  104. {
  105. GrabKeyDialog.KeyBinding b[] = new GrabKeyDialog.KeyBinding[2];
  106. b[0] = createBinding(name,label,
  107. jEdit.getProperty(name + ".shortcut"));
  108. b[1] = createBinding(name,label,
  109. jEdit.getProperty(name + ".shortcut2"));
  110. bindings.addElement(b);
  111. }
  112. private GrabKeyDialog.KeyBinding createBinding(String name,
  113. String label, String shortcut)
  114. {
  115. if(shortcut != null && shortcut.length() == 0)
  116. shortcut = null;
  117. GrabKeyDialog.KeyBinding binding
  118. = new GrabKeyDialog.KeyBinding(name,label,shortcut,false);
  119. allBindings.addElement(binding);
  120. return binding;
  121. }
  122. // private members
  123. private JTable keyTable;
  124. private Vector models;
  125. private ShortcutsModel currentModel;
  126. private JComboBox selectModel;
  127. private Vector allBindings;
  128. class HeaderMouseHandler extends MouseAdapter
  129. {
  130. public void mouseClicked(MouseEvent evt)
  131. {
  132. switch(keyTable.getTableHeader().columnAtPoint(evt.getPoint()))
  133. {
  134. case 0:
  135. currentModel.sort(0);
  136. break;
  137. case 1:
  138. currentModel.sort(1);
  139. break;
  140. case 2:
  141. currentModel.sort(2);
  142. break;
  143. }
  144. }
  145. }
  146. class TableMouseHandler extends MouseAdapter
  147. {
  148. public void mouseClicked(MouseEvent evt)
  149. {
  150. int row = keyTable.getSelectedRow();
  151. int col = keyTable.getSelectedColumn();
  152. if(col != 0 && row != -1)
  153. {
  154. GrabKeyDialog gkd = new GrabKeyDialog(
  155. GUIUtilities.getParentDialog(
  156. ShortcutsOptionPane.this),
  157. currentModel.getBindingAt(row,col-1),
  158. allBindings,null);
  159. if(gkd.isOK())
  160. currentModel.setValueAt(
  161. gkd.getShortcut(),row,col);
  162. }
  163. }
  164. }
  165. class ActionHandler implements ActionListener
  166. {
  167. public void actionPerformed(ActionEvent evt)
  168. {
  169. ShortcutsModel newModel
  170. = (ShortcutsModel)selectModel.getSelectedItem();
  171. if(currentModel != newModel)
  172. {
  173. currentModel = newModel;
  174. keyTable.setModel(currentModel);
  175. }
  176. }
  177. }
  178. class ShortcutsModel extends AbstractTableModel
  179. {
  180. private Vector bindings;
  181. private String name;
  182. ShortcutsModel(String name, Vector bindings)
  183. {
  184. this.name = name;
  185. this.bindings = bindings;
  186. sort(0);
  187. }
  188. public void sort(int col)
  189. {
  190. MiscUtilities.quicksort(bindings,new KeyCompare(col));
  191. fireTableDataChanged();
  192. }
  193. public int getColumnCount()
  194. {
  195. return 3;
  196. }
  197. public int getRowCount()
  198. {
  199. return bindings.size();
  200. }
  201. public Object getValueAt(int row, int col)
  202. {
  203. switch(col)
  204. {
  205. case 0:
  206. return getBindingAt(row,0).label;
  207. case 1:
  208. return getBindingAt(row,0).shortcut;
  209. case 2:
  210. return getBindingAt(row,1).shortcut;
  211. default:
  212. return null;
  213. }
  214. }
  215. public void setValueAt(Object value, int row, int col)
  216. {
  217. if(col == 0)
  218. return;
  219. getBindingAt(row,col-1).shortcut = (String)value;
  220. // redraw the whole table because a second shortcut
  221. // might have changed, too
  222. fireTableDataChanged();
  223. }
  224. public String getColumnName(int index)
  225. {
  226. switch(index)
  227. {
  228. case 0:
  229. return jEdit.getProperty("options.shortcuts.name");
  230. case 1:
  231. return jEdit.getProperty("options.shortcuts.shortcut1");
  232. case 2:
  233. return jEdit.getProperty("options.shortcuts.shortcut2");
  234. default:
  235. return null;
  236. }
  237. }
  238. public void save()
  239. {
  240. Enumeration enum = bindings.elements();
  241. while(enum.hasMoreElements())
  242. {
  243. GrabKeyDialog.KeyBinding binding[]
  244. = (GrabKeyDialog.KeyBinding[])
  245. enum.nextElement();
  246. jEdit.setProperty(
  247. binding[0].name + ".shortcut",
  248. binding[0].shortcut);
  249. jEdit.setProperty(
  250. binding[1].name + ".shortcut2",
  251. binding[1].shortcut);
  252. }
  253. }
  254. public GrabKeyDialog.KeyBinding getBindingAt(int row, int nr)
  255. {
  256. GrabKeyDialog.KeyBinding binding[]
  257. = (GrabKeyDialog.KeyBinding[])
  258. bindings.elementAt(row);
  259. return binding[nr];
  260. }
  261. public String toString()
  262. {
  263. return name;
  264. }
  265. class KeyCompare implements MiscUtilities.Compare
  266. {
  267. int col;
  268. KeyCompare(int col)
  269. {
  270. this.col = col;
  271. }
  272. public int compare(Object obj1, Object obj2)
  273. {
  274. GrabKeyDialog.KeyBinding[] k1
  275. = (GrabKeyDialog.KeyBinding[])obj1;
  276. GrabKeyDialog.KeyBinding[] k2
  277. = (GrabKeyDialog.KeyBinding[])obj2;
  278. String label1 = k1[0].label.toLowerCase();
  279. String label2 = k2[0].label.toLowerCase();
  280. if(col == 0)
  281. return MiscUtilities.compareStrings(
  282. label1,label2,true);
  283. else
  284. {
  285. String shortcut1, shortcut2;
  286. if(col == 1)
  287. {
  288. shortcut1 = k1[0].shortcut;
  289. shortcut2 = k2[0].shortcut;
  290. }
  291. else
  292. {
  293. shortcut1 = k1[1].shortcut;
  294. shortcut2 = k2[1].shortcut;
  295. }
  296. if(shortcut1 == null && shortcut2 != null)
  297. return 1;
  298. else if(shortcut2 == null && shortcut1 != null)
  299. return -1;
  300. else if(shortcut1 == null && shortcut2 == null)
  301. return MiscUtilities.compareStrings(label1,label2,true);
  302. else
  303. return MiscUtilities.compareStrings(shortcut1,shortcut2,true);
  304. }
  305. }
  306. }
  307. }
  308. }