use of org.eclipse.che.api.promises.client.Function in project che by eclipse.
the class LanguageServerSignatureHelp method signatureHelp.
@Override
public Promise<Optional<SignatureHelp>> signatureHelp(Document document, int offset) {
TextDocumentPositionParamsDTO paramsDTO = helper.createTDPP(document, offset);
Promise<SignatureHelpDTO> promise = client.signatureHelp(paramsDTO);
return promise.then(new Function<SignatureHelpDTO, Optional<SignatureHelp>>() {
@Override
public Optional<SignatureHelp> apply(SignatureHelpDTO arg) throws FunctionException {
if (arg == null) {
return Optional.absent();
}
return Optional.<SignatureHelp>of(new SignatureHelpImpl(arg));
}
}).catchError(new Function<PromiseError, Optional<SignatureHelp>>() {
@Override
public Optional<SignatureHelp> apply(PromiseError arg) throws FunctionException {
notificationManager.notify(arg.getMessage(), StatusNotification.Status.FAIL, StatusNotification.DisplayMode.EMERGE_MODE);
return Optional.absent();
}
});
}
use of org.eclipse.che.api.promises.client.Function in project che by eclipse.
the class AbstractDebugger method connect.
@Override
public Promise<Void> connect(Map<String, String> connectionProperties) {
if (isConnected()) {
return Promises.reject(JsPromiseError.create("Debugger already connected"));
}
Promise<DebugSessionDto> connect = service.connect(debuggerType, connectionProperties);
final DebuggerDescriptor debuggerDescriptor = toDescriptor(connectionProperties);
Promise<Void> promise = connect.then(new Function<DebugSessionDto, Void>() {
@Override
public Void apply(final DebugSessionDto arg) throws FunctionException {
DebuggerInfo debuggerInfo = arg.getDebuggerInfo();
debuggerDescriptor.setInfo(debuggerInfo.getName() + " " + debuggerInfo.getVersion());
setDebugSession(arg);
preserveDebuggerState();
startCheckingEvents();
startDebugger(arg);
return null;
}
}).catchError(new Operation<PromiseError>() {
@Override
public void apply(PromiseError arg) throws OperationException {
Log.error(AbstractDebugger.class, arg.getMessage());
throw new OperationException(arg.getCause());
}
});
for (DebuggerObserver observer : observers) {
observer.onDebuggerAttached(debuggerDescriptor, promise);
}
return promise;
}
use of org.eclipse.che.api.promises.client.Function in project che by eclipse.
the class ResourceManager method getRemoteResources.
Promise<Resource[]> getRemoteResources(final Container container, final int depth, boolean includeFiles) {
checkArgument(depth > -2, "Invalid depth");
if (depth == DEPTH_ZERO) {
return promises.resolve(NO_RESOURCES);
}
int depthToReload = depth;
final Optional<Resource[]> descendants = store.getAll(container.getLocation());
if (depthToReload != -1 && descendants.isPresent()) {
for (Resource resource : descendants.get()) {
if (resource.getLocation().segmentCount() - container.getLocation().segmentCount() > depth) {
depthToReload = resource.getLocation().segmentCount() - container.getLocation().segmentCount();
}
}
}
return ps.getTree(container.getLocation(), depthToReload, includeFiles).then(new Function<TreeElement, Resource[]>() {
@Override
public Resource[] apply(TreeElement tree) throws FunctionException {
class Visitor implements ResourceVisitor {
Resource[] resources;
//size of total items
private int size = 0;
//step to increase resource array
private int incStep = 50;
private Visitor() {
this.resources = NO_RESOURCES;
}
@Override
public void visit(Resource resource) {
if (resource.getResourceType() == PROJECT) {
final Optional<ProjectConfigDto> optionalConfig = findProjectConfigDto(resource.getLocation());
if (optionalConfig.isPresent()) {
final Optional<ProblemProjectMarker> optionalMarker = getProblemMarker(optionalConfig.get());
if (optionalMarker.isPresent()) {
resource.addMarker(optionalMarker.get());
}
}
}
if (size > resources.length - 1) {
//check load factor and increase resource array
resources = copyOf(resources, resources.length + incStep);
}
resources[size++] = resource;
}
}
final Visitor visitor = new Visitor();
traverse(tree, visitor);
return copyOf(visitor.resources, visitor.size);
}
}).then(new Function<Resource[], Resource[]>() {
@Override
public Resource[] apply(Resource[] reloaded) throws FunctionException {
Resource[] result = new Resource[0];
if (descendants.isPresent()) {
Resource[] outdated = descendants.get();
final Resource[] removed = removeAll(outdated, reloaded, false);
for (Resource resource : removed) {
store.dispose(resource.getLocation(), false);
eventBus.fireEvent(new ResourceChangedEvent(new ResourceDeltaImpl(resource, REMOVED)));
}
final Resource[] updated = removeAll(outdated, reloaded, true);
for (Resource resource : updated) {
store.register(resource);
eventBus.fireEvent(new ResourceChangedEvent(new ResourceDeltaImpl(resource, UPDATED)));
final Optional<Resource> registered = store.getResource(resource.getLocation());
if (registered.isPresent()) {
result = Arrays.add(result, registered.get());
}
}
final Resource[] added = removeAll(reloaded, outdated, false);
for (Resource resource : added) {
store.register(resource);
eventBus.fireEvent(new ResourceChangedEvent(new ResourceDeltaImpl(resource, ADDED)));
final Optional<Resource> registered = store.getResource(resource.getLocation());
if (registered.isPresent()) {
result = Arrays.add(result, registered.get());
}
}
} else {
for (Resource resource : reloaded) {
store.register(resource);
eventBus.fireEvent(new ResourceChangedEvent(new ResourceDeltaImpl(resource, ADDED)));
final Optional<Resource> registered = store.getResource(resource.getLocation());
if (registered.isPresent()) {
result = Arrays.add(result, registered.get());
}
}
}
return result;
}
});
}
use of org.eclipse.che.api.promises.client.Function in project che by eclipse.
the class PreferencesPresenter method onRefreshClicked.
@Override
public void onRefreshClicked() {
Promise<Map<String, String>> promise = Promises.resolve(null);
for (final PreferencesManager preferencesManager : managers) {
promise = promise.thenPromise(new Function<Map<String, String>, Promise<Map<String, String>>>() {
@Override
public Promise<Map<String, String>> apply(Map<String, String> arg) throws FunctionException {
return preferencesManager.loadPreferences().catchError(new Operation<PromiseError>() {
@Override
public void apply(PromiseError error) throws OperationException {
notificationManagerProvider.get().notify(locale.unableToLoadPreference(), error.getMessage(), FAIL, FLOAT_MODE);
}
});
}
});
}
/**
* Revert changes on every preference page
*/
promise.then(new Operation<Map<String, String>>() {
@Override
public void apply(Map<String, String> arg) throws OperationException {
for (PreferencePagePresenter p : PreferencesPresenter.this.preferences) {
p.revertChanges();
}
}
});
}
use of org.eclipse.che.api.promises.client.Function in project che by eclipse.
the class PreferencesPresenter method onSaveClicked.
@Override
public void onSaveClicked() {
for (PreferencePagePresenter preference : preferences) {
if (preference.isDirty()) {
preference.storeChanges();
}
}
Promise<Void> promise = Promises.resolve(null);
final List<PromiseError> promiseErrorList = new ArrayList<>();
for (final PreferencesManager preferencesManager : managers) {
promise = promise.thenPromise(new Function<Void, Promise<Void>>() {
@Override
public Promise<Void> apply(Void arg) throws FunctionException {
return preferencesManager.flushPreferences().catchError(new Operation<PromiseError>() {
@Override
public void apply(PromiseError error) throws OperationException {
notificationManagerProvider.get().notify(locale.unableToSavePreference(), error.getMessage(), FAIL, FLOAT_MODE);
promiseErrorList.add(error);
}
});
}
});
}
promise.then(new Operation<Void>() {
@Override
public void apply(Void aVoid) throws OperationException {
if (promiseErrorList.isEmpty()) {
view.enableSaveButton(false);
}
}
});
}
Aggregations