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