PageRenderTime 301ms CodeModel.GetById 283ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/jedit/gui/GrabKeyDialog.java

#
Java | 552 lines | 362 code | 70 blank | 120 comment | 86 complexity | 9f5a9e4458326c53dd7d268889730400 MD5 | raw file
  1/*
  2 * GrabKeyDialog.java - Grabs keys from the keyboard
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright (C) 2001, 2002 Slava Pestov
  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
 23package org.gjt.sp.jedit.gui;
 24
 25//{{{ Imports
 26import javax.swing.border.*;
 27import javax.swing.*;
 28import java.awt.event.*;
 29import java.awt.*;
 30import java.lang.reflect.Field;
 31import java.util.*;
 32import org.gjt.sp.jedit.*;
 33import org.gjt.sp.util.Log;
 34//}}}
 35
 36/**
 37 * A dialog for getting shortcut keys.
 38 */
 39public class GrabKeyDialog extends JDialog
 40{
 41	//{{{ toString() method
 42	public static String toString(KeyEvent evt)
 43	{
 44		String id;
 45		switch(evt.getID())
 46		{
 47		case KeyEvent.KEY_PRESSED:
 48			id = "KEY_PRESSED";
 49			break;
 50		case KeyEvent.KEY_RELEASED:
 51			id = "KEY_RELEASED";
 52			break;
 53		case KeyEvent.KEY_TYPED:
 54			id = "KEY_TYPED";
 55			break;
 56		default:
 57			id = "unknown type";
 58			break;
 59		}
 60
 61		return id + ",keyCode=0x"
 62			+ Integer.toString(evt.getKeyCode(),16)
 63			+ ",keyChar=0x"
 64			+ Integer.toString(evt.getKeyChar(),16)
 65			+ ",modifiers=0x"
 66			+ Integer.toString(evt.getModifiers(),16);
 67	} //}}}
 68
 69	//{{{ GrabKeyDialog constructor
 70	/**
 71	 * Create and show a new modal dialog.
 72	 *
 73	 * @param  parent  center dialog on this component.
 74	 * @param  binding  the action/macro that should get a binding.
 75	 * @param  allBindings  all other key bindings.
 76	 * @param  debugBuffer  debug info will be dumped to this buffer
 77	 * (may be null)
 78	 * @since jEdit 4.1pre7
 79	 */
 80	public GrabKeyDialog(Dialog parent, KeyBinding binding,
 81		Vector allBindings, Buffer debugBuffer)
 82	{
 83		super(parent,jEdit.getProperty("grab-key.title"),true);
 84
 85		init(binding,allBindings,debugBuffer);
 86	} //}}}
 87
 88	//{{{ GrabKeyDialog constructor
 89	/**
 90	 * Create and show a new modal dialog.
 91	 *
 92	 * @param  parent  center dialog on this component.
 93	 * @param  binding  the action/macro that should get a binding.
 94	 * @param  allBindings  all other key bindings.
 95	 * @param  debugBuffer  debug info will be dumped to this buffer
 96	 * (may be null)
 97	 * @since jEdit 4.1pre7
 98	 */
 99	public GrabKeyDialog(Frame parent, KeyBinding binding,
100		Vector allBindings, Buffer debugBuffer)
101	{
102		super(parent,jEdit.getProperty("grab-key.title"),true);
103
104		init(binding,allBindings,debugBuffer);
105	} //}}}
106
107	//{{{ getShortcut() method
108	/**
109	 * Returns the shortcut, or null if the current shortcut should be
110	 * removed or the dialog either has been cancelled. Use isOK()
111	 * to determine if the latter is true.
112	 */
113	public String getShortcut()
114	{
115		if(isOK)
116			return shortcut.getText();
117		else
118			return null;
119	} //}}}
120
121	//{{{ isOK() method
122	/**
123	 * Returns true, if the dialog has not been cancelled.
124	 * @since jEdit 3.2pre9
125	 */
126	public boolean isOK()
127	{
128		return isOK;
129	} //}}}
130
131	//{{{ isManagingFocus() method
132	/**
133	 * Returns if this component can be traversed by pressing the
134	 * Tab key. This returns false.
135	 */
136	public boolean isManagingFocus()
137	{
138		return false;
139	} //}}}
140
141	//{{{ getFocusTraversalKeysEnabled() method
142	/**
143	 * Makes the tab key work in Java 1.4.
144	 * @since jEdit 3.2pre4
145	 */
146	public boolean getFocusTraversalKeysEnabled()
147	{
148		return false;
149	} //}}}
150
151	//{{{ processKeyEvent() method
152	protected void processKeyEvent(KeyEvent evt)
153	{
154		shortcut.processKeyEvent(evt);
155	} //}}}
156
157	//{{{ Private members
158
159	//{{{ Instance variables
160	private InputPane shortcut; // this is a bad hack
161	private JLabel assignedTo;
162	private JButton ok;
163	private JButton remove;
164	private JButton cancel;
165	private JButton clear;
166	private boolean isOK;
167	private KeyBinding binding;
168	private Vector allBindings;
169	private Buffer debugBuffer;
170	//}}}
171
172	//{{{ init() method
173	private void init(KeyBinding binding, Vector allBindings, Buffer debugBuffer)
174	{
175		this.binding = binding;
176		this.allBindings = allBindings;
177		this.debugBuffer = debugBuffer;
178
179		enableEvents(AWTEvent.KEY_EVENT_MASK);
180
181		// create a panel with a BoxLayout. Can't use Box here
182		// because Box doesn't have setBorder().
183		JPanel content = new JPanel(new GridLayout(0,1,0,6))
184		{
185			/**
186			 * Returns if this component can be traversed by pressing the
187			 * Tab key. This returns false.
188			 */
189			public boolean isManagingFocus()
190			{
191				return false;
192			}
193
194			/**
195			 * Makes the tab key work in Java 1.4.
196			 * @since jEdit 3.2pre4
197			 */
198			public boolean getFocusTraversalKeysEnabled()
199			{
200				return false;
201			}
202		};
203		content.setBorder(new EmptyBorder(12,12,12,12));
204		setContentPane(content);
205
206		JLabel label = new JLabel(
207			debugBuffer == null ? jEdit.getProperty(
208			"grab-key.caption",new String[] { binding.label })
209			: jEdit.getProperty("grab-key.keyboard-test"));
210
211		Box input = Box.createHorizontalBox();
212
213		shortcut = new InputPane();
214		input.add(shortcut);
215		input.add(Box.createHorizontalStrut(12));
216
217		clear = new JButton(jEdit.getProperty("grab-key.clear"));
218		clear.addActionListener(new ActionHandler());
219		input.add(clear);
220
221		assignedTo = new JLabel();
222		if(debugBuffer == null)
223			updateAssignedTo(null);
224
225		Box buttons = Box.createHorizontalBox();
226		buttons.add(Box.createGlue());
227
228		if(debugBuffer == null)
229		{
230			ok = new JButton(jEdit.getProperty("common.ok"));
231			ok.addActionListener(new ActionHandler());
232			buttons.add(ok);
233			buttons.add(Box.createHorizontalStrut(12));
234
235			if(binding.isAssigned()) {
236				// show "remove" button
237				remove = new JButton(jEdit.getProperty("grab-key.remove"));
238				remove.addActionListener(new ActionHandler());
239				buttons.add(remove);
240				buttons.add(Box.createHorizontalStrut(12));
241			}
242		}
243
244		cancel = new JButton(jEdit.getProperty("common.cancel"));
245		cancel.addActionListener(new ActionHandler());
246		buttons.add(cancel);
247		buttons.add(Box.createGlue());
248
249		content.add(label);
250		content.add(input);
251		if(debugBuffer == null)
252			content.add(assignedTo);
253		content.add(buttons);
254
255		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
256
257		pack();
258		setLocationRelativeTo(getParent());
259		setResizable(false);
260		show();
261	} //}}}
262
263	//{{{ getSymbolicName() method
264	private String getSymbolicName(int keyCode)
265	{
266		if(keyCode == KeyEvent.VK_UNDEFINED)
267			return null;
268		/* else if(keyCode == KeyEvent.VK_OPEN_BRACKET)
269			return "[";
270		else if(keyCode == KeyEvent.VK_CLOSE_BRACKET)
271			return "]"; */
272
273		if(keyCode >= KeyEvent.VK_A && keyCode <= KeyEvent.VK_Z)
274			return String.valueOf(Character.toLowerCase((char)keyCode));
275
276		try
277		{
278			Field[] fields = KeyEvent.class.getFields();
279			for(int i = 0; i < fields.length; i++)
280			{
281				Field field = fields[i];
282				String name = field.getName();
283				if(name.startsWith("VK_")
284					&& field.getInt(null) == keyCode)
285				{
286					return name.substring(3);
287				}
288			}
289		}
290		catch(Exception e)
291		{
292			Log.log(Log.ERROR,this,e);
293		}
294
295		return null;
296	} //}}}
297
298	//{{{ updateAssignedTo() method
299	private void updateAssignedTo(String shortcut)
300	{
301		String text = jEdit.getProperty("grab-key.assigned-to.none");
302		KeyBinding kb = getKeyBinding(shortcut);
303
304		if(kb != null)
305			if(kb.isPrefix)
306				text = jEdit.getProperty(
307					"grab-key.assigned-to.prefix",
308					new String[] { shortcut });
309			else
310				text = kb.label;
311
312		if(ok != null)
313			ok.setEnabled(kb == null || !kb.isPrefix);
314
315		assignedTo.setText(
316			jEdit.getProperty("grab-key.assigned-to",
317				new String[] { text }));
318	} //}}}
319
320	//{{{ getKeyBinding() method
321	private KeyBinding getKeyBinding(String shortcut)
322	{
323		if(shortcut == null || shortcut.length() == 0)
324			return null;
325
326		String spacedShortcut = shortcut + " ";
327		Enumeration e = allBindings.elements();
328
329		while(e.hasMoreElements())
330		{
331			KeyBinding kb = (KeyBinding)e.nextElement();
332
333			if(!kb.isAssigned())
334				continue;
335
336			String spacedKbShortcut = kb.shortcut + " ";
337
338			// eg, trying to bind C+n C+p if C+n already bound
339			if(spacedShortcut.startsWith(spacedKbShortcut))
340				return kb;
341
342			// eg, trying to bind C+e if C+e is a prefix
343			if(spacedKbShortcut.startsWith(spacedShortcut))
344			{
345				// create a temporary (synthetic) prefix
346				// KeyBinding, that won't be saved
347				return new KeyBinding(kb.name,kb.label,
348					shortcut,true);
349			}
350		}
351
352		return null;
353	} //}}}
354
355	//}}}
356
357	//{{{ KeyBinding class
358	/**
359	 * A jEdit action or macro with its two possible shortcuts.
360	 * @since jEdit 3.2pre8
361	 */
362	public static class KeyBinding
363	{
364		public KeyBinding(String name, String label,
365			String shortcut, boolean isPrefix)
366		{
367			this.name = name;
368			this.label = label;
369			this.shortcut = shortcut;
370			this.isPrefix = isPrefix;
371		}
372
373		public String name;
374		public String label;
375		public String shortcut;
376		public boolean isPrefix;
377
378		public boolean isAssigned()
379		{
380			return shortcut != null && shortcut.length() > 0;
381		}
382	} //}}}
383
384	//{{{ InputPane class
385	class InputPane extends JTextField
386	{
387		//{{{ getFocusTraversalKeysEnabled() method
388		/**
389		 * Makes the tab key work in Java 1.4.
390		 * @since jEdit 3.2pre4
391		 */
392		public boolean getFocusTraversalKeysEnabled()
393		{
394			return false;
395		} //}}}
396
397		//{{{ processKeyEvent() method
398		protected void processKeyEvent(KeyEvent _evt)
399		{
400			KeyEvent evt = KeyEventWorkaround.processKeyEvent(_evt);
401			if(debugBuffer != null)
402			{
403				debugBuffer.insert(debugBuffer.getLength(),
404					"Event " + GrabKeyDialog.toString(_evt)
405					+ (evt == null ? " filtered\n"
406					: " passed\n"));
407			}
408
409			if(evt == null)
410				return;
411
412			evt.consume();
413
414			KeyEventTranslator.Key key = KeyEventTranslator
415				.translateKeyEvent(evt);
416			if(key == null)
417				return;
418
419			if(debugBuffer != null)
420			{
421				debugBuffer.insert(debugBuffer.getLength(),
422					"==> Translated to " + key + "\n");
423			}
424
425			StringBuffer keyString = new StringBuffer(getText());
426
427			if(getDocument().getLength() != 0)
428				keyString.append(' ');
429
430			if(key.modifiers != null)
431				keyString.append(key.modifiers).append('+');
432
433			if(key.input == ' ')
434				keyString.append("SPACE");
435			else if(key.input != '\0')
436				keyString.append(key.input);
437			else
438			{
439				String symbolicName = getSymbolicName(key.key);
440
441				if(symbolicName == null)
442					return;
443
444				keyString.append(symbolicName);
445			}
446
447			setText(keyString.toString());
448			if(debugBuffer == null)
449				updateAssignedTo(keyString.toString());
450		} //}}}
451	} //}}}
452
453	//{{{ ActionHandler class
454	class ActionHandler implements ActionListener
455	{
456		//{{{ actionPerformed() method
457		public void actionPerformed(ActionEvent evt)
458		{
459			if(evt.getSource() == ok)
460			{
461				if(canClose())
462					dispose();
463			}
464			else if(evt.getSource() == remove)
465			{
466				shortcut.setText(null);
467				isOK = true;
468				dispose();
469			}
470			else if(evt.getSource() == cancel)
471				dispose();
472			else if(evt.getSource() == clear)
473			{
474				shortcut.setText(null);
475				if(debugBuffer == null)
476					updateAssignedTo(null);
477				shortcut.requestFocus();
478			}
479		} //}}}
480
481		//{{{ canClose() method
482		private boolean canClose()
483		{
484			String shortcutString = shortcut.getText();
485			if(shortcutString.length() == 0
486				&& binding.isAssigned())
487			{
488				// ask whether to remove the old shortcut
489				int answer = GUIUtilities.confirm(
490					GrabKeyDialog.this,
491					"grab-key.remove-ask",
492					null,
493					JOptionPane.YES_NO_OPTION,
494					JOptionPane.QUESTION_MESSAGE);
495				if(answer == JOptionPane.YES_OPTION)
496				{
497					shortcut.setText(null);
498					isOK = true;
499				}
500				else
501					return false;
502			}
503
504			// check whether this shortcut already exists
505			KeyBinding other = getKeyBinding(shortcutString);
506			if(other == null || other == binding)
507			{
508				isOK = true;
509				return true;
510			}
511
512			// check whether the other shortcut is the alt. shortcut
513			if(other.name == binding.name)
514			{
515				// we don't need two identical shortcuts
516				GUIUtilities.error(GrabKeyDialog.this,
517					"grab-key.duplicate-alt-shortcut",
518					null);
519				return false;
520			}
521
522			// check whether shortcut is a prefix to others
523			if(other.isPrefix)
524			{
525				// can't override prefix shortcuts
526				GUIUtilities.error(GrabKeyDialog.this,
527					"grab-key.prefix-shortcut",
528					null);
529				return false;
530			}
531
532			// ask whether to override that other shortcut
533			int answer = GUIUtilities.confirm(GrabKeyDialog.this,
534				"grab-key.duplicate-shortcut",
535				new Object[] { other.label },
536				JOptionPane.YES_NO_OPTION,
537				JOptionPane.QUESTION_MESSAGE);
538			if(answer == JOptionPane.YES_OPTION)
539			{
540				if(other.shortcut != null
541					&& shortcutString.startsWith(other.shortcut))
542				{
543					other.shortcut = null;
544				}
545				isOK = true;
546				return true;
547			}
548			else
549				return false;
550		} //}}}
551	} //}}}
552}