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