Search in sources :

Example 1 with AsyncResult

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

the class MavenAttachSourcesProvider method getActions.

@Override
@NotNull
public Collection<AttachSourcesAction> getActions(final List<LibraryOrderEntry> orderEntries, final PsiFile psiFile) {
    Collection<MavenProject> projects = getMavenProjects(psiFile);
    if (projects.isEmpty())
        return Collections.emptyList();
    if (findArtifacts(projects, orderEntries).isEmpty())
        return Collections.emptyList();
    return Collections.singleton(new AttachSourcesAction() {

        @Override
        public String getName() {
            return ProjectBundle.message("maven.action.download.sources");
        }

        @Override
        public String getBusyText() {
            return ProjectBundle.message("maven.action.download.sources.busy.text");
        }

        @Override
        public ActionCallback perform(List<LibraryOrderEntry> orderEntries) {
            // may have been changed by this time...
            Collection<MavenProject> mavenProjects = getMavenProjects(psiFile);
            if (mavenProjects.isEmpty()) {
                return ActionCallback.REJECTED;
            }
            MavenProjectsManager manager = MavenProjectsManager.getInstance(psiFile.getProject());
            Collection<MavenArtifact> artifacts = findArtifacts(mavenProjects, orderEntries);
            if (artifacts.isEmpty())
                return ActionCallback.REJECTED;
            final AsyncResult<MavenArtifactDownloader.DownloadResult> result = new AsyncResult<>();
            manager.scheduleArtifactsDownloading(mavenProjects, artifacts, true, false, result);
            final ActionCallback resultWrapper = new ActionCallback();
            result.doWhenDone(new Consumer<MavenArtifactDownloader.DownloadResult>() {

                @Override
                public void consume(MavenArtifactDownloader.DownloadResult downloadResult) {
                    if (!downloadResult.unresolvedSources.isEmpty()) {
                        final StringBuilder message = new StringBuilder();
                        message.append("<html>Sources not found for:");
                        int count = 0;
                        for (MavenId each : downloadResult.unresolvedSources) {
                            if (count++ > 5) {
                                message.append("<br>and more...");
                                break;
                            }
                            message.append("<br>").append(each.getDisplayString());
                        }
                        message.append("</html>");
                        Notifications.Bus.notify(new Notification(MavenUtil.MAVEN_NOTIFICATION_GROUP, "Cannot download sources", message.toString(), NotificationType.WARNING), psiFile.getProject());
                    }
                    if (downloadResult.resolvedSources.isEmpty()) {
                        resultWrapper.setRejected();
                    } else {
                        resultWrapper.setDone();
                    }
                }
            });
            return resultWrapper;
        }
    });
}
Also used : MavenProjectsManager(org.jetbrains.idea.maven.project.MavenProjectsManager) ActionCallback(com.intellij.openapi.util.ActionCallback) LibraryOrderEntry(com.intellij.openapi.roots.LibraryOrderEntry) Notification(com.intellij.notification.Notification) MavenArtifactDownloader(org.jetbrains.idea.maven.project.MavenArtifactDownloader) MavenId(org.jetbrains.idea.maven.model.MavenId) MavenProject(org.jetbrains.idea.maven.project.MavenProject) Consumer(com.intellij.util.Consumer) Collection(java.util.Collection) AsyncResult(com.intellij.openapi.util.AsyncResult) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with AsyncResult

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

the class WindowSystemPlaybackCall method waitForDialog.

public static AsyncResult<String> waitForDialog(final PlaybackContext context, final String title) {
    final AsyncResult<String> result = new AsyncResult<>();
    final Ref<AWTEventListener> listener = new Ref<>();
    listener.set(new AWTEventListener() {

        @Override
        public void eventDispatched(AWTEvent event) {
            if (event.getID() == WindowEvent.WINDOW_ACTIVATED) {
                final Window wnd = ((WindowEvent) event).getWindow();
                if (wnd instanceof JDialog) {
                    if (title.equals(((JDialog) wnd).getTitle())) {
                        Toolkit.getDefaultToolkit().removeAWTEventListener(listener.get());
                        SwingUtilities.invokeLater(() -> getUiReady(context).notify(result));
                    }
                }
            }
        }
    });
    Toolkit.getDefaultToolkit().addAWTEventListener(listener.get(), WindowEvent.WINDOW_EVENT_MASK);
    SimpleTimer.getInstance().setUp(() -> {
        Toolkit.getDefaultToolkit().removeAWTEventListener(listener.get());
        if (!result.isProcessed()) {
            result.setRejected("Timed out waiting for window: " + title);
        }
    }, Registry.intValue("actionSystem.commandProcessingTimeout"));
    return result;
}
Also used : ToolWindow(com.intellij.openapi.wm.ToolWindow) Ref(com.intellij.openapi.util.Ref) AWTEventListener(java.awt.event.AWTEventListener) AsyncResult(com.intellij.openapi.util.AsyncResult)

Example 3 with AsyncResult

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

the class WindowSystemPlaybackCall method waitForToolWindow.

public static AsyncResult<String> waitForToolWindow(final PlaybackContext context, final String id) {
    final AsyncResult<String> result = new AsyncResult<>();
    findProject().doWhenDone(new Consumer<Project>() {

        @Override
        public void consume(Project project) {
            ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(id);
            if (toolWindow == null) {
                result.setRejected("Cannot find tool window with id: " + id);
                return;
            }
            toolWindow.getReady(context).doWhenDone(result.createSetDoneRunnable()).doWhenRejected(() -> result.setRejected("Cannot activate tool window with id:" + id));
        }
    }).doWhenRejected(() -> result.setRejected("Cannot retrieve open project"));
    return result;
}
Also used : Project(com.intellij.openapi.project.Project) ToolWindow(com.intellij.openapi.wm.ToolWindow) Consumer(com.intellij.util.Consumer) AsyncResult(com.intellij.openapi.util.AsyncResult)

Example 4 with AsyncResult

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

the class WindowSystemPlaybackCall method findProject.

public static AsyncResult<Project> findProject() {
    final AsyncResult<Project> project = new AsyncResult<>();
    final IdeFocusManager fm = IdeFocusManager.getGlobalInstance();
    fm.doWhenFocusSettlesDown(() -> {
        Component parent = UIUtil.findUltimateParent(fm.getFocusOwner());
        if (parent instanceof IdeFrame) {
            IdeFrame frame = (IdeFrame) parent;
            if (frame.getProject() != null) {
                project.setDone(frame.getProject());
                return;
            }
        }
        project.setRejected();
    });
    return project;
}
Also used : Project(com.intellij.openapi.project.Project) IdeFocusManager(com.intellij.openapi.wm.IdeFocusManager) AsyncResult(com.intellij.openapi.util.AsyncResult) IdeFrame(com.intellij.openapi.wm.IdeFrame)

Example 5 with AsyncResult

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

the class CallCommand method _execute.

@Override
protected ActionCallback _execute(final PlaybackContext context) {
    final ActionCallback cmdResult = new ActionCallback();
    final String cmd = getText().substring(PREFIX.length()).trim();
    final int open = cmd.indexOf("(");
    if (open == -1) {
        context.error("( expected", getLine());
        return ActionCallback.DONE;
    }
    final int close = cmd.lastIndexOf(")");
    if (close == -1) {
        context.error(") expected", getLine());
        return ActionCallback.DONE;
    }
    final String methodName = cmd.substring(0, open);
    String[] args = cmd.substring(open + 1, close).split(",");
    final boolean noArgs = args.length == 1 && args[0].length() == 0;
    Class[] types = noArgs ? new Class[1] : new Class[args.length + 1];
    types[0] = PlaybackContext.class;
    for (int i = 1; i < types.length; i++) {
        types[i] = String.class;
    }
    try {
        Pair<Method, Class> methodClass = findMethod(context, methodName, types);
        if (methodClass == null) {
            context.error("No method \"" + methodName + "\" found in facade classes: " + context.getCallClasses(), getLine());
            return ActionCallback.REJECTED;
        }
        Method m = methodClass.getFirst();
        if (!m.getReturnType().isAssignableFrom(AsyncResult.class)) {
            context.error("Method " + methodClass.getSecond() + ":" + methodName + " must return AsyncResult object", getLine());
            return ActionCallback.REJECTED;
        }
        Object[] actualArgs = noArgs ? new Object[1] : new Object[args.length + 1];
        actualArgs[0] = context;
        System.arraycopy(args, 0, actualArgs, 1, actualArgs.length - 1);
        AsyncResult result = (AsyncResult<String>) m.invoke(null, actualArgs);
        if (result == null) {
            context.error("Method " + methodClass.getSecond() + ":" + methodName + " must return AsyncResult object, but was null", getLine());
            return ActionCallback.REJECTED;
        }
        result.doWhenDone(new Consumer<String>() {

            @Override
            public void consume(String s) {
                if (s != null) {
                    context.message(s, getLine());
                }
                cmdResult.setDone();
            }
        }).doWhenRejected(s -> {
            context.error(s, getLine());
            cmdResult.setRejected();
        });
    } catch (InvocationTargetException ignored) {
        context.error("InvocationTargetException while executing command: " + cmd, getLine());
    } catch (IllegalAccessException ignored) {
        context.error("IllegalAccessException while executing command: " + cmd, getLine());
    }
    return cmdResult;
}
Also used : ActionCallback(com.intellij.openapi.util.ActionCallback) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) Consumer(com.intellij.util.Consumer) AsyncResult(com.intellij.openapi.util.AsyncResult)

Aggregations

AsyncResult (com.intellij.openapi.util.AsyncResult)17 Consumer (com.intellij.util.Consumer)8 Project (com.intellij.openapi.project.Project)6 ActionCallback (com.intellij.openapi.util.ActionCallback)5 MouseEvent (java.awt.event.MouseEvent)4 Disposable (com.intellij.openapi.Disposable)3 NotNull (org.jetbrains.annotations.NotNull)3 DocumentInfo (com.intellij.flex.uiDesigner.DocumentFactoryManager.DocumentInfo)2 DataManager (com.intellij.ide.DataManager)2 UISettings (com.intellij.ide.ui.UISettings)2 CustomActionsSchema (com.intellij.ide.ui.customization.CustomActionsSchema)2 com.intellij.openapi.actionSystem (com.intellij.openapi.actionSystem)2 DataContext (com.intellij.openapi.actionSystem.DataContext)2 ApplicationManager (com.intellij.openapi.application.ApplicationManager)2 CommandProcessor (com.intellij.openapi.command.CommandProcessor)2 Document (com.intellij.openapi.editor.Document)2 Editor (com.intellij.openapi.editor.Editor)2 Module (com.intellij.openapi.module.Module)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 IdeFocusManager (com.intellij.openapi.wm.IdeFocusManager)2