]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.annotation.ui/src/org/simantics/annotation/ui/wizard/AnnotationTypeExportWizard.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.annotation.ui / src / org / simantics / annotation / ui / wizard / AnnotationTypeExportWizard.java
index aaa10a115045bc9db4867320010fee017879011a..95b416aef70191bc637f2df1535836db1ac367af 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2012 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.annotation.ui.wizard;\r
-\r
-import java.io.File;\r
-import java.io.IOException;\r
-import java.lang.reflect.InvocationTargetException;\r
-import java.util.Deque;\r
-\r
-import org.eclipse.core.runtime.preferences.InstanceScope;\r
-import org.eclipse.jface.dialogs.MessageDialog;\r
-import org.eclipse.jface.preference.IPersistentPreferenceStore;\r
-import org.eclipse.jface.preference.IPreferenceStore;\r
-import org.eclipse.jface.viewers.IStructuredSelection;\r
-import org.eclipse.jface.wizard.Wizard;\r
-import org.eclipse.jface.wizard.WizardPage;\r
-import org.eclipse.ui.IExportWizard;\r
-import org.eclipse.ui.IWorkbench;\r
-import org.eclipse.ui.preferences.ScopedPreferenceStore;\r
-import org.simantics.annotation.ui.Activator;\r
-import org.simantics.db.management.ISessionContext;\r
-import org.simantics.modeling.ui.utils.NoProjectPage;\r
-import org.simantics.project.IProject;\r
-import org.simantics.project.ProjectKeys;\r
-import org.simantics.ui.SimanticsUI;\r
-import org.simantics.utils.ui.ErrorLogger;\r
-import org.simantics.utils.ui.ExceptionUtils;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- * @author Teemu Mätäsniemi\r
- * @author Antti Villberg\r
- */\r
-public class AnnotationTypeExportWizard extends Wizard implements IExportWizard {\r
-\r
-    private static final int MAX_RECENT_EXPORT_PATHS = 10;\r
-\r
-    Deque<String>            recentExportPaths;\r
-    boolean                  overwrite;\r
-\r
-    ExportPlan        exportModel;\r
-\r
-    private boolean readPreferences() {\r
-        IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);\r
-\r
-        String recentPathsPref = store.getString(Preferences.RECENT_ANNOTATION_TYPE_EXPORT_LOCATIONS);\r
-        recentExportPaths = Preferences.decodePaths(recentPathsPref);\r
-        overwrite = store.getBoolean(Preferences.ANNOTATION_TYPE_EXPORT_OVERWRITE);\r
-\r
-        return true;\r
-    }\r
-\r
-    private void writePreferences() throws IOException {\r
-        IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);\r
-\r
-        store.putValue(Preferences.RECENT_ANNOTATION_TYPE_EXPORT_LOCATIONS, Preferences.encodePaths(recentExportPaths));\r
-        store.setValue(Preferences.ANNOTATION_TYPE_EXPORT_OVERWRITE, exportModel.overwrite);\r
-\r
-        if (store.needsSaving())\r
-            store.save();\r
-    }\r
-\r
-    public AnnotationTypeExportWizard() {\r
-        setWindowTitle("Export Annotation Type");\r
-        setNeedsProgressMonitor(true);\r
-    }\r
-\r
-    @Override\r
-    public void init(IWorkbench workbench, IStructuredSelection selection) {\r
-        readPreferences();\r
-\r
-        ISessionContext ctx = SimanticsUI.getSessionContext();\r
-        if (ctx == null)\r
-            return;\r
-        IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);\r
-        if (project == null)\r
-            return;\r
-\r
-        exportModel = new ExportPlan(ctx, recentExportPaths);\r
-        exportModel.project = project;\r
-        exportModel.selection = selection;\r
-        exportModel.overwrite = overwrite;\r
-    }\r
-\r
-    @Override\r
-    public void addPages() {\r
-        super.addPages();\r
-        if (exportModel != null) {\r
-            addPage(new AnnotationTypeExportPage(exportModel));\r
-        } else {\r
-            addPage(new NoProjectPage("Export Annotation Type"));\r
-        }\r
-    }\r
-\r
-    @Override\r
-    public boolean performFinish() {\r
-        try {\r
-            recentExportPaths.addFirst(exportModel.exportLocation.getAbsolutePath());\r
-            Preferences.removeDuplicates(recentExportPaths);\r
-            if (recentExportPaths.size() > MAX_RECENT_EXPORT_PATHS)\r
-                recentExportPaths.pollLast();\r
-\r
-            writePreferences();\r
-        } catch (IOException e) {\r
-            ErrorLogger.defaultLogError("Failed to write preferences", e);\r
-        }\r
-\r
-        final File output = exportModel.exportLocation;\r
-        if (output.exists()) {\r
-            if (!exportModel.overwrite) {\r
-                boolean ok = MessageDialog.openConfirm(getShell(), "Overwrite", "A file by the name " + output.getAbsolutePath() + " already exists.\n\nDo you want to overwrite?");\r
-                if (!ok) {\r
-                    return false;\r
-                }\r
-            }\r
-            if (!output.delete()) {\r
-                MessageDialog.openError(getShell(), "Delete Problem", "Could not overwrite previously existing file " + output);\r
-                return false;\r
-            }\r
-        }\r
-\r
-        try {\r
-            getContainer().run(true, true, new AnnotationTypeExporter(exportModel));\r
-        } catch (InvocationTargetException e) {\r
-            Throwable t = e.getTargetException();\r
-            WizardPage cp = (WizardPage) getContainer().getCurrentPage();\r
-            if (t instanceof IOException) {\r
-                ErrorLogger.defaultLogError("An I/O problem occurred while exporting the annotation type. See exception for details.", t);\r
-                cp.setErrorMessage("An I/O problem occurred while exporting the annotation type.\n\nMessage: " + e.getMessage());\r
-            } else {\r
-                ErrorLogger.defaultLogError("Unexpected exception while exporting the annotation type. See exception for details.", t);\r
-                cp.setErrorMessage("Unexpected exception while exporting the annotation type. See error log for details.\n\nMessage: " + e.getMessage());\r
-            }\r
-            return false;\r
-        } catch (InterruptedException e) {\r
-            ExceptionUtils.logAndShowError(e);\r
-            return false;\r
-        }\r
-\r
-        return true;\r
-    }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.annotation.ui.wizard;
+
+import java.io.File;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.util.Deque;
+
+import org.eclipse.core.runtime.preferences.InstanceScope;
+import org.eclipse.jface.dialogs.MessageDialog;
+import org.eclipse.jface.preference.IPersistentPreferenceStore;
+import org.eclipse.jface.preference.IPreferenceStore;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.wizard.Wizard;
+import org.eclipse.jface.wizard.WizardPage;
+import org.eclipse.ui.IExportWizard;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.preferences.ScopedPreferenceStore;
+import org.simantics.annotation.ui.Activator;
+import org.simantics.db.management.ISessionContext;
+import org.simantics.modeling.ui.utils.NoProjectPage;
+import org.simantics.project.IProject;
+import org.simantics.project.ProjectKeys;
+import org.simantics.ui.SimanticsUI;
+import org.simantics.utils.ui.ErrorLogger;
+import org.simantics.utils.ui.ExceptionUtils;
+
+/**
+ * @author Tuukka Lehtonen
+ * @author Teemu Mätäsniemi
+ * @author Antti Villberg
+ */
+public class AnnotationTypeExportWizard extends Wizard implements IExportWizard {
+
+    private static final int MAX_RECENT_EXPORT_PATHS = 10;
+
+    Deque<String>            recentExportPaths;
+    boolean                  overwrite;
+
+    ExportPlan        exportModel;
+
+    private boolean readPreferences() {
+        IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
+
+        String recentPathsPref = store.getString(Preferences.RECENT_ANNOTATION_TYPE_EXPORT_LOCATIONS);
+        recentExportPaths = Preferences.decodePaths(recentPathsPref);
+        overwrite = store.getBoolean(Preferences.ANNOTATION_TYPE_EXPORT_OVERWRITE);
+
+        return true;
+    }
+
+    private void writePreferences() throws IOException {
+        IPersistentPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);
+
+        store.putValue(Preferences.RECENT_ANNOTATION_TYPE_EXPORT_LOCATIONS, Preferences.encodePaths(recentExportPaths));
+        store.setValue(Preferences.ANNOTATION_TYPE_EXPORT_OVERWRITE, exportModel.overwrite);
+
+        if (store.needsSaving())
+            store.save();
+    }
+
+    public AnnotationTypeExportWizard() {
+        setWindowTitle("Export Annotation Type");
+        setNeedsProgressMonitor(true);
+    }
+
+    @Override
+    public void init(IWorkbench workbench, IStructuredSelection selection) {
+        readPreferences();
+
+        ISessionContext ctx = SimanticsUI.getSessionContext();
+        if (ctx == null)
+            return;
+        IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);
+        if (project == null)
+            return;
+
+        exportModel = new ExportPlan(ctx, recentExportPaths);
+        exportModel.project = project;
+        exportModel.selection = selection;
+        exportModel.overwrite = overwrite;
+    }
+
+    @Override
+    public void addPages() {
+        super.addPages();
+        if (exportModel != null) {
+            addPage(new AnnotationTypeExportPage(exportModel));
+        } else {
+            addPage(new NoProjectPage("Export Annotation Type"));
+        }
+    }
+
+    @Override
+    public boolean performFinish() {
+        try {
+            recentExportPaths.addFirst(exportModel.exportLocation.getAbsolutePath());
+            Preferences.removeDuplicates(recentExportPaths);
+            if (recentExportPaths.size() > MAX_RECENT_EXPORT_PATHS)
+                recentExportPaths.pollLast();
+
+            writePreferences();
+        } catch (IOException e) {
+            ErrorLogger.defaultLogError("Failed to write preferences", e);
+        }
+
+        final File output = exportModel.exportLocation;
+        if (output.exists()) {
+            if (!exportModel.overwrite) {
+                boolean ok = MessageDialog.openConfirm(getShell(), "Overwrite", "A file by the name " + output.getAbsolutePath() + " already exists.\n\nDo you want to overwrite?");
+                if (!ok) {
+                    return false;
+                }
+            }
+            if (!output.delete()) {
+                MessageDialog.openError(getShell(), "Delete Problem", "Could not overwrite previously existing file " + output);
+                return false;
+            }
+        }
+
+        try {
+            getContainer().run(true, true, new AnnotationTypeExporter(exportModel));
+        } catch (InvocationTargetException e) {
+            Throwable t = e.getTargetException();
+            WizardPage cp = (WizardPage) getContainer().getCurrentPage();
+            if (t instanceof IOException) {
+                ErrorLogger.defaultLogError("An I/O problem occurred while exporting the annotation type. See exception for details.", t);
+                cp.setErrorMessage("An I/O problem occurred while exporting the annotation type.\n\nMessage: " + e.getMessage());
+            } else {
+                ErrorLogger.defaultLogError("Unexpected exception while exporting the annotation type. See exception for details.", t);
+                cp.setErrorMessage("Unexpected exception while exporting the annotation type. See error log for details.\n\nMessage: " + e.getMessage());
+            }
+            return false;
+        } catch (InterruptedException e) {
+            ExceptionUtils.logAndShowError(e);
+            return false;
+        }
+
+        return true;
+    }
+
+}