use of com.intellij.openapi.project.DumbAwareAction in project intellij-community by JetBrains.
the class RunIdeConsoleAction method actionPerformed.
@Override
public void actionPerformed(AnActionEvent e) {
List<String> languages = IdeScriptEngineManager.getInstance().getLanguages();
if (languages.size() == 1) {
runConsole(e, languages.iterator().next());
return;
}
DefaultActionGroup actions = new DefaultActionGroup(ContainerUtil.map(languages, (NotNullFunction<String, AnAction>) language -> new DumbAwareAction(language) {
@Override
public void actionPerformed(@NotNull AnActionEvent e1) {
runConsole(e1, language);
}
}));
JBPopupFactory.getInstance().createActionGroupPopup("Script Engine", actions, e.getDataContext(), JBPopupFactory.ActionSelectionAid.NUMBERING, false).showInBestPositionFor(e.getDataContext());
}
use of com.intellij.openapi.project.DumbAwareAction in project intellij-community by JetBrains.
the class ChooseByNameBase method initUI.
/**
* @param modalityState - if not null rebuilds list in given {@link ModalityState}
*/
protected void initUI(final ChooseByNamePopupComponent.Callback callback, final ModalityState modalityState, final boolean allowMultipleSelection) {
myPreviouslyFocusedComponent = WindowManagerEx.getInstanceEx().getFocusedComponent(myProject);
myActionListener = callback;
myTextFieldPanel.setLayout(new BoxLayout(myTextFieldPanel, BoxLayout.Y_AXIS));
final JPanel hBox = new JPanel();
hBox.setLayout(new BoxLayout(hBox, BoxLayout.X_AXIS));
JPanel caption2Tools = new JPanel(new BorderLayout());
if (myModel.getPromptText() != null) {
JLabel label = new JLabel(myModel.getPromptText());
label.setFont(UIUtil.getLabelFont().deriveFont(Font.BOLD));
caption2Tools.add(label, BorderLayout.WEST);
}
caption2Tools.add(hBox, BorderLayout.EAST);
// space between checkbox and filter/show all in view buttons
myCardContainer.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 4));
final String checkBoxName = myModel.getCheckBoxName();
myCheckBox.setText(checkBoxName != null ? checkBoxName + (myCheckBoxShortcut != null && myCheckBoxShortcut.getShortcuts().length > 0 ? " (" + KeymapUtil.getShortcutsText(myCheckBoxShortcut.getShortcuts()) + ")" : "") : "");
myCheckBox.setAlignmentX(SwingConstants.RIGHT);
if (!SystemInfo.isMac) {
myCheckBox.setBorder(null);
}
myCheckBox.setSelected(myModel.loadInitialCheckBoxState());
if (checkBoxName == null) {
myCheckBox.setVisible(false);
}
addCard(myCheckBox, CHECK_BOX_CARD);
addCard(new HintLabel(myModel.getNotInMessage()), NOT_FOUND_IN_PROJECT_CARD);
addCard(new HintLabel(IdeBundle.message("label.choosebyname.no.matches.found")), NOT_FOUND_CARD);
JPanel searching = new JPanel(new BorderLayout(5, 0));
searching.add(new AsyncProcessIcon("searching"), BorderLayout.WEST);
searching.add(new HintLabel(IdeBundle.message("label.choosebyname.searching")), BorderLayout.CENTER);
addCard(searching, SEARCHING_CARD);
myCard.show(myCardContainer, CHECK_BOX_CARD);
if (isCheckboxVisible()) {
hBox.add(myCardContainer);
}
final DefaultActionGroup group = new DefaultActionGroup();
group.add(new ShowFindUsagesAction() {
@Override
public PsiElement[][] getElements() {
final List<Object> objects = myListModel.getItems();
final List<PsiElement> prefixMatchElements = new ArrayList<>(objects.size());
final List<PsiElement> nonPrefixMatchElements = new ArrayList<>(objects.size());
List<PsiElement> curElements = prefixMatchElements;
for (Object object : objects) {
if (object instanceof PsiElement) {
curElements.add((PsiElement) object);
} else if (object instanceof DataProvider) {
final PsiElement psi = CommonDataKeys.PSI_ELEMENT.getData((DataProvider) object);
if (psi != null) {
curElements.add(psi);
}
} else if (object == NON_PREFIX_SEPARATOR) {
curElements = nonPrefixMatchElements;
}
}
return new PsiElement[][] { PsiUtilCore.toPsiElementArray(prefixMatchElements), PsiUtilCore.toPsiElementArray(nonPrefixMatchElements) };
}
});
final ActionToolbar actionToolbar = ActionManager.getInstance().createActionToolbar(ActionPlaces.UNKNOWN, group, true);
actionToolbar.setLayoutPolicy(ActionToolbar.NOWRAP_LAYOUT_POLICY);
// we need valid ActionToolbar.getPreferredSize() to calc size of popup
actionToolbar.updateActionsImmediately();
final JComponent toolbarComponent = actionToolbar.getComponent();
toolbarComponent.setBorder(null);
if (myToolArea == null) {
myToolArea = new JLabel(JBUI.scale(EmptyIcon.create(1, 24)));
}
hBox.add(myToolArea);
hBox.add(toolbarComponent);
myTextFieldPanel.add(caption2Tools);
new MyCopyReferenceAction().registerCustomShortcutSet(ActionManager.getInstance().getAction(IdeActions.ACTION_COPY).getShortcutSet(), myTextField);
myTextFieldPanel.add(myTextField);
Font editorFont = EditorUtil.getEditorFont();
myTextField.setFont(editorFont);
if (checkBoxName != null) {
if (myCheckBoxShortcut != null) {
new DumbAwareAction("change goto check box", null, null) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
myCheckBox.setSelected(!myCheckBox.isSelected());
}
}.registerCustomShortcutSet(myCheckBoxShortcut, myTextField);
}
}
if (isCloseByFocusLost()) {
myTextField.addFocusListener(new FocusAdapter() {
@Override
public void focusLost(@NotNull final FocusEvent e) {
if (Registry.is("focus.follows.mouse.workarounds")) {
if (myFocusPoint != null) {
PointerInfo pointerInfo = MouseInfo.getPointerInfo();
if (pointerInfo != null && myFocusPoint.equals(pointerInfo.getLocation())) {
// Ignore the loss of focus if the mouse hasn't moved between the last dropdown resize
// and the loss of focus event. This happens in focus follows mouse mode if the mouse is
// over the dropdown and it resizes to leave the mouse outside the dropdown.
IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);
myFocusPoint = null;
return;
}
}
myFocusPoint = null;
}
// cancel thread as early as possible
cancelListUpdater();
myHideAlarm.addRequest(() -> {
JBPopup popup = JBPopupFactory.getInstance().getChildFocusedPopup(e.getComponent());
if (popup != null) {
popup.addListener(new JBPopupListener.Adapter() {
@Override
public void onClosed(@NotNull LightweightWindowEvent event) {
if (event.isOk()) {
hideHint();
}
}
});
} else {
Component oppositeComponent = e.getOppositeComponent();
if (oppositeComponent == myCheckBox) {
IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);
return;
}
if (oppositeComponent != null && !(oppositeComponent instanceof JFrame) && myList.isShowing() && (oppositeComponent == myList || SwingUtilities.isDescendingFrom(myList, oppositeComponent))) {
// Otherwise me may skip some KeyEvents
IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);
return;
}
if (isDescendingFromTemporarilyFocusableToolWindow(oppositeComponent)) {
// Allow toolwindows to gain focus (used by QuickDoc shown in a toolwindow)
return;
}
hideHint();
}
}, 5);
}
});
}
myCheckBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(@NotNull ItemEvent e) {
rebuildList(false);
}
});
myCheckBox.setFocusable(false);
myTextField.getDocument().addDocumentListener(new DocumentAdapter() {
@Override
protected void textChanged(DocumentEvent e) {
rebuildList(false);
}
});
final Set<KeyStroke> upShortcuts = getShortcuts(IdeActions.ACTION_EDITOR_MOVE_CARET_UP);
final Set<KeyStroke> downShortcuts = getShortcuts(IdeActions.ACTION_EDITOR_MOVE_CARET_DOWN);
myTextField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(@NotNull KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_ENTER && (e.getModifiers() & InputEvent.SHIFT_MASK) != 0) {
myClosedByShiftEnter = true;
close(true);
}
if (!myListScrollPane.isVisible()) {
return;
}
final int keyCode;
// Add support for user-defined 'caret up/down' shortcuts.
KeyStroke stroke = KeyStroke.getKeyStrokeForEvent(e);
if (upShortcuts.contains(stroke)) {
keyCode = KeyEvent.VK_UP;
} else if (downShortcuts.contains(stroke)) {
keyCode = KeyEvent.VK_DOWN;
} else {
keyCode = e.getKeyCode();
}
switch(keyCode) {
case KeyEvent.VK_DOWN:
ScrollingUtil.moveDown(myList, e.getModifiersEx());
break;
case KeyEvent.VK_UP:
ScrollingUtil.moveUp(myList, e.getModifiersEx());
break;
case KeyEvent.VK_PAGE_UP:
ScrollingUtil.movePageUp(myList);
break;
case KeyEvent.VK_PAGE_DOWN:
ScrollingUtil.movePageDown(myList);
break;
case KeyEvent.VK_TAB:
close(true);
break;
case KeyEvent.VK_ENTER:
if (myList.getSelectedValue() == EXTRA_ELEM) {
myMaximumListSizeLimit += myListSizeIncreasing;
rebuildList(myList.getSelectedIndex(), myRebuildDelay, ModalityState.current(), null);
e.consume();
}
break;
}
if (myList.getSelectedValue() == NON_PREFIX_SEPARATOR) {
if (keyCode == KeyEvent.VK_UP || keyCode == KeyEvent.VK_PAGE_UP) {
ScrollingUtil.moveUp(myList, e.getModifiersEx());
} else {
ScrollingUtil.moveDown(myList, e.getModifiersEx());
}
}
}
});
myTextField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(@NotNull ActionEvent actionEvent) {
doClose(true);
}
});
myList.setFocusable(false);
myList.setSelectionMode(allowMultipleSelection ? ListSelectionModel.MULTIPLE_INTERVAL_SELECTION : ListSelectionModel.SINGLE_SELECTION);
new ClickListener() {
@Override
public boolean onClick(@NotNull MouseEvent e, int clickCount) {
if (!myTextField.hasFocus()) {
IdeFocusManager.getInstance(myProject).requestFocus(myTextField, true);
}
if (clickCount == 2) {
int selectedIndex = myList.getSelectedIndex();
Rectangle selectedCellBounds = myList.getCellBounds(selectedIndex, selectedIndex);
if (selectedCellBounds != null && selectedCellBounds.contains(e.getPoint())) {
// Otherwise it was reselected in the selection listener
if (myList.getSelectedValue() == EXTRA_ELEM) {
myMaximumListSizeLimit += myListSizeIncreasing;
rebuildList(selectedIndex, myRebuildDelay, ModalityState.current(), null);
} else {
doClose(true);
}
}
return true;
}
return false;
}
}.installOn(myList);
myList.setCellRenderer(myModel.getListCellRenderer());
myList.setFont(editorFont);
myList.addListSelectionListener(new ListSelectionListener() {
private int myPreviousSelectionIndex = 0;
@Override
public void valueChanged(@NotNull ListSelectionEvent e) {
if (myList.getSelectedValue() != NON_PREFIX_SEPARATOR) {
myPreviousSelectionIndex = myList.getSelectedIndex();
chosenElementMightChange();
updateDocumentation();
} else if (allowMultipleSelection) {
myList.setSelectedIndex(myPreviousSelectionIndex);
}
}
});
myListScrollPane = ScrollPaneFactory.createScrollPane(myList);
myListScrollPane.setViewportBorder(JBUI.Borders.empty());
myTextFieldPanel.setBorder(JBUI.Borders.empty(5));
showTextFieldPanel();
myInitialized = true;
if (modalityState != null) {
rebuildList(myInitialIndex, 0, modalityState, null);
}
}
use of com.intellij.openapi.project.DumbAwareAction in project intellij-community by JetBrains.
the class ProjectSdksModel method createAddActions.
public void createAddActions(@NotNull DefaultActionGroup group, @NotNull final JComponent parent, @Nullable final Sdk selectedSdk, @NotNull final Consumer<Sdk> updateTree, @Nullable Condition<SdkTypeId> filter) {
final SdkType[] types = SdkType.getAllTypes();
for (final SdkType type : types) {
if (filter != null && !filter.value(type))
continue;
final AnAction addAction = new DumbAwareAction(type.getPresentableName(), null, type.getIconForAddAction()) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
doAdd(parent, selectedSdk, type, updateTree);
}
};
group.add(addAction);
}
}
use of com.intellij.openapi.project.DumbAwareAction in project intellij-community by JetBrains.
the class PerFileConfigurableBase method createActionListGroup.
@NotNull
protected ActionGroup createActionListGroup(@Nullable Object target, @NotNull Consumer<T> onChosen) {
DefaultActionGroup group = new DefaultActionGroup();
String clearText = getClearValueText(target);
Function<T, AnAction> choseAction = t -> {
String nullValue = StringUtil.notNullize(clearText);
AnAction a = new DumbAwareAction(renderValue(t, nullValue), "", getActionListIcon(target, t)) {
@Override
public void actionPerformed(AnActionEvent e) {
onChosen.consume(t);
}
};
a.getTemplatePresentation().setText(renderValue(t, nullValue));
return a;
};
if (clearText != null) {
group.add(choseAction.fun(null));
}
SimpleColoredText text = new SimpleColoredText();
List<T> values = new ArrayList<>(getValueVariants(target));
Function<T, String> toString = o -> {
text.clear();
renderValue(target, o, text);
return text.toString();
};
Collections.sort(values, (o1, o2) -> StringUtil.naturalCompare(toString.fun(o1), toString.fun(o2)));
for (T t : values) {
group.add(choseAction.fun(t));
}
return group;
}
use of com.intellij.openapi.project.DumbAwareAction in project intellij-community by JetBrains.
the class ChooseFileEncodingAction method fillCharsetActions.
private void fillCharsetActions(@NotNull DefaultActionGroup group, @Nullable final VirtualFile virtualFile, @NotNull List<Charset> charsets, @NotNull final Function<Charset, String> charsetFilter) {
for (final Charset charset : charsets) {
AnAction action = new DumbAwareAction(charset.displayName(), null, EmptyIcon.ICON_16) {
@Override
public void actionPerformed(AnActionEvent e) {
chosen(virtualFile, charset);
}
@Override
public void update(AnActionEvent e) {
super.update(e);
String description = charsetFilter.fun(charset);
e.getPresentation().setIcon(description == null ? AllIcons.General.Warning : null);
e.getPresentation().setDescription(description);
}
};
group.add(action);
}
}
Aggregations