Search in sources :

Example 6 with KeyEventDispatcher

use of java.awt.KeyEventDispatcher in project tetrad by cmu-phil.

the class AbstractWorkbench method registerKeys.

/**
 * Registers the remove and backspace keys to remove selected objects.
 */
private void registerKeys() {
    getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0), "DELETE");
    getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0), "DELETE");
    Action deleteAction = new AbstractAction() {

        public void actionPerformed(ActionEvent e) {
            AbstractWorkbench workbench = (AbstractWorkbench) e.getSource();
            List<Component> components = workbench.getSelectedComponents();
            int numNodes = 0, numEdges = 0;
            for (Component c : components) {
                if (c instanceof DisplayNode) {
                    numNodes++;
                } else if (c instanceof DisplayEdge) {
                    numEdges++;
                }
            }
            StringBuilder buf = new StringBuilder();
            if (isDeleteVariablesAllowed()) {
                buf.append("Number of nodes selected = ");
                buf.append(numNodes);
            }
            buf.append("\nNumber of edges selected = ");
            buf.append(numEdges);
            buf.append("\n\nDelete selected items?");
            int ret = JOptionPane.showConfirmDialog(workbench, buf.toString());
            if (ret != JOptionPane.YES_OPTION) {
                return;
            }
            deleteSelectedObjects();
        }
    };
    getActionMap().put("DELETE", deleteAction);
    if (controlDispatcher == null) {
        controlDispatcher = new KeyEventDispatcher() {

            public boolean dispatchKeyEvent(KeyEvent e) {
                return respondToControlKey(e);
            }
        };
    }
    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(controlDispatcher);
}
Also used : KeyEvent(java.awt.event.KeyEvent) AbstractAction(javax.swing.AbstractAction) Action(javax.swing.Action) ActionEvent(java.awt.event.ActionEvent) KeyEventDispatcher(java.awt.KeyEventDispatcher) Component(java.awt.Component) JComponent(javax.swing.JComponent) AbstractAction(javax.swing.AbstractAction) Endpoint(edu.cmu.tetrad.graph.Endpoint) Point(java.awt.Point)

Aggregations

KeyEventDispatcher (java.awt.KeyEventDispatcher)6 KeyEvent (java.awt.event.KeyEvent)5 Component (java.awt.Component)2 ActionEvent (java.awt.event.ActionEvent)2 JTabbedPane (javax.swing.JTabbedPane)2 ArrowFocusOption (CCDD.CcddConstants.ArrowFocusOption)1 InputDataType (CCDD.CcddConstants.InputDataType)1 Endpoint (edu.cmu.tetrad.graph.Endpoint)1 SmaliArea (jadx.gui.ui.codearea.SmaliArea)1 Graphics2D (java.awt.Graphics2D)1 Point (java.awt.Point)1 ActionListener (java.awt.event.ActionListener)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 AbstractAction (javax.swing.AbstractAction)1 Action (javax.swing.Action)1 JButton (javax.swing.JButton)1 JCheckBox (javax.swing.JCheckBox)1 JComboBox (javax.swing.JComboBox)1 JComponent (javax.swing.JComponent)1