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