PageRenderTime 295ms CodeModel.GetById 271ms app.highlight 3ms RepoModel.GetById 17ms app.codeStats 1ms

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

#
Unknown | 290 lines | 266 code | 24 blank | 0 comment | 0 complexity | f8433b6d19918d3c354eb690963ed1ed 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) 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
 49import javax.swing.table.*;
 50
 51/*
 52 * method for creating vectors of row data describing shortcuts.
 53 */
 54
 55Vector makeShortcutsVector()
 56{
 57	v = new Vector();
 58
 59	EditAction[] actions = jEdit.getActions();
 60	for(int i = 0; i < actions.length; i++)
 61	{
 62		EditAction action = actions[i];
 63		String name = action.getName();
 64		String label = action.getLabel();
 65		if(label == null)
 66			label = name;
 67		else label = GUIUtilities.prettifyMenuLabel(label);
 68		String shortcut1 = jEdit.getProperty(name + ".shortcut");
 69		if(shortcut1 == null)
 70			shortcut1 = "";
 71		String shortcut2 = jEdit.getProperty(name + ".shortcut2");
 72		if(shortcut2 == null)
 73			shortcut2 = "";
 74		if(shortcut1.length() != 0 || shortcut2.length() != 0)
 75		{
 76			v.addElement(makeRow(label,shortcut1,shortcut2));
 77		}
 78	}
 79
 80	return v;
 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 *
142 */
143void showShortcuts()
144{
145	v = makeShortcutsVector();
146	MiscUtilities.quicksort(v, new MiscUtilities.StringICaseCompare());
147
148	table = new JTable(v, makeRow( "Name", "Shortcut-1", "Shortcut-2"));
149	table.getColumnModel().getColumn(0).setPreferredWidth(200);
150	table.setRowSelectionAllowed(true);
151	/* 	The next line prevents the table from being edited.
152	 * 	The normal approach in Java would be to subclass the TableModel
153	 * 	associated with the JTable and define TableModel.isCellEditable()
154	 * 	to return false.  However, BeanShell does not allow conventional
155	 *  class creation, and the desired behavior cannot be achieved using
156	 *  its scripted object feature.
157	 */
158	table.setDefaultEditor(Object.class, null);
159	if(table.getRowCount() != 0)
160	{
161		table.setRowSelectionInterval(0, 0);
162		table.setColumnSelectionInterval(1, 1);
163	}
164
165	tablePane = new JScrollPane(table);
166	tablePane.setPreferredSize(new Dimension(450, 300));
167
168	close = new JButton("Close");
169	close.addActionListener(this);
170	write = new JButton("Write to buffer");
171	write.addActionListener(this);
172	void actionPerformed(e)
173	{
174		if(e.getSource().getText().equals("Close"))
175			dialog.hide();
176		else writeTableToNewBuffer(v);
177	}
178	buttonPanel = new JPanel(new FlowLayout());
179	buttonPanel.add(write);
180	buttonPanel.add(close);
181
182	title = "Keyboard shortcut list";
183	dialog = new JDialog(view, title, false);
184	dialog.getContentPane().add(tablePane, "Center");
185	dialog.getContentPane().add(buttonPanel, "South");
186	dialog.getRootPane().setDefaultButton(close);
187
188
189	table.addKeyListener(this);
190	void keyPressed(e)
191	{
192		if(e.getKeyCode() == KeyEvent.VK_ESCAPE ||
193			e.getKeyCode() == KeyEvent.VK_ENTER)
194		{
195			dialog.hide();
196		}
197		else
198		{
199			char ch = e.getKeyChar();
200			if(Character.isLetter(ch))
201			{
202				e.consume();
203				row = findFirstItem(ch);
204				/* The next few lines set the last visible row
205				 * of the table so that you can look ahead of
206				 * the selected row.
207				 */
208				visibleRows =
209					table.getVisibleRect().height / table.getRowHeight();
210				oldRow = table.getSelectedRow();
211				table.setRowSelectionInterval(row,row);
212				if (visibleRows > 5 && row - oldRow > visibleRows - 3)
213				{
214					row = Math.min( v.size() - 1, row + 3);
215				}
216				table.scrollRectToVisible(table.getCellRect(row,0,true));
217			}
218		}
219	}
220	/*
221	 * Having these members of KeyListener implemented will speedup execution;
222	 * BeanShell will otherwise throw and handle an exception.
223	 * This idiom is required under BeanShell 1.2
224	 */
225	void keyReleased(e) {}
226	void keyTyped(e) {}
227
228
229	/*
230	 * A simple linear search for the table entry that begins with the
231	 * given letter.  It returns the first row with an entry beginning with
232	 * the letter, or the immdediately preceding row if there is no match
233	 * on the letter.  If PUT_MACROS_AT_END is set to true, they will not be
234	 * searched.
235	 *
236	 */
237	int data_midpoint = 0;
238	int findFirstItem(char ch)
239	{
240		ch = Character.toUpperCase(ch);
241		int row = 0;
242		for(int i = (ch > 'L' ? data_midpoint : 0); i < v.size(); ++i)
243		{
244			String name = ((Vector)v.elementAt(i)).elementAt(0);
245			char ch_test = Character.toUpperCase(name.charAt(0));
246			if( ch_test > ch) break;
247			else
248			{
249				row = i;
250				if( ch_test == ch) break;
251			}
252		}
253		return row;
254	}
255	/* This line caches the row that starts the second half of the
256	 * alphabet to speed up searches.  'M' was chosen as the midpoint
257	 * to speed up searches.
258	 */
259	data_midpoint = findFirstItem('M');
260	dialog.pack();
261	dialog.setLocationRelativeTo(view);
262	dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
263	dialog.show();
264}
265
266showShortcuts();
267
268/*
269	Macro index data (in DocBook format)
270
271<listitem>
272    <para><filename>Display_Shortcuts.bsh</filename></para>
273    <abstract><para>
274        Displays a sorted list of the keyboard shortcuts currently in effect.
275    </para></abstract>
276    <para>
277        The macro provides a combined read-only view of command, macro
278        and plugin shortcuts. Pressing a letter key will
279        scroll the table to the first entry beginning with that letter.
280        A further option is provided to write the shortcut assignments in a
281        text buffer for printing as a reference. Notes in the source code
282        listing point out some display options that are configured by
283        modifying global variables.
284    </para>
285</listitem>
286
287*/
288
289// end Display_Shortcuts.bsh
290