]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/ExportModelActionFactory.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / adapters / ExportModelActionFactory.java
diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/ExportModelActionFactory.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/adapters/ExportModelActionFactory.java
new file mode 100644 (file)
index 0000000..f3a15f6
--- /dev/null
@@ -0,0 +1,93 @@
+/*******************************************************************************\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.adapters;\r
+\r
+import java.io.File;\r
+\r
+import org.eclipse.swt.SWT;\r
+import org.eclipse.swt.widgets.Display;\r
+import org.eclipse.swt.widgets.FileDialog;\r
+import org.eclipse.swt.widgets.Shell;\r
+import org.simantics.Simantics;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.common.primitiverequest.PossibleRelatedValue;\r
+import org.simantics.db.common.request.UniqueRead;\r
+import org.simantics.db.common.utils.Logger;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.adapter.ActionFactory;\r
+import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;\r
+import org.simantics.db.layer0.util.TransferableGraphConfiguration2;\r
+import org.simantics.graph.db.TransferableGraphSource;\r
+import org.simantics.graph.db.TransferableGraphs;\r
+import org.simantics.layer0.Layer0;\r
+\r
+public class ExportModelActionFactory implements ActionFactory {\r
+\r
+    String format;\r
+    String extension;\r
+    \r
+    public ExportModelActionFactory(String extension, String format) {\r
+        this.extension = extension;\r
+        this.format = format;\r
+    }\r
+\r
+    @Override\r
+    public Runnable create(Object target_) {\r
+        final Resource target = (Resource)target_;\r
+        return new Runnable() {\r
+            @Override\r
+            public void run() {\r
+\r
+               Shell shell = Display.getDefault().getActiveShell();\r
\r
+               Session session = Simantics.getSession();\r
+\r
+               try {\r
+\r
+                       final Layer0 L0 = Layer0.getInstance(session);\r
+\r
+                       final String name = session.syncRequest(new PossibleRelatedValue<String>(target, L0.HasName, Bindings.STRING));\r
+\r
+                       FileDialog dialog = new FileDialog(shell, SWT.SAVE);\r
+                       dialog.setFilterExtensions(new String[] { "*." + extension });\r
+                       dialog.setFileName(name + "." + extension);\r
+                       String newFileName = dialog.open();\r
+                       if(newFileName == null) return;\r
+\r
+                       if(!newFileName.endsWith(extension))\r
+                               newFileName += "." + extension;\r
+\r
+                       try (TransferableGraphSource s = session.sync(new ModelTransferableGraphSourceRequest(createConf(session, target)))) {\r
+                           TransferableGraphs.writeTransferableGraph(session, format, 1, s, new File(newFileName));\r
+                       }\r
+               \r
+                       } catch (Exception e) {\r
+                               Logger.defaultLogError(e);\r
+                       }\r
+\r
+            }\r
+        };\r
+    }\r
+\r
+    private TransferableGraphConfiguration2 createConf(Session session, final Resource model) throws DatabaseException {\r
+        return session.sync(new UniqueRead<TransferableGraphConfiguration2>() {\r
+            @Override\r
+            public TransferableGraphConfiguration2 perform(ReadGraph graph) throws DatabaseException {\r
+                return new TransferableGraphConfiguration2(graph, model, true, false);\r
+            }\r
+        });\r
+    }\r
+\r
+}\r