X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.export.ui%2Fsrc%2Forg%2Fsimantics%2Fexport%2Fui%2FExportCoreWizard.java;h=ea2cb05dc9c1bdaaf9755dfcb3176f8d8e567705;hp=e18efbdeb969122537f514503d73f15b3c864abe;hb=e88be95edf1f80781646cfdf717ec1b663264179;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.export.ui/src/org/simantics/export/ui/ExportCoreWizard.java b/bundles/org.simantics.export.ui/src/org/simantics/export/ui/ExportCoreWizard.java index e18efbdeb..ea2cb05dc 100644 --- a/bundles/org.simantics.export.ui/src/org/simantics/export/ui/ExportCoreWizard.java +++ b/bundles/org.simantics.export.ui/src/org/simantics/export/ui/ExportCoreWizard.java @@ -1,171 +1,171 @@ -package org.simantics.export.ui; - -import java.io.IOException; -import java.lang.reflect.InvocationTargetException; -import java.util.List; - -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.SubMonitor; -import org.eclipse.jface.operation.IRunnableWithProgress; -import org.eclipse.jface.viewers.IStructuredSelection; -import org.eclipse.jface.wizard.IWizardPage; -import org.eclipse.jface.wizard.Wizard; -import org.eclipse.jface.wizard.WizardPage; -import org.eclipse.ui.IExportWizard; -import org.eclipse.ui.IWorkbench; -import org.osgi.service.prefs.BackingStoreException; -import org.simantics.db.exception.DatabaseException; -import org.simantics.export.core.ExportContext; -import org.simantics.export.core.Exports; -import org.simantics.export.core.error.ExportException; -import org.simantics.export.core.manager.ExportManager; -import org.simantics.export.core.manager.ExportPlan; -import org.simantics.export.core.manager.ExportWizardResult; -import org.simantics.export.core.util.ExporterUtils; -import org.simantics.ui.SimanticsUI; -import org.simantics.utils.datastructures.collections.CollectionUtils; -import org.simantics.utils.ui.ErrorLogger; -import org.simantics.utils.ui.ExceptionUtils; - -public class ExportCoreWizard extends Wizard implements IExportWizard { - - ExportContext ctx; - List selection; - - ContentSelectionPage contentPage; - OptionsPage optionsPage; - - public ExportCoreWizard() { - setWindowTitle("Export PDF files"); - setNeedsProgressMonitor(true); - } - - public void init(IWorkbench workbench, final IStructuredSelection selection) { - try { - // Create export context - ctx = ExportContext.create( SimanticsUI.getSessionContext(), selection ); - - // Create extension point registry - ctx.eep = Exports.createExtensionPoint(); - - } catch (DatabaseException e) { - ExceptionUtils.logAndShowError(e); - } - } - - @Override - public boolean performFinish() { - - // User clicked finish on the first page. Preapare options page. - if ( getContainer().getCurrentPage() == contentPage ) { - contentPage.validatePage(); - optionsPage.update( contentPage.getContentSelection() ); - } - - final boolean[] canceled = new boolean[1]; - - try { - contentPage.savePrefs(); - optionsPage.savePrefs(); - } catch (ExportException e) { - e.printStackTrace(); - ExceptionUtils.logError(e); - } - - try { - final ExportPlan plan = new ExportPlan(); - final ExportWizardResult wizardResult = optionsPage.getOutput(); - final ExportManager em = new ExportManager( wizardResult.options, ctx ); - wizardResult.createPlan( ctx, plan ); - System.out.println(wizardResult); - - List exportProblems = em.validate(ctx, plan); - if ( !exportProblems.isEmpty() ) { - CollectionUtils.unique(exportProblems); - WizardPage cp = (WizardPage) getContainer().getCurrentPage(); - String str = CollectionUtils.toString(exportProblems, "\n"); - cp.setErrorMessage( str ); - return false; - } - - getContainer().run(true, true, new IRunnableWithProgress() { - @Override - public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { - SubMonitor mon = SubMonitor.convert(monitor, plan.label, 1000000); - try { - em.execute(ctx, mon.newChild(1000000, SubMonitor.SUPPRESS_NONE), plan); - } catch (Exception e) { - throw new InvocationTargetException(e); - } finally { - canceled[0] = monitor.isCanceled(); - monitor.done(); - } - } - }); - - try { - ctx.store.flush(); - } catch (BackingStoreException e) { - ErrorLogger.defaultLogError("Failed to persist wizard preferences.", e); - ExceptionUtils.logError(e); - } - } catch (InvocationTargetException e) { - Throwable t = e.getTargetException(); - WizardPage cp = (WizardPage) getContainer().getCurrentPage(); - - if ( t instanceof ExportException && t.getCause()!=null) { - ExportException ee = (ExportException) t; - if ( ee.getCause() != null ) t = ee.getCause(); - } - - if (canceled[0]) { - cp.setErrorMessage("Export canceled."); - } else if (t instanceof IOException) { - ErrorLogger.defaultLogError("An I/O problem occurred while exporting the model. See exception for details.", t); - cp.setErrorMessage("An I/O problem occurred while exporting the model.\nMessage: " + t.getMessage()); - } else { - ErrorLogger.defaultLogError("Unexpected exception while exporting the model. See exception for details.", t); - cp.setErrorMessage("Unexpected exception while exporting the model. See error log for details.\nMessage: " + t.getMessage()); - } - return false; - } catch (InterruptedException e) { - ExceptionUtils.logAndShowError(e); - } catch (ExportException e) { - ExceptionUtils.logAndShowError(e); - } - - return true; - } - - @Override - public void addPages() { - super.addPages(); - try { - contentPage = new ContentSelectionPage(ctx); - optionsPage = new OptionsPage(ctx); - addPage( contentPage ); - addPage( optionsPage ); - } catch (ExportException e) { - e.printStackTrace(); - ExceptionUtils.logError(e); - } - } - - @Override - public IWizardPage getNextPage(IWizardPage page) { - if ( page == optionsPage ) { - try { - optionsPage.getOutput(); - } catch (ExportException e) { - return null; - } - } - - if ( page == contentPage ) { - contentPage.validatePage(); - optionsPage.update( ExporterUtils.sortContent( contentPage.getContentSelection() ) ); - } - return super.getNextPage(page); - } - -} +package org.simantics.export.ui; + +import java.io.IOException; +import java.lang.reflect.InvocationTargetException; +import java.util.List; + +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.SubMonitor; +import org.eclipse.jface.operation.IRunnableWithProgress; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.jface.wizard.IWizardPage; +import org.eclipse.jface.wizard.Wizard; +import org.eclipse.jface.wizard.WizardPage; +import org.eclipse.ui.IExportWizard; +import org.eclipse.ui.IWorkbench; +import org.osgi.service.prefs.BackingStoreException; +import org.simantics.Simantics; +import org.simantics.db.exception.DatabaseException; +import org.simantics.export.core.ExportContext; +import org.simantics.export.core.Exports; +import org.simantics.export.core.error.ExportException; +import org.simantics.export.core.manager.ExportManager; +import org.simantics.export.core.manager.ExportPlan; +import org.simantics.export.core.manager.ExportWizardResult; +import org.simantics.export.core.util.ExporterUtils; +import org.simantics.utils.datastructures.collections.CollectionUtils; +import org.simantics.utils.ui.ErrorLogger; +import org.simantics.utils.ui.ExceptionUtils; + +public class ExportCoreWizard extends Wizard implements IExportWizard { + + ExportContext ctx; + List selection; + + ContentSelectionPage contentPage; + OptionsPage optionsPage; + + public ExportCoreWizard() { + setWindowTitle("Export PDF files"); + setNeedsProgressMonitor(true); + } + + public void init(IWorkbench workbench, final IStructuredSelection selection) { + try { + // Create export context + ctx = ExportContext.create( Simantics.getSessionContext(), selection ); + + // Create extension point registry + ctx.eep = Exports.createExtensionPoint(); + + } catch (DatabaseException e) { + ExceptionUtils.logAndShowError(e); + } + } + + @Override + public boolean performFinish() { + + // User clicked finish on the first page. Preapare options page. + if ( getContainer().getCurrentPage() == contentPage ) { + contentPage.validatePage(); + optionsPage.update( contentPage.getContentSelection() ); + } + + final boolean[] canceled = new boolean[1]; + + try { + contentPage.savePrefs(); + optionsPage.savePrefs(); + } catch (ExportException e) { + e.printStackTrace(); + ExceptionUtils.logError(e); + } + + try { + final ExportPlan plan = new ExportPlan(); + final ExportWizardResult wizardResult = optionsPage.getOutput(); + final ExportManager em = new ExportManager( wizardResult.options, ctx ); + wizardResult.createPlan( ctx, plan ); + System.out.println(wizardResult); + + List exportProblems = em.validate(ctx, plan); + if ( !exportProblems.isEmpty() ) { + CollectionUtils.unique(exportProblems); + WizardPage cp = (WizardPage) getContainer().getCurrentPage(); + String str = CollectionUtils.toString(exportProblems, "\n"); + cp.setErrorMessage( str ); + return false; + } + + getContainer().run(true, true, new IRunnableWithProgress() { + @Override + public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { + SubMonitor mon = SubMonitor.convert(monitor, plan.label, 1000000); + try { + em.execute(ctx, mon.newChild(1000000, SubMonitor.SUPPRESS_NONE), plan); + } catch (Exception e) { + throw new InvocationTargetException(e); + } finally { + canceled[0] = monitor.isCanceled(); + monitor.done(); + } + } + }); + + try { + ctx.store.flush(); + } catch (BackingStoreException e) { + ErrorLogger.defaultLogError("Failed to persist wizard preferences.", e); + ExceptionUtils.logError(e); + } + } catch (InvocationTargetException e) { + Throwable t = e.getTargetException(); + WizardPage cp = (WizardPage) getContainer().getCurrentPage(); + + if ( t instanceof ExportException && t.getCause()!=null) { + ExportException ee = (ExportException) t; + if ( ee.getCause() != null ) t = ee.getCause(); + } + + if (canceled[0]) { + cp.setErrorMessage("Export canceled."); + } else if (t instanceof IOException) { + ErrorLogger.defaultLogError("An I/O problem occurred while exporting the model. See exception for details.", t); + cp.setErrorMessage("An I/O problem occurred while exporting the model.\nMessage: " + t.getMessage()); + } else { + ErrorLogger.defaultLogError("Unexpected exception while exporting the model. See exception for details.", t); + cp.setErrorMessage("Unexpected exception while exporting the model. See error log for details.\nMessage: " + t.getMessage()); + } + return false; + } catch (InterruptedException e) { + ExceptionUtils.logAndShowError(e); + } catch (ExportException e) { + ExceptionUtils.logAndShowError(e); + } + + return true; + } + + @Override + public void addPages() { + super.addPages(); + try { + contentPage = new ContentSelectionPage(ctx); + optionsPage = new OptionsPage(ctx); + addPage( contentPage ); + addPage( optionsPage ); + } catch (ExportException e) { + e.printStackTrace(); + ExceptionUtils.logError(e); + } + } + + @Override + public IWizardPage getNextPage(IWizardPage page) { + if ( page == optionsPage ) { + try { + optionsPage.getOutput(); + } catch (ExportException e) { + return null; + } + } + + if ( page == contentPage ) { + contentPage.validatePage(); + optionsPage.update( ExporterUtils.sortContent( contentPage.getContentSelection() ) ); + } + return super.getNextPage(page); + } + +}