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