/** * 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 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(); }