PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-0-pre3/macros/Misc/Display_Shortcuts.bsh

#
Unknown | 286 lines | 264 code | 22 blank | 0 comment | 0 complexity | 2fa4719d9ccfa51d00561fd3c7626944 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 3873 2001-11-06 17:57:35Z jgellene $
  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. EditAction[] actions = jEdit.getActions();
  55. for(int i = 0; i < actions.length; i++)
  56. {
  57. EditAction action = actions[i];
  58. String name = action.getName();
  59. String label = action.getLabel();
  60. if(label == null)
  61. label = name;
  62. else label = GUIUtilities.prettifyMenuLabel(label);
  63. String shortcut1 = jEdit.getProperty(name + ".shortcut");
  64. if(shortcut1 == null)
  65. shortcut1 = "";
  66. String shortcut2 = jEdit.getProperty(name + ".shortcut2");
  67. if(shortcut2 == null)
  68. shortcut2 = "";
  69. if(shortcut1.length() != 0 || shortcut2.length() != 0)
  70. {
  71. v.addElement(makeRow(label,shortcut1,shortcut2));
  72. }
  73. }
  74. }
  75. /*
  76. * helper method to make vector of row data for table
  77. */
  78. Vector makeRow(String name, String shortcut1, String shortcut2)
  79. {
  80. Vector row = new Vector(3);
  81. row.addElement(name);
  82. row.addElement(shortcut1);
  83. row.addElement(shortcut2);
  84. return row;
  85. }
  86. /*
  87. * methods for formatting and writing shortcut data to a text buffer
  88. */
  89. void writeTableToNewBuffer(Vector v)
  90. {
  91. jEdit.newFile(view);
  92. textArea.setSelectedText("jEdit Keyboard Shortcut Table\n\n");
  93. headings = makeRow("Name", "Shortcut - 1", "Shortcut - 2");
  94. writeLine(headings);
  95. textArea.setSelectedText("\n");
  96. for(int i = 0; i < v.size(); ++i)
  97. {
  98. writeLine((Vector)v.elementAt(i));
  99. }
  100. }
  101. void writeLine(Vector row)
  102. {
  103. StringBuffer sb = new StringBuffer(85);
  104. spaceString = " ";
  105. char[] space = spaceString.toCharArray();
  106. displayName = row.elementAt(0);
  107. if(displayName.length() > 38)
  108. displayName = displayName.substring(0, 34) + "...";
  109. sb.append(displayName);
  110. sb.append(space, 0, 40 - (displayName.length()));
  111. shortcut1 = row.elementAt(1);
  112. if(shortcut1 != null)
  113. {
  114. sb.append(shortcut1);
  115. sb.append(space, 0, 20 - (shortcut1.length()));
  116. }
  117. else sb.append(space, 0, 20);
  118. shortcut2 = row.elementAt(2);
  119. if(shortcut2 != null)
  120. {
  121. sb.append(shortcut2);
  122. sb.append(space, 0, 20 - (shortcut2.length()));
  123. }
  124. sb.append('\n');
  125. textArea.setSelectedText(sb.toString());
  126. }
  127. /*
  128. * main routine
  129. *
  130. */
  131. void showShortcuts()
  132. {
  133. v = makeShortcutsVector();
  134. MiscUtilities.quicksort(v, new MiscUtilities.StringICaseCompare());
  135. table = new JTable(v, makeRow( "Name", "Shortcut-1", "Shortcut-2"));
  136. table.getColumnModel().getColumn(0).setPreferredWidth(200);
  137. table.setRowSelectionAllowed(true);
  138. /* The next line prevents the table from being edited.
  139. * The normal approach in Java would be to subclass the TableModel
  140. * associated with the JTable and define TableModel.isCellEditable()
  141. * to return false. However, BeanShell does not allow conventional
  142. * class creation, and the desired behavior cannot be achieved using
  143. * its scripted object feature.
  144. */
  145. table.setDefaultEditor(Object.class, null);
  146. if(table.getRowCount() != 0)
  147. {
  148. table.setRowSelectionInterval(0, 0);
  149. table.setColumnSelectionInterval(1, 1);
  150. }
  151. tablePane = new JScrollPane(table);
  152. tablePane.setPreferredSize(new Dimension(450, 300));
  153. close = new JButton("Close");
  154. close.addActionListener(this);
  155. write = new JButton("Write to buffer");
  156. write.addActionListener(this);
  157. void actionPerformed(e)
  158. {
  159. if(e.getSource().getText().equals("Close"))
  160. dialog.hide();
  161. else writeTableToNewBuffer(v);
  162. }
  163. buttonPanel = new JPanel(new FlowLayout());
  164. buttonPanel.add(write);
  165. buttonPanel.add(close);
  166. title = "Keyboard shortcut list";
  167. dialog = new JDialog(view, title, false);
  168. dialog.getContentPane().add(tablePane, "Center");
  169. dialog.getContentPane().add(buttonPanel, "South");
  170. dialog.getRootPane().setDefaultButton(close);
  171. table.addKeyListener(this);
  172. void keyPressed(e)
  173. {
  174. if(e.getKeyCode() == KeyEvent.VK_ESCAPE ||
  175. e.getKeyCode() == KeyEvent.VK_ENTER)
  176. {
  177. dialog.hide();
  178. }
  179. else
  180. {
  181. char ch = e.getKeyChar();
  182. if(Character.isLetter(ch))
  183. {
  184. e.consume();
  185. row = findFirstItem(ch);
  186. /* The next few lines set the last visible row
  187. * of the table so that you can look ahead of
  188. * the selected row.
  189. */
  190. visibleRows =
  191. table.getVisibleRect().height / table.getRowHeight();
  192. oldRow = table.getSelectedRow();
  193. table.setRowSelectionInterval(row,row);
  194. if (visibleRows > 5 && row - oldRow > visibleRows - 3)
  195. {
  196. row = Math.min( v.size() - 1, row + 3);
  197. }
  198. table.scrollRectToVisible(table.getCellRect(row,0,true));
  199. }
  200. }
  201. }
  202. /*
  203. * Having these members of KeyListener implemented will speedup execution;
  204. * BeanShell will otherwise throw and handle an exception.
  205. * This idiom is required under BeanShell 1.2
  206. */
  207. void keyReleased(e) {}
  208. void keyTyped(e) {}
  209. /*
  210. * A simple linear search for the table entry that begins with the
  211. * given letter. It returns the first row with an entry beginning with
  212. * the letter, or the immdediately preceding row if there is no match
  213. * on the letter. If PUT_MACROS_AT_END is set to true, they will not be
  214. * searched.
  215. *
  216. */
  217. int data_midpoint = 0;
  218. int findFirstItem(char ch)
  219. {
  220. ch = Character.toUpperCase(ch);
  221. int row = 0;
  222. for(int i = (ch > 'L' ? data_midpoint : 0); i < v.size(); ++i)
  223. {
  224. String name = ((Vector)v.elementAt(i)).elementAt(0);
  225. char ch_test = Character.toUpperCase(name.charAt(0));
  226. if( ch_test > ch) break;
  227. else
  228. {
  229. row = i;
  230. if( ch_test == ch) break;
  231. }
  232. }
  233. return row;
  234. }
  235. /* This line caches the row that starts the second half of the
  236. * alphabet to speed up searches. 'M' was chosen as the midpoint
  237. * to speed up searches.
  238. */
  239. data_midpoint = findFirstItem('M');
  240. dialog.pack();
  241. dialog.setLocationRelativeTo(view);
  242. dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
  243. dialog.show();
  244. }
  245. showShortcuts();
  246. /*
  247. Macro index data (in DocBook format)
  248. <listitem>
  249. <para><filename>Display_Shortcuts.bsh</filename></para>
  250. <abstract><para>
  251. Displays a sorted list of the keyboard shortcuts currently in effect.
  252. </para></abstract>
  253. <para>
  254. The macro provides a combined read-only view of command, macro
  255. and plugin shortcuts. Pressing a letter key will
  256. scroll the table to the first entry beginning with that letter.
  257. A further option is provided to write the shortcut assignments in a
  258. text buffer for printing as a reference. Notes in the source code
  259. listing point out some display options that are configured by
  260. modifying global variables.
  261. </para>
  262. </listitem>
  263. */
  264. // end Display_Shortcuts.bsh