]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.ui/src/org/simantics/modeling/ui/sharedontology/wizard/ModelExportWizard.java
Remove usage of deprecated SimanticsUI-methods
[simantics/platform.git] / bundles / org.simantics.modeling.ui / src / org / simantics / modeling / ui / sharedontology / wizard / ModelExportWizard.java
1 /*******************************************************************************
2  * Copyright (c) 2012 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.modeling.ui.sharedontology.wizard;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.lang.reflect.InvocationTargetException;
17 import java.util.Deque;
18
19 import org.eclipse.core.runtime.preferences.InstanceScope;
20 import org.eclipse.jface.dialogs.MessageDialog;
21 import org.eclipse.jface.preference.IPersistentPreferenceStore;
22 import org.eclipse.jface.preference.IPreferenceStore;
23 import org.eclipse.jface.viewers.IStructuredSelection;
24 import org.eclipse.jface.wizard.Wizard;
25 import org.eclipse.jface.wizard.WizardPage;
26 import org.eclipse.ui.IExportWizard;
27 import org.eclipse.ui.IWorkbench;
28 import org.eclipse.ui.preferences.ScopedPreferenceStore;
29 import org.simantics.Simantics;
30 import org.simantics.db.management.ISessionContext;
31 import org.simantics.modeling.ui.Activator;
32 import org.simantics.modeling.ui.utils.NoProjectPage;
33 import org.simantics.project.IProject;
34 import org.simantics.project.ProjectKeys;
35 import org.simantics.utils.ui.ErrorLogger;
36 import org.simantics.utils.ui.ExceptionUtils;
37
38 /**
39  * @author Tuukka Lehtonen
40  * @author Teemu Mätäsniemi
41  * @author Antti Villberg
42  */
43 public class ModelExportWizard extends Wizard implements IExportWizard {
44
45     private static final int MAX_RECENT_EXPORT_PATHS = 10;
46
47     Deque<String>            recentExportPaths;
48     boolean                  overwrite;
49     boolean                  includeDependencies;
50
51     ExportPlan               exportModel;
52
53     private boolean readPreferences() {
54         IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
55
56         String recentPathsPref = store.getString(Preferences.RECENT_SHARED_LIBRARY_EXPORT_LOCATIONS);
57         recentExportPaths = Preferences.decodePaths(recentPathsPref);
58         overwrite = store.getBoolean(Preferences.SHARED_LIBRARY_EXPORT_OVERWRITE);
59         includeDependencies = store.getBoolean(Preferences.EXPORT_INCLUDE_DEPENDENCIES);
60
61         return true;
62     }
63
64     private void writePreferences() throws IOException {
65         IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
66
67         store.putValue(Preferences.RECENT_SHARED_LIBRARY_EXPORT_LOCATIONS, Preferences.encodePaths(recentExportPaths));
68         store.setValue(Preferences.SHARED_LIBRARY_EXPORT_OVERWRITE, exportModel.overwrite);
69         store.setValue(Preferences.EXPORT_INCLUDE_DEPENDENCIES, exportModel.includeDependencies);
70
71         if (store.needsSaving())
72             store.save();
73     }
74
75     public ModelExportWizard() {
76         setWindowTitle("Export Model");
77         setNeedsProgressMonitor(true);
78     }
79
80     @Override
81     public void init(IWorkbench workbench, IStructuredSelection selection) {
82         readPreferences();
83
84         ISessionContext ctx = Simantics.getSessionContext();
85         if (ctx == null)
86             return;
87         IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);
88         if (project == null)
89             return;
90
91         exportModel = new ExportPlan(ctx, recentExportPaths);
92         exportModel.project = project;
93         exportModel.selection = selection;
94         exportModel.overwrite = overwrite;
95         exportModel.includeDependencies = includeDependencies;
96     }
97
98     @Override
99     public void addPages() {
100         super.addPages();
101         if (exportModel != null) {
102             addPage(new ModelExportPage(exportModel));
103         } else {
104             addPage(new NoProjectPage("Export Model"));
105         }
106     }
107
108     @Override
109     public boolean performFinish() {
110         try {
111             recentExportPaths.addFirst(exportModel.exportLocation.getAbsolutePath());
112             Preferences.removeDuplicates(recentExportPaths);
113             if (recentExportPaths.size() > MAX_RECENT_EXPORT_PATHS)
114                 recentExportPaths.pollLast();
115
116             writePreferences();
117         } catch (IOException e) {
118             ErrorLogger.defaultLogError("Failed to write preferences", e);
119         }
120
121         final File output = exportModel.exportLocation;
122         if (output.exists()) {
123             if (!exportModel.overwrite) {
124                 boolean ok = MessageDialog.openConfirm(getShell(), "Overwrite", "A file by the name " + output.getAbsolutePath() + " already exists.\n\nDo you want to overwrite?");
125                 if (!ok) {
126                     return false;
127                 }
128             }
129             if (!output.delete()) {
130                 MessageDialog.openError(getShell(), "Delete Problem", "Could not overwrite previously existing file " + output);
131                 return false;
132             }
133         }
134
135         try {
136             getContainer().run(true, true, new ModelExporter(exportModel));
137         } catch (InvocationTargetException e) {
138             Throwable t = e.getTargetException();
139             WizardPage cp = (WizardPage) getContainer().getCurrentPage();
140             if (t instanceof IOException) {
141                 ErrorLogger.defaultLogError("An I/O problem occurred while exporting the shared library. See exception for details.", t);
142                 cp.setErrorMessage("An I/O problem occurred while exporting the shared library.\n\nMessage: " + e.getMessage());
143             } else {
144                 ErrorLogger.defaultLogError("Unexpected exception while exporting the shared library. See exception for details.", t);
145                 cp.setErrorMessage("Unexpected exception while exporting the shared library. See error log for details.\n\nMessage: " + e.getMessage());
146             }
147             return false;
148         } catch (InterruptedException e) {
149             ExceptionUtils.logAndShowError(e);
150             return false;
151         }
152
153         return true;
154     }
155
156 }