use of com.intellij.openapi.project.DumbAwareAction in project intellij-community by JetBrains.
the class JBTabbedTerminalWidget method convertActions.
public static void convertActions(@NotNull JComponent component, @NotNull List<TerminalAction> actions, @Nullable final Predicate<KeyEvent> elseAction) {
for (final TerminalAction action : actions) {
if (action.isHidden()) {
continue;
}
AnAction a = new DumbAwareAction() {
@Override
public void actionPerformed(AnActionEvent e) {
KeyEvent event = e.getInputEvent() instanceof KeyEvent ? (KeyEvent) e.getInputEvent() : null;
if (!action.perform(event)) {
if (elseAction != null) {
elseAction.apply(event);
}
}
}
};
a.registerCustomShortcutSet(action.getKeyCode(), action.getModifiers(), component);
}
}
use of com.intellij.openapi.project.DumbAwareAction in project intellij-community by JetBrains.
the class CaptureConfigurable method createComponent.
@Nullable
@Override
public JComponent createComponent() {
myTableModel = new MyTableModel();
JBTable table = new JBTable(myTableModel);
table.setColumnSelectionAllowed(false);
TableColumnModel columnModel = table.getColumnModel();
TableUtil.setupCheckboxColumn(columnModel.getColumn(MyTableModel.ENABLED_COLUMN));
ToolbarDecorator decorator = ToolbarDecorator.createDecorator(table);
decorator.setAddAction(new AnActionButtonRunnable() {
@Override
public void run(AnActionButton button) {
myTableModel.addRow();
}
});
decorator.setRemoveAction(new AnActionButtonRunnable() {
@Override
public void run(AnActionButton button) {
TableUtil.removeSelectedItems(table);
}
});
decorator.setMoveUpAction(new AnActionButtonRunnable() {
@Override
public void run(AnActionButton button) {
TableUtil.moveSelectedItemsUp(table);
}
});
decorator.setMoveDownAction(new AnActionButtonRunnable() {
@Override
public void run(AnActionButton button) {
TableUtil.moveSelectedItemsDown(table);
}
});
decorator.addExtraAction(new DumbAwareActionButton("Duplicate", "Duplicate", PlatformIcons.COPY_ICON) {
@Override
public boolean isEnabled() {
return table.getSelectedRowCount() == 1;
}
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
selectedCapturePoints(table).forEach(c -> {
try {
int idx = myTableModel.add(c.clone());
table.getSelectionModel().setSelectionInterval(idx, idx);
} catch (CloneNotSupportedException ex) {
LOG.error(ex);
}
});
}
});
decorator.addExtraAction(new DumbAwareActionButton("Enable Selected", "Enable Selected", PlatformIcons.SELECT_ALL_ICON) {
@Override
public boolean isEnabled() {
return table.getSelectedRowCount() > 0;
}
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
selectedCapturePoints(table).forEach(c -> c.myEnabled = true);
table.repaint();
}
});
decorator.addExtraAction(new DumbAwareActionButton("Disable Selected", "Disable Selected", PlatformIcons.UNSELECT_ALL_ICON) {
@Override
public boolean isEnabled() {
return table.getSelectedRowCount() > 0;
}
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
selectedCapturePoints(table).forEach(c -> c.myEnabled = false);
table.repaint();
}
});
new DumbAwareAction("Toggle") {
@Override
public void update(@NotNull AnActionEvent e) {
e.getPresentation().setEnabled(table.getSelectedRowCount() == 1);
}
@Override
public void actionPerformed(@NotNull final AnActionEvent e) {
selectedCapturePoints(table).forEach(c -> c.myEnabled = !c.myEnabled);
table.repaint();
}
}.registerCustomShortcutSet(new CustomShortcutSet(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0)), table);
decorator.addExtraAction(new DumbAwareActionButton("Import", "Import", AllIcons.Actions.Install) {
@Override
public void actionPerformed(@NotNull final AnActionEvent e) {
FileChooserDescriptor descriptor = new FileChooserDescriptor(true, false, true, false, true, true) {
@Override
public boolean isFileVisible(VirtualFile file, boolean showHiddenFiles) {
return super.isFileVisible(file, showHiddenFiles) && (file.isDirectory() || "xml".equals(file.getExtension()) || file.getFileType() == FileTypes.ARCHIVE);
}
@Override
public boolean isFileSelectable(VirtualFile file) {
return file.getFileType() == StdFileTypes.XML;
}
};
descriptor.setDescription("Please select a file to import.");
descriptor.setTitle("Import Capture Points");
VirtualFile[] files = FileChooser.chooseFiles(descriptor, e.getProject(), null);
if (ArrayUtil.isEmpty(files))
return;
table.getSelectionModel().clearSelection();
for (VirtualFile file : files) {
try {
Document document = JDOMUtil.loadDocument(file.getInputStream());
List<Element> children = document.getRootElement().getChildren();
children.forEach(element -> {
int idx = myTableModel.addIfNeeded(XmlSerializer.deserialize(element, CapturePoint.class));
table.getSelectionModel().addSelectionInterval(idx, idx);
});
} catch (Exception ex) {
final String msg = ex.getLocalizedMessage();
Messages.showErrorDialog(e.getProject(), msg != null && msg.length() > 0 ? msg : ex.toString(), "Export Failed");
}
}
}
});
decorator.addExtraAction(new DumbAwareActionButton("Export", "Export", AllIcons.Actions.Export) {
@Override
public void actionPerformed(@NotNull final AnActionEvent e) {
VirtualFileWrapper wrapper = FileChooserFactory.getInstance().createSaveFileDialog(new FileSaverDescriptor("Export Selected Capture Points to File...", "", "xml"), e.getProject()).save(null, null);
if (wrapper == null)
return;
Element rootElement = new Element("capture-points");
selectedCapturePoints(table).forEach(c -> {
try {
CapturePoint clone = c.clone();
clone.myEnabled = false;
rootElement.addContent(XmlSerializer.serialize(clone));
} catch (CloneNotSupportedException ex) {
LOG.error(ex);
}
});
try {
JDOMUtil.write(rootElement, wrapper.getFile());
} catch (Exception ex) {
final String msg = ex.getLocalizedMessage();
Messages.showErrorDialog(e.getProject(), msg != null && msg.length() > 0 ? msg : ex.toString(), "Export Failed");
}
}
@Override
public boolean isEnabled() {
return table.getSelectedRowCount() > 0;
}
});
BorderLayoutPanel panel = JBUI.Panels.simplePanel();
panel.addToCenter(decorator.createPanel());
myCaptureVariables = new JCheckBox(DebuggerBundle.message("label.capture.configurable.capture.variables"));
panel.addToBottom(myCaptureVariables);
return panel;
}
use of com.intellij.openapi.project.DumbAwareAction in project intellij-community by JetBrains.
the class ShowUsagesAction method createUsagePopup.
@NotNull
private JBPopup createUsagePopup(@NotNull final List<Usage> usages, @NotNull Set<UsageNode> visibleNodes, @NotNull final FindUsagesHandler handler, final Editor editor, @NotNull final RelativePoint popupPosition, final int maxUsages, @NotNull final UsageViewImpl usageView, @NotNull final FindUsagesOptions options, @NotNull final JTable table, @NotNull final Runnable itemChoseCallback, @NotNull final UsageViewPresentation presentation, @NotNull final AsyncProcessIcon processIcon) {
ApplicationManager.getApplication().assertIsDispatchThread();
PopupChooserBuilder builder = new PopupChooserBuilder(table);
final String title = presentation.getTabText();
if (title != null) {
String result = getFullTitle(usages, title, false, visibleNodes.size() - 1, true);
builder.setTitle(result);
builder.setAdText(getSecondInvocationTitle(options, handler));
}
builder.setMovable(true).setResizable(true);
builder.setMovable(true).setResizable(true);
builder.setItemChoosenCallback(itemChoseCallback);
final JBPopup[] popup = new JBPopup[1];
KeyboardShortcut shortcut = UsageViewImpl.getShowUsagesWithSettingsShortcut();
if (shortcut != null) {
new DumbAwareAction() {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
cancel(popup[0]);
showDialogAndFindUsages(handler, popupPosition, editor, maxUsages);
}
@Override
public boolean startInTransaction() {
return true;
}
}.registerCustomShortcutSet(new CustomShortcutSet(shortcut.getFirstKeyStroke()), table);
}
shortcut = getShowUsagesShortcut();
if (shortcut != null) {
new DumbAwareAction() {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
cancel(popup[0]);
searchEverywhere(options, handler, editor, popupPosition, maxUsages);
}
@Override
public boolean startInTransaction() {
return true;
}
}.registerCustomShortcutSet(new CustomShortcutSet(shortcut.getFirstKeyStroke()), table);
}
InplaceButton settingsButton = createSettingsButton(handler, popupPosition, editor, maxUsages, () -> cancel(popup[0]));
ActiveComponent spinningProgress = new ActiveComponent.Adapter() {
@Override
public JComponent getComponent() {
return processIcon;
}
};
final DefaultActionGroup pinGroup = new DefaultActionGroup();
final ActiveComponent pin = createPinButton(handler, usageView, options, popup, pinGroup);
builder.setCommandButton(new CompositeActiveComponent(spinningProgress, settingsButton, pin));
DefaultActionGroup toolbar = new DefaultActionGroup();
usageView.addFilteringActions(toolbar);
toolbar.add(UsageGroupingRuleProviderImpl.createGroupByFileStructureAction(usageView));
ActionToolbar actionToolbar = ActionManager.getInstance().createActionToolbar(ActionPlaces.USAGE_VIEW_TOOLBAR, toolbar, true);
actionToolbar.setReservePlaceAutoPopupIcon(false);
final JComponent toolBar = actionToolbar.getComponent();
toolBar.setOpaque(false);
builder.setSettingButton(toolBar);
builder.setCancelKeyEnabled(false);
popup[0] = builder.createPopup();
JComponent content = popup[0].getContent();
myWidth = (int) (toolBar.getPreferredSize().getWidth() + new JLabel(getFullTitle(usages, title, false, visibleNodes.size() - 1, true)).getPreferredSize().getWidth() + settingsButton.getPreferredSize().getWidth());
myWidth = -1;
for (AnAction action : toolbar.getChildren(null)) {
action.unregisterCustomShortcutSet(usageView.getComponent());
action.registerCustomShortcutSet(action.getShortcutSet(), content);
}
for (AnAction action : pinGroup.getChildren(null)) {
action.unregisterCustomShortcutSet(usageView.getComponent());
action.registerCustomShortcutSet(action.getShortcutSet(), content);
}
return popup[0];
}
use of com.intellij.openapi.project.DumbAwareAction in project intellij-community by JetBrains.
the class GotoActionAction method createPopup.
@Nullable
private static ChooseByNamePopup createPopup(@Nullable Project project, @NotNull final GotoActionModel model, String initialText, int initialIndex, final Component component, final AnActionEvent e) {
ChooseByNamePopup oldPopup = project == null ? null : project.getUserData(ChooseByNamePopup.CHOOSE_BY_NAME_POPUP_IN_PROJECT_KEY);
if (oldPopup != null) {
oldPopup.close(false);
}
final Disposable disposable = Disposer.newDisposable();
final ChooseByNamePopup popup = new ChooseByNamePopup(project, model, new GotoActionItemProvider(model), oldPopup, initialText, false, initialIndex) {
private boolean myPaintInternalInfo;
@Override
protected void initUI(Callback callback, ModalityState modalityState, boolean allowMultipleSelection) {
super.initUI(callback, modalityState, allowMultipleSelection);
myList.addListSelectionListener(new ListSelectionListener() {
@Override
public void valueChanged(ListSelectionEvent e) {
Object value = myList.getSelectedValue();
String text = getText(value);
if (text != null && myDropdownPopup != null) {
myDropdownPopup.setAdText(text, SwingConstants.LEFT);
}
String description = getValueDescription(value);
ActionMenu.showDescriptionInStatusBar(true, myList, description);
}
@Nullable
private String getText(@Nullable Object o) {
if (o instanceof GotoActionModel.MatchedValue) {
GotoActionModel.MatchedValue mv = (GotoActionModel.MatchedValue) o;
if (myPaintInternalInfo) {
if (mv.value instanceof GotoActionModel.ActionWrapper) {
AnAction action = ((GotoActionModel.ActionWrapper) mv.value).getAction();
String actionId = ActionManager.getInstance().getId(action);
return StringUtil.notNullize(actionId, "class: " + action.getClass().getName());
}
}
if (mv.value instanceof BooleanOptionDescription || mv.value instanceof GotoActionModel.ActionWrapper && ((GotoActionModel.ActionWrapper) mv.value).getAction() instanceof ToggleAction) {
return "Press " + KeymapUtil.getKeystrokeText(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0)) + " to toggle option";
}
}
return getAdText();
}
});
myList.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
int index = myList.locationToIndex(e.getPoint());
if (index == -1)
return;
Object value = myList.getModel().getElementAt(index);
String description = getValueDescription(value);
ActionMenu.showDescriptionInStatusBar(true, myList, description);
}
});
if (Registry.is("show.configurables.ids.in.settings")) {
new HeldDownKeyListener() {
@Override
protected void heldKeyTriggered(JComponent component, boolean pressed) {
myPaintInternalInfo = pressed;
// an easy way to repaint the AdText
ListSelectionEvent event = new ListSelectionEvent(this, -1, -1, false);
for (ListSelectionListener listener : myList.getListSelectionListeners()) {
listener.valueChanged(event);
}
}
}.installOn(myTextField);
}
}
@Nullable
private String getValueDescription(@Nullable Object value) {
if (value instanceof GotoActionModel.MatchedValue) {
GotoActionModel.MatchedValue mv = (GotoActionModel.MatchedValue) value;
if (mv.value instanceof GotoActionModel.ActionWrapper) {
AnAction action = ((GotoActionModel.ActionWrapper) mv.value).getAction();
return action.getTemplatePresentation().getDescription();
}
}
return null;
}
@NotNull
@Override
protected Set<Object> filter(@NotNull Set<Object> elements) {
return super.filter(model.sortItems(elements));
}
@Override
protected boolean closeForbidden(boolean ok) {
if (!ok)
return false;
Object element = getChosenElement();
return element instanceof GotoActionModel.MatchedValue && processOptionInplace(((GotoActionModel.MatchedValue) element).value, this, component, e) || super.closeForbidden(true);
}
@Override
public void setDisposed(boolean disposedFlag) {
super.setDisposed(disposedFlag);
Disposer.dispose(disposable);
ActionMenu.showDescriptionInStatusBar(true, myList, null);
for (ListSelectionListener listener : myList.getListSelectionListeners()) {
myList.removeListSelectionListener(listener);
}
UIUtil.dispose(myList);
}
};
ApplicationManager.getApplication().getMessageBus().connect(disposable).subscribe(ProgressWindow.TOPIC, new ProgressWindow.Listener() {
@Override
public void progressWindowCreated(ProgressWindow pw) {
Disposer.register(pw, new Disposable() {
@Override
public void dispose() {
if (!popup.checkDisposed()) {
popup.repaintList();
}
}
});
}
});
if (project != null) {
project.putUserData(ChooseByNamePopup.CHOOSE_BY_NAME_POPUP_IN_PROJECT_KEY, popup);
}
popup.addMouseClickListener(new MouseAdapter() {
@Override
public void mouseClicked(@NotNull MouseEvent me) {
Object element = popup.getSelectionByPoint(me.getPoint());
if (element instanceof GotoActionModel.MatchedValue) {
if (processOptionInplace(((GotoActionModel.MatchedValue) element).value, popup, component, e)) {
me.consume();
}
}
}
});
CustomShortcutSet shortcutSet = new CustomShortcutSet(KeymapManager.getInstance().getActiveKeymap().getShortcuts(IdeActions.ACTION_SHOW_INTENTION_ACTIONS));
new DumbAwareAction() {
@Override
public void actionPerformed(AnActionEvent e) {
Object o = popup.getChosenElement();
if (o instanceof GotoActionModel.MatchedValue) {
Comparable value = ((GotoActionModel.MatchedValue) o).value;
if (value instanceof GotoActionModel.ActionWrapper) {
GotoActionModel.ActionWrapper aw = (GotoActionModel.ActionWrapper) value;
boolean available = aw.isAvailable();
if (available) {
AnAction action = aw.getAction();
String id = ActionManager.getInstance().getId(action);
KeymapManagerImpl km = ((KeymapManagerImpl) KeymapManager.getInstance());
Keymap k = km.getActiveKeymap();
if (!k.canModify())
return;
KeymapPanel.addKeyboardShortcut(id, ActionShortcutRestrictions.getInstance().getForActionId(id), k, component);
popup.repaintListImmediate();
}
}
}
}
}.registerCustomShortcutSet(shortcutSet, popup.getTextField(), disposable);
return popup;
}
use of com.intellij.openapi.project.DumbAwareAction in project intellij-community by JetBrains.
the class GotoActionBase method showNavigationPopup.
protected <T> void showNavigationPopup(final GotoActionCallback<T> callback, @Nullable final String findUsagesTitle, final ChooseByNamePopup popup, final boolean allowMultipleSelection) {
final Class startedAction = myInAction;
LOG.assertTrue(startedAction != null);
popup.setCheckBoxShortcut(getShortcutSet());
popup.setFindUsagesTitle(findUsagesTitle);
final ChooseByNameFilter<T> filter = callback.createFilter(popup);
if (historyEnabled() && popup.getAdText() == null) {
popup.setAdText("Press " + KeymapUtil.getKeystrokeText(KeyStroke.getKeyStroke(KeyEvent.VK_UP, InputEvent.CTRL_MASK)) + " or " + KeymapUtil.getKeystrokeText(KeyStroke.getKeyStroke(KeyEvent.VK_DOWN, InputEvent.CTRL_MASK)) + " to navigate through the history");
}
popup.invoke(new ChooseByNamePopupComponent.Callback() {
@Override
public void onClose() {
//noinspection ConstantConditions
if (startedAction != null && startedAction.equals(myInAction)) {
String text = popup.getEnteredText();
ourLastStrings.put(myInAction, Pair.create(text, popup.getSelectedIndex()));
updateHistory(text);
myInAction = null;
}
if (filter != null) {
filter.close();
}
}
private void updateHistory(@Nullable String text) {
if (!StringUtil.isEmptyOrSpaces(text)) {
List<String> history = ourHistory.get(myInAction);
if (history == null)
history = ContainerUtil.newArrayList();
if (!text.equals(ContainerUtil.getFirstItem(history))) {
history.add(0, text);
}
ourHistory.put(myInAction, history);
}
}
@Override
public void elementChosen(Object element) {
callback.elementChosen(popup, element);
}
}, ModalityState.current(), allowMultipleSelection);
final JTextField editor = popup.getTextField();
final DocumentAdapter historyResetListener = new DocumentAdapter() {
@Override
protected void textChanged(DocumentEvent e) {
myHistoryIndex = 0;
}
};
abstract class HistoryAction extends DumbAwareAction {
@Override
public void update(@NotNull AnActionEvent e) {
e.getPresentation().setEnabled(historyEnabled());
}
void setText(@NotNull List<String> strings) {
javax.swing.text.Document document = editor.getDocument();
document.removeDocumentListener(historyResetListener);
editor.setText(strings.get(myHistoryIndex));
document.addDocumentListener(historyResetListener);
editor.selectAll();
}
}
editor.getDocument().addDocumentListener(historyResetListener);
new HistoryAction() {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
List<String> strings = ourHistory.get(myInAction);
setText(strings);
myHistoryIndex = myHistoryIndex >= strings.size() - 1 ? 0 : myHistoryIndex + 1;
}
}.registerCustomShortcutSet(CustomShortcutSet.fromString("ctrl UP"), editor);
new HistoryAction() {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
List<String> strings = ourHistory.get(myInAction);
setText(strings);
myHistoryIndex = myHistoryIndex <= 0 ? strings.size() - 1 : myHistoryIndex - 1;
}
}.registerCustomShortcutSet(CustomShortcutSet.fromString("ctrl DOWN"), editor);
}
Aggregations