use of org.eclipse.che.api.promises.client.Promise in project che by eclipse.
the class DeleteResourceManager method onConfirm.
private ConfirmCallback onConfirm(final Resource[] resources, final AsyncCallback<Void> callback) {
return new ConfirmCallback() {
@Override
public void accepted() {
if (resources == null) {
//sometimes we may occur NPE here while reading length
callback.onFailure(new IllegalStateException());
return;
}
Promise<?>[] deleteAll = new Promise<?>[resources.length];
for (int i = 0; i < resources.length; i++) {
final Resource resource = resources[i];
deleteAll[i] = resource.delete().catchError(new Operation<PromiseError>() {
@Override
public void apply(PromiseError error) throws OperationException {
notificationManager.notify("Failed to delete '" + resource.getName() + "'", error.getMessage(), FAIL, StatusNotification.DisplayMode.FLOAT_MODE);
}
});
}
promiseProvider.all(deleteAll).then(new Operation<JsArrayMixed>() {
@Override
public void apply(JsArrayMixed arg) throws OperationException {
callback.onSuccess(null);
}
});
}
};
}
use of org.eclipse.che.api.promises.client.Promise in project che by eclipse.
the class JavaProjectNode method getChildrenImpl.
@Override
protected Promise<List<Node>> getChildrenImpl() {
return createFromAsyncRequest(callback -> {
final List<Node> childrenNodes = new ArrayList<>();
for (PackageFragmentRoot packageFragmentRoot : project.getPackageFragmentRoots()) {
final List<PackageFragment> packageFragments = packageFragmentRoot.getPackageFragments();
final List<Node> nodes = packageFragments.stream().map(packageFragment -> nodeFactory.create(packageFragment, matches, packageFragmentRoot)).collect(Collectors.toList());
childrenNodes.addAll(nodes);
}
callback.onSuccess(childrenNodes);
});
}
use of org.eclipse.che.api.promises.client.Promise in project che by eclipse.
the class TypeNode method getChildrenImpl.
@Override
protected Promise<List<Node>> getChildrenImpl() {
return createFromAsyncRequest(callback -> {
List<Node> children = new ArrayList<>();
if (compilationUnit != null && type.isPrimary()) {
for (ImportDeclaration importDeclaration : compilationUnit.getImports()) {
createNodeForAllMatches(importDeclaration.getHandleIdentifier(), children);
}
for (Type subType : compilationUnit.getTypes()) {
if (subType == type) {
continue;
}
children.add(nodeFactory.create(subType, compilationUnit, classFile, matches));
}
}
createNodeForAllMatches(type.getHandleIdentifier(), children);
for (Initializer initializer : type.getInitializers()) {
createNodeForAllMatches(initializer.getHandleIdentifier(), children);
}
for (Field field : type.getFields()) {
createNodeForAllMatches(field.getHandleIdentifier(), children);
}
final List<Node> typeNodes = type.getTypes().stream().map(subType -> nodeFactory.create(subType, compilationUnit, classFile, matches)).collect(Collectors.toList());
children.addAll(typeNodes);
final List<Node> methodNodes = type.getMethods().stream().map(method -> nodeFactory.create(method, matches, compilationUnit, classFile)).collect(Collectors.toList());
children.addAll(methodNodes);
Collections.sort(children, new NodeComparator());
callback.onSuccess(children);
});
}
use of org.eclipse.che.api.promises.client.Promise in project che by eclipse.
the class ProjectWizard method complete.
/** {@inheritDoc} */
@Override
public void complete(@NotNull final CompleteCallback callback) {
if (mode == CREATE) {
appContext.getWorkspaceRoot().newProject().withBody(dataObject).send().then(onComplete(callback)).catchError(onFailure(callback));
} else if (mode == UPDATE) {
appContext.getWorkspaceRoot().getContainer(Path.valueOf(dataObject.getPath())).then(new Operation<Optional<Container>>() {
@Override
public void apply(Optional<Container> optContainer) throws OperationException {
checkState(optContainer.isPresent(), "Failed to update non existed path");
final Container container = optContainer.get();
if (container.getResourceType() == PROJECT) {
((Project) container).update().withBody(dataObject).send().then(onComplete(callback)).catchError(onFailure(callback));
} else if (container.getResourceType() == FOLDER) {
((Folder) container).toProject().withBody(dataObject).send().then(onComplete(callback)).catchError(onFailure(callback));
}
}
});
} else if (mode == IMPORT) {
appContext.getWorkspaceRoot().newProject().withBody(dataObject).send().thenPromise(new Function<Project, Promise<Project>>() {
@Override
public Promise<Project> apply(Project project) throws FunctionException {
return project.update().withBody(dataObject).send();
}
}).then(addCommands(callback)).catchError(onFailure(callback));
}
}
use of org.eclipse.che.api.promises.client.Promise in project che by eclipse.
the class DeleteResourceManager method delete.
/**
* Deletes the given resources and its descendants in the standard manner from file system.
* Method requires a confirmation from the user before resource will be removed.
*
* @param needConfirmation
* true if confirmation is need
* @param resources
* the resources to delete
* @return the {@link Promise} with void if removal has successfully completed
* @see #delete(Resource...)
*/
public Promise<Void> delete(boolean needConfirmation, Resource... resources) {
checkArgument(resources != null, "Null resource occurred");
checkArgument(resources.length > 0, "No resources were provided to remove");
final Resource[] filtered = filterDescendants(resources);
if (!needConfirmation) {
Promise<?>[] deleteAll = new Promise<?>[resources.length];
for (int i = 0; i < resources.length; i++) {
deleteAll[i] = resources[i].delete();
}
return promiseProvider.all(deleteAll).then(new Function<JsArrayMixed, Void>() {
@Override
public Void apply(JsArrayMixed arg) throws FunctionException {
return null;
}
});
}
List<Resource> projectsList = newArrayList();
for (Resource resource : filtered) {
if (resource.getResourceType() == PROJECT) {
projectsList.add(resource);
}
}
Resource[] projects = projectsList.toArray(new Resource[projectsList.size()]);
if (projectsList.isEmpty()) {
//if no project were found in nodes list
return promptUserToDelete(filtered);
} else if (projects.length < filtered.length) {
//inform user that we can't delete mixed list of the nodes
return promiseProvider.reject(JsPromiseError.create(localization.mixedProjectDeleteMessage()));
} else {
//delete only project nodes
return promptUserToDelete(projects);
}
}
Aggregations