/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/GrabKeyDialog.java
Java | 540 lines | 361 code | 69 blank | 110 comment | 89 complexity | 470ce54bada5a5d07c227e7aef61f4b1 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
- /*
- * GrabKeyDialog.java - Grabs keys from the keyboard
- * :tabSize=8:indentSize=8:noTabs=false:
- * :folding=explicit:collapseFolds=1:
- *
- * Copyright (C) 2001, 2002 Slava Pestov
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
- package org.gjt.sp.jedit.gui;
- //{{{ Imports
- import javax.swing.border.*;
- import javax.swing.*;
- import java.awt.event.*;
- import java.awt.*;
- import java.lang.reflect.Field;
- import java.util.List;
- import org.gjt.sp.jedit.*;
- import org.gjt.sp.jedit.input.AbstractInputHandler;
- import org.gjt.sp.util.Log;
- //}}}
- /**
- * A dialog for getting shortcut keys.
- */
- public class GrabKeyDialog extends JDialog
- {
- //{{{ GrabKeyDialog constructor
- /**
- * Create and show a new modal dialog.
- *
- * @param parent center dialog on this component.
- * @param binding the action/macro that should get a binding.
- * @param allBindings all other key bindings.
- * @param debugBuffer debug info will be dumped to this buffer
- * (may be null)
- * @since jEdit 4.1pre7
- */
- public GrabKeyDialog(Dialog parent, KeyBinding binding,
- List<KeyBinding> allBindings, Buffer debugBuffer)
- {
- super(parent,jEdit.getProperty("grab-key.title"),true);
- init(binding,allBindings,debugBuffer);
- } //}}}
- //{{{ GrabKeyDialog constructor
- /**
- * Create and show a new modal dialog.
- *
- * @param parent center dialog on this component.
- * @param binding the action/macro that should get a binding.
- * @param allBindings all other key bindings.
- * @param debugBuffer debug info will be dumped to this buffer
- * (may be null)
- * @since jEdit 4.1pre7
- */
- public GrabKeyDialog(Frame parent, KeyBinding binding,
- List<KeyBinding> allBindings, Buffer debugBuffer)
- {
- super(parent,jEdit.getProperty("grab-key.title"),true);
- init(binding,allBindings,debugBuffer);
- } //}}}
- //{{{ getShortcut() method
- /**
- * Returns the shortcut, or null if the current shortcut should be
- * removed or the dialog either has been cancelled. Use isOK()
- * to determine if the latter is true.
- */
- public String getShortcut()
- {
- if(isOK)
- return shortcut.getText();
- else
- return null;
- } //}}}
- //{{{ isOK() method
- /**
- * Returns true, if the dialog has not been cancelled.
- * @since jEdit 3.2pre9
- */
- public boolean isOK()
- {
- return isOK;
- } //}}}
- //{{{ getFocusTraversalKeysEnabled() method
- /**
- * Makes the tab key work in Java 1.4.
- * @since jEdit 3.2pre4
- */
- @Override
- public boolean getFocusTraversalKeysEnabled()
- {
- return false;
- } //}}}
- //{{{ processKeyEvent() method
- @Override
- protected void processKeyEvent(KeyEvent evt)
- {
- shortcut.processKeyEvent(evt);
- } //}}}
- //{{{ Private members
- //{{{ Instance variables
- private InputPane shortcut; // this is a bad hack
- private JLabel assignedTo;
- private JButton ok;
- private JButton remove;
- private JButton cancel;
- private JButton clear;
- private boolean isOK;
- private KeyBinding binding;
- private List<KeyBinding> allBindings;
- private Buffer debugBuffer;
- //}}}
- //{{{ init() method
- private void init(KeyBinding binding, List<KeyBinding> allBindings, Buffer debugBuffer)
- {
- this.binding = binding;
- this.allBindings = allBindings;
- this.debugBuffer = debugBuffer;
- enableEvents(AWTEvent.KEY_EVENT_MASK);
- // create a panel with a BoxLayout. Can't use Box here
- // because Box doesn't have setBorder().
- JPanel content = new JPanel(new GridLayout(0,1,0,6))
- {
- /**
- * Makes the tab key work in Java 1.4.
- * @since jEdit 3.2pre4
- */
- @Override
- public boolean getFocusTraversalKeysEnabled()
- {
- return false;
- }
- };
- content.setBorder(new EmptyBorder(12,12,12,12));
- setContentPane(content);
- JLabel label = new JLabel(
- debugBuffer == null ? jEdit.getProperty(
- "grab-key.caption",new String[] { binding.label })
- : jEdit.getProperty("grab-key.keyboard-test"));
- Box input = Box.createHorizontalBox();
- shortcut = new InputPane();
- Dimension size = shortcut.getPreferredSize();
- size.width = Integer.MAX_VALUE;
- shortcut.setMaximumSize(size);
- input.add(shortcut);
- input.add(Box.createHorizontalStrut(12));
- clear = new JButton(jEdit.getProperty("grab-key.clear"));
- clear.addActionListener(new ActionHandler());
- input.add(clear);
- assignedTo = new JLabel();
- if(debugBuffer == null)
- updateAssignedTo(null);
- Box buttons = Box.createHorizontalBox();
- buttons.add(Box.createGlue());
- if(debugBuffer == null)
- {
- ok = new JButton(jEdit.getProperty("common.ok"));
- ok.addActionListener(new ActionHandler());
- buttons.add(ok);
- buttons.add(Box.createHorizontalStrut(12));
- if(binding.isAssigned())
- {
- // show "remove" button
- remove = new JButton(jEdit.getProperty("grab-key.remove"));
- remove.addActionListener(new ActionHandler());
- buttons.add(remove);
- buttons.add(Box.createHorizontalStrut(12));
- }
- }
- cancel = new JButton(jEdit.getProperty("common.cancel"));
- cancel.addActionListener(new ActionHandler());
- buttons.add(cancel);
- buttons.add(Box.createGlue());
- content.add(label);
- content.add(input);
- if(debugBuffer == null)
- content.add(assignedTo);
- content.add(buttons);
- setDefaultCloseOperation(DISPOSE_ON_CLOSE);
- pack();
- setLocationRelativeTo(getParent());
- setResizable(false);
- setVisible(true);
- } //}}}
- //{{{ getSymbolicName() method
- public static String getSymbolicName(int keyCode)
- {
- if (Debug.DUMP_KEY_EVENTS)
- {
- Log.log(Log.DEBUG,GrabKeyDialog.class,"getSymbolicName("+keyCode+").");
- }
- if(keyCode == KeyEvent.VK_UNDEFINED)
- return null;
- /* else if(keyCode == KeyEvent.VK_OPEN_BRACKET)
- return "[";
- else if(keyCode == KeyEvent.VK_CLOSE_BRACKET)
- return "]"; */
- if(keyCode >= KeyEvent.VK_A && keyCode <= KeyEvent.VK_Z)
- return String.valueOf(Character.toLowerCase((char)keyCode));
- try
- {
- Field[] fields = KeyEvent.class.getFields();
- for(int i = 0; i < fields.length; i++)
- {
-