]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.export.ui/src/org/simantics/export/ui/ExportCoreWizard.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.export.ui / src / org / simantics / export / ui / ExportCoreWizard.java
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
new file mode 100644 (file)
index 0000000..e18efbd
--- /dev/null
@@ -0,0 +1,171 @@
+package org.simantics.export.ui;\r
+\r
+import java.io.IOException;\r
+import java.lang.reflect.InvocationTargetException;\r
+import java.util.List;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.SubMonitor;\r
+import org.eclipse.jface.operation.IRunnableWithProgress;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.eclipse.jface.wizard.IWizardPage;\r
+import org.eclipse.jface.wizard.Wizard;\r
+import org.eclipse.jface.wizard.WizardPage;\r
+import org.eclipse.ui.IExportWizard;\r
+import org.eclipse.ui.IWorkbench;\r
+import org.osgi.service.prefs.BackingStoreException;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.export.core.ExportContext;\r
+import org.simantics.export.core.Exports;\r
+import org.simantics.export.core.error.ExportException;\r
+import org.simantics.export.core.manager.ExportManager;\r
+import org.simantics.export.core.manager.ExportPlan;\r
+import org.simantics.export.core.manager.ExportWizardResult;\r
+import org.simantics.export.core.util.ExporterUtils;\r
+import org.simantics.ui.SimanticsUI;\r
+import org.simantics.utils.datastructures.collections.CollectionUtils;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+import org.simantics.utils.ui.ExceptionUtils;\r
+\r
+public class ExportCoreWizard extends Wizard implements IExportWizard {\r
+\r
+       ExportContext ctx;\r
+       List<String> selection;\r
+\r
+       ContentSelectionPage contentPage;\r
+       OptionsPage optionsPage;\r
+\r
+       public ExportCoreWizard() {\r
+               setWindowTitle("Export PDF files");\r
+               setNeedsProgressMonitor(true);\r
+       }\r
+\r
+       public void init(IWorkbench workbench, final IStructuredSelection selection) {\r
+               try {\r
+                       // Create export context\r
+                       ctx = ExportContext.create( SimanticsUI.getSessionContext(), selection );\r
+\r
+                       // Create extension point registry\r
+                       ctx.eep = Exports.createExtensionPoint();\r
+\r
+               } catch (DatabaseException e) {\r
+                       ExceptionUtils.logAndShowError(e);\r
+               }\r
+       }\r
+\r
+       @Override\r
+       public boolean performFinish() {\r
+\r
+               // User clicked finish on the first page. Preapare options page.\r
+               if ( getContainer().getCurrentPage() == contentPage ) {\r
+                       contentPage.validatePage();\r
+                       optionsPage.update( contentPage.getContentSelection() );\r
+               }\r
+\r
+               final boolean[] canceled = new boolean[1];\r
+\r
+               try {\r
+                       contentPage.savePrefs();\r
+                       optionsPage.savePrefs();\r
+               } catch (ExportException e) {\r
+                       e.printStackTrace();\r
+                       ExceptionUtils.logError(e);\r
+               }\r
+\r
+               try {\r
+                       final ExportPlan plan = new ExportPlan();\r
+                       final ExportWizardResult wizardResult = optionsPage.getOutput();\r
+                       final ExportManager em = new ExportManager( wizardResult.options, ctx );\r
+                       wizardResult.createPlan( ctx, plan );\r
+                       System.out.println(wizardResult);\r
+\r
+                       List<String> exportProblems = em.validate(ctx, plan);\r
+                       if ( !exportProblems.isEmpty() ) {\r
+                               CollectionUtils.unique(exportProblems);\r
+                               WizardPage cp = (WizardPage) getContainer().getCurrentPage();\r
+                               String str = CollectionUtils.toString(exportProblems, "\n");\r
+                               cp.setErrorMessage( str );\r
+                               return false;\r
+                       }\r
+\r
+                       getContainer().run(true, true, new IRunnableWithProgress() {\r
+                               @Override\r
+                               public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
+                                       SubMonitor mon = SubMonitor.convert(monitor, plan.label, 1000000);\r
+                                       try {\r
+                                               em.execute(ctx, mon.newChild(1000000, SubMonitor.SUPPRESS_NONE), plan);\r
+                                       } catch (Exception e) {\r
+                                               throw new InvocationTargetException(e);\r
+                                       } finally {\r
+                                               canceled[0] = monitor.isCanceled();\r
+                                               monitor.done();\r
+                                       }\r
+                               }\r
+                       });\r
+\r
+                       try {\r
+                               ctx.store.flush();\r
+                       } catch (BackingStoreException e) {\r
+                               ErrorLogger.defaultLogError("Failed to persist wizard preferences.", e);\r
+                               ExceptionUtils.logError(e);\r
+                       }\r
+               } catch (InvocationTargetException e) {\r
+                       Throwable t = e.getTargetException();\r
+                       WizardPage cp = (WizardPage) getContainer().getCurrentPage();\r
+\r
+                       if ( t instanceof ExportException && t.getCause()!=null) {\r
+                               ExportException ee = (ExportException) t;\r
+                               if ( ee.getCause() != null ) t = ee.getCause();\r
+                       }\r
+\r
+                       if (canceled[0]) {\r
+                               cp.setErrorMessage("Export canceled.");\r
+                       } else if (t instanceof IOException) {\r
+                               ErrorLogger.defaultLogError("An I/O problem occurred while exporting the model. See exception for details.", t);\r
+                               cp.setErrorMessage("An I/O problem occurred while exporting the model.\nMessage: " + t.getMessage());\r
+                       } else {\r
+                               ErrorLogger.defaultLogError("Unexpected exception while exporting the model. See exception for details.", t);\r
+                               cp.setErrorMessage("Unexpected exception while exporting the model. See error log for details.\nMessage: " + t.getMessage());\r
+                       }\r
+                       return false;\r
+               } catch (InterruptedException e) {\r
+                       ExceptionUtils.logAndShowError(e);\r
+               } catch (ExportException e) {\r
+                       ExceptionUtils.logAndShowError(e);\r
+               }\r
+\r
+               return true;\r
+       }\r
+\r
+       @Override\r
+       public void addPages() {\r
+               super.addPages();\r
+               try {\r
+                       contentPage = new ContentSelectionPage(ctx);\r
+                       optionsPage = new OptionsPage(ctx);\r
+                       addPage( contentPage );\r
+                       addPage( optionsPage );\r
+               } catch (ExportException e) {\r
+                       e.printStackTrace();\r
+                       ExceptionUtils.logError(e);                     \r
+               }\r
+       }\r
+\r
+       @Override\r
+       public IWizardPage getNextPage(IWizardPage page) {\r
+               if ( page == optionsPage ) {\r
+                       try {\r
+                               optionsPage.getOutput();\r
+                       } catch (ExportException e) {\r
+                               return null;\r
+                       }\r
+               }\r
+\r
+               if ( page == contentPage ) {\r
+                       contentPage.validatePage();\r
+                       optionsPage.update( ExporterUtils.sortContent( contentPage.getContentSelection() ) );\r
+               }\r
+               return super.getNextPage(page);\r
+       }\r
+\r
+}\r