use of com.intellij.codeInsight.template.TemplateContextType in project intellij-community by JetBrains.
the class LiveTemplateSettingsEditor method addContextNode.
private static void addContextNode(MultiMap<TemplateContextType, TemplateContextType> hierarchy, CheckedTreeNode parent, TemplateContextType type, TemplateContext context) {
final Collection<TemplateContextType> children = hierarchy.get(type);
final String name = UIUtil.removeMnemonic(type.getPresentableName());
final CheckedTreeNode node = new CheckedTreeNode(Pair.create(children.isEmpty() ? type : null, name));
parent.add(node);
if (children.isEmpty()) {
node.setChecked(context.isEnabled(type));
} else {
for (TemplateContextType child : children) {
addContextNode(hierarchy, node, child, context);
}
final CheckedTreeNode other = new CheckedTreeNode(Pair.create(type, "Other"));
other.setChecked(context.isEnabled(type));
node.add(other);
}
}
use of com.intellij.codeInsight.template.TemplateContextType 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;
}
use of com.intellij.codeInsight.template.TemplateContextType in project intellij-community by JetBrains.
the class TemplateContext method writeTemplateContext.
@VisibleForTesting
@Nullable
public Element writeTemplateContext(@Nullable TemplateContext defaultContext, @NotNull Lazy<Map<String, TemplateContextType>> idToType) {
if (myContextStates.isEmpty()) {
return null;
}
Element element = new Element(TemplateSettings.CONTEXT);
List<Map.Entry<String, Boolean>> entries = new ArrayList<>(myContextStates.entrySet());
entries.sort(Comparator.comparing(Map.Entry::getKey));
for (Map.Entry<String, Boolean> entry : entries) {
Boolean ownValue = entry.getValue();
if (ownValue == null) {
continue;
}
TemplateContextType type = idToType.getValue().get(entry.getKey());
if (type == null) {
// https://youtrack.jetbrains.com/issue/IDEA-155623#comment=27-1721029
JdomKt.addOptionTag(element, entry.getKey(), ownValue.toString());
} else if (isValueChanged(ownValue, type, defaultContext)) {
JdomKt.addOptionTag(element, type.getContextId(), ownValue.toString());
}
}
return element;
}
Aggregations