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