]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.export.core/src/org/simantics/export/core/ExportContext.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.export.core / src / org / simantics / export / core / ExportContext.java
diff --git a/bundles/org.simantics.export.core/src/org/simantics/export/core/ExportContext.java b/bundles/org.simantics.export.core/src/org/simantics/export/core/ExportContext.java
new file mode 100644 (file)
index 0000000..abe5d82
--- /dev/null
@@ -0,0 +1,113 @@
+package org.simantics.export.core;\r
+\r
+import java.io.File;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
+import org.eclipse.core.runtime.Platform;\r
+import org.eclipse.core.runtime.preferences.ConfigurationScope;\r
+import org.eclipse.jface.viewers.IStructuredSelection;\r
+import org.osgi.service.prefs.Preferences;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.Databoard;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.Session;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.exception.AssumptionException;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.ServiceException;\r
+import org.simantics.db.exception.ValidationException;\r
+import org.simantics.db.layer0.request.ActiveModels;\r
+import org.simantics.db.management.ISessionContext;\r
+import org.simantics.export.core.manager.Content;\r
+import org.simantics.project.IProject;\r
+import org.simantics.project.ProjectKeys;\r
+import org.simantics.utils.ui.AdaptionUtils;\r
+\r
+public class ExportContext {\r
+\r
+       // Context data\r
+       public ISessionContext sessionCtx;\r
+       public Session session;\r
+       public String project;\r
+       public List<String> activeModels;\r
+       \r
+       // Other data\r
+       /** Selected content */\r
+       public List<String> selection;\r
+       \r
+       public ExportExtensionPoint eep;\r
+       public File workarea;\r
+       public Preferences store;\r
+       public Databoard databoard;\r
+       \r
+       public static ExportContext create(ISessionContext ctx, final IStructuredSelection selection) throws DatabaseException {\r
+               final ExportContext result = new ExportContext();\r
+               result.sessionCtx = ctx;\r
+               result.session = ctx.getSession();\r
+               result.selection = new ArrayList<String>();\r
+               result.activeModels = new ArrayList<String>();\r
+        result.workarea = Platform.getLocation().toFile();\r
+        result.store = ConfigurationScope.INSTANCE.getNode("org.simantics.export.core");\r
+        result.databoard = new Databoard();\r
+               \r
+        result.session.syncRequest(new ReadRequest() {\r
+            public void run(ReadGraph graph) throws DatabaseException {\r
+\r
+                       IProject iproject = result.sessionCtx.getHint( ProjectKeys.KEY_PROJECT );\r
+               Resource project = iproject==null?null:iproject.get();\r
+               if ( project != null ) {\r
+                       result.project = graph.getURI(project);\r
+               }\r
+               \r
+               for (Resource activeModel : graph.sync( new ActiveModels( project ) )) {\r
+                       try { \r
+                                       result.activeModels.add( graph.getURI( activeModel ) );\r
+                       } catch (AssumptionException e) {\r
+                       } catch (ValidationException e) {\r
+                       } catch (ServiceException e) {\r
+                       }\r
+               }\r
+\r
+               if ( selection != null ) {\r
+                       for (Resource r : AdaptionUtils.adaptToCollection(selection, Resource.class)) {\r
+                               try {                                   \r
+                                       result.selection.add( graph.getURI(r) );\r
+                               } catch (AssumptionException e) {\r
+                               } catch (ValidationException e) {\r
+                               } catch (ServiceException e) {\r
+                               }\r
+                       }\r
+               }\r
+               \r
+            }\r
+        });\r
+               \r
+               //IExperiment experiment = ExperimentManager.getActiveExperiment();             \r
+//        ISelection selection = HandlerUtil.getCurrentSelection(event);\r
+//        ImagesNode images = AdaptionUtils.adaptToSingle(selection, ImagesNode.class);\r
+               return result;\r
+       }\r
+\r
+       /**\r
+        * Get preference store for a combination of selections\r
+        * @param contents\r
+        * @return selections\r
+        */\r
+       public Preferences store(List<Content> contents)\r
+       {\r
+               int hash = _hash(contents);             \r
+               return store.node( "Selection-"+hash );         \r
+       }\r
+\r
+       public static int _hash(List<Content> contents) {\r
+               int hash = 0x234234;\r
+               for ( Content c : contents ) {\r
+                       hash = 13*hash + c.url.hashCode();\r
+                       hash = 13*hash + c.formatId.hashCode();\r
+               }\r
+               return hash;\r
+       }\r
+       \r
+}\r