Search in sources :

Example 11 with Function

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();
        }
    });
}
Also used : TextDocumentPositionParamsDTO(org.eclipse.che.api.languageserver.shared.lsapi.TextDocumentPositionParamsDTO) Function(org.eclipse.che.api.promises.client.Function) PromiseError(org.eclipse.che.api.promises.client.PromiseError) Optional(com.google.common.base.Optional) SignatureHelpDTO(org.eclipse.che.api.languageserver.shared.lsapi.SignatureHelpDTO) FunctionException(org.eclipse.che.api.promises.client.FunctionException) SignatureHelp(org.eclipse.che.ide.api.editor.signature.SignatureHelp)

Example 12 with Function

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;
}
Also used : DebuggerInfo(org.eclipse.che.api.debug.shared.model.DebuggerInfo) DebuggerDescriptor(org.eclipse.che.ide.debug.DebuggerDescriptor) Function(org.eclipse.che.api.promises.client.Function) JsPromiseError(org.eclipse.che.api.promises.client.js.JsPromiseError) PromiseError(org.eclipse.che.api.promises.client.PromiseError) DebugSessionDto(org.eclipse.che.api.debug.shared.dto.DebugSessionDto) DebuggerObserver(org.eclipse.che.ide.debug.DebuggerObserver) OperationException(org.eclipse.che.api.promises.client.OperationException)

Example 13 with Function

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;
        }
    });
}
Also used : Optional(com.google.common.base.Optional) NewProjectConfigDto(org.eclipse.che.api.workspace.shared.dto.NewProjectConfigDto) ProjectConfigDto(org.eclipse.che.api.workspace.shared.dto.ProjectConfigDto) Resource(org.eclipse.che.ide.api.resources.Resource) ProblemProjectMarker(org.eclipse.che.ide.api.resources.Project.ProblemProjectMarker) FunctionException(org.eclipse.che.api.promises.client.FunctionException) TreeElement(org.eclipse.che.api.project.shared.dto.TreeElement) Function(org.eclipse.che.api.promises.client.Function) ResourceChangedEvent(org.eclipse.che.ide.api.resources.ResourceChangedEvent)

Example 14 with Function

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();
            }
        }
    });
}
Also used : Function(org.eclipse.che.api.promises.client.Function) PromiseError(org.eclipse.che.api.promises.client.PromiseError) PreferencePagePresenter(org.eclipse.che.ide.api.preferences.PreferencePagePresenter) HashMap(java.util.HashMap) Map(java.util.Map) PreferencesManager(org.eclipse.che.ide.api.preferences.PreferencesManager) OperationException(org.eclipse.che.api.promises.client.OperationException)

Example 15 with Function

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);
            }
        }
    });
}
Also used : Function(org.eclipse.che.api.promises.client.Function) PreferencePagePresenter(org.eclipse.che.ide.api.preferences.PreferencePagePresenter) PromiseError(org.eclipse.che.api.promises.client.PromiseError) ArrayList(java.util.ArrayList) PreferencesManager(org.eclipse.che.ide.api.preferences.PreferencesManager) OperationException(org.eclipse.che.api.promises.client.OperationException)

Aggregations

Function (org.eclipse.che.api.promises.client.Function)17 FunctionException (org.eclipse.che.api.promises.client.FunctionException)11 Promise (org.eclipse.che.api.promises.client.Promise)9 Resource (org.eclipse.che.ide.api.resources.Resource)9 PromiseError (org.eclipse.che.api.promises.client.PromiseError)8 Optional (com.google.common.base.Optional)6 OperationException (org.eclipse.che.api.promises.client.OperationException)5 Project (org.eclipse.che.ide.api.resources.Project)5 ResourceChangedEvent (org.eclipse.che.ide.api.resources.ResourceChangedEvent)5 Path (org.eclipse.che.ide.resource.Path)5 NewProjectConfigDto (org.eclipse.che.api.workspace.shared.dto.NewProjectConfigDto)4 ProjectConfigDto (org.eclipse.che.api.workspace.shared.dto.ProjectConfigDto)4 MutableProjectConfig (org.eclipse.che.ide.api.project.MutableProjectConfig)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 SourceStorage (org.eclipse.che.api.core.model.project.SourceStorage)3 AsyncCallback (com.google.gwt.user.client.rpc.AsyncCallback)2 Operation (org.eclipse.che.api.promises.client.Operation)2 RequestCall (org.eclipse.che.api.promises.client.callback.AsyncPromiseHelper.RequestCall)2