public void actionPerformed(ActionEvent ae) { String cname = nameF.getText().trim(); int state = conditions[stateC.getSelectedIndex()]; try { if (isSpecialCase(cname)) { handleSpecialCase(cname, state); } else { JComponent comp = (JComponent) Class.forName(cname).newInstance(); ComponentUI cui = UIManager.getUI(comp); cui.installUI(comp); results.setText("Map entries for " + cname + ":\n\n"); if (inputB.isSelected()) { loadInputMap(comp.getInputMap(state), ""); results.append("\n"); } if (actionB.isSelected()) { loadActionMap(comp.getActionMap(), ""); results.append("\n"); } if (bindingB.isSelected()) { loadBindingMap(comp, state); } } } catch (ClassCastException cce) { results.setText(cname + " is not a subclass of JComponent."); } catch (ClassNotFoundException cnfe) { results.setText(cname + " was not found."); } catch (InstantiationException ie) { results.setText(cname + " could not be instantiated."); } catch (Exception e) { results.setText("Exception found:\n" + e); e.printStackTrace(); } }
private void AboutBoxKeyEventActionIntialization() { Action ESCactionListener = new AbstractAction() { public void actionPerformed(ActionEvent actionEvent) { setVisible(false); dispose(); } }; KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0, true); JComponent comp = this.getRootPane(); comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, "ESCAPE"); ActionMap actionMap = comp.getActionMap(); actionMap.put("ESCAPE", ESCactionListener); /* OK button Action Solves MAC , Linux and Window enter key issue*/ Action OKactionListener = new AbstractAction() { public void actionPerformed(ActionEvent actionEvent) { btnOKActionPerformed(actionEvent); } }; KeyStroke enter_ok = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0, true); btnOK.getInputMap(JButton.WHEN_FOCUSED).put(enter_ok, "OK"); actionMap = btnOK.getActionMap(); actionMap.put("OK", OKactionListener); btnOK.setActionMap(actionMap); }
/** * Set up key bindings and focus listener for the FieldEditor. * * @param component */ public void setupJTextComponent(final JComponent component, final AutoCompleteListener acl) { // Here we add focus listeners to the component. The funny code is because we need // to guarantee that the AutoCompleteListener - if used - is called before fieldListener // on a focus lost event. The AutoCompleteListener is responsible for removing any // current suggestion when focus is lost, and this must be done before fieldListener // stores the current edit. Swing doesn't guarantee the order of execution of event // listeners, so we handle this by only adding the AutoCompleteListener and telling // it to call fieldListener afterwards. If no AutoCompleteListener is used, we // add the fieldListener normally. if (acl != null) { component.addKeyListener(acl); component.addFocusListener(acl); acl.setNextFocusListener(fieldListener); } else component.addFocusListener(fieldListener); InputMap im = component.getInputMap(JComponent.WHEN_FOCUSED); ActionMap am = component.getActionMap(); im.put(Globals.prefs.getKey("Entry editor, previous entry"), "prev"); am.put("prev", parent.prevEntryAction); im.put(Globals.prefs.getKey("Entry editor, next entry"), "next"); am.put("next", parent.nextEntryAction); im.put(Globals.prefs.getKey("Entry editor, store field"), "store"); am.put("store", parent.storeFieldAction); im.put(Globals.prefs.getKey("Entry editor, next panel"), "right"); im.put(Globals.prefs.getKey("Entry editor, next panel 2"), "right"); am.put("left", parent.switchLeftAction); im.put(Globals.prefs.getKey("Entry editor, previous panel"), "left"); im.put(Globals.prefs.getKey("Entry editor, previous panel 2"), "left"); am.put("right", parent.switchRightAction); im.put(Globals.prefs.getKey("Help"), "help"); am.put("help", parent.helpAction); im.put(Globals.prefs.getKey("Save database"), "save"); am.put("save", parent.saveDatabaseAction); im.put(Globals.prefs.getKey("Next tab"), "nexttab"); am.put("nexttab", parent.frame.nextTab); im.put(Globals.prefs.getKey("Previous tab"), "prevtab"); am.put("prevtab", parent.frame.prevTab); try { HashSet<AWTKeyStroke> keys = new HashSet<AWTKeyStroke>( component.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS)); keys.clear(); keys.add(AWTKeyStroke.getAWTKeyStroke("pressed TAB")); component.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, keys); keys = new HashSet<AWTKeyStroke>( component.getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS)); keys.clear(); keys.add(KeyStroke.getKeyStroke("shift pressed TAB")); component.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, keys); } catch (Throwable t) { System.err.println(t); } }
public void addDialogCloser(JComponent comp) { AbstractAction closeAction = new AbstractAction() { public void actionPerformed(ActionEvent e) { setVisible(false); } }; // Then create a keystroke to use for it KeyStroke ks = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); // Finally, bind the keystroke and the action to *any* component // within the dialog. Note the WHEN_IN_FOCUSED bit...this is what // stops you having to do it for all components comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(ks, "close"); comp.getActionMap().put("close", closeAction); }
public static void replaceUIActionMap(final JComponent component, final ActionMap uiActionMap) { ActionMap actionMap = component.getActionMap(); if (actionMap == null) { component.setActionMap(uiActionMap); } else { if (uiActionMap != null) { actionMap = getUIActionMapChild(actionMap); actionMap.setParent(uiActionMap); } else { while (actionMap != null) { if (actionMap.getParent() instanceof UIResource) { actionMap.setParent(actionMap.getParent().getParent()); } else { actionMap = actionMap.getParent(); } } } } }
private void handleSpecialCase(String cname, int condition) { Component comp = null; JComponent mapComp = null; try { if (cname.equals("javax.swing.JApplet") || cname.equals("javax.swing.JDialog") || cname.equals("javax.swing.JFrame") || cname.equals("javax.swing.JWindow")) { comp = (Component) Class.forName(cname).newInstance(); mapComp = (JComponent) ((JApplet) comp).getLayeredPane(); results.setText("Map entries for " + cname + " (delegated to JRootPane):\n\n"); } else if (cname.equals("javax.swing.JInternalFrame")) { JDesktopPane jdp = new JDesktopPane(); JInternalFrame jif = new JInternalFrame("Demo"); jif.setVisible(true); jdp.add(jif); mapComp = jif; } if (inputB.isSelected()) { loadInputMap(mapComp.getInputMap(condition), ""); results.append("\n"); } if (actionB.isSelected()) { loadActionMap(mapComp.getActionMap(), ""); results.append("\n"); } if (bindingB.isSelected()) { loadBindingMap(mapComp, condition); } } catch (ClassCastException cce) { results.setText(cname + " is not a subclass of JComponent."); } catch (ClassNotFoundException cnfe) { results.setText(cname + " was not found."); } catch (InstantiationException ie) { results.setText(cname + " could not be instantiated."); } catch (Exception e) { results.setText("Exception found:\n" + e); e.printStackTrace(); } }
// {{{ _preprocessKeyEvent() method private KeyEvent _preprocessKeyEvent(KeyEvent evt) { if (view.isClosed()) return null; Component focusOwner = view.getFocusOwner(); if (focusOwner instanceof JComponent) { JComponent comp = (JComponent) focusOwner; InputMap map = comp.getInputMap(); ActionMap am = comp.getActionMap(); if (map != null && am != null && comp.isEnabled()) { KeyStroke keyStroke = KeyStroke.getKeyStrokeForEvent(evt); Object binding = map.get(keyStroke); if (binding != null && am.get(binding) != null) { return null; } } } if (focusOwner instanceof JTextComponent) { // fix for the bug where key events in JTextComponents // inside views are also handled by the input handler if (evt.getID() == KeyEvent.KEY_PRESSED) { switch (evt.getKeyCode()) { case KeyEvent.VK_ENTER: case KeyEvent.VK_TAB: case KeyEvent.VK_BACK_SPACE: case KeyEvent.VK_SPACE: return null; } } } if (evt.isConsumed()) return null; if (Debug.DUMP_KEY_EVENTS) { Log.log(Log.DEBUG, this, "Key event (preprocessing) : " + AbstractInputHandler.toString(evt)); } return KeyEventWorkaround.processKeyEvent(evt); } // }}}
/** * Registers the keystroke of the given action as "command" of the given component. * * <p>This code is based on the Sulky-tools, found at <http://github.com/huxi/sulky>. * * @param aComponent the component that should react on the keystroke, cannot be <code>null</code> * ; * @param aAction the action of the keystroke, cannot be <code>null</code>; * @param aCommandName the name of the command to register the keystore under. */ public static void registerKeystroke( final JComponent aComponent, final Action aAction, final String aCommandName) { final KeyStroke keyStroke = (KeyStroke) aAction.getValue(Action.ACCELERATOR_KEY); if (keyStroke == null) { return; } InputMap inputMap = aComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); ActionMap actionMap = aComponent.getActionMap(); inputMap.put(keyStroke, aCommandName); actionMap.put(aCommandName, aAction); inputMap = aComponent.getInputMap(JComponent.WHEN_FOCUSED); Object value = inputMap.get(keyStroke); if (value != null) { inputMap.put(keyStroke, aCommandName); } inputMap = aComponent.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); value = inputMap.get(keyStroke); if (value != null) { inputMap.put(keyStroke, aCommandName); } }
public void loadBindingMap(JComponent c, int condition) { loadBindingMap(c.getInputMap(condition), c.getActionMap()); }
public static ActionMap getUIActionMap(final JComponent component) { ActionMap actionMap = getUIActionMapChild(component.getActionMap()); return (actionMap != null) ? actionMap.getParent() : null; }
public static void performActionOnFocusedJComponent(String action) { JComponent c = getFocusedJComponent(); if (c != null) c.getActionMap().get(action).actionPerformed(null); }
public BundleOptionsFrame(String displayName, String instanceName, List<OptionGroup> options) { setResizable(false); this.displayName = displayName; content = (JComponent) getContentPane(); content.setLayout(new GridBagLayout()); final BundleOptionsFrame frame = this; // Close button Action closeAction = new AbstractAction("Close") { @Override public void actionPerformed(ActionEvent e) { frame.setVisible(false); } }; JButton close_button = new JButton(closeAction); GridBagConstraints c = new GridBagConstraints(); c.gridx = 0; c.gridy = currentRow++; c.anchor = GridBagConstraints.LINE_START; c.insets = new Insets(2, 2, 2, 2); content.add(close_button, c); // Escape key binding JComponent root = frame.getRootPane(); root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close"); root.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW) .put(KeyStroke.getKeyStroke("ctrl W"), "close"); root.getActionMap().put("close", closeAction); if (instanceName != null) { // Predicate name StringOption opt = new StringOption(); opt.setDisplayName("Predicate name"); opt.setDefault(instanceName); instanceNameField = new StringField(opt); instanceNameField.addChangeListener( new ChangeListener() { @Override public void stateChanged(ChangeEvent e) { updateTitle(); } }); addField(instanceNameField); JTextField tf = (JTextField) instanceNameField.getComponent(); tf.selectAll(); tf.requestFocusInWindow(); } else { // We're a codec; no instance name instanceNameField = null; } updateTitle(); // Options ExampleField example = null; for (OptionGroup group : options) { addSeparator(group.getDisplayName()); for (Option option : group.getOptions()) { OptionField field; if (option instanceof BooleanOption) { field = new BooleanField((BooleanOption) option); } else if (option instanceof StringOption) { field = new StringField((StringOption) option); } else if (option instanceof NumberOption) { field = new NumberField((NumberOption) option); } else if (option instanceof ChoiceOption) { field = new ChoiceField((ChoiceOption) option); } else if (option instanceof ExampleOption) { if (example != null) { throw new IllegalArgumentException("Cannot display more than one ExampleOption"); } example = new ExampleField((ExampleOption) option); field = example; } else { throw new IllegalArgumentException("Unknown option type"); } addField(field); optionFields.add(field); } } this.exampleField = example; pack(); }