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