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(); } }
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); }
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(); } }
public void loadBindingMap(JComponent c, int condition) { loadBindingMap(c.getInputMap(condition), c.getActionMap()); }
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(); }