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

# · Java · 364 lines · 285 code · 47 blank · 32 comment · 41 complexity · 24f790fbad47e7952ecb8c252c287be8 MD5 · raw file

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