private void removeDefaultKeys() {
    InputMap mainMap = SwingUtilities.getUIInputMap(this, JComponent.WHEN_FOCUSED);

    mainMap.remove(KeyStroke.getKeyStroke("ctrl C"));
    mainMap.remove(KeyStroke.getKeyStroke("ctrl X"));
    mainMap.remove(KeyStroke.getKeyStroke("ctrl V"));
  }
  /**
   * A helper for creating and updating key bindings for components with mnemonics. The {@code
   * pressed} action will be invoked when the mnemonic is activated and the {@code released} action
   * will be invoked when the mnemonic is deactivated.
   *
   * <p>TODO establish an interface for the mnemonic properties, such as {@code MnemonicEnabled} and
   * change signature to {@code public static <T extends JComponent & MnemonicEnabled> void
   * updateMnemonicBinding(T c, String pressed, String released)}
   *
   * @param c the component bindings to update
   * @param pressed the name of the action in the action map to invoke when the mnemonic is pressed
   * @param released the name of the action in the action map to invoke when the mnemonic is
   *     released (if the action is a toggle style, then this parameter should be {@code null})
   * @throws NullPointerException if the component is {@code null}
   */
  public static void updateMnemonicBinding(JComponent c, String pressed, String released) {
    Class<?> clazz = c.getClass();
    int m = -1;

    try {
      Method mtd = clazz.getMethod("getMnemonic");
      m = (Integer) mtd.invoke(c);
    } catch (RuntimeException e) {
      throw e;
    } catch (Exception e) {
      throw new IllegalArgumentException("unable to access mnemonic", e);
    }

    InputMap map = SwingUtilities.getUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW);

    if (m != 0) {
      if (map == null) {
        map = new ComponentInputMapUIResource(c);
        SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW, map);
      }

      map.clear();

      // TODO is ALT_MASK right for all platforms?
      map.put(KeyStroke.getKeyStroke(m, InputEvent.ALT_MASK, false), pressed);
      map.put(KeyStroke.getKeyStroke(m, InputEvent.ALT_MASK, true), released);
      map.put(KeyStroke.getKeyStroke(m, 0, true), released);
    } else {
      if (map != null) {
        map.clear();
      }
    }
  }
 /** Installs the state needed for mnemonics. */
 private void initMnemonics() {
   mnemonicToIndexMap = new Hashtable();
   mnemonicInputMap = new InputMapUIResource();
   mnemonicInputMap.setParent(
       SwingUtilities.getUIInputMap(tabPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT));
   SwingUtilities.replaceUIInputMap(
       tabPane, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, mnemonicInputMap);
 }
  /*
   * Load key bindings from UIDefaults to InputMap. This occurs
   * when the default button is assigned.
   */
  private void loadDefaultButtonKeyBindings(final JComponent root) {
    Object[] bindings = ((Object[]) UIManager.get("RootPane.defaultButtonWindowKeyBindings"));

    if (bindings != null) {
      InputMap map =
          SwingUtilities.getUIInputMap(root, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
      LookAndFeel.loadKeyBindings(map, bindings);
    }
  }
Example #5
0
 // TODO accelerators for buttons
 private static void installAccelerator(Action action, final JButton button) {
   if (action.getValue(Action.ACCELERATOR_KEY) instanceof KeyStroke) {
     KeyStroke keyStroke = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY);
     InputMap windowInputMap =
         SwingUtilities.getUIInputMap(button, JComponent.WHEN_IN_FOCUSED_WINDOW);
     if (windowInputMap == null) {
       windowInputMap = new ComponentInputMapUIResource(button);
       SwingUtilities.replaceUIInputMap(button, JComponent.WHEN_IN_FOCUSED_WINDOW, windowInputMap);
     }
     windowInputMap.put(keyStroke, keyStroke.toString());
     button.getActionMap().put(keyStroke.toString(), action);
   }
 }
  private void unloadDefaultButtonKeyBindings(final JComponent root) {
    Object[] bindings = ((Object[]) UIManager.get("RootPane.defaultButtonWindowKeyBindings"));

    if (bindings != null) {
      InputMap map =
          SwingUtilities.getUIInputMap(root, JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
      for (int i = 0; i < bindings.length; i += 2) {
        if (bindings[i] instanceof String) {
          map.remove(KeyStroke.getKeyStroke((String) bindings[i]));
        } else {
          map.remove((KeyStroke) bindings[i]);
        }
      }
    }
  }
 private void uninstallTracking() {
   InputMap currMap = SwingUtilities.getUIInputMap(this.jcomp, JComponent.WHEN_FOCUSED);
   if (currMap != null) {
     InputMap newMap = new InputMap();
     KeyStroke[] kss = currMap.allKeys();
     for (int i = 0; i < kss.length; i++) {
       KeyStroke stroke = kss[i];
       Object val = currMap.get(stroke);
       if (stroke.equals(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0))
           && "flipTextSelection".equals(val)) {
         continue;
       }
       newMap.put(stroke, val);
     }
     SwingUtilities.replaceUIInputMap(this.jcomp, JComponent.WHEN_FOCUSED, newMap);
   }
   this.jcomp.getActionMap().remove("flipTextSelection");
 }
  private void updateMnemonic(int lastMnemonic, int mnemonic) {
    if (mnemonic == lastMnemonic) {
      return;
    }
    InputMap windowInputMap = SwingUtilities.getUIInputMap(this, JComponent.WHEN_IN_FOCUSED_WINDOW);

    int mask = SystemInfo.isMac ? InputEvent.ALT_MASK | InputEvent.CTRL_MASK : InputEvent.ALT_MASK;
    if (lastMnemonic != 0 && windowInputMap != null) {
      windowInputMap.remove(KeyStroke.getKeyStroke(lastMnemonic, mask, false));
    }
    if (mnemonic != 0) {
      if (windowInputMap == null) {
        windowInputMap = new ComponentInputMapUIResource(this);
        SwingUtilities.replaceUIInputMap(this, JComponent.WHEN_IN_FOCUSED_WINDOW, windowInputMap);
      }
      windowInputMap.put(KeyStroke.getKeyStroke(mnemonic, mask, false), "doClick");
    }
  }
  private void installTracking() {
    InputMap currMap = SwingUtilities.getUIInputMap(this.jcomp, JComponent.WHEN_FOCUSED);

    InputMap newMap = new InputMap();
    if (currMap != null) {
      KeyStroke[] kss = currMap.allKeys();
      for (int i = 0; i < kss.length; i++) {
        KeyStroke stroke = kss[i];
        Object val = currMap.get(stroke);
        newMap.put(stroke, val);
      }
    }

    newMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "flipTextSelection");

    this.jcomp
        .getActionMap()
        .put(
            "flipTextSelection",
            new AbstractAction() {
              public void actionPerformed(ActionEvent e) {
                SwingUtilities.invokeLater(
                    new Runnable() {
                      public void run() {
                        int selectionLength = jcomp.getSelectionEnd() - jcomp.getSelectionStart();
                        if (selectionLength == 0) {
                          jcomp.selectAll();
                        } else {
                          int lastPos = jcomp.getSelectionEnd();
                          jcomp.setSelectionStart(0);
                          jcomp.setSelectionEnd(0);
                          jcomp.setCaretPosition(lastPos);
                        }
                      }
                    });
              }
            });

    SwingUtilities.replaceUIInputMap(this.jcomp, JComponent.WHEN_FOCUSED, newMap);
  }