Example usage for com.intellij.openapi.keymap KeymapManager getInstance

List of usage examples for com.intellij.openapi.keymap KeymapManager getInstance

Introduction

In this page you can find the example usage for com.intellij.openapi.keymap KeymapManager getInstance.

Prototype

public static KeymapManager getInstance() 

Source Link

Usage

From source file:automation.commands.ActionCommand.java

License:Apache License

private static InputEvent getInputEvent(String actionName) {
    final Shortcut[] shortcuts = KeymapManager.getInstance().getActiveKeymap().getShortcuts(actionName);
    KeyStroke keyStroke = null;/* ww w  . j  av a  2  s .c o m*/
    for (Shortcut each : shortcuts) {
        if (each instanceof KeyboardShortcut) {
            keyStroke = ((KeyboardShortcut) each).getFirstKeyStroke();
            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);
    }
}

From source file:com.android.tools.idea.AndroidInitialConfigurator.java

License:Apache License

private static void setDefaultMacKeymap() {
    KeymapManagerImpl instance = (KeymapManagerImpl) KeymapManager.getInstance();
    Keymap mac105Keymap = getMac105Keymap();
    if (mac105Keymap != null) {
        instance.setActiveKeymap(mac105Keymap);
    }/*from  w  w w .ja  v a 2s.  c om*/
}

From source file:com.android.tools.idea.fd.actions.HotswapAction.java

License:Apache License

@Nullable
private static String getShortcutText() {
    Keymap activeKeymap = KeymapManager.getInstance().getActiveKeymap();
    Shortcut[] shortcuts = activeKeymap.getShortcuts("Android.HotswapChanges");
    return shortcuts.length > 0 ? " (" + KeymapUtil.getShortcutText(shortcuts[0]) + ") " : "";
}

From source file:com.android.tools.idea.tests.gui.framework.fixture.EditorFixture.java

License:Apache License

/** Invokes {@code editorAction} via its (first) keyboard shortcut in the active keymap. */
public EditorFixture invokeAction(@NotNull EditorAction editorAction) {
    AnAction anAction = ActionManager.getInstance().getAction(editorAction.id);
    assertTrue(editorAction.id + " is not enabled", anAction.getTemplatePresentation().isEnabled());

    Keymap keymap = KeymapManager.getInstance().getActiveKeymap();
    Shortcut shortcut = keymap.getShortcuts(editorAction.id)[0];
    if (shortcut instanceof KeyboardShortcut) {
        KeyboardShortcut cs = (KeyboardShortcut) shortcut;
        KeyStroke firstKeyStroke = cs.getFirstKeyStroke();
        Component component = getFocusedEditor();
        if (component != null) {
            ComponentDriver driver = new ComponentDriver(robot);
            driver.pressAndReleaseKey(component, firstKeyStroke.getKeyCode(),
                    new int[] { firstKeyStroke.getModifiers() });
            KeyStroke secondKeyStroke = cs.getSecondKeyStroke();
            if (secondKeyStroke != null) {
                driver.pressAndReleaseKey(component, secondKeyStroke.getKeyCode(),
                        new int[] { secondKeyStroke.getModifiers() });
            }//from w  ww.j  a  va  2s .c  om
        } else {
            fail("Editor not focused for action");
        }
    } else {
        fail("Unsupported shortcut type " + shortcut.getClass().getName());
    }
    return this;
}

From source file:com.favorite.FavoriteBaseShowRecentFilesAction.java

License:Apache License

protected void show(final Project project) {
    final DefaultListModel model = new DefaultListModel();

    FileEditorManagerEx fem = FileEditorManagerEx.getInstanceEx(project);
    VirtualFile[] selectedFiles = fem.getSelectedFiles();
    VirtualFile currentFile = fem.getCurrentFile();
    VirtualFile[] files = filesToShow(project);
    FileEditorProviderManager editorProviderManager = FileEditorProviderManager.getInstance();

    for (int i = files.length - 1; i >= 0; i--) { // reverse order of files
        VirtualFile file = files[i];/*from w  ww  . ja v  a2 s  .c  o m*/
        boolean isSelected = ArrayUtil.find(selectedFiles, file) >= 0;
        boolean equals = file.equals(currentFile);
        FileEditorProvider[] providers = editorProviderManager.getProviders(project, file);
        boolean length = true;//providers.length > 0;
        if ((!isSelected || !equals) && length) {
            // 1. do not put currently selected file
            // 2. do not include file with no corresponding editor providers
            model.addElement(file);
        }
    }

    final JLabel pathLabel = new JLabel(" ");
    pathLabel.setHorizontalAlignment(SwingConstants.RIGHT);

    if (true /*SystemInfo.isMac*/) {
        final Font font = pathLabel.getFont();
        pathLabel.setFont(font.deriveFont((float) 10));
    }

    final JList list = new JBList(model);
    list.addKeyListener(new KeyAdapter() {
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_DELETE) {
                int index = list.getSelectedIndex();
                if (index == -1 || index >= list.getModel().getSize()) {
                    return;
                }
                Object[] values = list.getSelectedValues();
                for (Object value : values) {
                    VirtualFile file = (VirtualFile) value;
                    model.removeElement(file);
                    if (model.getSize() > 0) {
                        if (model.getSize() == index) {
                            list.setSelectedIndex(model.getSize() - 1);
                        } else if (model.getSize() > index) {
                            list.setSelectedIndex(index);
                        }
                    } else {
                        list.clearSelection();
                    }
                    EditorHistoryManager.getInstance(project).removeFile(file);
                }
            }
        }
    });

    final MyListSelectionListener listSelectionListener = new MyListSelectionListener(pathLabel, list);
    list.getSelectionModel().addListSelectionListener(listSelectionListener);

    Runnable runnable = new Runnable() {
        public void run() {
            Object[] values = list.getSelectedValues();
            for (Object value : values) {
                VirtualFile file = (VirtualFile) value;
                if (file.isDirectory()) {
                    String path = file.getPath();
                    ProjectUtil.openOrImport(path, project, false);
                } else {
                    FileEditorManager.getInstance(project).openFile(file, true, true);
                }
                //String path = "/Volumes/SHARE/MacSystem/Home/Users/djzhang/NetBeansProjects/intellijPlugDemo";
                //String path = file.getPath();
                //final Project project = PlatformDataKeys.PROJECT.getData(e.getDataContext());

                //FileEditorManager.getInstance(project).openFile(file, true, true);

                //OpenFileAction.openFile(file, project);

            }
        }
    };

    if (list.getModel().getSize() == 0) {
        list.clearSelection();
    }

    list.setCellRenderer(new RecentFilesRenderer(project));

    /*
    TODO:
    if (model.getSize() > 0) {
      Dimension listPreferredSize = list.getPreferredSize();
      list.setVisibleRowCount(0);
      Dimension viewPreferredSize = new Dimension(listPreferredSize.width, Math.min(listPreferredSize.height, r.height - 20));
      ((JViewport)list.getParent()).setPreferredSize(viewPreferredSize);
    }
    */

    JPanel footerPanel = new JPanel(new BorderLayout()) {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(BORDER_COLOR);
            g.drawLine(0, 0, getWidth(), 0);
        }
    };

    footerPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
    footerPanel.add(pathLabel);

    final PopupChooserBuilder builder = new PopupChooserBuilder(list).setTitle(getTitle()).setAdText(" ")
            .setMovable(true).setItemChoosenCallback(runnable).setModalContext(false)
            .addAdditionalChooseKeystroke(getAdditionalSelectKeystroke())
            .setFilteringEnabled(new Function<Object, String>() {
                public String fun(Object o) {
                    return o instanceof VirtualFile ? ((VirtualFile) o).getName() : "";
                }
            });
    final Shortcut[] shortcuts = KeymapManager.getInstance().getActiveKeymap().getShortcuts(getPeerActionId());
    final PeerListener listener = new PeerListener(project, getPeerActionId());
    for (Shortcut shortcut : shortcuts) {
        if (shortcut instanceof KeyboardShortcut
                && ((KeyboardShortcut) shortcut).getSecondKeyStroke() == null) {
            final KeyStroke keyStroke = ((KeyboardShortcut) shortcut).getFirstKeyStroke();
            builder.registerKeyboardAction(keyStroke, listener);
        }
    }
    JBPopup popup = builder.createPopup();
    listener.setPopup(popup);
    listSelectionListener.setPopup(popup);
    popup.showCenteredInCurrentWindow(project);
}

From source file:com.favorite.FavoriteBaseShowRecentFilesAction.java

License:Apache License

private static KeyStroke getAdditionalSelectKeystroke() {
    Shortcut[] shortcuts = KeymapManager.getInstance().getActiveKeymap()
            .getShortcuts(IdeActions.ACTION_EDIT_SOURCE);
    for (Shortcut shortcut : shortcuts) {
        if (shortcut instanceof KeyboardShortcut) {
            return ((KeyboardShortcut) shortcut).getFirstKeyStroke();
        }//from  ww w .  j av  a  2 s .c  o m
    }
    return null;
}

From source file:com.headwire.aem.tooling.intellij.explorer.AbstractSlingServerNodeDescriptor.java

License:Apache License

public static boolean addShortcutText(String actionId, CompositeAppearance appearance) {
    Keymap activeKeymap = KeymapManager.getInstance().getActiveKeymap();
    Shortcut[] shortcuts = activeKeymap.getShortcuts(actionId);
    if (shortcuts != null && shortcuts.length > 0) {
        appearance.getEnding().addText(" (" + KeymapUtil.getShortcutText(shortcuts[0]) + ")",
                SimpleTextAttributes.GRAY_ATTRIBUTES);
        return true;
    } else/*from   ww w .j  ava 2  s . c o m*/
        return false;
}

From source file:com.hp.alm.ali.idea.action.UndoAction.java

License:Apache License

public static void installUndoRedoSupport(JTextComponent textComponent) {
    final UndoManager undoManager = new UndoManager();
    Keymap keymap = KeymapManager.getInstance().getActiveKeymap();
    new UndoAction(undoManager).registerCustomShortcutSet(new CustomShortcutSet(keymap.getShortcuts("$Undo")),
            textComponent);/*  ww  w.  j a va  2  s. c o m*/
    new RedoAction(undoManager).registerCustomShortcutSet(new CustomShortcutSet(keymap.getShortcuts("$Redo")),
            textComponent);
    textComponent.getDocument().addUndoableEditListener(new UndoableEditListener() {
        public void undoableEditHappened(UndoableEditEvent event) {
            undoManager.addEdit(event.getEdit());
        }
    });
}

From source file:com.hp.alm.ali.idea.ui.dialog.RestErrorDetailDialog.java

License:Apache License

public RestErrorDetailDialog(Project project, Exception exception) {
    super(project, "Error Detail", true, true, Arrays.asList(Button.Close));

    final JPanel areaPanel = new JPanel(new BorderLayout());
    areaPanel.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(20, 20, 20, 20),
            BorderFactory.createEtchedBorder()));
    JTextPane area = new JTextPane();
    area.setCaret(new NonAdjustingCaret());
    boolean showArea = true;

    if (exception.getMessage().startsWith("<?xml ") || exception.getMessage().startsWith("<QCRestException>")) {
        Matcher matcher = Pattern.compile("^.*?<Title>(.*?)</Title>.*", Pattern.MULTILINE | Pattern.DOTALL)
                .matcher(exception.getMessage());
        if (matcher.matches()) {
            JPanel labelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
            labelPanel.setBorder(new EmptyBorder(10, 15, 5, 15));
            JLabel label = new JLabel(matcher.group(1));
            // message can be very long, make sure we fit into the dialog
            Dimension size = label.getPreferredSize();
            size.width = Math.min(700, size.width);
            label.setPreferredSize(size);
            labelPanel.add(label);//from w  w  w  .j av  a2s .  co m
            labelPanel.add(new LinkLabel("(detail)", null, new LinkListener() {
                public void linkSelected(LinkLabel aSource, Object aLinkData) {
                    if (areaPanel.getParent() == null) {
                        getContentPane().add(areaPanel, BorderLayout.CENTER);
                    } else {
                        getContentPane().remove(areaPanel);
                    }
                    packAndCenter(800, 600, false);
                }
            }));
            getContentPane().add(labelPanel, BorderLayout.NORTH);
            showArea = false;
            // adjust the area panel border to reflect our own
            areaPanel.setBorder(BorderFactory.createCompoundBorder(new EmptyBorder(5, 20, 20, 20),
                    BorderFactory.createEtchedBorder()));
        }
    } else {
        // assume ALM 12 HTML format
        area.setEditorKit(new HTMLEditorKit());
        if (exception instanceof RestException) {
            try {
                ((HTMLDocument) area.getDocument()).setBase(new URL(((RestException) exception).getLocation()));
            } catch (MalformedURLException mfe) {
                // formatting will be broken
            }
        }
    }

    area.setEditable(false);
    area.setText(exception.getMessage());
    JBScrollPane scrollPane = new JBScrollPane(area);
    areaPanel.add(scrollPane, BorderLayout.CENTER);
    if (showArea) {
        getContentPane().add(areaPanel, BorderLayout.CENTER);
    }

    getRootPane().setDefaultButton(getButton(Button.Close));
    // although close is default button, hitting enter doesn't close the dialog when JTextPane holds the focus - override this
    new AnAction() {
        public void actionPerformed(AnActionEvent e) {
            buttonPerformed(Button.Close);
        }
    }.registerCustomShortcutSet(
            new CustomShortcutSet(
                    KeymapManager.getInstance().getActiveKeymap().getShortcuts(IdeActions.ACTION_EDITOR_ENTER)),
            area);

    pack();
    Dimension size = getSize();
    size.width = Math.min(800, size.width);
    size.height = Math.min(600, size.height);
    setSize(size);
    centerOnOwner();
}

From source file:com.hp.alm.ali.idea.ui.editor.field.HTMLAreaField.java

License:Apache License

public static void enableCapability(final JTextPane desc, Project project, String value, boolean editable,
        boolean navigation) {
    value = removeSmallFont(value);//from w  ww.j  av  a  2  s . c  o  m
    HTMLEditorKit kit = new HTMLLetterWrappingEditorKit();
    desc.setEditorKit(kit);
    desc.setDocument(kit.createDefaultDocument());
    if (!editable && navigation) {
        value = NavigationDecorator.explodeHtml(project, value);
    }
    desc.setText(value);
    if (!editable) {
        desc.setCaret(new NonAdjustingCaret());
    }
    desc.addCaretListener(new BodyLimitCaretListener(desc));
    if (editable) {
        String element = checkElements(desc.getDocument().getDefaultRootElement());
        if (element != null) {
            desc.setToolTipText("Found unsupported element '" + element + "', editing is disabled.");
            editable = false;
        }
    }
    desc.setEditable(editable);

    if (editable && SpellCheckerManager.isAvailable()
            && ApplicationManager.getApplication().getComponent(AliConfiguration.class).spellChecker) {
        desc.getDocument().addDocumentListener(new SpellCheckDocumentListener(project, desc));
    }

    Font font = UIManager.getFont("Label.font");
    String bodyRule = "body { font-family: " + font.getFamily() + "; " + "font-size: " + font.getSize()
            + "pt; }";
    ((HTMLDocument) desc.getDocument()).getStyleSheet().addRule(bodyRule);

    // AGM uses plain "p" to create lines, we need to avoid excessive spacing this by default creates
    String paragraphRule = "p { margin-top: 0px; }";
    ((HTMLDocument) desc.getDocument()).getStyleSheet().addRule(paragraphRule);

    Keymap keymap = KeymapManager.getInstance().getActiveKeymap();
    new AnAction() {
        public void actionPerformed(AnActionEvent e) {
            // following is needed to make copy work in the IDE
            try {
                StringSelection selection = new StringSelection(desc.getText(desc.getSelectionStart(),
                        desc.getSelectionEnd() - desc.getSelectionStart()));
                CopyPasteManager.getInstance().setContents(selection);
            } catch (Exception ex) {
                // no clipboard, so what
            }
        }
    }.registerCustomShortcutSet(new CustomShortcutSet(keymap.getShortcuts(IdeActions.ACTION_COPY)), desc);
    new AnAction() {
        public void actionPerformed(AnActionEvent e) {
            // avoid pasting non-supported HTML markup by always converting to plain text
            Transferable contents = CopyPasteManager.getInstance().getContents();
            try {
                desc.getActionMap().get(DefaultEditorKit.cutAction).actionPerformed(null);
                desc.getDocument().insertString(desc.getSelectionStart(),
                        (String) contents.getTransferData(DataFlavor.stringFlavor), null);
            } catch (Exception ex) {
                // no clipboard, so what
            }
        }
    }.registerCustomShortcutSet(new CustomShortcutSet(keymap.getShortcuts(IdeActions.ACTION_PASTE)), desc);
    installNavigationShortCuts(desc);

}