PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
Java | 357 lines | 278 code | 50 blank | 29 comment | 39 complexity | d48bff4106b8ccdc93c4664abd0ef449 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 3828 2001-10-07 10:42:45Z 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.getActions()));
  83. }
  84. }
  85. currentModel = (ShortcutsModel)models.elementAt(0);
  86. }
  87. private ShortcutsModel createModel(String modelLabel, EditAction[] actions)
  88. {
  89. Vector bindings = new Vector(actions.length);
  90. for(int i = 0; i < actions.length; i++)
  91. {
  92. EditAction action = actions[i];
  93. String name = action.getName();
  94. String label = action.getLabel();
  95. // Skip certain actions this way (ENTER, TAB)
  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. ShortcutsOptionPane.this,
  156. currentModel.getBindingAt(row,col-1),
  157. allBindings);
  158. if(gkd.isOK())
  159. currentModel.setValueAt(
  160. gkd.getShortcut(),row,col);
  161. }
  162. }
  163. }
  164. class ActionHandler implements ActionListener
  165. {
  166. public void actionPerformed(ActionEvent evt)
  167. {
  168. ShortcutsModel newModel
  169. = (ShortcutsModel)selectModel.getSelectedItem();
  170. if(currentModel != newModel)
  171. {
  172. currentModel = newModel;
  173. keyTable.setModel(currentModel);
  174. }
  175. }
  176. }
  177. class ShortcutsModel extends AbstractTableModel
  178. {
  179. private Vector bindings;
  180. private String name;
  181. ShortcutsModel(String name, Vector bindings)
  182. {
  183. this.name = name;
  184. this.bindings = bindings;
  185. sort(0);
  186. }
  187. public void sort(int col)
  188. {
  189. MiscUtilities.quicksort(bindings,new KeyCompare(col));
  190. fireTableDataChanged();
  191. }
  192. public int getColumnCount()
  193. {
  194. return 3;
  195. }
  196. public int getRowCount()
  197. {
  198. return bindings.size();
  199. }
  200. public Object getValueAt(int row, int col)
  201. {
  202. switch(col)
  203. {
  204. case 0:
  205. return getBindingAt(row,0).label;
  206. case 1:
  207. return getBindingAt(row,0).shortcut;
  208. case 2:
  209. return getBindingAt(row,1).shortcut;
  210. default:
  211. return null;
  212. }
  213. }
  214. public void setValueAt(Object value, int row, int col)
  215. {
  216. if(col == 0)
  217. return;
  218. getBindingAt(row,col-1).shortcut = (String)value;
  219. // redraw the whole table because a second shortcut
  220. // might have changed, too
  221. fireTableDataChanged();
  222. }
  223. public String getColumnName(int index)
  224. {
  225. switch(index)
  226. {
  227. case 0:
  228. return jEdit.getProperty("options.shortcuts.name");
  229. case 1:
  230. return jEdit.getProperty("options.shortcuts.shortcut1");
  231. case 2:
  232. return jEdit.getProperty("options.shortcuts.shortcut2");
  233. default:
  234. return null;
  235. }
  236. }
  237. public void save()
  238. {
  239. Enumeration enum = bindings.elements();
  240. while(enum.hasMoreElements())
  241. {
  242. GrabKeyDialog.KeyBinding binding[]
  243. = (GrabKeyDialog.KeyBinding[])
  244. enum.nextElement();
  245. jEdit.setProperty(
  246. binding[0].name + ".shortcut",
  247. binding[0].shortcut);
  248. jEdit.setProperty(
  249. binding[1].name + ".shortcut2",
  250. binding[1].shortcut);
  251. }
  252. }
  253. public GrabKeyDialog.KeyBinding getBindingAt(int row, int nr)
  254. {
  255. GrabKeyDialog.KeyBinding binding[]
  256. = (GrabKeyDialog.KeyBinding[])
  257. bindings.elementAt(row);
  258. return binding[nr];
  259. }
  260. public String toString()
  261. {
  262. return name;
  263. }
  264. class KeyCompare implements MiscUtilities.Compare
  265. {
  266. int col;
  267. KeyCompare(int col)
  268. {
  269. this.col = col;
  270. }
  271. public int compare(Object obj1, Object obj2)
  272. {
  273. GrabKeyDialog.KeyBinding[] k1
  274. = (GrabKeyDialog.KeyBinding[])obj1;
  275. GrabKeyDialog.KeyBinding[] k2
  276. = (GrabKeyDialog.KeyBinding[])obj2;
  277. String label1 = k1[0].label.toLowerCase();
  278. String label2 = k2[0].label.toLowerCase();
  279. if(col == 0)
  280. return MiscUtilities.compareStrings(
  281. label1,label2,true);
  282. else
  283. {
  284. String shortcut1, shortcut2;
  285. if(col == 1)
  286. {
  287. shortcut1 = k1[0].shortcut;
  288. shortcut2 = k2[0].shortcut;
  289. }
  290. else
  291. {
  292. shortcut1 = k1[1].shortcut;
  293. shortcut2 = k2[1].shortcut;
  294. }
  295. if(shortcut1 == null && shortcut2 != null)
  296. return 1;
  297. else if(shortcut2 == null && shortcut1 != null)
  298. return -1;
  299. else if(shortcut1 == null && shortcut2 == null)
  300. return MiscUtilities.compareStrings(label1,label2,true);
  301. else
  302. return MiscUtilities.compareStrings(shortcut1,shortcut2,true);
  303. }
  304. }
  305. }
  306. }
  307. }