Search in sources :

Example 61 with IRunnableWithProgress

use of org.eclipse.jface.operation.IRunnableWithProgress in project hale by halestudio.

the class OrientInstanceService method addSourceInstances.

/**
 * @see InstanceService#addSourceInstances(InstanceCollection)
 */
@Override
public void addSourceInstances(InstanceCollection sourceInstances) {
    notifyDatasetAboutToChange(DataSet.SOURCE);
    final HaleStoreInstancesJob storeInstances = new HaleStoreInstancesJob("Load source instances into database", source, sourceInstances) {

        @Override
        protected void onComplete() {
            notifyDatasetChanged(DataSet.SOURCE);
            retransform();
        }
    };
    // so instead, now the data is loaded in a progress dialog
    try {
        ThreadProgressMonitor.runWithProgressDialog(new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                storeInstances.run(monitor);
            }
        }, true);
    } catch (Exception e) {
        log.error("Error starting process to load source data", e);
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException) ExecutionException(org.eclipse.core.commands.ExecutionException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Example 62 with IRunnableWithProgress

use of org.eclipse.jface.operation.IRunnableWithProgress 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 63 with IRunnableWithProgress

use of org.eclipse.jface.operation.IRunnableWithProgress in project hale by halestudio.

the class URLSource method runDetectContentType.

/**
 * Run content type detection.
 */
protected void runDetectContentType() {
    try {
        getPage().getWizard().getContainer().run(false, false, new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                monitor.beginTask("Detect content type", IProgressMonitor.UNKNOWN);
                final IContentType detected = detectContentType();
                PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {

                    @Override
                    public void run() {
                        if (detected != null) {
                            types.setSelection(new StructuredSelection(detected));
                            getPage().setMessage(MessageFormat.format("Detected {0} as content type", detected.getName()), DialogPage.INFORMATION);
                            updateState(true);
                        } else {
                            types.setSelection(new StructuredSelection());
                            getPage().setMessage("Could not detect content type. The resource might be not available or it has no matching content type.", DialogPage.WARNING);
                            updateState(true);
                        }
                    }
                });
                monitor.done();
            }
        });
    } catch (Throwable t) {
        getPage().setErrorMessage("Starting the task to detect the content type failed");
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) IContentType(org.eclipse.core.runtime.content.IContentType) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress)

Example 64 with IRunnableWithProgress

use of org.eclipse.jface.operation.IRunnableWithProgress in project hale by halestudio.

the class ExportGraphAction method run.

/**
 * @see Action#run()
 */
@Override
public void run() {
    FileDialog dialog = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE);
    // XXX if called from TTreeExporter during transformation, the active
    // shell may be null!
    dialog.setOverwrite(true);
    dialog.setText("Export graph to file");
    String[] imageExtensions = ImageIO.getWriterFileSuffixes();
    StringBuffer extensions = new StringBuffer("*.svg;*.gv;*.dot");
    for (String imageExt : imageExtensions) {
        extensions.append(";*.");
        extensions.append(imageExt);
    }
    dialog.setFilterExtensions(new String[] { extensions.toString() });
    dialog.setFilterNames(new String[] { "Image, SVG or dot file (" + extensions + ")" });
    String fileName = dialog.open();
    if (fileName != null) {
        final File file = new File(fileName);
        // //XXX use an off-screen graph (testing)
        // OffscreenGraph graph = new OffscreenGraph(1000, 1000) {
        // 
        // @Override
        // protected void configureViewer(GraphViewer viewer) {
        // viewer.setContentProvider(RenderAction.this.viewer.getContentProvider());
        // viewer.setLabelProvider(RenderAction.this.viewer.getLabelProvider());
        // viewer.setInput(RenderAction.this.viewer.getInput());
        // viewer.setLayoutAlgorithm(new TreeLayoutAlgorithm(TreeLayoutAlgorithm.LEFT_RIGHT), false);
        // }
        // };
        // get the graph
        final Graph graph = viewer.getGraphControl();
        final String ext = FilenameUtils.getExtension(file.getAbsolutePath());
        final IFigure root = graph.getRootLayer();
        ProgressMonitorDialog progress = new ProgressMonitorDialog(Display.getCurrent().getActiveShell());
        try {
            progress.run(false, false, new IRunnableWithProgress() {

                @Override
                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                    try {
                        OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
                        if (ext.equalsIgnoreCase("gv") || ext.equalsIgnoreCase("dot")) {
                            OffscreenGraph.saveDot(graph, out);
                        } else // else if (ext.equalsIgnoreCase("svg")) {
                        // OffscreenGraph.saveSVG(root, out);
                        // }
                        {
                            OffscreenGraph.saveImage(root, out, ext);
                        }
                    } catch (Throwable e) {
                        log.userError("Error saving graph to file", e);
                    }
                }
            });
        } catch (Throwable e) {
            log.error("Error launching graph export", e);
        }
    }
}
Also used : ProgressMonitorDialog(org.eclipse.jface.dialogs.ProgressMonitorDialog) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Graph(org.eclipse.zest.core.widgets.Graph) FileOutputStream(java.io.FileOutputStream) FileDialog(org.eclipse.swt.widgets.FileDialog) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream) IFigure(org.eclipse.draw2d.IFigure)

Example 65 with IRunnableWithProgress

use of org.eclipse.jface.operation.IRunnableWithProgress in project hale by halestudio.

the class TransformDataWizardSourcePage method createControl.

/**
 * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
 */
@Override
public void createControl(Composite parent) {
    Composite content = new Composite(parent, SWT.NONE);
    content.setLayout(GridLayoutFactory.swtDefaults().create());
    final ListViewer listViewer = new ListViewer(content);
    listViewer.getControl().setLayoutData(GridDataFactory.fillDefaults().grab(true, true).create());
    if (!useProjectData) {
        Button addButton = new Button(content, SWT.PUSH);
        addButton.setText("Add source file");
        addButton.setLayoutData(GridDataFactory.swtDefaults().align(SWT.END, SWT.CENTER).create());
        addButton.addSelectionListener(new SelectionAdapter() {

            @Override
            public void widgetSelected(SelectionEvent e) {
                InstanceImportWizard importWizard = new InstanceImportWizard();
                TransformDataImportAdvisor advisor = new TransformDataImportAdvisor();
                // specifying null as actionId results in no call to
                // ProjectService.rememberIO
                importWizard.setAdvisor(advisor, null);
                if (new HaleWizardDialog(getShell(), importWizard).open() == Dialog.OK) {
                    if (advisor.getInstances() != null) {
                        sourceCollections.add(advisor.getInstances());
                        listViewer.add(advisor.getLocation());
                        getContainer().updateButtons();
                    }
                }
            }
        });
    } else {
        // initialize project source data
        IRunnableWithProgress op = new IRunnableWithProgress() {

            @Override
            public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
                monitor.beginTask("Prepare data sources", IProgressMonitor.UNKNOWN);
                ProjectService ps = PlatformUI.getWorkbench().getService(ProjectService.class);
                final List<URI> locations = new ArrayList<>();
                for (Resource resource : ps.getResources()) {
                    if (InstanceIO.ACTION_LOAD_SOURCE_DATA.equals(resource.getActionId())) {
                        // resource is source data
                        IOConfiguration conf = resource.copyConfiguration(true);
                        TransformDataImportAdvisor advisor = new TransformDataImportAdvisor();
                        ProjectResourcesUtil.executeConfiguration(conf, advisor, false, null);
                        if (advisor.getInstances() != null) {
                            sourceCollections.add(advisor.getInstances());
                            locations.add(advisor.getLocation());
                        }
                    }
                }
                PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {

                    @Override
                    public void run() {
                        for (URI location : locations) {
                            listViewer.add(location);
                        }
                    }
                });
                monitor.done();
            }
        };
        try {
            ThreadProgressMonitor.runWithProgressDialog(op, false);
        } catch (Exception e) {
            throw new IllegalStateException(e);
        }
    }
    setControl(content);
}
Also used : InstanceImportWizard(eu.esdihumboldt.hale.ui.io.instance.InstanceImportWizard) ListViewer(org.eclipse.jface.viewers.ListViewer) Composite(org.eclipse.swt.widgets.Composite) IOConfiguration(eu.esdihumboldt.hale.common.core.io.project.model.IOConfiguration) SelectionAdapter(org.eclipse.swt.events.SelectionAdapter) ArrayList(java.util.ArrayList) Resource(eu.esdihumboldt.hale.common.core.io.project.model.Resource) ProjectService(eu.esdihumboldt.hale.ui.service.project.ProjectService) URI(java.net.URI) InvocationTargetException(java.lang.reflect.InvocationTargetException) IRunnableWithProgress(org.eclipse.jface.operation.IRunnableWithProgress) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) Button(org.eclipse.swt.widgets.Button) SelectionEvent(org.eclipse.swt.events.SelectionEvent) HaleWizardDialog(eu.esdihumboldt.hale.ui.util.wizard.HaleWizardDialog)

Aggregations

IRunnableWithProgress (org.eclipse.jface.operation.IRunnableWithProgress)417 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)397 InvocationTargetException (java.lang.reflect.InvocationTargetException)386 ProgressMonitorDialog (org.eclipse.jface.dialogs.ProgressMonitorDialog)194 CoreException (org.eclipse.core.runtime.CoreException)123 ArrayList (java.util.ArrayList)86 IStatus (org.eclipse.core.runtime.IStatus)67 IOException (java.io.IOException)65 List (java.util.List)54 Status (org.eclipse.core.runtime.Status)53 IFile (org.eclipse.core.resources.IFile)51 File (java.io.File)47 Shell (org.eclipse.swt.widgets.Shell)44 IProject (org.eclipse.core.resources.IProject)40 PartInitException (org.eclipse.ui.PartInitException)32 IPath (org.eclipse.core.runtime.IPath)26 Display (org.eclipse.swt.widgets.Display)26 IResource (org.eclipse.core.resources.IResource)25 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)24 Path (org.eclipse.core.runtime.Path)23