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);
}
Aggregations