Example usage for com.intellij.openapi.ui JBMenuItem JBMenuItem

List of usage examples for com.intellij.openapi.ui JBMenuItem JBMenuItem

Introduction

In this page you can find the example usage for com.intellij.openapi.ui JBMenuItem JBMenuItem.

Prototype

public JBMenuItem(Action a) 

Source Link

Usage

From source file:com.android.tools.idea.avdmanager.AvdActionPanel.java

License:Apache License

public AvdActionPanel(@NotNull AvdInfo avdInfo, int numVisibleActions, AvdRefreshProvider refreshProvider) {
    myRefreshProvider = refreshProvider;
    setOpaque(true);/* w w  w  .ja  v  a  2 s  . c  o m*/
    setBorder(IdeBorderFactory.createEmptyBorder(10, 10, 10, 10));
    myAvdInfo = avdInfo;
    List<AvdUiAction> actions = getActions();
    setLayout(new FlowLayout(FlowLayout.RIGHT, 3, 0));
    int visibleActionCount = 0;
    boolean errorState = false;
    if (avdInfo.getStatus() != AvdInfo.AvdStatus.OK) {
        AvdUiAction action = null;
        if (AvdManagerConnection.isSystemImageDownloadProblem(avdInfo.getStatus())) {
            action = new InstallSystemImageAction(this);
        } else if (AvdManagerConnection.isAvdRepairable(avdInfo.getStatus())) {
            action = new RepairAvdAction(this);
        }
        if (action != null) {
            FocusableHyperlinkLabel repairAction = new FocusableHyperlinkLabel(action.getText(),
                    action.getIcon());
            add(repairAction);
            repairAction.addHyperlinkListener(action);
            myVisibleComponents.add(repairAction);
        } else {
            add(new JBLabel("Failed to load", AllIcons.General.BalloonError, SwingConstants.LEADING));
        }
        numVisibleActions = 0;
        errorState = true;
    }

    for (AvdUiAction action : actions) {
        JComponent actionLabel;
        // Add extra items to the overflow menu
        if (errorState || numVisibleActions != -1 && visibleActionCount >= numVisibleActions) {
            JBMenuItem menuItem = new JBMenuItem(action);
            myOverflowMenu.add(menuItem);
            actionLabel = menuItem;
        } else {
            // Add visible items to the panel
            actionLabel = new FocusableHyperlinkLabel("", action.getIcon());
            ((FocusableHyperlinkLabel) actionLabel).addHyperlinkListener(action);
            add(actionLabel);
            myVisibleComponents.add((FocusableHyperlinkLabel) actionLabel);
            visibleActionCount++;
        }
        actionLabel.setToolTipText(action.getDescription());
        actionLabel.setBorder(myMargins);
    }
    myOverflowMenuButton.setBorder(myMargins);
    add(myOverflowMenuButton);
    myVisibleComponents.add(myOverflowMenuButton);
    myOverflowMenuButton.addHyperlinkListener(new HyperlinkListener() {
        @Override
        public void hyperlinkUpdate(HyperlinkEvent e) {
            myOverflowMenu.show(myOverflowMenuButton,
                    myOverflowMenuButton.getX() - myOverflowMenu.getPreferredSize().width,
                    myOverflowMenuButton.getY());
        }
    });
    addKeyListener(new KeyAdapter() {
        @Override
        public void keyTyped(KeyEvent e) {
            if (e.getKeyChar() == KeyEvent.VK_ENTER || e.getKeyChar() == KeyEvent.VK_SPACE) {
                runFocusedAction();
            }
        }
    });
}

From source file:com.android.tools.idea.avdmanager.DeviceDefinitionList.java

License:Apache License

@NotNull
private static JBMenuItem createMenuItem(@NotNull DeviceUiAction action) {
    JBMenuItem item = new JBMenuItem(action);
    item.setText(action.getText());/*  w w w . j av  a  2s .c  o  m*/
    return item;
}

From source file:com.android.tools.idea.editors.manifest.ManifestPanel.java

License:Apache License

private void createPopupMenu() {
    myPopup = new JBPopupMenu();
    JMenuItem gotoItem = new JBMenuItem("Go to Declaration");
    gotoItem.addActionListener(e -> {
        TreePath treePath = myTree.getSelectionPath();
        final ManifestTreeNode node = (ManifestTreeNode) treePath.getLastPathComponent();
        if (node != null) {
            goToDeclaration(node.getUserObject());
        }//from  w w w.  j  a v a2 s.com
    });
    myPopup.add(gotoItem);
    myRemoveItem = new JBMenuItem("Remove");
    myRemoveItem.addActionListener(e -> {
        TreePath treePath = myTree.getSelectionPath();
        final ManifestTreeNode node = (ManifestTreeNode) treePath.getLastPathComponent();

        new WriteCommandAction.Simple(myFacet.getModule().getProject(), "Removing manifest tag",
                ManifestUtils.getMainManifest(myFacet)) {
            @Override
            protected void run() throws Throwable {
                ManifestUtils.toolsRemove(ManifestUtils.getMainManifest(myFacet), node.getUserObject());
            }
        }.execute();
    });
    myPopup.add(myRemoveItem);

    MouseListener ml = new MouseAdapter() {
        @Override
        public void mousePressed(@NotNull MouseEvent e) {
            if (e.isPopupTrigger()) {
                handlePopup(e);
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                handlePopup(e);
            }
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2 && e.getButton() == MouseEvent.BUTTON1) {
                TreePath treePath = myTree.getPathForLocation(e.getX(), e.getY());
                if (treePath != null) {
                    ManifestTreeNode node = (ManifestTreeNode) treePath.getLastPathComponent();
                    Node attribute = node.getUserObject();
                    if (attribute instanceof Attr) {
                        goToDeclaration(attribute);
                    }
                }
            }
        }

        private void handlePopup(@NotNull MouseEvent e) {
            TreePath treePath = myTree.getPathForLocation(e.getX(), e.getY());
            if (treePath == null || e.getSource() == myDetails) {
                // Use selection instead
                treePath = myTree.getSelectionPath();
            }
            if (treePath != null) {
                ManifestTreeNode node = (ManifestTreeNode) treePath.getLastPathComponent();
                myRemoveItem.setEnabled(canRemove(node.getUserObject()));
                myPopup.show(e.getComponent(), e.getX(), e.getY());
            }
        }
    };
    myTree.addMouseListener(ml);
    myDetails.addMouseListener(ml);
}

From source file:com.android.tools.idea.editors.navigation.NavigationView.java

License:Apache License

public NavigationView(RenderingParameters renderingParams, NavigationModel model, SelectionModel selectionModel,
        CodeGenerator codeGenerator) {// w  ww. j a  v  a  2 s.  com
    myRenderingParams = renderingParams;
    myNavigationModel = model;
    mySelectionModel = selectionModel;
    myCodeGenerator = codeGenerator;

    setFocusable(true);
    setLayout(null);

    // Mouse listener
    MouseAdapter mouseListener = new MyMouseListener();
    addMouseListener(mouseListener);
    addMouseMotionListener(mouseListener);

    // Popup menu
    final JPopupMenu menu = new JBPopupMenu();
    final JMenuItem anItem = new JBMenuItem("New Activity...");
    anItem.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent actionEvent) {
            Module module = myRenderingParams.facet.getModule();
            NewAndroidActivityWizard dialog = new NewAndroidActivityWizard(module, null, null);
            dialog.init();
            dialog.setOpenCreatedFiles(false);
            dialog.show();
        }
    });
    menu.add(anItem);
    setComponentPopupMenu(menu);

    // Focus listener
    addFocusListener(new FocusListener() {
        @Override
        public void focusGained(FocusEvent focusEvent) {
            repaint();
        }

        @Override
        public void focusLost(FocusEvent focusEvent) {
            repaint();
        }
    });

    // Drag and Drop listener
    final DnDManager dndManager = DnDManager.getInstance();
    dndManager.registerTarget(new MyDnDTarget(), this);

    // Key listeners
    Action remove = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
            setSelection(Selections.NULL);
        }
    };
    registerKeyBinding(KeyEvent.VK_DELETE, "delete", remove);
    registerKeyBinding(KeyEvent.VK_BACK_SPACE, "backspace", remove);

    // Model listener
    myNavigationModel.getListeners().add(new Listener<Event>() {
        @Override
        public void notify(@NotNull Event event) {
            if (DEBUG)
                LOG.info("NavigationView:: <listener> " + myStateCacheIsValid + " "
                        + myTransitionEditorCacheIsValid);
            if (event.operandType.isAssignableFrom(State.class)) {
                myStateCacheIsValid = false;
            }
            if (event.operandType.isAssignableFrom(Transition.class)) {
                myTransitionEditorCacheIsValid = false;
            }
            if (event == NavigationEditor.PROJECT_READ) {
                setSelection(Selections.NULL);
            }
            revalidate();
            repaint();
        }
    });
}

From source file:com.android.tools.idea.editors.theme.ui.VariantsComboBox.java

License:Apache License

@NotNull
protected JPopupMenu createPopupMenu() {
    JPopupMenu menu = new JBPopupMenu();
    Border existingBorder = menu.getBorder();

    if (existingBorder != null) {
        menu.setBorder(BorderFactory.createCompoundBorder(existingBorder, VARIANT_MENU_BORDER));
    } else {//from  w  w  w  .ja  va2  s . co  m
        menu.setBorder(VARIANT_MENU_BORDER);
    }
    menu.setBackground(VARIANT_MENU_BACKGROUND_COLOR);

    int nElements = myModel.getSize();
    for (int i = 0; i < nElements; i++) {
        final Object element = myModel.getElementAt(i);
        JMenuItem item = new JBMenuItem(element.toString());
        item.setFont(ThemeEditorUtils.scaleFontForAttribute(item.getFont()));
        item.setBorder(VARIANT_ITEM_BORDER);
        if (i == 0) {
            // Pre-select the first element
            item.setArmed(true);
        }
        item.setBackground(VARIANT_MENU_BACKGROUND_COLOR);
        item.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Object selectedItem = myModel.getSelectedItem();

                if (selectedItem != null) {
                    fireItemSelectionChanged(new ItemEvent(VariantsComboBox.this, ItemEvent.ITEM_STATE_CHANGED,
                            selectedItem, ItemEvent.DESELECTED));
                }

                myModel.setSelectedItem(element);
                fireModelUpdated();

                fireItemSelectionChanged(new ItemEvent(VariantsComboBox.this, ItemEvent.ITEM_STATE_CHANGED,
                        element, ItemEvent.SELECTED));
            }
        });
        menu.add(item);
    }

    if (!myActions.isEmpty()) {
        if (nElements > 0) {
            menu.addSeparator();
        }
        for (Action action : myActions) {
            JMenuItem newMenuItem = new JBMenuItem(action);
            newMenuItem.setFont(ThemeEditorUtils.scaleFontForAttribute(newMenuItem.getFont()));
            newMenuItem.setBackground(VARIANT_MENU_BACKGROUND_COLOR);
            newMenuItem.setBorder(VARIANT_ITEM_BORDER);
            menu.add(newMenuItem);
        }
    }

    return menu;
}

From source file:com.intellij.application.options.TabbedLanguageCodeStylePanel.java

License:Apache License

private void fillLanguages(JComponent parentMenu) {
    Language[] languages = LanguageCodeStyleSettingsProvider.getLanguagesWithCodeStyleSettings();
    @SuppressWarnings("UnnecessaryFullyQualifiedName")
    java.util.List<JMenuItem> langItems = new ArrayList<JMenuItem>();
    for (final Language lang : languages) {
        if (!lang.equals(getDefaultLanguage())) {
            final String langName = LanguageCodeStyleSettingsProvider.getLanguageName(lang);
            JMenuItem langItem = new JBMenuItem(langName);
            langItem.addActionListener(new ActionListener() {
                @Override/* ww  w .  j a  v  a2  s  .  c  om*/
                public void actionPerformed(ActionEvent e) {
                    applyLanguageSettings(lang);
                }
            });
            langItems.add(langItem);
        }
    }
    Collections.sort(langItems, new Comparator<JMenuItem>() {
        @Override
        public int compare(JMenuItem item1, JMenuItem item2) {
            return item1.getText().compareToIgnoreCase(item2.getText());
        }
    });
    for (JMenuItem langItem : langItems) {
        parentMenu.add(langItem);
    }
}

From source file:com.intellij.application.options.TabbedLanguageCodeStylePanel.java

License:Apache License

private void fillPredefined(JMenuItem parentMenu) {
    for (final PredefinedCodeStyle predefinedCodeStyle : myPredefinedCodeStyles) {
        JMenuItem predefinedItem = new JBMenuItem(predefinedCodeStyle.getName());
        parentMenu.add(predefinedItem);// w ww. j av a  2  s  .c om
        predefinedItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                applyPredefinedStyle(predefinedCodeStyle.getName());
            }
        });
    }
}

From source file:com.intellij.codeInsight.daemon.impl.DaemonEditorPopup.java

License:Apache License

@Override
public void invokePopup(final Component comp, final int x, final int y) {
    if (ApplicationManager.getApplication() == null)
        return;/* w  w  w.j a v a2 s . com*/
    final JRadioButtonMenuItem errorsFirst = createRadioButtonMenuItem(
            EditorBundle.message("errors.panel.go.to.errors.first.radio"));
    errorsFirst.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            DaemonCodeAnalyzerSettings.getInstance().NEXT_ERROR_ACTION_GOES_TO_ERRORS_FIRST = errorsFirst
                    .isSelected();
        }
    });
    final JPopupMenu popupMenu = new JBPopupMenu();
    popupMenu.add(errorsFirst);

    final JRadioButtonMenuItem next = createRadioButtonMenuItem(
            EditorBundle.message("errors.panel.go.to.next.error.warning.radio"));
    next.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            DaemonCodeAnalyzerSettings.getInstance().NEXT_ERROR_ACTION_GOES_TO_ERRORS_FIRST = !next
                    .isSelected();
        }
    });
    popupMenu.add(next);

    ButtonGroup group = new ButtonGroup();
    group.add(errorsFirst);
    group.add(next);

    popupMenu.addSeparator();
    final JMenuItem hLevel = new JBMenuItem(EditorBundle.message("customize.highlighting.level.menu.item"));
    popupMenu.add(hLevel);

    final boolean isErrorsFirst = DaemonCodeAnalyzerSettings
            .getInstance().NEXT_ERROR_ACTION_GOES_TO_ERRORS_FIRST;
    errorsFirst.setSelected(isErrorsFirst);
    next.setSelected(!isErrorsFirst);
    hLevel.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            final PsiFile psiFile = myPsiFile;
            if (psiFile == null)
                return;
            final HectorComponent component = new HectorComponent(psiFile);
            final Dimension dimension = component.getPreferredSize();
            Point point = new Point(x, y);
            component.showComponent(new RelativePoint(comp, new Point(point.x - dimension.width, point.y)));
        }
    });

    final JBCheckboxMenuItem previewCheckbox = new JBCheckboxMenuItem(
            IdeBundle.message("checkbox.show.editor.preview.popup"),
            UISettings.getInstance().SHOW_EDITOR_TOOLTIP);
    popupMenu.addSeparator();
    popupMenu.add(previewCheckbox);
    previewCheckbox.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            UISettings.getInstance().SHOW_EDITOR_TOOLTIP = previewCheckbox.isSelected();
            UISettings.getInstance().fireUISettingsChanged();
        }
    });

    PsiFile file = myPsiFile;
    if (file != null && DaemonCodeAnalyzer.getInstance(myPsiFile.getProject()).isHighlightingAvailable(file)) {
        popupMenu.show(comp, x, y);
    }
}

From source file:com.intellij.ui.components.JBOptionButton.java

License:Apache License

private JPopupMenu fillMenu(boolean under) {
    final JPopupMenu result = new JBPopupMenu();

    if (under && myOptions.length > 0) {
        final JMenuItem mainAction = new JBMenuItem(getAction());
        configureItem(getMenuInfo(getAction()), mainAction);
        result.add(mainAction);/*from w w  w . jav  a2 s  .c  om*/
        result.addSeparator();
    }

    for (Action each : myOptions) {
        if (getAction() == each)
            continue;
        final OptionInfo info = getMenuInfo(each);
        final JMenuItem eachItem = new JBMenuItem(each);

        configureItem(info, eachItem);
        result.add(eachItem);
    }

    if (!under && myOptions.length > 0) {
        result.addSeparator();
        final JMenuItem mainAction = new JBMenuItem(getAction());
        configureItem(getMenuInfo(getAction()), mainAction);
        result.add(mainAction);
    }

    return result;
}

From source file:com.intellij.ui.SearchTextField.java

License:Apache License

public SearchTextField(boolean historyEnabled) {
    super(new BorderLayout());

    myModel = new MyModel();

    myTextField = new TextFieldWithProcessing() {
        @Override//from   w  w  w  .  jav  a  2 s. c om
        public void processKeyEvent(final KeyEvent e) {
            if (preprocessEventForTextField(e))
                return;
            super.processKeyEvent(e);
        }

        @Override
        public void setBackground(final Color bg) {
            super.setBackground(bg);
            if (!hasIconsOutsideOfTextField()) {
                if (myClearFieldLabel != null) {
                    myClearFieldLabel.setBackground(bg);
                }
            }
            if (myToggleHistoryLabel != null) {
                myToggleHistoryLabel.setBackground(bg);
            }
        }
    };
    myTextField.setColumns(15);
    myTextField.addFocusListener(new FocusAdapter() {
        @Override
        public void focusLost(FocusEvent e) {
            onFocusLost();
            super.focusLost(e);
        }

        @Override
        public void focusGained(FocusEvent e) {
            onFocusGained();
            super.focusGained(e);
        }
    });
    add(myTextField, BorderLayout.CENTER);
    myTextField.addKeyListener(new KeyAdapter() {
        @Override
        public void keyPressed(KeyEvent e) {
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                if (isSearchControlUISupported() && myNativeSearchPopup != null) {
                    myNativeSearchPopup.show(myTextField, 5, myTextField.getHeight());
                } else if (myPopup == null || !myPopup.isVisible()) {
                    showPopup();
                }
            }
        }
    });

    if (isSearchControlUISupported() || UIUtil.isUnderBuildInLaF()) {
        myTextField.putClientProperty("JTextField.variant", "search");
    }
    if (isSearchControlUISupported()) {
        if (historyEnabled) {
            myNativeSearchPopup = new JBPopupMenu();
            myNoItems = new JBMenuItem("No recent searches");
            myNoItems.setEnabled(false);

            updateMenu();
            myTextField.putClientProperty("JTextField.Search.FindPopup", myNativeSearchPopup);
        }
    } else {
        myToggleHistoryLabel = new JLabel(AllIcons.Actions.Search);
        myToggleHistoryLabel.setOpaque(true);
        myToggleHistoryLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                togglePopup();
            }
        });
        if (historyEnabled) {
            add(myToggleHistoryLabel, BorderLayout.WEST);
        }

        myClearFieldLabel = new JLabel(
                UIUtil.isUnderDarcula() ? AllIcons.Actions.Clean : AllIcons.Actions.CleanLight);
        myClearFieldLabel.setOpaque(true);
        add(myClearFieldLabel, BorderLayout.EAST);
        myClearFieldLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                myTextField.setText("");
                onFieldCleared();
            }
        });

        if (!hasIconsOutsideOfTextField()) {
            final Border originalBorder;
            if (SystemInfo.isMac) {
                originalBorder = BorderFactory.createLoweredBevelBorder();
            } else {
                originalBorder = myTextField.getBorder();
            }

            myToggleHistoryLabel.setBackground(myTextField.getBackground());
            myClearFieldLabel.setBackground(myTextField.getBackground());

            setBorder(new CompoundBorder(IdeBorderFactory.createEmptyBorder(2, 0, 2, 0), originalBorder));

            myTextField.setOpaque(true);
            myTextField.setBorder(IdeBorderFactory.createEmptyBorder(0, 5, 0, 5));
        } else {
            setBorder(IdeBorderFactory.createEmptyBorder(2, 0, 2, 0));
        }
    }

    if (ApplicationManager.getApplication() != null) { //tests
        final ActionManager actionManager = ActionManager.getInstance();
        if (actionManager != null) {
            final AnAction clearTextAction = actionManager.getAction(IdeActions.ACTION_CLEAR_TEXT);
            if (clearTextAction.getShortcutSet().getShortcuts().length == 0) {
                clearTextAction.registerCustomShortcutSet(CommonShortcuts.ESCAPE, this);
            }
        }
    }
}