1 /*******************************************************************************
2 * Copyright (c) 2012 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.charts.ui;
15 import java.io.IOException;
16 import java.lang.reflect.InvocationTargetException;
17 import java.util.Deque;
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.db.management.ISessionContext;
30 import org.simantics.modeling.ui.Activator;
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;
39 * @author Antti Villberg
41 public class CSVExportWizard extends Wizard implements IExportWizard {
43 private static final int MAX_RECENT_EXPORT_PATHS = 10;
45 Deque<String> recentExportPaths;
50 private boolean readPreferences() {
51 IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
53 String recentPathsPref = store.getString(CSVExportPreferences.RECENT_LOCATIONS);
54 recentExportPaths = CSVExportPreferences.decodePaths(recentPathsPref);
55 overwrite = store.getBoolean(CSVExportPreferences.EXPORT_OVERWRITE);
60 private void writePreferences() throws IOException {
61 IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
63 store.putValue(CSVExportPreferences.RECENT_LOCATIONS, CSVExportPreferences.encodePaths(recentExportPaths));
64 store.setValue(CSVExportPreferences.EXPORT_OVERWRITE, plan.overwrite);
66 if (store.needsSaving())
70 public CSVExportWizard() {
71 setWindowTitle("Export Subscription Data As CSV Text");
72 setNeedsProgressMonitor(true);
76 public void init(IWorkbench workbench, IStructuredSelection selection) {
79 ISessionContext ctx = SimanticsUI.getSessionContext();
82 IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);
86 plan = new CSVExportPlan(ctx, recentExportPaths);
87 plan.project = project;
88 plan.selection = selection;
89 plan.overwrite = overwrite;
93 public void addPages() {
96 addPage(new CSVExportPage(plan));
98 addPage(new NoProjectPage("Export Subscription Data As CSV Text"));
103 public boolean performFinish() {
105 recentExportPaths.addFirst(plan.exportLocation.getAbsolutePath());
106 CSVExportPreferences.removeDuplicates(recentExportPaths);
107 if (recentExportPaths.size() > MAX_RECENT_EXPORT_PATHS)
108 recentExportPaths.pollLast();
111 } catch (IOException e) {
112 ErrorLogger.defaultLogError("Failed to write preferences", e);
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?");
123 if (!output.delete()) {
124 MessageDialog.openError(getShell(), "Delete Problem", "Could not overwrite previously existing file " + output);
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());
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());
142 } catch (InterruptedException e) {
143 ExceptionUtils.logAndShowError(e);