private void init() { getActionMap().put("startEditing", new StartEditingAction()); // NOI18N getActionMap().put("cancel", new CancelEditingAction()); // NOI18N addMouseListener(new MouseListener()); getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F2, 0), "startEditing"); // NOI18N getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel"); // NOI18N putClientProperty("terminateEditOnFocusLost", Boolean.TRUE); // NOI18N }
/** * This method updates the input and action maps with new ToggleAction. * * @param isToggleDynamic * @param isToggleLarger * @param key * @param keyStroke */ private void installToggleAction( boolean isToggleDynamic, boolean isToggleLarger, String key, String keyStroke) { Action action = new ToggleAction(isToggleDynamic, isToggleLarger); KeyStroke ks = KeyStroke.getKeyStroke(keyStroke); table.getInputMap().put(ks, key); table.getActionMap().put(key, action); }
/** * This method updates the input and action maps with a new ColumnAction. * * @param isSelectedColumn * @param isAdjust * @param key * @param keyStroke */ private void installColumnAction( boolean isSelectedColumn, boolean isAdjust, String key, String keyStroke) { Action action = new ColumnAction(isSelectedColumn, isAdjust); KeyStroke ks = KeyStroke.getKeyStroke(keyStroke); table.getInputMap().put(ks, key); table.getActionMap().put(key, action); }
// ------------------------------------------------------------------ // from Java Swing 1.2 Orielly - Robert Eckstein // ------------------------------------------------------------------ protected JTextComponent updateKeymapForWord(JTextComponent textComp) { // create a new child keymap Keymap map = JTextComponent.addKeymap("NslmMap", textComp.getKeymap()); // define the keystrokeds to be added KeyStroke next = KeyStroke.getKeyStroke(KeyEvent.VK_RIGHT, InputEvent.CTRL_MASK, false); // add the new mappings used DefaultEditorKit actions map.addActionForKeyStroke(next, getAction(DefaultEditorKit.nextWordAction)); KeyStroke prev = KeyStroke.getKeyStroke(KeyEvent.VK_LEFT, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(prev, getAction(DefaultEditorKit.previousWordAction)); KeyStroke selNext = KeyStroke.getKeyStroke( KeyEvent.VK_RIGHT, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK, false); map.addActionForKeyStroke(selNext, getAction(DefaultEditorKit.selectionNextWordAction)); KeyStroke selPrev = KeyStroke.getKeyStroke( KeyEvent.VK_LEFT, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK, false); map.addActionForKeyStroke(selPrev, getAction(DefaultEditorKit.selectionPreviousWordAction)); KeyStroke find = KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(find, getAction("find")); KeyStroke findAgain = KeyStroke.getKeyStroke(KeyEvent.VK_G, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(findAgain, getAction("findAgain")); // set the keymap for the text component textComp.setKeymap(map); return (textComp); } // end updateKeymapForWord
/** * This method is called when the focused component (and none of its ancestors) want the key * event. This will look up the keystroke to see if any chidren (or subchildren) of the specified * container want a crack at the event. If one of them wants it, then it will "DO-THE-RIGHT-THING" */ public boolean fireKeyboardAction(KeyEvent e, boolean pressed, Container topAncestor) { if (e.isConsumed()) { System.out.println("Acquired pre-used event!"); Thread.dumpStack(); } // There may be two keystrokes associated with a low-level key event; // in this case a keystroke made of an extended key code has a priority. KeyStroke ks; KeyStroke ksE = null; if (e.getID() == KeyEvent.KEY_TYPED) { ks = KeyStroke.getKeyStroke(e.getKeyChar()); } else { if (e.getKeyCode() != e.getExtendedKeyCode()) { ksE = KeyStroke.getKeyStroke(e.getExtendedKeyCode(), e.getModifiers(), !pressed); } ks = KeyStroke.getKeyStroke(e.getKeyCode(), e.getModifiers(), !pressed); } Hashtable keyMap = containerMap.get(topAncestor); if (keyMap != null) { // this container isn't registered, so bail Object tmp = null; // extended code has priority if (ksE != null) { tmp = keyMap.get(ksE); if (tmp != null) { ks = ksE; } } if (tmp == null) { tmp = keyMap.get(ks); } if (tmp == null) { // don't do anything } else if (tmp instanceof JComponent) { JComponent c = (JComponent) tmp; if (c.isShowing() && c.isEnabled()) { // only give it out if enabled and visible fireBinding(c, ks, e, pressed); } } else if (tmp instanceof Vector) { // more than one comp registered for this Vector v = (Vector) tmp; // There is no well defined order for WHEN_IN_FOCUSED_WINDOW // bindings, but we give precedence to those bindings just // added. This is done so that JMenus WHEN_IN_FOCUSED_WINDOW // bindings are accessed before those of the JRootPane (they // both have a WHEN_IN_FOCUSED_WINDOW binding for enter). for (int counter = v.size() - 1; counter >= 0; counter--) { JComponent c = (JComponent) v.elementAt(counter); // System.out.println("Trying collision: " + c + " vector = "+ v.size()); if (c.isShowing() && c.isEnabled()) { // don't want to give these out fireBinding(c, ks, e, pressed); if (e.isConsumed()) return true; } } } else { System.out.println("Unexpected condition in fireKeyboardAction " + tmp); // This means that tmp wasn't null, a JComponent, or a Vector. What is it? Thread.dumpStack(); } } if (e.isConsumed()) { return true; } // if no one else handled it, then give the menus a crack // The're handled differently. The key is to let any JMenuBars // process the event if (keyMap != null) { Vector v = (Vector) keyMap.get(JMenuBar.class); if (v != null) { Enumeration iter = v.elements(); while (iter.hasMoreElements()) { JMenuBar mb = (JMenuBar) iter.nextElement(); if (mb.isShowing() && mb.isEnabled()) { // don't want to give these out boolean extended = (ksE != null) && !ksE.equals(ks); if (extended) { fireBinding(mb, ksE, e, pressed); } if (!extended || !e.isConsumed()) { fireBinding(mb, ks, e, pressed); } if (e.isConsumed()) { return true; } } } } } return e.isConsumed(); }
public static void makeMenuBar(JFrame frame, final AirspaceBuilderController controller) { JMenuBar menuBar = new JMenuBar(); final JCheckBoxMenuItem resizeNewShapesItem; final JCheckBoxMenuItem enableEditItem; JMenu menu = new JMenu("File"); { JMenuItem item = new JMenuItem("Open..."); item.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_O, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())); item.setActionCommand(OPEN); item.addActionListener(controller); menu.add(item); item = new JMenuItem("Open URL..."); item.setActionCommand(OPEN_URL); item.addActionListener(controller); menu.add(item); item = new JMenuItem("Save..."); item.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_S, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())); item.setActionCommand(SAVE); item.addActionListener(controller); menu.add(item); menu.addSeparator(); item = new JMenuItem("Load Demo Shapes"); item.setActionCommand(OPEN_DEMO_AIRSPACES); item.addActionListener(controller); menu.add(item); } menuBar.add(menu); menu = new JMenu("Shape"); { JMenu subMenu = new JMenu("New"); for (final AirspaceFactory factory : defaultAirspaceFactories) { JMenuItem item = new JMenuItem(factory.toString()); item.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { controller.createNewEntry(factory); } }); subMenu.add(item); } menu.add(subMenu); resizeNewShapesItem = new JCheckBoxMenuItem("Fit new shapes to viewport"); resizeNewShapesItem.setActionCommand(SIZE_NEW_SHAPES_TO_VIEWPORT); resizeNewShapesItem.addActionListener(controller); resizeNewShapesItem.setState(controller.isResizeNewShapesToViewport()); menu.add(resizeNewShapesItem); enableEditItem = new JCheckBoxMenuItem("Enable shape editing"); enableEditItem.setActionCommand(ENABLE_EDIT); enableEditItem.addActionListener(controller); enableEditItem.setState(controller.isEnableEdit()); menu.add(enableEditItem); } menuBar.add(menu); menu = new JMenu("Selection"); { JMenuItem item = new JMenuItem("Deselect"); item.setAccelerator( KeyStroke.getKeyStroke( KeyEvent.VK_D, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask())); item.setActionCommand(CLEAR_SELECTION); item.addActionListener(controller); menu.add(item); item = new JMenuItem("Delete"); item.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_DELETE, 0)); item.setActionCommand(REMOVE_SELECTED); item.addActionListener(controller); menu.add(item); } menuBar.add(menu); frame.setJMenuBar(menuBar); controller.addPropertyChangeListener( new PropertyChangeListener() { public void propertyChange(PropertyChangeEvent e) { if (SIZE_NEW_SHAPES_TO_VIEWPORT.equals((e.getPropertyName()))) { resizeNewShapesItem.setSelected(controller.isResizeNewShapesToViewport()); } else if (ENABLE_EDIT.equals(e.getPropertyName())) { enableEditItem.setSelected(controller.isEnableEdit()); } } }); }
// ------------------------------------------------------------------ // from Java Swing 1.2 Orielly - Robert Eckstein // ------------------------------------------------------------------ protected JTextComponent updateKeymapForEmacs(JTextComponent textComp) { // note: it does not look like a key can do more than one action // thus no modes. // todo: not all of these are correct. such as ctrlK // todo: add saving - ctrlXS // create a new child keymap Keymap map = JTextComponent.addKeymap("NslmMap", textComp.getKeymap()); KeyStroke selNext = KeyStroke.getKeyStroke( KeyEvent.VK_RIGHT, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK, false); map.addActionForKeyStroke(selNext, getAction(DefaultEditorKit.selectionNextWordAction)); KeyStroke selPrev = KeyStroke.getKeyStroke( KeyEvent.VK_LEFT, InputEvent.CTRL_MASK | InputEvent.SHIFT_MASK, false); map.addActionForKeyStroke(selPrev, getAction(DefaultEditorKit.selectionPreviousWordAction)); KeyStroke next = KeyStroke.getKeyStroke(KeyEvent.VK_F, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(next, getAction(DefaultEditorKit.forwardAction)); KeyStroke prev = KeyStroke.getKeyStroke(KeyEvent.VK_B, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(prev, getAction(DefaultEditorKit.backwardAction)); KeyStroke selectionDown = KeyStroke.getKeyStroke(KeyEvent.VK_N, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(selectionDown, getAction(DefaultEditorKit.downAction)); KeyStroke selectionUp = KeyStroke.getKeyStroke(KeyEvent.VK_P, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(selectionUp, getAction(DefaultEditorKit.upAction)); KeyStroke pageDown = KeyStroke.getKeyStroke(KeyEvent.VK_V, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(pageDown, getAction(DefaultEditorKit.pageDownAction)); KeyStroke pageUp = KeyStroke.getKeyStroke(KeyEvent.VK_U, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(pageUp, getAction(DefaultEditorKit.pageUpAction)); KeyStroke endDoc = KeyStroke.getKeyStroke( KeyEvent.VK_GREATER, InputEvent.META_MASK | InputEvent.SHIFT_MASK, false); map.addActionForKeyStroke(endDoc, getAction(DefaultEditorKit.endAction)); KeyStroke beginingDoc = KeyStroke.getKeyStroke( KeyEvent.VK_LESS, InputEvent.META_MASK | InputEvent.SHIFT_MASK, false); map.addActionForKeyStroke(beginingDoc, getAction(DefaultEditorKit.beginAction)); // the VK_SPACE and VK_W not working as in Emacs - space deleting // KeyStroke // selectionStart=KeyStroke.getKeyStroke(KeyEvent.VK_SPACE,InputEvent.CTRL_MASK,false); // map.addActionForKeyStroke(selectionStart,getAction(DefaultEditorKit.selectionForwardAction)); // //todo: setCharPosAction // this is doing nothing because only one char to can be assigned to cut // KeyStroke cut1=KeyStroke.getKeyStroke(KeyEvent.VK_W,InputEvent.CTRL_MASK,false); // map.addActionForKeyStroke(cut1,getAction(DefaultEditorKit.cutAction)); // if we do save as XS, this will have to change KeyStroke cut = KeyStroke.getKeyStroke(KeyEvent.VK_X, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(cut, getAction(DefaultEditorKit.cutAction)); KeyStroke paste = KeyStroke.getKeyStroke(KeyEvent.VK_Y, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(paste, getAction(DefaultEditorKit.pasteAction)); KeyStroke moveToEndLine = KeyStroke.getKeyStroke(KeyEvent.VK_E, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(moveToEndLine, getAction(DefaultEditorKit.endLineAction)); // not emacs like KeyStroke selWord = KeyStroke.getKeyStroke(KeyEvent.VK_T, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(selWord, getAction(DefaultEditorKit.selectWordAction)); KeyStroke selLine = KeyStroke.getKeyStroke(KeyEvent.VK_K, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(selLine, getAction(DefaultEditorKit.selectLineAction)); KeyStroke delNext = KeyStroke.getKeyStroke(KeyEvent.VK_D, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(delNext, getAction(DefaultEditorKit.deleteNextCharAction)); KeyStroke insertLine = KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(insertLine, getAction(DefaultEditorKit.insertBreakAction)); KeyStroke searchBackward = KeyStroke.getKeyStroke(KeyEvent.VK_R, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(searchBackward, getAction("findAgain")); KeyStroke searchForward = KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK, false); map.addActionForKeyStroke(searchForward, getAction("findAgain")); // set the keymap for the text component textComp.setKeymap(map); return (textComp); } // end updateKeymapForEmacs