1 package org.simantics.export.ui;
\r
3 import java.io.IOException;
\r
4 import java.lang.reflect.InvocationTargetException;
\r
5 import java.util.List;
\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
30 public class ExportCoreWizard extends Wizard implements IExportWizard {
\r
33 List<String> selection;
\r
35 ContentSelectionPage contentPage;
\r
36 OptionsPage optionsPage;
\r
38 public ExportCoreWizard() {
\r
39 setWindowTitle("Export PDF files");
\r
40 setNeedsProgressMonitor(true);
\r
43 public void init(IWorkbench workbench, final IStructuredSelection selection) {
\r
45 // Create export context
\r
46 ctx = ExportContext.create( SimanticsUI.getSessionContext(), selection );
\r
48 // Create extension point registry
\r
49 ctx.eep = Exports.createExtensionPoint();
\r
51 } catch (DatabaseException e) {
\r
52 ExceptionUtils.logAndShowError(e);
\r
57 public boolean performFinish() {
\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
65 final boolean[] canceled = new boolean[1];
\r
68 contentPage.savePrefs();
\r
69 optionsPage.savePrefs();
\r
70 } catch (ExportException e) {
\r
71 e.printStackTrace();
\r
72 ExceptionUtils.logError(e);
\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
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
91 getContainer().run(true, true, new IRunnableWithProgress() {
\r
93 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
\r
94 SubMonitor mon = SubMonitor.convert(monitor, plan.label, 1000000);
\r
96 em.execute(ctx, mon.newChild(1000000, SubMonitor.SUPPRESS_NONE), plan);
\r
97 } catch (Exception e) {
\r
98 throw new InvocationTargetException(e);
\r
100 canceled[0] = monitor.isCanceled();
\r
108 } catch (BackingStoreException e) {
\r
109 ErrorLogger.defaultLogError("Failed to persist wizard preferences.", e);
\r
110 ExceptionUtils.logError(e);
\r
112 } catch (InvocationTargetException e) {
\r
113 Throwable t = e.getTargetException();
\r
114 WizardPage cp = (WizardPage) getContainer().getCurrentPage();
\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
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
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
131 } catch (InterruptedException e) {
\r
132 ExceptionUtils.logAndShowError(e);
\r
133 } catch (ExportException e) {
\r
134 ExceptionUtils.logAndShowError(e);
\r
141 public void addPages() {
\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
155 public IWizardPage getNextPage(IWizardPage page) {
\r
156 if ( page == optionsPage ) {
\r
158 optionsPage.getOutput();
\r
159 } catch (ExportException e) {
\r
164 if ( page == contentPage ) {
\r
165 contentPage.validatePage();
\r
166 optionsPage.update( ExporterUtils.sortContent( contentPage.getContentSelection() ) );
\r
168 return super.getNextPage(page);
\r