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;
}
});
}
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;
}
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;
}
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;
}
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;
}
Aggregations