]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/wizard/DrawingTemplateExportWizard.java
Remove usage of deprecated SimanticsUI-methods
[simantics/platform.git] / bundles / org.simantics.modeling.template2d.ui / src / org / simantics / modeling / template2d / ui / wizard / DrawingTemplateExportWizard.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.template2d.ui.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.template2d.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  */
42 public class DrawingTemplateExportWizard extends Wizard implements IExportWizard {
43
44     private static final int MAX_RECENT_EXPORT_PATHS = 10;
45
46     Deque<String>            recentExportPaths;
47     boolean                  overwrite;
48
49     ExportPlan        exportModel;
50
51     private boolean readPreferences() {
52         IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
53
54         String recentPathsPref = store.getString(Preferences.RECENT_DRAWING_TEMPLATE_EXPORT_LOCATIONS);
55         recentExportPaths = Preferences.decodePaths(recentPathsPref);
56         overwrite = store.getBoolean(Preferences.DRAWING_TEMPLATE_EXPORT_OVERWRITE);
57
58         return true;
59     }
60
61     private void writePreferences() throws IOException {
62         IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
63
64         store.putValue(Preferences.RECENT_DRAWING_TEMPLATE_EXPORT_LOCATIONS, Preferences.encodePaths(recentExportPaths));
65         store.setValue(Preferences.DRAWING_TEMPLATE_EXPORT_OVERWRITE, exportModel.overwrite);
66
67         if (store.needsSaving())
68             store.save();
69     }
70
71     public DrawingTemplateExportWizard() {
72         setWindowTitle("Export Diagram Template");
73         setNeedsProgressMonitor(true);
74     }
75
76     @Override
77     public void init(IWorkbench workbench, IStructuredSelection selection) {
78         readPreferences();
79
80         ISessionContext ctx = Simantics.getSessionContext();
81         if (ctx == null)
82             return;
83         IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);
84         if (project == null)
85             return;
86
87         exportModel = new ExportPlan(ctx, recentExportPaths);
88         exportModel.project = project;
89         exportModel.selection = selection;
90         exportModel.overwrite = overwrite;
91     }
92
93     @Override
94     public void addPages() {
95         super.addPages();
96         if (exportModel != null) {
97             addPage(new DrawingTemplateExportPage(exportModel));
98         } else {
99             addPage(new NoProjectPage("Export Diagram Template"));
100         }
101     }
102
103     @Override
104     public boolean performFinish() {
105         try {
106             recentExportPaths.addFirst(exportModel.exportLocation.getAbsolutePath());
107             Preferences.removeDuplicates(recentExportPaths);
108             if (recentExportPaths.size() > MAX_RECENT_EXPORT_PATHS)
109                 recentExportPaths.pollLast();
110
111             writePreferences();
112         } catch (IOException e) {
113             ErrorLogger.defaultLogError("Failed to write preferences", e);
114         }
115
116         final File output = exportModel.exportLocation;
117         if (output.exists()) {
118             if (!exportModel.overwrite) {
119                 boolean ok = MessageDialog.openConfirm(getShell(), "Overwrite", "A file by the name " + output.getAbsolutePath() + " already exists.\n\nDo you want to overwrite?");
120                 if (!ok) {
121                     return false;
122                 }
123             }
124             if (!output.delete()) {
125                 MessageDialog.openError(getShell(), "Delete Problem", "Could not overwrite previously existing file " + output);
126                 return false;
127             }
128         }
129
130         try {
131             getContainer().run(true, true, new DrawingTemplateExporter(exportModel));
132         } catch (InvocationTargetException e) {
133             Throwable t = e.getTargetException();
134             WizardPage cp = (WizardPage) getContainer().getCurrentPage();
135             if (t instanceof IOException) {
136                 ErrorLogger.defaultLogError("An I/O problem occurred while exporting diagram template. See exception for details.", t);
137                 cp.setErrorMessage("An I/O problem occurred while exporting diagram template.\n\nMessage: " + e.getMessage());
138             } else {
139                 ErrorLogger.defaultLogError("Unexpected exception while exporting diagram template. See exception for details.", t);
140                 cp.setErrorMessage("Unexpected exception while exporting diagram template. See error log for details.\n\nMessage: " + e.getMessage());
141             }
142             return false;
143         } catch (InterruptedException e) {
144             ExceptionUtils.logAndShowError(e);
145             return false;
146         }
147
148         return true;
149     }
150
151 }