1 package org.simantics.export.ui;
3 import java.io.IOException;
4 import java.lang.reflect.InvocationTargetException;
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.db.exception.DatabaseException;
18 import org.simantics.export.core.ExportContext;
19 import org.simantics.export.core.Exports;
20 import org.simantics.export.core.error.ExportException;
21 import org.simantics.export.core.manager.ExportManager;
22 import org.simantics.export.core.manager.ExportPlan;
23 import org.simantics.export.core.manager.ExportWizardResult;
24 import org.simantics.export.core.util.ExporterUtils;
25 import org.simantics.ui.SimanticsUI;
26 import org.simantics.utils.datastructures.collections.CollectionUtils;
27 import org.simantics.utils.ui.ErrorLogger;
28 import org.simantics.utils.ui.ExceptionUtils;
30 public class ExportCoreWizard extends Wizard implements IExportWizard {
33 List<String> selection;
35 ContentSelectionPage contentPage;
36 OptionsPage optionsPage;
38 public ExportCoreWizard() {
39 setWindowTitle("Export PDF files");
40 setNeedsProgressMonitor(true);
43 public void init(IWorkbench workbench, final IStructuredSelection selection) {
45 // Create export context
46 ctx = ExportContext.create( SimanticsUI.getSessionContext(), selection );
48 // Create extension point registry
49 ctx.eep = Exports.createExtensionPoint();
51 } catch (DatabaseException e) {
52 ExceptionUtils.logAndShowError(e);
57 public boolean performFinish() {
59 // User clicked finish on the first page. Preapare options page.
60 if ( getContainer().getCurrentPage() == contentPage ) {
61 contentPage.validatePage();
62 optionsPage.update( contentPage.getContentSelection() );
65 final boolean[] canceled = new boolean[1];
68 contentPage.savePrefs();
69 optionsPage.savePrefs();
70 } catch (ExportException e) {
72 ExceptionUtils.logError(e);
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);
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 );
91 getContainer().run(true, true, new IRunnableWithProgress() {
93 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
94 SubMonitor mon = SubMonitor.convert(monitor, plan.label, 1000000);
96 em.execute(ctx, mon.newChild(1000000, SubMonitor.SUPPRESS_NONE), plan);
97 } catch (Exception e) {
98 throw new InvocationTargetException(e);
100 canceled[0] = monitor.isCanceled();
108 } catch (BackingStoreException e) {
109 ErrorLogger.defaultLogError("Failed to persist wizard preferences.", e);
110 ExceptionUtils.logError(e);
112 } catch (InvocationTargetException e) {
113 Throwable t = e.getTargetException();
114 WizardPage cp = (WizardPage) getContainer().getCurrentPage();
116 if ( t instanceof ExportException && t.getCause()!=null) {
117 ExportException ee = (ExportException) t;
118 if ( ee.getCause() != null ) t = ee.getCause();
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());
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());
131 } catch (InterruptedException e) {
132 ExceptionUtils.logAndShowError(e);
133 } catch (ExportException e) {
134 ExceptionUtils.logAndShowError(e);
141 public void addPages() {
144 contentPage = new ContentSelectionPage(ctx);
145 optionsPage = new OptionsPage(ctx);
146 addPage( contentPage );
147 addPage( optionsPage );
148 } catch (ExportException e) {
150 ExceptionUtils.logError(e);
155 public IWizardPage getNextPage(IWizardPage page) {
156 if ( page == optionsPage ) {
158 optionsPage.getOutput();
159 } catch (ExportException e) {
164 if ( page == contentPage ) {
165 contentPage.validatePage();
166 optionsPage.update( ExporterUtils.sortContent( contentPage.getContentSelection() ) );
168 return super.getNextPage(page);