PageRenderTime 15ms CodeModel.GetById 8ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

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

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