PageRenderTime 33ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/macros/Misc/Display_Shortcuts.bsh

#
Unknown | 307 lines | 283 code | 24 blank | 0 comment | 0 complexity | 6a5dc3a8d410be796c73e8d828d82eb7 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. * Display_shortcuts.bsh - a BeanShell macro script for the
  3. * jEdit text editor - displays all shortcut key assignments
  4. * Copyright (C) 2003 Russell Inman
  5. * email: RInman@csustan.edu
  6. *
  7. * Code based on Display_Shortcuts.bsh, 1.3
  8. * Copyright (C) 2001 John Gellene
  9. * email: jgellene@nyc.rr.com
  10. * http://community.jedit.org
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version 2
  15. * of the License, or any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  25. *
  26. *
  27. * Display_Shortcuts.bsh, v1.3.1 2003-11-13
  28. *
  29. *
  30. * requires jEdit3.1pre5
  31. *
  32. * Notes on use:
  33. *
  34. * This macro will display a sorted list of all keyboard shortcuts
  35. * in a dialog.
  36. *
  37. * Pressing a letter key will cause the table to scroll to the first row
  38. * with a label beginning with the letter (or the imeediately preceding row if
  39. * no item begins with that letter). The table is read-only; the dialog is
  40. * dismissed by clicking "OK" or pressing Esc or Enter.
  41. *
  42. * General comment:
  43. *
  44. * This macro illustrates the limitations of BeanShell, which cannot create
  45. * true derived subclasses. To get the same features, the alternative to using
  46. * Vectors to hold the data would be to write a highly customized sorting
  47. * routine. Sorting by keyname would require a second custom sorting routine.
  48. * There is also a hack in the code creating the table.
  49. *
  50. * Checked for jEdit 4.2pre6 API
  51. */
  52. import javax.swing.table.*;
  53. /*
  54. * method for creating vectors of row data describing shortcuts.
  55. */
  56. void makeShortcutsVector(Vector p_v)
  57. {
  58. String[] actionNames = jEdit.getActionNames();
  59. for(int i = 0; i < actionNames.length; i++)
  60. {
  61. EditAction action = jEdit.getAction(actionNames[i]);
  62. String name = action.getName();
  63. String label = action.getLabel();
  64. if(label == null)
  65. label = name;
  66. else label = GUIUtilities.prettifyMenuLabel(label);
  67. String shortcut1 = jEdit.getProperty(name + ".shortcut");
  68. if(shortcut1 == null)
  69. shortcut1 = "";
  70. String shortcut2 = jEdit.getProperty(name + ".shortcut2");
  71. if(shortcut2 == null)
  72. shortcut2 = "";
  73. if(shortcut1.length() != 0 || shortcut2.length() != 0)
  74. {
  75. p_v.addElement(makeRow(label,shortcut1,shortcut2));
  76. }
  77. }
  78. }
  79. /*
  80. * helper method to make vector of row data for table
  81. */
  82. Vector makeRow(String name, String shortcut1, String shortcut2)
  83. {
  84. Vector row = new Vector(3);
  85. row.addElement(name);
  86. row.addElement(shortcut1);
  87. row.addElement(shortcut2);
  88. return row;
  89. }
  90. /*
  91. * methods for formatting and writing shortcut data to a text buffer
  92. */
  93. void writeTableToNewBuffer(Vector v)
  94. {
  95. jEdit.newFile(view);
  96. textArea.setSelectedText("jEdit Keyboard Shortcut Table\n\n");
  97. headings = makeRow("Name", "Shortcut - 1", "Shortcut - 2");
  98. writeLine(headings);
  99. textArea.setSelectedText("\n");
  100. for(int i = 0; i < v.size(); ++i)
  101. {
  102. writeLine((Vector)v.elementAt(i));
  103. }
  104. }
  105. void writeLine(Vector row)
  106. {
  107. StringBuffer sb = new StringBuffer(85);
  108. spaceString = " ";
  109. char[] space = spaceString.toCharArray();
  110. displayName = row.elementAt(0);
  111. if(displayName.length() > 38)
  112. displayName = displayName.substring(0, 34) + "...";
  113. sb.append(displayName);
  114. sb.append(space, 0, 40 - (displayName.length()));
  115. shortcut1 = row.elementAt(1);
  116. if(shortcut1 != null)
  117. {
  118. sb.append(shortcut1);
  119. sb.append(space, 0, 20 - (shortcut1.length()));
  120. }
  121. else sb.append(space, 0, 20);
  122. shortcut2 = row.elementAt(2);
  123. if(shortcut2 != null)
  124. {
  125. sb.append(shortcut2);
  126. sb.append(space, 0, 20 - (shortcut2.length()));
  127. }
  128. sb.append('\n');
  129. textArea.setSelectedText(sb.toString());
  130. }
  131. /*
  132. * main routine
  133. */
  134. void showShortcuts(View view)
  135. {
  136. this.view = view;
  137. Vector v = new Vector();
  138. makeShortcutsVector(v);
  139. MiscUtilities.quicksort(v, new MiscUtilities.StringICaseCompare());
  140. table = new JTable(v, makeRow( "Name", "Shortcut-1", "Shortcut-2"));
  141. table.getColumnModel().getColumn(0).setPreferredWidth(200);
  142. table.setRowSelectionAllowed(true);
  143. /* The next line prevents the table from being edited.
  144. * The normal approach in Java would be to subclass the TableModel
  145. * associated with the JTable and define TableModel.isCellEditable()
  146. * to return false. However, BeanShell does not allow conventional
  147. * class creation, and the desired behavior cannot be achieved using
  148. * its scripted object feature.
  149. */
  150. table.setDefaultEditor(Object.class, null);
  151. if(table.getRowCount() != 0)
  152. {
  153. table.setRowSelectionInterval(0, 0);
  154. table.setColumnSelectionInterval(1, 1);
  155. }
  156. tablePane = new JScrollPane(table);
  157. tablePane.setPreferredSize(new Dimension(450, 300));
  158. close = new JButton("Close");
  159. close.addActionListener(this);
  160. write = new JButton("Write to buffer");
  161. write.addActionListener(this);
  162. void actionPerformed(e)
  163. {
  164. if(e.getSource().getText().equals("Close"))
  165. dialog.hide();
  166. else writeTableToNewBuffer(v);
  167. }
  168. buttonPanel = new JPanel(new FlowLayout());
  169. buttonPanel.add(write);
  170. buttonPanel.add(close);
  171. title = "Keyboard shortcut list";
  172. dialog = new JDialog(view, title, false);
  173. dialog.getContentPane().add(tablePane, "Center");
  174. dialog.getContentPane().add(buttonPanel, "South");
  175. dialog.getRootPane().setDefaultButton(close);
  176. table.addKeyListener(this);
  177. void keyPressed(e)
  178. {
  179. if(e.getKeyCode() == KeyEvent.VK_ESCAPE ||
  180. e.getKeyCode() == KeyEvent.VK_ENTER)
  181. {
  182. dialog.hide();
  183. }
  184. else
  185. {
  186. char ch = e.getKeyChar();
  187. if(Character.isLetter(ch))
  188. {
  189. e.consume();
  190. row = findFirstItem(ch);
  191. /* The next few lines set the last visible row
  192. * of the table so that you can look ahead of
  193. * the selected row.
  194. */
  195. visibleRows =
  196. table.getVisibleRect().height / table.getRowHeight();
  197. oldRow = table.getSelectedRow();
  198. table.setRowSelectionInterval(row,row);
  199. if (visibleRows > 5 && row - oldRow > visibleRows - 3)
  200. {
  201. row = Math.min( v.size() - 1, row + 3);
  202. }
  203. table.scrollRectToVisible(table.getCellRect(row,0,true));
  204. }
  205. }
  206. }
  207. /*
  208. * Having these members of KeyListener implemented will speedup execution;
  209. * BeanShell will otherwise throw and handle an exception.
  210. * This idiom is required under BeanShell 1.2
  211. */
  212. void keyReleased(e) {}
  213. void keyTyped(e) {}
  214. /*
  215. * A simple linear search for the table entry that begins with the
  216. * given letter. It returns the first row with an entry beginning with
  217. * the letter, or the immdediately preceding row if there is no match
  218. * on the letter. If PUT_MACROS_AT_END is set to true, they will not be
  219. * searched.
  220. *
  221. */
  222. int data_midpoint = 0;
  223. int findFirstItem(char ch)
  224. {
  225. ch = Character.toUpperCase(ch);
  226. int row = 0;
  227. int fix_err = ch > 'L' ? data_midpoint : 0;
  228. for(int i = fix_err; i < v.size(); ++i)
  229. {
  230. String name = ((Vector)v.elementAt(i)).elementAt(0);
  231. char ch_test = Character.toUpperCase(name.charAt(0));
  232. if( ch_test > ch) break;
  233. else
  234. {
  235. row = i;
  236. if( ch_test == ch) break;
  237. }
  238. }
  239. return row;
  240. }
  241. /* This line caches the row that starts the second half of the
  242. * alphabet to speed up searches. 'M' was chosen as the midpoint
  243. * to speed up searches.
  244. */
  245. data_midpoint = findFirstItem('M');
  246. dialog.pack();
  247. dialog.setLocationRelativeTo(view);
  248. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  249. dialog.show();
  250. }
  251. showShortcuts(view);
  252. /*
  253. Macro index data (in DocBook format)
  254. <listitem>
  255. <para><filename>Display_Shortcuts.bsh</filename></para>
  256. <abstract><para>
  257. Displays a sorted list of the keyboard shortcuts currently in effect.
  258. </para></abstract>
  259. <para>
  260. The macro provides a combined read-only view of command, macro
  261. and plugin shortcuts. Pressing a letter key will
  262. scroll the table to the first entry beginning with that letter.
  263. A further option is provided to write the shortcut assignments in a
  264. text buffer for printing as a reference. Notes in the source code
  265. listing point out some display options that are configured by
  266. modifying global variables.
  267. </para>
  268. </listitem>
  269. */
  270. /*
  271. Changes from v1.3
  272. - Since makeShortcutsVector() had no return statement, the return type was
  273. changed to void.
  274. - Removed deprecated jEdit.getEditActions() call from makeShortcutsVector
  275. - The original makeShortcutsVector method referenced a variable v,
  276. presumably from showShortcuts(), which was outside of its scope.
  277. I don't know if some new scoping rule in BeanShell broke this or not.
  278. At any rate, the method has been updated so that it takes a
  279. Vector as a parameter.
  280. - Added an explicit declaration for Vector v to showShortcuts in order to
  281. pass it to makeShortcutsVector
  282. */
  283. // end Display_Shortcuts.bsh