Search in sources :

Example 56 with ActionCallback

use of com.intellij.openapi.util.ActionCallback in project intellij-community by JetBrains.

the class AbstractCommand method execute.

public final ActionCallback execute(final PlaybackContext context) {
    try {
        if (isToDumpCommand()) {
            dumpCommand(context);
        }
        final ActionCallback result = new ActionCallback();
        Runnable runnable = () -> {
            try {
                _execute(context).notify(result);
            } catch (Throwable e) {
                LOG.error(e);
                context.error(e.getMessage(), getLine());
                result.setRejected();
            }
        };
        if (isAwtThread()) {
            // prevent previous action context affecting next action.
            // E.g. previous action may have called callback.setDone from inside write action, while
            // next action may not expect that
            //noinspection SSBasedInspection
            SwingUtilities.invokeLater(runnable);
        } else {
            ApplicationManager.getApplication().executeOnPooledThread(runnable);
        }
        return result;
    } catch (Throwable e) {
        context.error(e.getMessage(), getLine());
        return ActionCallback.REJECTED;
    }
}
Also used : ActionCallback(com.intellij.openapi.util.ActionCallback)

Example 57 with ActionCallback

use of com.intellij.openapi.util.ActionCallback in project intellij-community by JetBrains.

the class ActionCommand method _execute.

protected ActionCallback _execute(final PlaybackContext context) {
    final String actionName = getText().substring(PREFIX.length()).trim();
    final ActionManager am = ActionManager.getInstance();
    final AnAction targetAction = am.getAction(actionName);
    if (targetAction == null) {
        dumpError(context, "Unknown action: " + actionName);
        return ActionCallback.REJECTED;
    }
    if (!context.isUseDirectActionCall()) {
        final Shortcut[] sc = KeymapManager.getInstance().getActiveKeymap().getShortcuts(actionName);
        KeyStroke stroke = null;
        for (Shortcut each : sc) {
            if (each instanceof KeyboardShortcut) {
                final KeyboardShortcut ks = (KeyboardShortcut) each;
                final KeyStroke first = ks.getFirstKeyStroke();
                final KeyStroke second = ks.getSecondKeyStroke();
                if (second == null) {
                    stroke = KeyStroke.getKeyStroke(first.getKeyCode(), first.getModifiers(), false);
                    break;
                }
            }
        }
        if (stroke != null) {
            final ActionCallback result = new TimedOutCallback(Registry.intValue("actionSystem.commandProcessingTimeout"), "Timed out calling action id=" + actionName, new Throwable(), true) {

                @Override
                protected void dumpError() {
                    context.error(getMessage(), getLine());
                }
            };
            context.message("Invoking action via shortcut: " + stroke.toString(), getLine());
            final KeyStroke finalStroke = stroke;
            inWriteSafeContext(() -> {
                final Ref<AnActionListener> listener = new Ref<>();
                listener.set(new AnActionListener.Adapter() {

                    @Override
                    public void beforeActionPerformed(final AnAction action, DataContext dataContext, AnActionEvent event) {
                        ApplicationManager.getApplication().invokeLater(() -> {
                            if (context.isDisposed()) {
                                am.removeAnActionListener(listener.get());
                                return;
                            }
                            if (targetAction.equals(action)) {
                                context.message("Performed action: " + actionName, context.getCurrentLine());
                                am.removeAnActionListener(listener.get());
                                result.setDone();
                            }
                        }, ModalityState.any());
                    }
                });
                am.addAnActionListener(listener.get());
                context.runPooledThread(() -> type(context.getRobot(), finalStroke));
            });
            return result;
        }
    }
    final InputEvent input = getInputEvent(actionName);
    final ActionCallback result = new ActionCallback();
    context.getRobot().delay(Registry.intValue("actionSystem.playback.delay"));
    ApplicationManager.getApplication().invokeLater(() -> am.tryToExecute(targetAction, input, null, null, false).doWhenProcessed(result.createSetDoneRunnable()), ModalityState.any());
    return result;
}
Also used : TimedOutCallback(com.intellij.openapi.util.TimedOutCallback) ActionCallback(com.intellij.openapi.util.ActionCallback) Ref(com.intellij.openapi.util.Ref) AnActionListener(com.intellij.openapi.actionSystem.ex.AnActionListener) InputEvent(java.awt.event.InputEvent)

Example 58 with ActionCallback

use of com.intellij.openapi.util.ActionCallback in project intellij-community by JetBrains.

the class KeyCodeTypeCommand method _execute.

@Override
public ActionCallback _execute(final PlaybackContext context) {
    String text = getText().substring(PREFIX.length()).trim();
    int textDelim = text.indexOf(" ");
    final String codes;
    if (textDelim >= 0) {
        codes = text.substring(0, textDelim);
    } else {
        codes = text;
    }
    final String unicode;
    if (codes.length() + 1 < text.length()) {
        unicode = text.substring(textDelim + 1);
    } else {
        unicode = "";
    }
    final ActionCallback result = new ActionCallback();
    inWriteSafeContext(() -> {
        TypingTarget typingTarget = findTarget(context);
        if (typingTarget != null) {
            typingTarget.type(unicode).doWhenDone(result.createSetDoneRunnable()).doWhenRejected(() -> typeCodes(context, context.getRobot(), codes).notify(result));
        } else {
            typeCodes(context, context.getRobot(), codes).notify(result);
        }
    });
    return result;
}
Also used : TypingTarget(com.intellij.openapi.ui.TypingTarget) ActionCallback(com.intellij.openapi.util.ActionCallback)

Example 59 with ActionCallback

use of com.intellij.openapi.util.ActionCallback in project intellij-community by JetBrains.

the class GridCellImpl method restoreLastUiState.

public ActionCallback restoreLastUiState() {
    final ActionCallback result = new ActionCallback();
    restoreProportions();
    final Content[] contents = getContents();
    final List<Content> toMinimize = new SmartList<>();
    int window = 0;
    for (final Content each : contents) {
        final View view = myContainer.getStateFor(each);
        if (view.isMinimizedInGrid()) {
            toMinimize.add(each);
        }
        window = view.getWindow();
    }
    minimize(toMinimize.toArray(new Content[toMinimize.size()]));
    final Tab tab = myContainer.getTab();
    final boolean detached = (tab != null && tab.isDetached(myPlaceInGrid)) || window != myContext.getWindow();
    if (detached && contents.length > 0) {
        if (tab != null) {
            tab.setDetached(myPlaceInGrid, false);
        }
        myContext.detachTo(window, this).notifyWhenDone(result);
    } else {
        result.setDone();
    }
    return result;
}
Also used : ActionCallback(com.intellij.openapi.util.ActionCallback) Content(com.intellij.ui.content.Content) SmartList(com.intellij.util.SmartList) RelativePoint(com.intellij.ui.awt.RelativePoint)

Example 60 with ActionCallback

use of com.intellij.openapi.util.ActionCallback in project intellij-community by JetBrains.

the class FavoritesViewSelectInTarget method select.

private static ActionCallback select(@NotNull Project project, Object toSelect, VirtualFile virtualFile, boolean requestFocus) {
    final ActionCallback result = new ActionCallback();
    ToolWindowManager windowManager = ToolWindowManager.getInstance(project);
    final ToolWindow favoritesToolWindow = windowManager.getToolWindow(ToolWindowId.FAVORITES_VIEW);
    if (favoritesToolWindow != null) {
        final Runnable runnable = () -> {
            final FavoritesTreeViewPanel panel = UIUtil.findComponentOfType(favoritesToolWindow.getComponent(), FavoritesTreeViewPanel.class);
            if (panel != null) {
                panel.selectElement(toSelect, virtualFile, requestFocus);
                result.setDone();
            }
        };
        if (requestFocus) {
            favoritesToolWindow.activate(runnable, false);
        } else {
            favoritesToolWindow.show(runnable);
        }
    }
    return result;
}
Also used : ToolWindow(com.intellij.openapi.wm.ToolWindow) ActionCallback(com.intellij.openapi.util.ActionCallback) ToolWindowManager(com.intellij.openapi.wm.ToolWindowManager)

Aggregations

ActionCallback (com.intellij.openapi.util.ActionCallback)70 NotNull (org.jetbrains.annotations.NotNull)16 IOException (java.io.IOException)5 Notification (com.intellij.notification.Notification)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 ArrayList (java.util.ArrayList)4 ProjectView (com.intellij.ide.projectView.ProjectView)3 Configurable (com.intellij.openapi.options.Configurable)3 SearchableConfigurable (com.intellij.openapi.options.SearchableConfigurable)3 AsyncResult (com.intellij.openapi.util.AsyncResult)3 RelativePoint (com.intellij.ui.awt.RelativePoint)3 DocumentInfo (com.intellij.flex.uiDesigner.DocumentFactoryManager.DocumentInfo)2 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)2 Task (com.intellij.openapi.progress.Task)2 Project (com.intellij.openapi.project.Project)2 LibraryOrderEntry (com.intellij.openapi.roots.LibraryOrderEntry)2 MasterDetailsComponent (com.intellij.openapi.ui.MasterDetailsComponent)2 NamedConfigurable (com.intellij.openapi.ui.NamedConfigurable)2 TypingTarget (com.intellij.openapi.ui.TypingTarget)2 FocusCommand (com.intellij.openapi.wm.FocusCommand)2