]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 package org.simantics.export.ui;\r
2 \r
3 import java.io.IOException;\r
4 import java.lang.reflect.InvocationTargetException;\r
5 import java.util.List;\r
6 \r
7 import org.eclipse.core.runtime.IProgressMonitor;\r
8 import org.eclipse.core.runtime.SubMonitor;\r
9 import org.eclipse.jface.operation.IRunnableWithProgress;\r
10 import org.eclipse.jface.viewers.IStructuredSelection;\r
11 import org.eclipse.jface.wizard.IWizardPage;\r
12 import org.eclipse.jface.wizard.Wizard;\r
13 import org.eclipse.jface.wizard.WizardPage;\r
14 import org.eclipse.ui.IExportWizard;\r
15 import org.eclipse.ui.IWorkbench;\r
16 import org.osgi.service.prefs.BackingStoreException;\r
17 import org.simantics.db.exception.DatabaseException;\r
18 import org.simantics.export.core.ExportContext;\r
19 import org.simantics.export.core.Exports;\r
20 import org.simantics.export.core.error.ExportException;\r
21 import org.simantics.export.core.manager.ExportManager;\r
22 import org.simantics.export.core.manager.ExportPlan;\r
23 import org.simantics.export.core.manager.ExportWizardResult;\r
24 import org.simantics.export.core.util.ExporterUtils;\r
25 import org.simantics.ui.SimanticsUI;\r
26 import org.simantics.utils.datastructures.collections.CollectionUtils;\r
27 import org.simantics.utils.ui.ErrorLogger;\r
28 import org.simantics.utils.ui.ExceptionUtils;\r
29 \r
30 public class ExportCoreWizard extends Wizard implements IExportWizard {\r
31 \r
32         ExportContext ctx;\r
33         List<String> selection;\r
34 \r
35         ContentSelectionPage contentPage;\r
36         OptionsPage optionsPage;\r
37 \r
38         public ExportCoreWizard() {\r
39                 setWindowTitle("Export PDF files");\r
40                 setNeedsProgressMonitor(true);\r
41         }\r
42 \r
43         public void init(IWorkbench workbench, final IStructuredSelection selection) {\r
44                 try {\r
45                         // Create export context\r
46                         ctx = ExportContext.create( SimanticsUI.getSessionContext(), selection );\r
47 \r
48                         // Create extension point registry\r
49                         ctx.eep = Exports.createExtensionPoint();\r
50 \r
51                 } catch (DatabaseException e) {\r
52                         ExceptionUtils.logAndShowError(e);\r
53                 }\r
54         }\r
55 \r
56         @Override\r
57         public boolean performFinish() {\r
58 \r
59                 // User clicked finish on the first page. Preapare options page.\r
60                 if ( getContainer().getCurrentPage() == contentPage ) {\r
61                         contentPage.validatePage();\r
62                         optionsPage.update( contentPage.getContentSelection() );\r
63                 }\r
64 \r
65                 final boolean[] canceled = new boolean[1];\r
66 \r
67                 try {\r
68                         contentPage.savePrefs();\r
69                         optionsPage.savePrefs();\r
70                 } catch (ExportException e) {\r
71                         e.printStackTrace();\r
72                         ExceptionUtils.logError(e);\r
73                 }\r
74 \r
75                 try {\r
76                         final ExportPlan plan = new ExportPlan();\r
77                         final ExportWizardResult wizardResult = optionsPage.getOutput();\r
78                         final ExportManager em = new ExportManager( wizardResult.options, ctx );\r
79                         wizardResult.createPlan( ctx, plan );\r
80                         System.out.println(wizardResult);\r
81 \r
82                         List<String> exportProblems = em.validate(ctx, plan);\r
83                         if ( !exportProblems.isEmpty() ) {\r
84                                 CollectionUtils.unique(exportProblems);\r
85                                 WizardPage cp = (WizardPage) getContainer().getCurrentPage();\r
86                                 String str = CollectionUtils.toString(exportProblems, "\n");\r
87                                 cp.setErrorMessage( str );\r
88                                 return false;\r
89                         }\r
90 \r
91                         getContainer().run(true, true, new IRunnableWithProgress() {\r
92                                 @Override\r
93                                 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
94                                         SubMonitor mon = SubMonitor.convert(monitor, plan.label, 1000000);\r
95                                         try {\r
96                                                 em.execute(ctx, mon.newChild(1000000, SubMonitor.SUPPRESS_NONE), plan);\r
97                                         } catch (Exception e) {\r
98                                                 throw new InvocationTargetException(e);\r
99                                         } finally {\r
100                                                 canceled[0] = monitor.isCanceled();\r
101                                                 monitor.done();\r
102                                         }\r
103                                 }\r
104                         });\r
105 \r
106                         try {\r
107                                 ctx.store.flush();\r
108                         } catch (BackingStoreException e) {\r
109                                 ErrorLogger.defaultLogError("Failed to persist wizard preferences.", e);\r
110                                 ExceptionUtils.logError(e);\r
111                         }\r
112                 } catch (InvocationTargetException e) {\r
113                         Throwable t = e.getTargetException();\r
114                         WizardPage cp = (WizardPage) getContainer().getCurrentPage();\r
115 \r
116                         if ( t instanceof ExportException && t.getCause()!=null) {\r
117                                 ExportException ee = (ExportException) t;\r
118                                 if ( ee.getCause() != null ) t = ee.getCause();\r
119                         }\r
120 \r
121                         if (canceled[0]) {\r
122                                 cp.setErrorMessage("Export canceled.");\r
123                         } else if (t instanceof IOException) {\r
124                                 ErrorLogger.defaultLogError("An I/O problem occurred while exporting the model. See exception for details.", t);\r
125                                 cp.setErrorMessage("An I/O problem occurred while exporting the model.\nMessage: " + t.getMessage());\r
126                         } else {\r
127                                 ErrorLogger.defaultLogError("Unexpected exception while exporting the model. See exception for details.", t);\r
128                                 cp.setErrorMessage("Unexpected exception while exporting the model. See error log for details.\nMessage: " + t.getMessage());\r
129                         }\r
130                         return false;\r
131                 } catch (InterruptedException e) {\r
132                         ExceptionUtils.logAndShowError(e);\r
133                 } catch (ExportException e) {\r
134                         ExceptionUtils.logAndShowError(e);\r
135                 }\r
136 \r
137                 return true;\r
138         }\r
139 \r
140         @Override\r
141         public void addPages() {\r
142                 super.addPages();\r
143                 try {\r
144                         contentPage = new ContentSelectionPage(ctx);\r
145                         optionsPage = new OptionsPage(ctx);\r
146                         addPage( contentPage );\r
147                         addPage( optionsPage );\r
148                 } catch (ExportException e) {\r
149                         e.printStackTrace();\r
150                         ExceptionUtils.logError(e);                     \r
151                 }\r
152         }\r
153 \r
154         @Override\r
155         public IWizardPage getNextPage(IWizardPage page) {\r
156                 if ( page == optionsPage ) {\r
157                         try {\r
158                                 optionsPage.getOutput();\r
159                         } catch (ExportException e) {\r
160                                 return null;\r
161                         }\r
162                 }\r
163 \r
164                 if ( page == contentPage ) {\r
165                         contentPage.validatePage();\r
166                         optionsPage.update( ExporterUtils.sortContent( contentPage.getContentSelection() ) );\r
167                 }\r
168                 return super.getNextPage(page);\r
169         }\r
170 \r
171 }\r