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.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;
31 public class ExportCoreWizard extends Wizard implements IExportWizard {
34 List<String> selection;
36 ContentSelectionPage contentPage;
37 OptionsPage optionsPage;
39 public ExportCoreWizard() {
40 setWindowTitle(Messages.ExportCoreWizard_ExportPDFFiles);
41 setNeedsProgressMonitor(true);
44 public void init(IWorkbench workbench, final IStructuredSelection selection) {
46 // Create export context
47 ctx = ExportContext.create( Simantics.getSessionContext(), selection );
49 // Create extension point registry
50 ctx.eep = Exports.createExtensionPoint();
52 } catch (DatabaseException e) {
53 ExceptionUtils.logAndShowError(e);
58 public boolean performFinish() {
60 // User clicked finish on the first page. Preapare options page.
61 if ( getContainer().getCurrentPage() == contentPage ) {
62 contentPage.validatePage();
63 optionsPage.update( contentPage.getContentSelection() );
66 final boolean[] canceled = new boolean[1];
69 contentPage.savePrefs();
70 optionsPage.savePrefs();
71 } catch (ExportException e) {
73 ExceptionUtils.logError(e);
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);
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 );
92 getContainer().run(true, true, new IRunnableWithProgress() {
94 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
95 SubMonitor mon = SubMonitor.convert(monitor, plan.label, 1000000);
97 em.execute(ctx, mon.newChild(1000000, SubMonitor.SUPPRESS_NONE), plan);
98 } catch (Exception e) {
99 throw new InvocationTargetException(e);
101 canceled[0] = monitor.isCanceled();
109 } catch (BackingStoreException e) {
110 ErrorLogger.defaultLogError(Messages.ExportCoreWizard_FailedToSavePreferences, e);
111 ExceptionUtils.logError(e);
113 } catch (InvocationTargetException e) {
114 Throwable t = e.getTargetException();
115 WizardPage cp = (WizardPage) getContainer().getCurrentPage();
117 if ( t instanceof ExportException && t.getCause()!=null) {
118 ExportException ee = (ExportException) t;
119 if ( ee.getCause() != null ) t = ee.getCause();
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()));
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()));
132 } catch (InterruptedException e) {
133 ExceptionUtils.logAndShowError(e);
134 } catch (ExportException e) {
135 ExceptionUtils.logAndShowError(e);
142 public void addPages() {
145 contentPage = new ContentSelectionPage(ctx);
146 optionsPage = new OptionsPage(ctx);
147 addPage( contentPage );
148 addPage( optionsPage );
149 } catch (ExportException e) {
151 ExceptionUtils.logError(e);
156 public IWizardPage getNextPage(IWizardPage page) {
157 if ( page == optionsPage ) {
159 optionsPage.getOutput();
160 } catch (ExportException e) {
165 if ( page == contentPage ) {
166 contentPage.validatePage();
167 optionsPage.update( ExporterUtils.sortContent( contentPage.getContentSelection() ) );
169 return super.getNextPage(page);