Example usage for javax.swing JPopupMenu applyComponentOrientation

List of usage examples for javax.swing JPopupMenu applyComponentOrientation

Introduction

In this page you can find the example usage for javax.swing JPopupMenu applyComponentOrientation.

Prototype

public void applyComponentOrientation(ComponentOrientation o) 

Source Link

Document

Sets the ComponentOrientation property of this container and all components contained within it.

Usage

From source file:org.languagetool.gui.LanguageToolSupport.java

private void showPopup(MouseEvent event) {
    if (documentSpans.isEmpty() && languageTool.getDisabledRules().isEmpty()) {
        //No errors and no disabled Rules
        return;/*  ww w  .j av a  2  s.  co  m*/
    }

    int offset = this.textComponent.viewToModel(event.getPoint());
    Span span = getSpan(offset);
    JPopupMenu popup = new JPopupMenu("Grammar Menu");
    if (span != null) {
        JLabel msgItem = new JLabel("<html>"
                + span.msg.replace("<suggestion>", "<b>").replace("</suggestion>", "</b>") + "</html>");
        msgItem.setToolTipText(span.desc.replace("<suggestion>", "").replace("</suggestion>", ""));
        msgItem.setBorder(new JMenuItem().getBorder());
        popup.add(msgItem);

        popup.add(new JSeparator());

        for (String r : span.replacement) {
            ReplaceMenuItem item = new ReplaceMenuItem(r, span);
            popup.add(item);
            item.addActionListener(actionListener);
        }

        popup.add(new JSeparator());

        JMenuItem moreItem = new JMenuItem(messages.getString("guiMore"));
        moreItem.addActionListener(e -> showDialog(textComponent, span.msg, span.desc, span.rule, span.url));
        popup.add(moreItem);

        JMenuItem ignoreItem = new JMenuItem(messages.getString("guiTurnOffRule"));
        ignoreItem.addActionListener(e -> disableRule(span.rule.getId()));
        popup.add(ignoreItem);
        popup.applyComponentOrientation(ComponentOrientation.getOrientation(Locale.getDefault()));
    }

    List<Rule> disabledRules = getDisabledRules();
    if (!disabledRules.isEmpty()) {
        JMenu activateRuleMenu = new JMenu(messages.getString("guiActivateRule"));
        addDisabledRulesToMenu(disabledRules, activateRuleMenu);
        popup.add(activateRuleMenu);
    }

    if (span != null) {
        textComponent.setCaretPosition(span.start);
        textComponent.moveCaretPosition(span.end);
    }

    popup.addPopupMenuListener(new PopupMenuListener() {
        @Override
        public void popupMenuWillBecomeVisible(PopupMenuEvent e) {
        }

        @Override
        public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {
        }

        @Override
        public void popupMenuCanceled(PopupMenuEvent e) {
            if (span != null) {
                textComponent.setCaretPosition(span.start);
            }
        }
    });
    popup.show(textComponent, event.getPoint().x, event.getPoint().y);

}