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