]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/wizard/DrawingTemplateImportWizard.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.template2d.ui / src / org / simantics / modeling / template2d / ui / wizard / DrawingTemplateImportWizard.java
diff --git a/bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/wizard/DrawingTemplateImportWizard.java b/bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/wizard/DrawingTemplateImportWizard.java
new file mode 100644 (file)
index 0000000..d37592e
--- /dev/null
@@ -0,0 +1,140 @@
+/*******************************************************************************\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.modeling.template2d.ui.wizard;\r
+\r
+import java.io.IOException;\r
+import java.lang.reflect.InvocationTargetException;\r
+import java.util.Deque;\r
+\r
+import org.eclipse.core.runtime.IProgressMonitor;\r
+import org.eclipse.core.runtime.preferences.InstanceScope;\r
+import org.eclipse.jface.operation.IRunnableWithProgress;\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.IImportWizard;\r
+import org.eclipse.ui.IWorkbench;\r
+import org.eclipse.ui.preferences.ScopedPreferenceStore;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.modeling.template2d.DiagramTemplates;\r
+import org.simantics.modeling.template2d.ui.Activator;\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.ui.utils.ResourceAdaptionUtils;\r
+import org.simantics.utils.ui.ErrorLogger;\r
+import org.simantics.utils.ui.ExceptionUtils;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class DrawingTemplateImportWizard extends Wizard implements IImportWizard {\r
+\r
+    private static final int MAX_RECENT_IMPORT_PATHS = 10;\r
+\r
+    ImportPlan        importModel;\r
+\r
+    private boolean readPreferences(IStructuredSelection selection) {\r
+        IPreferenceStore store = new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID);\r
+\r
+        String recentPathsPref = store.getString(Preferences.RECENT_DRAWING_TEMPLATE_IMPORT_LOCATIONS);\r
+        Deque<String> recentImportPaths = Preferences.decodePaths(recentPathsPref);\r
+\r
+        ISessionContext ctx = SimanticsUI.getSessionContext();\r
+        if (ctx == null)\r
+            return false;\r
+        IProject project = ctx.getHint(ProjectKeys.KEY_PROJECT);\r
+        if (project == null)\r
+            return false;\r
+\r
+        importModel = new ImportPlan(ctx, recentImportPaths);\r
+        importModel.project = project;\r
+        importModel.selection = selection.getFirstElement();\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_DRAWING_TEMPLATE_IMPORT_LOCATIONS, Preferences.encodePaths(importModel.recentLocations));\r
+\r
+        if (store.needsSaving())\r
+            store.save();\r
+    }\r
+\r
+    public DrawingTemplateImportWizard() {\r
+        setWindowTitle("Import Diagram Template");\r
+        setNeedsProgressMonitor(true);\r
+    }\r
+\r
+    @Override\r
+    public void init(IWorkbench workbench, IStructuredSelection selection) {\r
+        readPreferences(selection);\r
+    }\r
+\r
+    @Override\r
+    public void addPages() {\r
+        super.addPages();\r
+        if (importModel != null) {\r
+            addPage(new DrawingTemplateImportPage(importModel));\r
+        } else {\r
+            addPage(new NoProjectPage("Import Diagram Template"));\r
+        }\r
+    }\r
+\r
+    @Override\r
+    public boolean performFinish() {\r
+        try {\r
+               importModel.recentLocations.addFirst(importModel.importLocation.getAbsolutePath());\r
+            Preferences.removeDuplicates(importModel.recentLocations);\r
+            if (importModel.recentLocations.size() > MAX_RECENT_IMPORT_PATHS)\r
+               importModel.recentLocations.pollLast();\r
+\r
+            writePreferences();\r
+        } catch (IOException e) {\r
+            ErrorLogger.defaultLogError("Failed to write preferences", e);\r
+        }\r
+\r
+        try {\r
+            getContainer().run(true, true, new IRunnableWithProgress() {\r
+                @Override\r
+                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {\r
+                    try {\r
+                        Resource target = ResourceAdaptionUtils.toSingleResource(importModel.selection);\r
+                        DiagramTemplates.importTemplate(monitor, importModel.sessionContext.getSession(), importModel.importLocation, target);\r
+                    } catch (Exception e) {\r
+                        throw new InvocationTargetException(e);\r
+                    }\r
+                }\r
+            });\r
+        } catch (InvocationTargetException e) {\r
+            Throwable t = e.getTargetException();\r
+            WizardPage cp = (WizardPage) getContainer().getCurrentPage();\r
+            if (t instanceof IOException) {\r
+                cp.setErrorMessage("An I/O problem occurred while importing a diagram template.\n\nMessage: " + e.getMessage());\r
+            }\r
+            ErrorLogger.defaultLogError(t);\r
+            return false;\r
+        } catch (InterruptedException e) {\r
+            ExceptionUtils.logAndShowError(e);\r
+            return false;\r
+        }\r
+\r
+        return true;\r
+    }\r
+\r
+}\r