use of com.intellij.openapi.ui.popup.LightweightWindowEvent 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.openapi.ui.popup.LightweightWindowEvent in project intellij-community by JetBrains.
the class IntroduceParameterHandler method chooseMethodToIntroduceParameter.
private void chooseMethodToIntroduceParameter(final Editor editor, final List<PsiMethod> validEnclosingMethods, final PairConsumer<PsiMethod, PsiMethod> consumer) {
final boolean unitTestMode = ApplicationManager.getApplication().isUnitTestMode();
if (validEnclosingMethods.size() == 1 || unitTestMode) {
final PsiMethod methodToIntroduceParameterTo = validEnclosingMethods.get(0);
if (methodToIntroduceParameterTo.findDeepestSuperMethod() == null || unitTestMode) {
consumer.consume(methodToIntroduceParameterTo, methodToIntroduceParameterTo);
return;
}
}
final JPanel panel = new JPanel(new BorderLayout());
final JCheckBox superMethod = new JCheckBox("Refactor super method", true);
superMethod.setMnemonic('U');
panel.add(superMethod, BorderLayout.SOUTH);
final JBList list = new JBList(validEnclosingMethods.toArray());
list.setVisibleRowCount(5);
list.setCellRenderer(new MethodCellRenderer());
list.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
list.setSelectedIndex(0);
final List<RangeHighlighter> highlighters = new ArrayList<>();
final TextAttributes attributes = EditorColorsManager.getInstance().getGlobalScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(final ListSelectionEvent e) {
final PsiMethod selectedMethod = (PsiMethod) list.getSelectedValue();
if (selectedMethod == null)
return;
dropHighlighters(highlighters);
updateView(selectedMethod, editor, attributes, highlighters, superMethod);
}
});
updateView(validEnclosingMethods.get(0), editor, attributes, highlighters, superMethod);
final JScrollPane scrollPane = ScrollPaneFactory.createScrollPane(list);
scrollPane.setBorder(null);
panel.add(scrollPane, BorderLayout.CENTER);
final List<Pair<ActionListener, KeyStroke>> keyboardActions = Collections.singletonList(Pair.<ActionListener, KeyStroke>create(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
final PsiMethod methodToSearchIn = (PsiMethod) list.getSelectedValue();
if (myEnclosingMethodsPopup != null && myEnclosingMethodsPopup.isVisible()) {
myEnclosingMethodsPopup.cancel();
}
final PsiMethod methodToSearchFor = superMethod.isEnabled() && superMethod.isSelected() ? methodToSearchIn.findDeepestSuperMethod() : methodToSearchIn;
consumer.consume(methodToSearchIn, methodToSearchFor);
}
}, KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)));
myEnclosingMethodsPopup = JBPopupFactory.getInstance().createComponentPopupBuilder(panel, list).setTitle("Introduce parameter to method").setMovable(false).setResizable(false).setRequestFocus(true).setKeyboardActions(keyboardActions).addListener(new JBPopupAdapter() {
@Override
public void onClosed(LightweightWindowEvent event) {
dropHighlighters(highlighters);
}
}).createPopup();
myEnclosingMethodsPopup.showInBestPositionFor(editor);
}
use of com.intellij.openapi.ui.popup.LightweightWindowEvent in project intellij-community by JetBrains.
the class IntroduceTargetChooser method showIntroduceTargetChooser.
public static <T extends IntroduceTarget> void showIntroduceTargetChooser(@NotNull Editor editor, @NotNull List<T> expressions, @NotNull Pass<T> callback, @NotNull @Nls String title, int selection) {
AtomicReference<ScopeHighlighter> highlighter = new AtomicReference<>(new ScopeHighlighter(editor));
CollectionListModel<T> model = new CollectionListModel<>(expressions);
JBList<T> list = new JBList<>(model);
// Set the accessible name so that screen readers announce the list tile (e.g. "Expression Types list").
AccessibleContextUtil.setName(list, title);
list.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
if (selection > -1)
list.setSelectedIndex(selection);
list.setCellRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Component rendererComponent = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
IntroduceTarget expr = (T) value;
if (expr.isValid()) {
String text = expr.render();
int firstNewLinePos = text.indexOf('\n');
String trimmedText = text.substring(0, firstNewLinePos != -1 ? firstNewLinePos : Math.min(100, text.length()));
if (trimmedText.length() != text.length())
trimmedText += " ...";
setText(trimmedText);
} else {
setForeground(JBColor.RED);
setText("Invalid");
}
return rendererComponent;
}
});
list.addListSelectionListener(e -> {
ScopeHighlighter h = highlighter.get();
if (h == null)
return;
h.dropHighlight();
T expr = list.getSelectedValue();
if (expr != null && expr.isValid()) {
TextRange range = expr.getTextRange();
h.highlight(Pair.create(range, Collections.singletonList(range)));
}
});
JBPopupFactory.getInstance().createListPopupBuilder(list).setTitle(title).setMovable(false).setResizable(false).setRequestFocus(true).setItemChoosenCallback(() -> {
T expr = list.getSelectedValue();
if (expr != null && expr.isValid()) {
callback.pass(expr);
}
}).addListener(new JBPopupAdapter() {
@Override
public void onClosed(LightweightWindowEvent event) {
highlighter.getAndSet(null).dropHighlight();
}
}).createPopup().showInBestPositionFor(editor);
}
use of com.intellij.openapi.ui.popup.LightweightWindowEvent in project intellij-community by JetBrains.
the class DebuggerTreeWithHistoryPopup method updateContainer.
@Override
protected void updateContainer(final Tree tree, String title) {
if (myPopup != null) {
myPopup.cancel();
}
tree.getModel().addTreeModelListener(createTreeListener(tree));
myPopup = JBPopupFactory.getInstance().createComponentPopupBuilder(createMainPanel(tree), tree).setRequestFocus(true).setTitle(title).setResizable(true).setMovable(true).setDimensionServiceKey(myProject, DIMENSION_SERVICE_KEY, false).setMayBeParent(true).setKeyEventHandler(event -> {
if (AbstractPopup.isCloseRequest(event)) {
SpeedSearchSupply supply = SpeedSearchSupply.getSupply(tree);
return supply != null && StringUtil.isEmpty(supply.getEnteredPrefix());
}
return false;
}).addListener(new JBPopupAdapter() {
@Override
public void onClosed(LightweightWindowEvent event) {
if (myHideRunnable != null) {
myHideRunnable.run();
}
}
}).setCancelCallback(() -> {
Window parent = SwingUtilities.getWindowAncestor(tree);
if (parent != null) {
for (Window child : parent.getOwnedWindows()) {
if (child.isShowing()) {
return false;
}
}
}
return true;
}).createPopup();
registerTreeDisposable(myPopup, tree);
//Editor may be disposed before later invokator process this action
if (myEditor.getComponent().getRootPane() == null) {
myPopup.cancel();
return;
}
myPopup.show(new RelativePoint(myEditor.getContentComponent(), myPoint));
updateInitialBounds(tree);
}
use of com.intellij.openapi.ui.popup.LightweightWindowEvent in project intellij-community by JetBrains.
the class JBComboBoxTableCellEditorComponent method initAndShowPopup.
private void initAndShowPopup() {
myList.removeAll();
myList.setModel(JBList.createDefaultListModel(myOptions));
if (myRenderer != null) {
myList.setCellRenderer(myRenderer);
}
final Rectangle rect = myTable.getCellRect(myRow, myColumn, true);
Point point = new Point(rect.x, rect.y);
final boolean surrendersFocusOnKeystrokeOldValue = myTable instanceof JBTable ? ((JBTable) myTable).surrendersFocusOnKeyStroke() : myTable.getSurrendersFocusOnKeystroke();
final JBPopup popup = JBPopupFactory.getInstance().createListPopupBuilder(myList).setItemChoosenCallback(() -> {
myValue = myList.getSelectedValue();
final ActionEvent event = new ActionEvent(myList, ActionEvent.ACTION_PERFORMED, "elementChosen");
for (ActionListener listener : myListeners) {
listener.actionPerformed(event);
}
TableUtil.stopEditing(myTable);
// on Mac getCellEditorValue() called before myValue is set.
myTable.setValueAt(myValue, myRow, myColumn);
// force repaint
myTable.tableChanged(new TableModelEvent(myTable.getModel(), myRow));
}).setCancelCallback(() -> {
TableUtil.stopEditing(myTable);
return true;
}).addListener(new JBPopupAdapter() {
@Override
public void beforeShown(LightweightWindowEvent event) {
super.beforeShown(event);
myTable.setSurrendersFocusOnKeystroke(false);
}
@Override
public void onClosed(LightweightWindowEvent event) {
myTable.setSurrendersFocusOnKeystroke(surrendersFocusOnKeystrokeOldValue);
super.onClosed(event);
}
}).setMinSize(myWide ? new Dimension(((int) rect.getSize().getWidth()), -1) : null).createPopup();
popup.show(new RelativePoint(myTable, point));
}
Aggregations