Search in sources :

Example 1 with EverywhereContextType

use of com.intellij.codeInsight.template.EverywhereContextType in project intellij-community by JetBrains.

the class LiveTemplateSettingsEditor method createShortContextPanel.

private JPanel createShortContextPanel(final boolean allowNoContexts) {
    JPanel panel = new JPanel(new BorderLayout());
    final JLabel ctxLabel = new JLabel();
    final JLabel change = new JLabel();
    change.setForeground(PlatformColors.BLUE);
    change.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    panel.add(ctxLabel, BorderLayout.CENTER);
    panel.add(change, BorderLayout.EAST);
    final Runnable updateLabel = () -> {
        myExpandByCombo.setEnabled(isExpandableFromEditor());
        updateHighlighter();
        StringBuilder sb = new StringBuilder();
        String oldPrefix = "";
        for (TemplateContextType type : getApplicableContexts()) {
            final TemplateContextType base = type.getBaseContextType();
            String ownName = UIUtil.removeMnemonic(type.getPresentableName());
            String prefix = "";
            if (base != null && !(base instanceof EverywhereContextType)) {
                prefix = UIUtil.removeMnemonic(base.getPresentableName()) + ": ";
                ownName = StringUtil.decapitalize(ownName);
            }
            if (type instanceof EverywhereContextType) {
                ownName = "Other";
            }
            if (sb.length() > 0) {
                sb.append(oldPrefix.equals(prefix) ? ", " : "; ");
            }
            if (!oldPrefix.equals(prefix)) {
                sb.append(prefix);
                oldPrefix = prefix;
            }
            sb.append(ownName);
        }
        String contexts = "Applicable in " + sb.toString();
        change.setText("Change");
        final boolean noContexts = sb.length() == 0;
        if (noContexts) {
            if (!allowNoContexts) {
                ctxLabel.setForeground(JBColor.RED);
            }
            contexts = "No applicable contexts" + (allowNoContexts ? "" : " yet");
            ctxLabel.setIcon(AllIcons.General.BalloonWarning);
            change.setText("Define");
        } else {
            ctxLabel.setForeground(UIUtil.getLabelForeground());
            ctxLabel.setIcon(null);
        }
        ctxLabel.setText(StringUtil.first(contexts + ". ", 100, true));
        myTemplateOptionsPanel.removeAll();
        myTemplateOptionsPanel.add(createTemplateOptionsPanel());
    };
    new ClickListener() {

        @Override
        public boolean onClick(@NotNull MouseEvent e, int clickCount) {
            if (disposeContextPopup())
                return false;
            final JPanel content = createPopupContextPanel(updateLabel, myContext);
            Dimension prefSize = content.getPreferredSize();
            if (myLastSize != null && (myLastSize.width > prefSize.width || myLastSize.height > prefSize.height)) {
                content.setPreferredSize(new Dimension(Math.max(prefSize.width, myLastSize.width), Math.max(prefSize.height, myLastSize.height)));
            }
            myContextPopup = JBPopupFactory.getInstance().createComponentPopupBuilder(content, null).setResizable(true).createPopup();
            myContextPopup.show(new RelativePoint(change, new Point(change.getWidth(), -content.getPreferredSize().height - 10)));
            myContextPopup.addListener(new JBPopupAdapter() {

                @Override
                public void onClosed(LightweightWindowEvent event) {
                    myLastSize = content.getSize();
                }
            });
            return true;
        }
    }.installOn(change);
    updateLabel.run();
    return panel;
}
Also used : EverywhereContextType(com.intellij.codeInsight.template.EverywhereContextType) RelativePoint(com.intellij.ui.awt.RelativePoint) RelativePoint(com.intellij.ui.awt.RelativePoint) RelativePoint(com.intellij.ui.awt.RelativePoint) LightweightWindowEvent(com.intellij.openapi.ui.popup.LightweightWindowEvent) JBPopupAdapter(com.intellij.openapi.ui.popup.JBPopupAdapter) TemplateContextType(com.intellij.codeInsight.template.TemplateContextType)

Aggregations

EverywhereContextType (com.intellij.codeInsight.template.EverywhereContextType)1 TemplateContextType (com.intellij.codeInsight.template.TemplateContextType)1 JBPopupAdapter (com.intellij.openapi.ui.popup.JBPopupAdapter)1 LightweightWindowEvent (com.intellij.openapi.ui.popup.LightweightWindowEvent)1 RelativePoint (com.intellij.ui.awt.RelativePoint)1