Search in sources :

Example 1 with ActionUI

use of eu.esdihumboldt.hale.ui.io.action.ActionUI in project hale by halestudio.

the class IOWizard method performFinish.

/**
 * @see Wizard#performFinish()
 *
 * @return <code>true</code> if executing the I/O provider was successful
 */
@Override
public boolean performFinish() {
    if (getProvider() == null) {
        return false;
    }
    if (!applyConfiguration()) {
        return false;
    }
    // create default report
    IOReporter defReport = provider.createReporter();
    // validate and execute provider
    try {
        // validate configuration
        provider.validate();
        ProjectService ps = PlatformUI.getWorkbench().getService(ProjectService.class);
        URI projectLoc = ps.getLoadLocation() == null ? null : ps.getLoadLocation();
        boolean isProjectResource = false;
        if (actionId != null) {
            // XXX instead move project resource to action?
            ActionUI factory = ActionUIExtension.getInstance().findActionUI(actionId);
            isProjectResource = factory.isProjectResource();
        }
        // prevent loading of duplicate resources
        if (isProjectResource && provider instanceof ImportProvider && !getProviderFactory().allowDuplicateResource()) {
            String currentResource = ((ImportProvider) provider).getSource().getLocation().toString();
            URI currentAbsolute = URI.create(currentResource);
            if (projectLoc != null && !currentAbsolute.isAbsolute()) {
                currentAbsolute = projectLoc.resolve(currentAbsolute);
            }
            for (IOConfiguration conf : ((Project) ps.getProjectInfo()).getResources()) {
                Value otherResourceValue = conf.getProviderConfiguration().get(ImportProvider.PARAM_SOURCE);
                if (otherResourceValue == null) {
                    continue;
                }
                String otherResource = otherResourceValue.as(String.class);
                URI otherAbsolute = URI.create(otherResource);
                if (projectLoc != null && !otherAbsolute.isAbsolute()) {
                    otherAbsolute = projectLoc.resolve(otherAbsolute);
                }
                String action = conf.getActionId();
                // resource is already loaded into the project
                if (currentAbsolute.equals(otherAbsolute) && Objects.equal(actionId, action)) {
                    // check if the resource is loaded with a provider that
                    // allows duplicates
                    boolean allowDuplicate = false;
                    IOProviderDescriptor providerFactory = IOProviderExtension.getInstance().getFactory(conf.getProviderId());
                    if (providerFactory != null) {
                        allowDuplicate = providerFactory.allowDuplicateResource();
                    }
                    if (!allowDuplicate) {
                        log.userError("Resource is already loaded. Loading duplicate resources is aborted!");
                        return false;
                    }
                }
            }
        }
        // enable provider internal caching
        if (isProjectResource && provider instanceof CachingImportProvider) {
            ((CachingImportProvider) provider).setProvideCache();
        }
        IOReport report = execute(provider, defReport);
        if (report != null) {
            // add report to report server
            ReportService repService = PlatformUI.getWorkbench().getService(ReportService.class);
            repService.addReport(report);
            // show message to user
            if (report.isSuccess()) {
                // let advisor handle results
                try {
                    getContainer().run(true, false, new IRunnableWithProgress() {

                        @Override
                        public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                            monitor.beginTask("Completing operation...", IProgressMonitor.UNKNOWN);
                            try {
                                advisor.handleResults(getProvider());
                            } finally {
                                monitor.done();
                            }
                        }
                    });
                } catch (InvocationTargetException e) {
                    log.userError("Error processing results:\n" + e.getCause().getLocalizedMessage(), e.getCause());
                    return false;
                } catch (Exception e) {
                    log.userError("Error processing results:\n" + e.getLocalizedMessage(), e);
                    return false;
                }
                // add to project service if necessary
                if (isProjectResource)
                    ps.rememberIO(actionId, getProviderFactory().getIdentifier(), provider);
                return true;
            } else {
                // error message
                log.userError(report.getSummary() + "\nPlease see the report for details.");
                return false;
            }
        } else
            return true;
    } catch (IOProviderConfigurationException e) {
        // user feedback
        log.userError("Validation of the provider configuration failed:\n" + e.getLocalizedMessage(), e);
        return false;
    }
}
Also used : IOProviderDescriptor(eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) ProjectService(eu.esdihumboldt.hale.ui.service.project.ProjectService) ActionUI(eu.esdihumboldt.hale.ui.io.action.ActionUI) CachingImportProvider(eu.esdihumboldt.hale.common.core.io.CachingImportProvider) ImportProvider(eu.esdihumboldt.hale.common.core.io.ImportProvider) IOReport(eu.esdihumboldt.hale.common.core.io.report.IOReport) URI(java.net.URI) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) Project(eu.esdihumboldt.hale.common.core.io.project.model.Project) IOReporter(eu.esdihumboldt.hale.common.core.io.report.IOReporter) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) IOProviderConfigurationException(eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException) ReportService(eu.esdihumboldt.hale.ui.service.report.ReportService) CachingImportProvider(eu.esdihumboldt.hale.common.core.io.CachingImportProvider) Value(eu.esdihumboldt.hale.common.core.io.Value)

Example 2 with ActionUI

use of eu.esdihumboldt.hale.ui.io.action.ActionUI in project hale by halestudio.

the class ActionUIWizardPage method createViewer.

/**
 * @see ViewerWizardSelectionPage#createViewer(Composite)
 */
@Override
protected Pair<StructuredViewer, Control> createViewer(Composite parent) {
    ListViewer viewer = new ListViewer(parent);
    viewer.setLabelProvider(new LabelProvider() {

        @Override
        public Image getImage(Object element) {
            if (element instanceof ActionUIWizardNode) {
                return ((ActionUIWizardNode) element).getImage();
            }
            return super.getImage(element);
        }

        @Override
        public String getText(Object element) {
            if (element instanceof ActionUIWizardNode) {
                return ((ActionUIWizardNode) element).getActionUI().getDisplayName();
            }
            return super.getText(element);
        }
    });
    viewer.setContentProvider(ArrayContentProvider.getInstance());
    List<ActionUI> list = ActionUIExtension.getInstance().getFactories(filter);
    List<ActionUIWizardNode> nodes = new ArrayList<ActionUIWizardNode>();
    for (ActionUI action : list) {
        nodes.add(new ActionUIWizardNode(action, getContainer()));
    }
    viewer.setInput(nodes);
    return new Pair<StructuredViewer, Control>(viewer, viewer.getControl());
}
Also used : ListViewer(org.eclipse.jface.viewers.ListViewer) ArrayList(java.util.ArrayList) ActionUI(eu.esdihumboldt.hale.ui.io.action.ActionUI) Image(org.eclipse.swt.graphics.Image) LabelProvider(org.eclipse.jface.viewers.LabelProvider) Pair(eu.esdihumboldt.util.Pair)

Example 3 with ActionUI

use of eu.esdihumboldt.hale.ui.io.action.ActionUI in project hale by halestudio.

the class ResourcesLabelProvider method getImage.

@Override
public Image getImage(Object element) {
    if (element instanceof IOAction) {
        IOAction action = (IOAction) element;
        Image actionImage = actionImages.get(action.getId());
        if (actionImage == null) {
            ActionUI actionUI = ActionUIExtension.getInstance().findActionUI(action.getId());
            URL iconUrl = actionUI.getIconURL();
            if (iconUrl != null) {
                actionImage = ImageDescriptor.createFromURL(iconUrl).createImage();
                actionImages.put(action.getId(), actionImage);
            }
        }
        if (actionImage != null) {
            return actionImage;
        }
        return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FOLDER);
    }
    if (element instanceof Resource) {
        return PlatformUI.getWorkbench().getSharedImages().getImage(ISharedImages.IMG_OBJ_FILE);
    }
    if (element instanceof ProjectToken) {
        return projectImage;
    }
    return null;
}
Also used : Resource(eu.esdihumboldt.hale.common.core.io.project.model.Resource) ActionUI(eu.esdihumboldt.hale.ui.io.action.ActionUI) IOAction(eu.esdihumboldt.hale.common.core.io.IOAction) Image(org.eclipse.swt.graphics.Image) URL(java.net.URL)

Example 4 with ActionUI

use of eu.esdihumboldt.hale.ui.io.action.ActionUI in project hale by halestudio.

the class RemoveResourceHandler method execute.

@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
    ISelection selection = HandlerUtil.getCurrentSelection(event);
    if (selection != null && !selection.isEmpty() && selection instanceof IStructuredSelection) {
        Object element = ((IStructuredSelection) selection).getFirstElement();
        if (element instanceof Resource) {
            Resource resource = (Resource) element;
            // retrieve action UI advisor
            ActionUI actionUI = ActionUIExtension.getInstance().findActionUI(resource.getActionId());
            if (actionUI != null) {
                IOAction action = IOActionExtension.getInstance().get(resource.getActionId());
                ActionUIAdvisor<?> advisor = actionUI.getUIAdvisor();
                if (advisor != null && advisor.supportsRemoval()) {
                    String name = null;
                    if (resource.getSource() != null) {
                        String location = resource.getSource().toString();
                        int index = location.lastIndexOf('/');
                        if (index > 0 && index < location.length()) {
                            name = location.substring(index + 1);
                        }
                    }
                    String resourceType = null;
                    if (action != null) {
                        resourceType = action.getResourceName();
                    }
                    if (resourceType == null) {
                        resourceType = "resource";
                    }
                    String message;
                    if (name == null) {
                        message = MessageFormat.format("Do you really want to remove this {0}?", resourceType);
                    } else {
                        message = MessageFormat.format("Do you really want to remove the {0} {1}?", resourceType, name);
                    }
                    if (MessageDialog.openQuestion(HandlerUtil.getActiveShell(event), "Remove resource", message)) {
                        // do the actual removal
                        String id = resource.getResourceId();
                        if (advisor.removeResource(id)) {
                            // removal succeeded, so remove from project as
                            // well
                            ProjectService ps = PlatformUI.getWorkbench().getService(ProjectService.class);
                            ps.removeResource(id);
                        }
                    }
                } else {
                    log.userError("Removing this resource is not supported.");
                }
            }
        }
    }
    return null;
}
Also used : ISelection(org.eclipse.jface.viewers.ISelection) Resource(eu.esdihumboldt.hale.common.core.io.project.model.Resource) ActionUI(eu.esdihumboldt.hale.ui.io.action.ActionUI) ProjectService(eu.esdihumboldt.hale.ui.service.project.ProjectService) IOAction(eu.esdihumboldt.hale.common.core.io.IOAction) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection)

Example 5 with ActionUI

use of eu.esdihumboldt.hale.ui.io.action.ActionUI in project hale by halestudio.

the class ActionUIWizardPage method acceptWizard.

/**
 * @see ViewerWizardSelectionPage#acceptWizard(IWizardNode)
 */
@Override
protected String acceptWizard(IWizardNode wizardNode) {
    if (wizardNode instanceof ActionUIWizardNode) {
        ActionUI actionUI = ((ActionUIWizardNode) wizardNode).getActionUI();
        Expression enabledWhen = actionUI.getEnabledWhen();
        if (enabledWhen == null) {
            return null;
        }
        IEvaluationService ies = PlatformUI.getWorkbench().getService(IEvaluationService.class);
        try {
            EvaluationResult evalResult = enabledWhen.evaluate(ies.getCurrentState());
            if (evalResult == EvaluationResult.FALSE) {
                // disabled
                return actionUI.getDisabledReason();
            }
            // enabled
            return null;
        } catch (CoreException e) {
            String message = "Could not evaluate enabledWhen expression";
            log.error(message, e);
            return message;
        }
    }
    return super.acceptWizard(wizardNode);
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) Expression(org.eclipse.core.expressions.Expression) ActionUI(eu.esdihumboldt.hale.ui.io.action.ActionUI) IEvaluationService(org.eclipse.ui.services.IEvaluationService) EvaluationResult(org.eclipse.core.expressions.EvaluationResult)

Aggregations

ActionUI (eu.esdihumboldt.hale.ui.io.action.ActionUI)5 IOAction (eu.esdihumboldt.hale.common.core.io.IOAction)2 Resource (eu.esdihumboldt.hale.common.core.io.project.model.Resource)2 ProjectService (eu.esdihumboldt.hale.ui.service.project.ProjectService)2 Image (org.eclipse.swt.graphics.Image)2 CachingImportProvider (eu.esdihumboldt.hale.common.core.io.CachingImportProvider)1 IOProviderConfigurationException (eu.esdihumboldt.hale.common.core.io.IOProviderConfigurationException)1 ImportProvider (eu.esdihumboldt.hale.common.core.io.ImportProvider)1 Value (eu.esdihumboldt.hale.common.core.io.Value)1 IOProviderDescriptor (eu.esdihumboldt.hale.common.core.io.extension.IOProviderDescriptor)1 IOConfiguration (eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration)1 Project (eu.esdihumboldt.hale.common.core.io.project.model.Project)1 IOReport (eu.esdihumboldt.hale.common.core.io.report.IOReport)1 IOReporter (eu.esdihumboldt.hale.common.core.io.report.IOReporter)1 ReportService (eu.esdihumboldt.hale.ui.service.report.ReportService)1 Pair (eu.esdihumboldt.util.Pair)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URI (java.net.URI)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1