/**
  * Returns the KeyStroke for this binding, as defined by the defaults, or in the Preferences, but
  * adapted for Mac users, with the Command key preferred instead of Control.
  */
 private KeyStroke getKeyForMac(KeyStroke ks) {
   if (ks == null) return null;
   int keyCode = ks.getKeyCode();
   if ((ks.getModifiers() & KeyEvent.CTRL_MASK) == 0) {
     return ks;
   } else {
     if ((ks.getModifiers() & KeyEvent.SHIFT_MASK) != 0) {
       return KeyStroke.getKeyStroke(keyCode, Globals.getShortcutMask() + KeyEvent.SHIFT_MASK);
     }
     return KeyStroke.getKeyStroke(keyCode, Globals.getShortcutMask());
   }
 }
  public static void registerHotKey(KeyStroke stroke, final ActionBase ab) {
    // under windows, use hotkey that can be used even when window
    // has not the focus. Note that all keys are nor hotkeys (given
    // by bHotkey flag)
    int index = hmIndexAction.size() - 1;
    jintellitype.registerSwingHotKey(index + 1, stroke.getModifiers(), stroke.getKeyCode());
    // register the action with its index
    hmIndexAction.put(index + 1, ab);
    // add the listener
    jintellitype.addHotKeyListener(
        new HotkeyListener() {

          public void onHotKey(int key) {
            // Leave if user disabled hotkeys
            if (!ConfigurationManager.getBoolean(CONF_OPTIONS_HOTKEYS)) {
              return;
            }
            // Check it is the right listener that caught the event
            if (ab.equals(hmIndexAction.get(key))) {
              try {
                // Call action itself
                ab.perform(null);
              } catch (Throwable e2) {
                Log.error(e2);
              } finally {
                ObservationManager.notify(new Event(EventSubject.EVENT_PLAYLIST_REFRESH));
              }
            }
          }
        });
  }
Beispiel #3
0
 /**
  * Converts a KeyStroke into a string representation for preference storage.
  *
  * @param prefsStroke the KeyStroke to convert
  * @return a string representation of the form &quot;modifiers keyCode&quot; or <code>null</code>
  *     if the prefsStroke is invalid or <code>null</code>
  */
 public static final String strokeToPrefs(KeyStroke prefsStroke) {
   if (prefsStroke == null) return null;
   else
     return String.valueOf(prefsStroke.getModifiers())
         + ' '
         + String.valueOf(prefsStroke.getKeyCode());
 }
Beispiel #4
0
 /**
  * Returns a human readable text for the shortcut.
  *
  * @return a human readable text for the shortcut
  */
 public String getKeyText() {
   KeyStroke keyStroke = getKeyStroke();
   if (keyStroke == null) return "";
   String modifText = KeyEvent.getKeyModifiersText(keyStroke.getModifiers());
   if ("".equals(modifText)) return KeyEvent.getKeyText(keyStroke.getKeyCode());
   return modifText + '+' + KeyEvent.getKeyText(keyStroke.getKeyCode());
 }
Beispiel #5
0
  public static String keyStroke2String(KeyStroke key) {
    StringBuffer s = new StringBuffer(50);
    int m = key.getModifiers();
    if ((m & 0x41) != 0) s.append("shift ");
    if ((m & 0x82) != 0) s.append("ctrl ");
    if ((m & 0x104) != 0) s.append("meta ");
    if ((m & 0x208) != 0) s.append("alt ");
    if ((m & 0x410) != 0) s.append("button1 ");
    if ((m & 0x808) != 0) s.append("button2 ");
    if ((m & 0x1004) != 0) s.append("button3 ");
    switch (key.getKeyEventType()) {
      case 400:
        s.append("typed ");
        s.append(key.getKeyChar() + " ");
        break;

      case 401:
        s.append("pressed ");
        s.append(getKeyText(key.getKeyCode()) + " ");
        break;

      case 402:
        s.append("released ");
        s.append(getKeyText(key.getKeyCode()) + " ");
        break;

      default:
        s.append("unknown-event-type");
        break;
    }
    return s.toString();
  }
Beispiel #6
0
  public void setActionItem(GUIAction aAction) {
    mAction = aAction;
    this.setText(mAction.getTitle());
    if (mAction.getMnemonic() != 0) this.setMnemonic(mAction.getMnemonic());

    setImage(mAction.getIcon());
    if (mAction.getIcon() != null)
      this.setIcon((ImageIcon) mAction.getIcon().resizeIcon(24, true).getData());
    else this.setIcon(null);
    KeyStroke key = KeyStroke.getKeyStroke(mAction.getKeyStroke());
    if (key != null) {
      int key_code = key.getKeyCode();
      int modifier = key.getModifiers();
      int new_modifier = 0;
      if ((modifier & java.awt.event.InputEvent.ALT_MASK) == java.awt.event.InputEvent.ALT_MASK)
        new_modifier += java.awt.event.InputEvent.ALT_MASK;
      if ((modifier & java.awt.event.InputEvent.SHIFT_MASK) == java.awt.event.InputEvent.SHIFT_MASK)
        new_modifier += java.awt.event.InputEvent.SHIFT_MASK;
      if ((modifier & java.awt.event.InputEvent.CTRL_MASK) == java.awt.event.InputEvent.CTRL_MASK)
        new_modifier += getControlKey();
      this.setAccelerator(KeyStroke.getKeyStroke(key_code, new_modifier));
    }
    this.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            mAction.actionPerformed();
          }
        });
  }
  public static InputEvent getInputEvent(String actionName) {
    final Shortcut[] shortcuts =
        KeymapManager.getInstance().getActiveKeymap().getShortcuts(actionName);
    KeyStroke keyStroke = null;
    for (Shortcut each : shortcuts) {
      if (each instanceof KeyboardShortcut) {
        keyStroke = ((KeyboardShortcut) each).getFirstKeyStroke();
        if (keyStroke != null) break;
      }
    }

    if (keyStroke != null) {
      return new KeyEvent(
          JOptionPane.getRootFrame(),
          KeyEvent.KEY_PRESSED,
          System.currentTimeMillis(),
          keyStroke.getModifiers(),
          keyStroke.getKeyCode(),
          keyStroke.getKeyChar(),
          KeyEvent.KEY_LOCATION_STANDARD);
    } else {
      return new MouseEvent(
          JOptionPane.getRootFrame(),
          MouseEvent.MOUSE_PRESSED,
          0,
          0,
          0,
          0,
          1,
          false,
          MouseEvent.BUTTON1);
    }
  }
 /**
  * This is hack. AWT doesn't allow to create KeyStroke with specified key code and key char
  * simultaneously. Therefore we are using reflection.
  */
 private static KeyStroke getKeyStrokeWithoutMouseModifiers(KeyStroke originalKeyStroke) {
   int modifier =
       originalKeyStroke.getModifiers()
           & ~InputEvent.BUTTON1_DOWN_MASK
           & ~InputEvent.BUTTON1_MASK
           & ~InputEvent.BUTTON2_DOWN_MASK
           & ~InputEvent.BUTTON2_MASK
           & ~InputEvent.BUTTON3_DOWN_MASK
           & ~InputEvent.BUTTON3_MASK;
   try {
     Method[] methods = AWTKeyStroke.class.getDeclaredMethods();
     Method getCachedStrokeMethod = null;
     for (Method method : methods) {
       if (GET_CACHED_STROKE_METHOD_NAME.equals(method.getName())) {
         getCachedStrokeMethod = method;
         getCachedStrokeMethod.setAccessible(true);
         break;
       }
     }
     if (getCachedStrokeMethod == null) {
       throw new IllegalStateException("not found method with name getCachedStrokeMethod");
     }
     Object[] getCachedStrokeMethodArgs =
         new Object[] {
           originalKeyStroke.getKeyChar(),
           originalKeyStroke.getKeyCode(),
           modifier,
           originalKeyStroke.isOnKeyRelease()
         };
     return (KeyStroke) getCachedStrokeMethod.invoke(originalKeyStroke, getCachedStrokeMethodArgs);
   } catch (Exception exc) {
     throw new IllegalStateException(exc.getMessage());
   }
 }
  /**
   * Get the mnemonic for a keystroke.
   *
   * @param key a keystroke
   * @return mnemonic of tghe keystroke.
   */
  private static String getKeyMnemonic(KeyStroke key) {
    String sk = org.openide.util.Utilities.keyToString(key);
    StringBuffer sb = new StringBuffer();
    int mods = key.getModifiers();
    if ((mods & KeyEvent.CTRL_MASK) != 0) {
      sb.append("Ctrl+"); // NOI18N
    }
    if ((mods & KeyEvent.ALT_MASK) != 0) {
      sb.append("Alt+"); // NOI18N
    }
    if ((mods & KeyEvent.SHIFT_MASK) != 0) {
      sb.append("Shift+"); // NOI18N
    }
    if ((mods & KeyEvent.META_MASK) != 0) {
      sb.append("Meta+"); // NOI18N
    }

    int i = sk.indexOf('-'); // NOI18N
    if (i != -1) {
      sk = sk.substring(i + 1);
    }
    sb.append(sk);

    return sb.toString();
  }
 public boolean isValid(final KeyStroke keystroke, final Character keyChar) {
   if (keystroke == null) {
     return true;
   }
   final Node menuBarNode = menuBuilder.getMenuBar(menuBuilder.get(key));
   if (menuBarNode == null) {
     return true;
   }
   if (keyChar != KeyEvent.CHAR_UNDEFINED
       && (keystroke.getModifiers() & (Event.ALT_MASK | Event.CTRL_MASK | Event.META_MASK))
           == 0) {
     final String keyTypeActionString =
         ResourceController.getResourceController()
             .getProperty("key_type_action", FirstAction.EDIT_CURRENT.toString());
     FirstAction keyTypeAction = FirstAction.valueOf(keyTypeActionString);
     return FirstAction.IGNORE.equals(keyTypeAction);
   }
   if ((keystroke.getModifiers() & (Event.ALT_MASK | Event.CTRL_MASK | Event.META_MASK))
       == Event.ALT_MASK) {
     final JMenuBar menuBar = (JMenuBar) menuBarNode.getUserObject();
     final int menuCount = menuBar.getMenuCount();
     for (int i = 0; i < menuCount; i++) {
       final JMenu menu = menuBar.getMenu(i);
       final char c = (char) menu.getMnemonic();
       if (Character.toLowerCase(keystroke.getKeyCode()) == Character.toLowerCase(c)) {
         JOptionPane.showMessageDialog(
             parentComponent,
             menu.getText(),
             TextUtils.getText("used_in_menu"),
             JOptionPane.WARNING_MESSAGE);
         return false;
       }
     }
   }
   if (!checkForOverwriteShortcut(menuBarNode, keystroke)) {
     return false;
   }
   final KeyStroke derivedKS = FreeplaneMenuBar.derive(keystroke, keyChar);
   if (derivedKS == keystroke) {
     return true;
   }
   return checkForOverwriteShortcut(menuBarNode, derivedKS);
 }
Beispiel #11
0
  public static boolean isDigraphStart(@NotNull KeyStroke key) {
    if ((key.getModifiers() & KeyEvent.CTRL_MASK) != 0) {
      if (key.getKeyCode() == KeyEvent.VK_K
          || key.getKeyCode() == KeyEvent.VK_V
          || key.getKeyCode() == KeyEvent.VK_Q) {
        return true;
      }
    }

    return false;
  }
    public List<KeyStroke> getAdditionalShortcuts(KeyStroke keystroke) {
        if (keystroke == null) {
            return null;
        }

        ArrayList<KeyStroke> ret = new ArrayList<KeyStroke>();
        Modifier mod1 = byPassDialog.getByPassDialogToggleModifier();
        if (mod1 != null) {
            ret.add(KeyStroke.getKeyStroke(keystroke.getKeyCode(), keystroke.getModifiers() | mod1.getModifier()));
        }

        Modifier mod2 = getDeleteFilesToggleModifier();
        if (mod2 != null) {
            ret.add(KeyStroke.getKeyStroke(keystroke.getKeyCode(), keystroke.getModifiers() | mod2.getModifier()));
        }

        if (mod2 != null && mod1 != null) {
            ret.add(KeyStroke.getKeyStroke(keystroke.getKeyCode(), keystroke.getModifiers() | mod2.getModifier() | mod1.getModifier()));
        }
        return ret;
    }
 private void pressKeyStrokeAndVerify(final int expectedKey) {
   assertThat(keyStroke.getModifiers()).as("no modifiers should be specified").isEqualTo(0);
   final KeyRecorder recorder = KeyRecorder.attachTo(textArea);
   pressInTextArea();
   pause(
       new Condition(concat("key with code ", expectedKey, " is pressed")) {
         @Override
         public boolean test() {
           return recorder.keysWerePressed(expectedKey);
         }
       },
       2000);
 }
Beispiel #14
0
  @NotNull
  public static String toKeyNotation(@NotNull KeyStroke key) {
    final char c = key.getKeyChar();
    final int keyCode = key.getKeyCode();
    final int modifiers = key.getModifiers();

    if (c != CHAR_UNDEFINED && !isControlCharacter(c)) {
      return String.valueOf(c);
    }

    String prefix = "";
    if ((modifiers & META_MASK) != 0) {
      prefix += "M-";
    }
    if ((modifiers & ALT_MASK) != 0) {
      prefix += "A-";
    }
    if ((modifiers & CTRL_MASK) != 0) {
      prefix += "C-";
    }
    if ((modifiers & SHIFT_MASK) != 0) {
      prefix += "S-";
    }

    String name = VIM_KEY_VALUES.get(keyCode);
    if (name != null) {
      if (UPPERCASE_DISPLAY_KEY_NAMES.contains(name)) {
        name = name.toUpperCase();
      } else {
        name = StringUtil.capitalize(name);
      }
    }
    if (name == null) {
      final String escape = toEscapeNotation(key);
      if (escape != null) {
        return escape;
      }

      try {
        name = String.valueOf(Character.toChars(keyCode));
      } catch (IllegalArgumentException ignored) {
      }
    }

    return name != null ? "<" + prefix + name + ">" : "<<" + key.toString() + ">>";
  }
Beispiel #15
0
    private boolean isPrintable(KeyStroke key, KeyEvent e) {
      // all control and alt keys are non-printable
      int modifiers = key.getModifiers();
      if (modifiers != 0 && modifiers != Event.SHIFT_MASK) return false;

      // action keys are non-printable
      if (e.isActionKey()) return false;

      // some action keys that the above function not recognises
      int keyCode = e.getKeyCode();
      if (keyCode == KeyEvent.VK_BACK_SPACE
          || keyCode == KeyEvent.VK_DELETE
          || keyCode == KeyEvent.VK_ENTER
          || keyCode == KeyEvent.VK_TAB
          || keyCode == KeyEvent.VK_ESCAPE) return false;

      // otherwise it's printable
      return true;
    }
  public static String getKeystrokeText(KeyStroke accelerator) {
    if (accelerator == null) return "";
    if (SystemInfo.isMac) {
      return MacKeymapUtil.getKeyStrokeText(accelerator);
    }
    String acceleratorText = "";
    int modifiers = accelerator.getModifiers();
    if (modifiers > 0) {
      acceleratorText = getModifiersText(modifiers);
    }

    final int code = accelerator.getKeyCode();
    String keyText = SystemInfo.isMac ? MacKeymapUtil.getKeyText(code) : KeyEvent.getKeyText(code);
    // [vova] this is dirty fix for bug #35092
    if (CANCEL_KEY_TEXT.equals(keyText)) {
      keyText = BREAK_KEY_TEXT;
    }

    acceleratorText += keyText;
    return acceleratorText.trim();
  }
  private int getMnemonicCharIndex(String text) {
    final int mnemonicIndex = myPresentation.getDisplayedMnemonicIndex();
    if (mnemonicIndex != -1) {
      return mnemonicIndex;
    }
    final ShortcutSet shortcutSet = myAction.getShortcutSet();
    final Shortcut[] shortcuts = shortcutSet.getShortcuts();
    for (Shortcut shortcut : shortcuts) {
      if (!(shortcut instanceof KeyboardShortcut)) continue;

      KeyboardShortcut keyboardShortcut = (KeyboardShortcut) shortcut;
      if (keyboardShortcut.getSecondKeyStroke()
          == null) { // we are interested only in "mnemonic-like" shortcuts
        final KeyStroke keyStroke = keyboardShortcut.getFirstKeyStroke();
        final int modifiers = keyStroke.getModifiers();
        if (BitUtil.isSet(modifiers, InputEvent.ALT_MASK)) {
          return (keyStroke.getKeyChar() != KeyEvent.CHAR_UNDEFINED)
              ? text.indexOf(keyStroke.getKeyChar())
              : text.indexOf(KeyEvent.getKeyText(keyStroke.getKeyCode()));
        }
      }
    }
    return -1;
  }
Beispiel #18
0
  /** Replaces ctrl- shortcuts with command- shortcuts for OSX */
  protected void replaceCtrlShortcutsWithMacShortcuts() {

    for (Object keyObj : UIManager.getLookAndFeelDefaults().keySet()) {
      String key = keyObj.toString();

      if (key.contains("InputMap")) {

        // System.out.println("MAP: " + key);
        Object val = UIManager.getLookAndFeelDefaults().get(key);

        if (val instanceof InputMapUIResource) {
          InputMapUIResource map = (InputMapUIResource) val;

          for (KeyStroke keyStroke : map.allKeys()) {

            int modifiers = keyStroke.getModifiers();

            if ((modifiers & KeyEvent.CTRL_MASK) > 0) {
              modifiers -= KeyEvent.CTRL_DOWN_MASK;
              modifiers -= KeyEvent.CTRL_MASK;
              modifiers += KeyEvent.META_DOWN_MASK + KeyEvent.META_MASK;

              KeyStroke k = KeyStroke.getKeyStroke(keyStroke.getKeyCode(), modifiers);

              Object mapVal = map.get(keyStroke);
              map.remove(keyStroke);
              map.put(k, mapVal);

              //                            System.out.println("Old: " + keyStroke);
              //                            System.out.println("New: " + k);
            }
          }
        }
      }
    }
  }
Beispiel #19
0
  /**
   * Return a good string representation of the given keystroke, since the toString method returns
   * more garbage than we want to see in a user interface.
   */
  public static String keyStrokeToString(KeyStroke key) {
    int modifiers = key.getModifiers();
    StringBuffer buffer = new StringBuffer();

    if ((modifiers & Event.SHIFT_MASK) == Event.SHIFT_MASK) {
      buffer.append("(Shift-");
      buffer.append(KeyEvent.getKeyText(key.getKeyCode()));
      buffer.append(")");
    }

    if ((modifiers & Event.CTRL_MASK) == Event.CTRL_MASK) {
      buffer.append("(Ctrl-");
      buffer.append(KeyEvent.getKeyText(key.getKeyCode()));
      buffer.append(")");
    }

    if ((modifiers & Event.META_MASK) == Event.META_MASK) {
      buffer.append("(Meta-");
      buffer.append(KeyEvent.getKeyText(key.getKeyCode()));
      buffer.append(")");
    }

    if ((modifiers & Event.ALT_MASK) == Event.ALT_MASK) {
      buffer.append("(Alt-");
      buffer.append(KeyEvent.getKeyText(key.getKeyCode()));
      buffer.append(")");
    }

    if (modifiers == 0) {
      buffer.append("(");
      buffer.append(KeyEvent.getKeyText(key.getKeyCode()));
      buffer.append(")");
    }

    return buffer.toString();
  }
  /**
   * This method is not being used to paint menu item since 6.0 This code left for compatibility
   * only. Do not use or override it, this will not cause any visible effect.
   */
  public static void paintMenuItem(
      Graphics g,
      JComponent c,
      Icon checkIcon,
      Icon arrowIcon,
      Color background,
      Color foreground,
      int defaultTextIconGap) {

    JMenuItem b = (JMenuItem) c;
    ButtonModel model = b.getModel();

    Dimension size = b.getSize();
    Insets i = c.getInsets();

    Rectangle viewRect = new Rectangle(size);

    viewRect.x += i.left;
    viewRect.y += i.top;
    viewRect.width -= (i.right + viewRect.x);
    viewRect.height -= (i.bottom + viewRect.y);

    Rectangle iconRect = new Rectangle();
    Rectangle textRect = new Rectangle();
    Rectangle acceleratorRect = new Rectangle();
    Rectangle checkRect = new Rectangle();
    Rectangle arrowRect = new Rectangle();

    Font holdf = g.getFont();
    Font f = c.getFont();
    g.setFont(f);
    FontMetrics fm = SwingUtilities2.getFontMetrics(c, g, f);
    FontMetrics fmAccel =
        SwingUtilities2.getFontMetrics(c, g, UIManager.getFont("MenuItem.acceleratorFont"));

    if (c.isOpaque()) {
      if (model.isArmed() || (c instanceof JMenu && model.isSelected())) {
        g.setColor(background);
      } else {
        g.setColor(c.getBackground());
      }
      g.fillRect(0, 0, size.width, size.height);
    }

    // get Accelerator text
    KeyStroke accelerator = b.getAccelerator();
    String acceleratorText = "";
    if (accelerator != null) {
      int modifiers = accelerator.getModifiers();
      if (modifiers > 0) {
        acceleratorText = KeyEvent.getKeyModifiersText(modifiers);
        acceleratorText += "+";
      }
      acceleratorText += KeyEvent.getKeyText(accelerator.getKeyCode());
    }

    // layout the text and icon
    String text =
        layoutMenuItem(
            c,
            fm,
            b.getText(),
            fmAccel,
            acceleratorText,
            b.getIcon(),
            checkIcon,
            arrowIcon,
            b.getVerticalAlignment(),
            b.getHorizontalAlignment(),
            b.getVerticalTextPosition(),
            b.getHorizontalTextPosition(),
            viewRect,
            iconRect,
            textRect,
            acceleratorRect,
            checkRect,
            arrowRect,
            b.getText() == null ? 0 : defaultTextIconGap,
            defaultTextIconGap);

    // Paint the Check
    Color holdc = g.getColor();
    if (checkIcon != null) {
      if (model.isArmed() || (c instanceof JMenu && model.isSelected())) g.setColor(foreground);
      checkIcon.paintIcon(c, g, checkRect.x, checkRect.y);
      g.setColor(holdc);
    }

    // Paint the Icon
    if (b.getIcon() != null) {
      Icon icon;
      if (!model.isEnabled()) {
        icon = (Icon) b.getDisabledIcon();
      } else if (model.isPressed() && model.isArmed()) {
        icon = (Icon) b.getPressedIcon();
        if (icon == null) {
          // Use default icon
          icon = (Icon) b.getIcon();
        }
      } else {
        icon = (Icon) b.getIcon();
      }

      if (icon != null) {
        icon.paintIcon(c, g, iconRect.x, iconRect.y);
      }
    }

    // Draw the Text
    if (text != null && !text.equals("")) {
      // Once BasicHTML becomes public, use BasicHTML.propertyKey
      // instead of the hardcoded string below!
      View v = (View) c.getClientProperty("html");
      if (v != null) {
        v.paint(g, textRect);
      } else {
        int mnemIndex = b.getDisplayedMnemonicIndex();

        if (!model.isEnabled()) {
          // *** paint the text disabled
          g.setColor(b.getBackground().brighter());
          SwingUtilities2.drawStringUnderlineCharAt(
              b, g, text, mnemIndex, textRect.x, textRect.y + fmAccel.getAscent());
          g.setColor(b.getBackground().darker());
          SwingUtilities2.drawStringUnderlineCharAt(
              b, g, text, mnemIndex, textRect.x - 1, textRect.y + fmAccel.getAscent() - 1);

        } else {
          // *** paint the text normally
          if (model.isArmed() || (c instanceof JMenu && model.isSelected())) {
            g.setColor(foreground);
          } else {
            g.setColor(b.getForeground());
          }
          SwingUtilities2.drawStringUnderlineCharAt(
              b, g, text, mnemIndex, textRect.x, textRect.y + fm.getAscent());
        }
      }
    }

    // Draw the Accelerator Text
    if (acceleratorText != null && !acceleratorText.equals("")) {

      // Get the maxAccWidth from the parent to calculate the offset.
      int accOffset = 0;
      Container parent = b.getParent();
      if (parent != null && parent instanceof JComponent) {
        JComponent p = (JComponent) parent;
        Integer maxValueInt = (Integer) p.getClientProperty(MotifGraphicsUtils.MAX_ACC_WIDTH);
        int maxValue = maxValueInt != null ? maxValueInt.intValue() : acceleratorRect.width;

        // Calculate the offset, with which the accelerator texts will be drawn with.
        accOffset = maxValue - acceleratorRect.width;
      }

      g.setFont(UIManager.getFont("MenuItem.acceleratorFont"));
      if (!model.isEnabled()) {
        // *** paint the acceleratorText disabled
        g.setColor(b.getBackground().brighter());
        SwingUtilities2.drawString(
            c,
            g,
            acceleratorText,
            acceleratorRect.x - accOffset,
            acceleratorRect.y + fm.getAscent());
        g.setColor(b.getBackground().darker());
        SwingUtilities2.drawString(
            c,
            g,
            acceleratorText,
            acceleratorRect.x - accOffset - 1,
            acceleratorRect.y + fm.getAscent() - 1);
      } else {
        // *** paint the acceleratorText normally
        if (model.isArmed() || (c instanceof JMenu && model.isSelected())) {
          g.setColor(foreground);
        } else {
          g.setColor(b.getForeground());
        }
        SwingUtilities2.drawString(
            c,
            g,
            acceleratorText,
            acceleratorRect.x - accOffset,
            acceleratorRect.y + fmAccel.getAscent());
      }
    }

    // Paint the Arrow
    if (arrowIcon != null) {
      if (model.isArmed() || (c instanceof JMenu && model.isSelected())) g.setColor(foreground);
      if (!(b.getParent() instanceof JMenuBar)) arrowIcon.paintIcon(c, g, arrowRect.x, arrowRect.y);
    }

    g.setColor(holdc);
    g.setFont(holdf);
  }
 private static boolean isEnterKeyStroke(KeyStroke keyStroke) {
   return keyStroke.getKeyCode() == KeyEvent.VK_ENTER && keyStroke.getModifiers() == 0;
 }
  protected ActionCallback _execute(final PlaybackContext context) {
    final String actionName = getText().substring(PREFIX.length()).trim();

    final ActionManager am = ActionManager.getInstance();
    final AnAction targetAction = am.getAction(actionName);
    if (targetAction == null) {
      dumpError(context, "Unknown action: " + actionName);
      return new ActionCallback.Rejected();
    }

    if (!context.isUseDirectActionCall()) {
      final Shortcut[] sc = KeymapManager.getInstance().getActiveKeymap().getShortcuts(actionName);
      KeyStroke stroke = null;
      for (Shortcut each : sc) {
        if (each instanceof KeyboardShortcut) {
          final KeyboardShortcut ks = (KeyboardShortcut) each;
          final KeyStroke first = ks.getFirstKeyStroke();
          final KeyStroke second = ks.getSecondKeyStroke();
          if (first != null && second == null) {
            stroke = KeyStroke.getKeyStroke(first.getKeyCode(), first.getModifiers(), false);
            break;
          }
        }
      }

      if (stroke != null) {
        final ActionCallback result =
            new TimedOutCallback(
                Registry.intValue("actionSystem.commandProcessingTimeout"),
                "Timed out calling action id=" + actionName,
                new Throwable(),
                true) {
              @Override
              protected void dumpError() {
                context.error(getMessage(), getLine());
              }
            };
        context.message("Invoking action via shortcut: " + stroke.toString(), getLine());

        final KeyStroke finalStroke = stroke;

        IdeFocusManager.getGlobalInstance()
            .doWhenFocusSettlesDown(
                new Runnable() {
                  @Override
                  public void run() {
                    final Ref<AnActionListener> listener = new Ref<AnActionListener>();
                    listener.set(
                        new AnActionListener.Adapter() {

                          @Override
                          public void beforeActionPerformed(
                              final AnAction action, DataContext dataContext, AnActionEvent event) {
                            SwingUtilities.invokeLater(
                                new Runnable() {
                                  @Override
                                  public void run() {
                                    if (context.isDisposed()) {
                                      am.removeAnActionListener(listener.get());
                                      return;
                                    }

                                    if (targetAction.equals(action)) {
                                      context.message(
                                          "Performed action: " + actionName,
                                          context.getCurrentLine());
                                      am.removeAnActionListener(listener.get());
                                      result.setDone();
                                    }
                                  }
                                });
                          }
                        });
                    am.addAnActionListener(listener.get());

                    context.runPooledThread(
                        new Runnable() {
                          @Override
                          public void run() {
                            type(context.getRobot(), finalStroke);
                          }
                        });
                  }
                });

        return result;
      }
    }

    final InputEvent input = getInputEvent(actionName);

    final ActionCallback result = new ActionCallback();

    context.getRobot().delay(Registry.intValue("actionSystem.playback.delay"));
    SwingUtilities.invokeLater(
        new Runnable() {
          public void run() {
            am.tryToExecute(targetAction, input, null, null, false)
                .doWhenProcessed(result.createSetDoneRunnable());
          }
        });

    return result;
  }
  /**
   * This method fills <code>myActions</code> list.
   *
   * @return true if there is a shortcut with second stroke found.
   */
  public KeyProcessorContext updateCurrentContext(
      Component component, Shortcut sc, boolean isModalContext) {
    myContext.setFoundComponent(null);
    myContext.getActions().clear();

    if (isControlEnterOnDialog(component, sc)) return myContext;

    boolean hasSecondStroke = false;

    // here we try to find "local" shortcuts

    for (; component != null; component = component.getParent()) {
      if (!(component instanceof JComponent)) {
        continue;
      }
      ArrayList listOfActions =
          (ArrayList) ((JComponent) component).getClientProperty(AnAction.ourClientProperty);
      if (listOfActions == null) {
        continue;
      }
      for (Object listOfAction : listOfActions) {
        if (!(listOfAction instanceof AnAction)) {
          continue;
        }
        AnAction action = (AnAction) listOfAction;
        hasSecondStroke |= addAction(action, sc);
      }
      // once we've found a proper local shortcut(s), we continue with non-local shortcuts
      if (!myContext.getActions().isEmpty()) {
        myContext.setFoundComponent((JComponent) component);
        break;
      }
    }

    // search in main keymap

    Keymap keymap = KeymapManager.getInstance().getActiveKeymap();
    String[] actionIds = keymap.getActionIds(sc);

    ActionManager actionManager = ActionManager.getInstance();
    for (String actionId : actionIds) {
      AnAction action = actionManager.getAction(actionId);
      if (action != null) {
        if (isModalContext && !action.isEnabledInModalContext()) {
          continue;
        }
        hasSecondStroke |= addAction(action, sc);
      }
    }

    if (!hasSecondStroke && sc instanceof KeyboardShortcut) {
      // little trick to invoke action which second stroke is a key w/o modifiers, but user still
      // holds the modifier key(s) of the first stroke

      final KeyboardShortcut keyboardShortcut = (KeyboardShortcut) sc;
      final KeyStroke firstKeyStroke = keyboardShortcut.getFirstKeyStroke();
      final KeyStroke secondKeyStroke = keyboardShortcut.getSecondKeyStroke();

      if (secondKeyStroke != null
          && secondKeyStroke.getModifiers() != 0
          && firstKeyStroke.getModifiers() != 0) {
        final KeyboardShortcut altShortCut =
            new KeyboardShortcut(
                firstKeyStroke, KeyStroke.getKeyStroke(secondKeyStroke.getKeyCode(), 0));
        final String[] additionalActions = keymap.getActionIds(altShortCut);

        for (final String actionId : additionalActions) {
          AnAction action = actionManager.getAction(actionId);
          if (action != null) {
            if (isModalContext && !action.isEnabledInModalContext()) {
              continue;
            }
            hasSecondStroke |= addAction(action, altShortCut);
          }
        }
      }
    }

    myContext.setHasSecondStroke(hasSecondStroke);

    Comparator<? super AnAction> comparator =
        PlatformDataKeys.ACTIONS_SORTER.getData(myContext.getDataContext());
    if (comparator != null) {
      Collections.sort(myContext.getActions(), comparator);
    }

    return myContext;
  }
 private void pressInTextArea() {
   driver.pressAndReleaseKey(
       textArea, keyStroke.getKeyCode(), new int[] {keyStroke.getModifiers()});
 }
Beispiel #25
0
 public static String formatKeyStroke(final KeyStroke keyStroke) {
   final String keyModifiersText = KeyEvent.getKeyModifiersText(keyStroke.getModifiers());
   final String keyText = KeyEvent.getKeyText(keyStroke.getKeyCode());
   return keyModifiersText.length() == 0 ? keyText : keyModifiersText + "+" + keyText;
 }
Beispiel #26
0
 public void registerKeyStroke(KeyStroke keyStroke, String description) {
   String modifiersText = KeyEvent.getKeyModifiersText(keyStroke.getModifiers());
   String keyText = KeyEvent.getKeyText(keyStroke.getKeyCode());
   keyboardEntries.add(
       (modifiersText.length() != 0 ? modifiersText + " " : "") + keyText + ": " + description);
 }
Beispiel #27
0
  @NotNull
  public DigraphResult processKey(@NotNull KeyStroke key, @NotNull Editor editor) {
    switch (digraphState) {
      case DIG_STATE_START:
        logger.debug("DIG_STATE_START");
        if (key.getKeyCode() == KeyEvent.VK_K && (key.getModifiers() & KeyEvent.CTRL_MASK) != 0) {
          logger.debug("found Ctrl-K");
          digraphState = DIG_STATE_DIG_ONE;
          return DigraphResult.OK;
        } else if ((key.getKeyCode() == KeyEvent.VK_V || key.getKeyCode() == KeyEvent.VK_Q)
            && (key.getModifiers() & KeyEvent.CTRL_MASK) != 0) {
          logger.debug("found Ctrl-V");
          digraphState = DIG_STATE_CODE_START;
          codeChars = new char[8];
          codeCnt = 0;
          return DigraphResult.OK;
        } else {
          return new DigraphResult(key);
        }
      case DIG_STATE_DIG_ONE:
        logger.debug("DIG_STATE_DIG_ONE");
        if (key.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
          digraphChar = key.getKeyChar();
          digraphState = DIG_STATE_DIG_TWO;

          return DigraphResult.OK;
        } else {
          digraphState = DIG_STATE_START;
          return DigraphResult.BAD;
        }
      case DIG_STATE_DIG_TWO:
        logger.debug("DIG_STATE_DIG_TWO");
        digraphState = DIG_STATE_START;
        if (key.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
          char ch = VimPlugin.getDigraph().getDigraph(digraphChar, key.getKeyChar());

          return new DigraphResult(KeyStroke.getKeyStroke(ch));
        }

        return DigraphResult.BAD;
      case DIG_STATE_CODE_START:
        logger.debug("DIG_STATE_CODE_START");
        switch (key.getKeyChar()) {
          case 'o':
          case 'O':
            codeMax = 3;
            digraphState = DIG_STATE_CODE_CHAR;
            codeType = 8;
            logger.debug("Octal");
            return DigraphResult.OK;
          case 'x':
          case 'X':
            codeMax = 2;
            digraphState = DIG_STATE_CODE_CHAR;
            codeType = 16;
            logger.debug("hex2");
            return DigraphResult.OK;
          case 'u':
            codeMax = 4;
            digraphState = DIG_STATE_CODE_CHAR;
            codeType = 16;
            logger.debug("hex4");
            return DigraphResult.OK;
          case 'U':
            codeMax = 8;
            digraphState = DIG_STATE_CODE_CHAR;
            codeType = 16;
            logger.debug("hex8");
            return DigraphResult.OK;
          case '0':
          case '1':
          case '2':
          case '3':
          case '4':
          case '5':
          case '6':
          case '7':
          case '8':
          case '9':
            codeMax = 3;
            digraphState = DIG_STATE_CODE_CHAR;
            codeType = 10;
            codeChars[codeCnt++] = key.getKeyChar();
            logger.debug("decimal");
            return DigraphResult.OK;
          default:
            switch (key.getKeyCode()) {
              case KeyEvent.VK_TAB:
                KeyStroke code = KeyStroke.getKeyStroke('\t');
                digraphState = DIG_STATE_START;

                return new DigraphResult(code);
              default:
                logger.debug("unknown");
                digraphState = DIG_STATE_START;

                return new DigraphResult(key);
            }
        }
      case DIG_STATE_CODE_CHAR:
        logger.debug("DIG_STATE_CODE_CHAR");
        boolean valid = false;
        switch (codeType) {
          case 10:
            if (key.getKeyChar() >= '0' && key.getKeyChar() <= '9') {
              valid = true;
            }
            break;
          case 8:
            if (key.getKeyChar() >= '0' && key.getKeyChar() <= '7') {
              valid = true;
            }
            break;
          case 16:
            if (key.getKeyChar() >= '0' && key.getKeyChar() <= '9'
                || key.getKeyChar() >= 'a' && key.getKeyChar() <= 'f'
                || key.getKeyChar() >= 'A' && key.getKeyChar() <= 'F') {
              valid = true;
            }
            break;
        }
        if (valid) {
          logger.debug("valid");
          codeChars[codeCnt++] = key.getKeyChar();
          if (codeCnt == codeMax) {
            String digits = new String(codeChars, 0, codeCnt);
            int val = Integer.parseInt(digits, codeType);
            KeyStroke code = KeyStroke.getKeyStroke((char) val);
            digraphState = DIG_STATE_START;

            return new DigraphResult(code);
          } else {
            return DigraphResult.OK;
          }
        } else if (codeCnt > 0) {
          logger.debug("invalid");
          String digits = new String(codeChars, 0, codeCnt);
          int val = Integer.parseInt(digits, codeType);
          digraphState = DIG_STATE_START;
          KeyStroke code = KeyStroke.getKeyStroke((char) val);

          VimPlugin.getMacro().postKey(key, editor);

          return new DigraphResult(code);
        } else {
          return DigraphResult.BAD;
        }
      default:
        return DigraphResult.BAD;
    }
  }
Beispiel #28
0
 private static boolean isControlKeyCode(@NotNull KeyStroke key) {
   return key.getKeyChar() == CHAR_UNDEFINED && key.getKeyCode() < 0x20 && key.getModifiers() == 0;
 }