use of com.intellij.openapi.ui.popup.JBPopupAdapter in project intellij-community by JetBrains.
the class OccurrencesChooser method showChooser.
public <C extends BaseReplaceChoice> void showChooser(final Pass<C> callback, final Map<C, List<T>> occurrencesMap, String title) {
if (occurrencesMap.size() == 1) {
callback.pass(occurrencesMap.keySet().iterator().next());
return;
}
final DefaultListModel<C> model = new DefaultListModel<>();
for (C choice : occurrencesMap.keySet()) {
model.addElement(choice);
}
final JList<C> list = new JBList<>(model);
list.setCellRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean cellHasFocus) {
final Component rendererComponent = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
@SuppressWarnings("unchecked") final C choices = (C) value;
if (choices != null) {
setText(choices.formatDescription(occurrencesMap.get(choices).size()));
}
return rendererComponent;
}
});
list.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(final ListSelectionEvent e) {
final C value = list.getSelectedValue();
if (value == null)
return;
dropHighlighters();
final MarkupModel markupModel = myEditor.getMarkupModel();
final List<T> occurrenceList = occurrencesMap.get(value);
for (T occurrence : occurrenceList) {
final TextRange textRange = getOccurrenceRange(occurrence);
final RangeHighlighter rangeHighlighter = markupModel.addRangeHighlighter(textRange.getStartOffset(), textRange.getEndOffset(), HighlighterLayer.SELECTION - 1, myAttributes, HighlighterTargetArea.EXACT_RANGE);
myRangeHighlighters.add(rangeHighlighter);
}
}
});
JBPopupFactory.getInstance().createListPopupBuilder(list).setTitle(title).setMovable(true).setResizable(false).setRequestFocus(true).setItemChoosenCallback(() -> callback.pass(list.getSelectedValue())).addListener(new JBPopupAdapter() {
@Override
public void onClosed(LightweightWindowEvent event) {
dropHighlighters();
}
}).createPopup().showInBestPositionFor(myEditor);
}
use of com.intellij.openapi.ui.popup.JBPopupAdapter 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;
}
Aggregations