PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-1-pre5/macros/Misc/Display_Shortcuts.bsh

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