use of com.intellij.openapi.util.ActionCallback in project intellij-community by JetBrains.
the class InternetAttachSourceProvider method getActions.
@NotNull
@Override
public Collection<AttachSourcesAction> getActions(List<LibraryOrderEntry> orderEntries, @Nullable PsiFile psiFile) {
final VirtualFile jar = getJarByPsiFile(psiFile);
if (jar == null)
return Collections.emptyList();
final String jarName = jar.getNameWithoutExtension();
int index = jarName.lastIndexOf('-');
if (index == -1)
return Collections.emptyList();
final String version = jarName.substring(index + 1);
final String artifactId = jarName.substring(0, index);
if (!ARTIFACT_IDENTIFIER.matcher(version).matches() || !ARTIFACT_IDENTIFIER.matcher(artifactId).matches()) {
return Collections.emptyList();
}
final Set<Library> libraries = new HashSet<>();
for (LibraryOrderEntry orderEntry : orderEntries) {
ContainerUtil.addIfNotNull(libraries, orderEntry.getLibrary());
}
if (libraries.isEmpty())
return Collections.emptyList();
final String sourceFileName = jarName + "-sources.jar";
for (Library library : libraries) {
for (VirtualFile file : library.getFiles(OrderRootType.SOURCES)) {
if (file.getPath().contains(sourceFileName)) {
if (isRootInExistingFile(file)) {
// Sources already attached, but source-jar doesn't contain current class.
return Collections.emptyList();
}
}
}
}
final File libSourceDir = getLibrarySourceDir();
final File sourceFile = new File(libSourceDir, sourceFileName);
if (sourceFile.exists()) {
return Collections.singleton(new LightAttachSourcesAction() {
@Override
public String getName() {
return "Attach downloaded source";
}
@Override
public String getBusyText() {
return getName();
}
@Override
public ActionCallback perform(List<LibraryOrderEntry> orderEntriesContainingFile) {
attachSourceJar(sourceFile, libraries);
return ActionCallback.DONE;
}
});
}
return Collections.singleton(new LightAttachSourcesAction() {
@Override
public String getName() {
return "Download...";
}
@Override
public String getBusyText() {
return "Searching...";
}
@Override
public ActionCallback perform(List<LibraryOrderEntry> orderEntriesContainingFile) {
final Task task = new Task.Modal(psiFile.getProject(), "Searching source...", true) {
@Override
public void run(@NotNull final ProgressIndicator indicator) {
String artifactUrl = null;
SourceSearcher[] searchers = { new MavenCentralSourceSearcher(), new SonatypeSourceSearcher() };
for (SourceSearcher searcher : searchers) {
try {
artifactUrl = searcher.findSourceJar(indicator, artifactId, version, jar);
} catch (SourceSearchException e) {
LOG.warn(e);
showMessage("Downloading failed", e.getMessage(), NotificationType.ERROR);
continue;
}
if (artifactUrl != null)
break;
}
if (artifactUrl == null) {
showMessage("Sources not found", "Sources for '" + jarName + ".jar' not found", NotificationType.WARNING);
return;
}
if (!(libSourceDir.isDirectory() || libSourceDir.mkdirs())) {
showMessage("Downloading failed", "Failed to create directory to store sources: " + libSourceDir, NotificationType.ERROR);
return;
}
try {
File tmpDownload = FileUtil.createTempFile(libSourceDir, "download.", ".tmp", false, false);
HttpRequests.request(artifactUrl).saveToFile(tmpDownload, indicator);
if (!sourceFile.exists() && !tmpDownload.renameTo(sourceFile)) {
LOG.warn("Failed to rename file " + tmpDownload + " to " + sourceFileName);
}
} catch (IOException e) {
LOG.warn(e);
showMessage("Downloading failed", "Connection problem. See log for more details.", NotificationType.ERROR);
}
}
@Override
public void onSuccess() {
attachSourceJar(sourceFile, libraries);
}
private void showMessage(String title, String message, NotificationType notificationType) {
new Notification("Source searcher", title, message, notificationType).notify(getProject());
}
};
task.queue();
return ActionCallback.DONE;
}
});
}
use of com.intellij.openapi.util.ActionCallback in project intellij-community by JetBrains.
the class SettingsTreeView method select.
ActionCallback select(@Nullable final Configurable configurable) {
if (myBuilder.isSelectionBeingAdjusted()) {
return ActionCallback.REJECTED;
}
final ActionCallback callback = new ActionCallback();
myQueuedConfigurable = configurable;
myQueue.queue(new Update(this) {
public void run() {
if (configurable == myQueuedConfigurable) {
if (configurable == null) {
fireSelected(null, callback);
} else {
myBuilder.getReady(this).doWhenDone(() -> {
if (configurable != myQueuedConfigurable)
return;
MyNode editorNode = findNode(configurable);
FilteringTreeStructure.FilteringNode editorUiNode = myBuilder.getVisibleNodeFor(editorNode);
if (editorUiNode == null)
return;
if (!myBuilder.getSelectedElements().contains(editorUiNode)) {
myBuilder.select(editorUiNode, () -> fireSelected(configurable, callback));
} else {
myBuilder.scrollSelectionToVisible(() -> fireSelected(configurable, callback), false);
}
});
}
}
}
@Override
public void setRejected() {
super.setRejected();
callback.setRejected();
}
});
return callback;
}
use of com.intellij.openapi.util.ActionCallback in project intellij-community by JetBrains.
the class ToolWindowImpl method getReady.
@NotNull
@Override
public ActionCallback getReady(@NotNull final Object requestor) {
final ActionCallback result = new ActionCallback();
myShowing.getReady(this).doWhenDone(() -> {
ArrayList<FinalizableCommand> cmd = new ArrayList<>();
cmd.add(new FinalizableCommand(null) {
@Override
public void run() {
IdeFocusManager.getInstance(myToolWindowManager.getProject()).doWhenFocusSettlesDown(() -> {
if (myContentManager.isDisposed())
return;
myContentManager.getReady(requestor).notify(result);
});
}
});
myToolWindowManager.execute(cmd);
});
return result;
}
use of com.intellij.openapi.util.ActionCallback in project android by JetBrains.
the class CompatibilityChecksMetadataUpdater method fetchMetadata.
@NotNull
private static ActionCallback fetchMetadata() {
ActionCallback callback = new ActionCallback();
ApplicationManager.getApplication().executeOnPooledThread(() -> {
String url = "https://dl.google.com/android/studio/metadata/android-component-compatibility.xml";
try {
Document metadata = HttpRequests.request(url).connect(request -> {
try {
return loadDocument(request.getInputStream());
} catch (JDOMException e) {
LOG.info("Failed to parse XML metadata", e);
return null;
} catch (Throwable e) {
LOG.info("Failed to parse XML metadata", e);
return null;
}
});
if (metadata != null) {
VersionCompatibilityChecker.getInstance().updateMetadata(metadata);
callback.setDone();
}
} catch (IOException e) {
LOG.info(String.format("Failed to connect to '%1$s'", url), e);
}
callback.setRejected();
});
return callback;
}
use of com.intellij.openapi.util.ActionCallback in project intellij-community by JetBrains.
the class UpdaterTreeState method processHangByParent.
private ActionCallback processHangByParent(Object each) {
ActionCallback result = new ActionCallback();
processNextHang(each, result);
return result;
}
Aggregations