]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/function/LinkSVGImage.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling.template2d.ui / src / org / simantics / modeling / template2d / ui / function / LinkSVGImage.java
diff --git a/bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/function/LinkSVGImage.java b/bundles/org.simantics.modeling.template2d.ui/src/org/simantics/modeling/template2d/ui/function/LinkSVGImage.java
new file mode 100644 (file)
index 0000000..912c74f
--- /dev/null
@@ -0,0 +1,189 @@
+package org.simantics.modeling.template2d.ui.function;\r
+\r
+import java.util.Vector;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.request.ReadRequest;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.db.layer0.variable.Variables;\r
+import org.simantics.diagram.function.PredefinedVariables;\r
+import org.simantics.diagram.stubs.DiagramResource;\r
+import org.simantics.image2.ontology.ImageResource;\r
+import org.simantics.modeling.template2d.ontology.Template2dResource;\r
+import org.simantics.project.ontology.ProjectResource;\r
+import org.simantics.simulation.ontology.SimulationResource;\r
+import org.simantics.utils.ui.dialogs.ShowMessage;\r
+\r
+public class LinkSVGImage {\r
+       \r
+    public static void linkSVGImage(WriteGraph graph, final Resource parent, final Resource child) {\r
+       \r
+       final String[] uri = {null};\r
+       final String[] msg = {null};\r
+       final String errmsg = "Creation of image reference failed. Activate an experiment and try to copy and paste an image again.";\r
+       \r
+       try {\r
+                       graph.syncRequest(new ReadRequest() {\r
+                               @Override\r
+                               public void run(ReadGraph g) throws DatabaseException {\r
+                                       if (parent == null || child == null) {\r
+                                               msg[0] = errmsg; \r
+                                               return;\r
+                                       }\r
+                                       //Layer0 L0 = Layer0.getInstance(g);\r
+                                       ImageResource IMG = ImageResource.getInstance(g);\r
+                                       if (!g.isInstanceOf(child, IMG.SvgImage)){\r
+                                               msg[0] = errmsg; \r
+                                               return;\r
+                                       }\r
+                                       \r
+                                       Variable parentV = Variables.getVariable(g, parent);\r
+                                       if (parentV == null){\r
+                                               msg[0] = errmsg; \r
+                                               return;\r
+                                       }\r
+                                       \r
+                                       Vector<Variable> grandOfParent = new Vector<Variable>();\r
+                                       Variable tmp = parentV;\r
+                                       do {\r
+                                               grandOfParent.add(0, tmp);\r
+                                               tmp = tmp.getParent(g);\r
+                                       }\r
+                                       while (tmp != null);\r
+                                       \r
+//                                     System.err.println(parentV.getURI(g));\r
+//                                     String parentUri = parentV.getURI(g);\r
+                                       \r
+                                       Variable childV = Variables.getVariable(g, child);\r
+                                       String childUri = null;\r
+                                       if (childV == null){\r
+                                               msg[0] = errmsg; \r
+                                               return;\r
+                                       }\r
+                                       \r
+                                       Vector<Variable> grandOfChild = new Vector<Variable>();\r
+                                       tmp = childV;\r
+                                       do {\r
+                                               grandOfChild.add(0, tmp);\r
+                                               tmp = tmp.getParent(g);\r
+                                       }\r
+                                       while (tmp != null);\r
+                                       \r
+                                       int k = -1; // index of the most nearest common parent\r
+                                       int maxLen = (grandOfChild.size() < grandOfParent.size())? grandOfChild.size() : grandOfParent.size(); \r
+                                       for (int i = 0;i < maxLen; i++){\r
+                                               if (grandOfChild.get(i).equals(grandOfParent.get(i)))\r
+                                                       k++;\r
+                                               else\r
+                                                       break;\r
+                                       }\r
+                                       if (k == -1) // no common parent\r
+                                       {\r
+                                               msg[0] = errmsg; \r
+                                               return;\r
+                                       }\r
+                                       //StructuralResource2 STR = StructuralResource2.getInstance(g);\r
+                                       SimulationResource SIMU = SimulationResource.getInstance(g);\r
+                                       ProjectResource PROJ = ProjectResource.getInstance(g);\r
+                    Template2dResource TEPLATE2D = Template2dResource.getInstance(g);\r
+                                       Resource root = g.getRootLibrary();;\r
+                                       Resource type = null;\r
+                                       String predefined = null;\r
+                                       Resource res = null;\r
+                                       for (;k >= 0;k--){\r
+                                               tmp = grandOfChild.get(k);\r
+                                               res = tmp.getRepresents(g);\r
+                                               if (res.equals(root)){\r
+                                                       predefined = PredefinedVariables.root;\r
+                                                       break;\r
+                                               }\r
+                                               type = g.getPossibleType(res, SIMU.Model);\r
+                                               if (type != null){\r
+                                                       predefined = PredefinedVariables.model;\r
+                                                       break;\r
+                                               }\r
+                                               type = g.getPossibleType(res, PROJ.Project);\r
+                                               if (type != null){\r
+                                                       predefined = PredefinedVariables.project;\r
+                                                       break;\r
+                                               }\r
+                                               type = g.getPossibleType(res, TEPLATE2D.DrawingTemplate);\r
+                                               if (type != null){\r
+                                                       predefined = PredefinedVariables.template;\r
+                                                       break;\r
+                                               }\r
+                                       }\r
+                                       if (predefined == null) // no predefined common parent\r
+                                       {\r
+                                               msg[0] = errmsg; \r
+                                               return;\r
+                                       }\r
+                                       Variable resV = Variables.getVariable(g, res);\r
+                                       String resUri = resV.getURI(g);\r
+                                       \r
+                                       childUri = childV.getURI(g);\r
+//                                     System.err.println(childUri);\r
+\r
+                                       Resource childModel = Variables.getModel(g, childV);\r
+                                       Resource parentModel = Variables.getModel(g, parentV);\r
+                                       if (childModel == null || parentModel == null){\r
+                                               msg[0] = errmsg; \r
+                                               return;\r
+                                       }\r
+                                       \r
+                                       if (!parentModel.equals(childModel)){\r
+                                               msg[0] = errmsg;\r
+                                               return;\r
+                                       }\r
+\r
+//                                     Variable modelV = Variables.getVariable(g, childModel);\r
+//                                     if (modelV == null){\r
+//                                             msg[0] = errmsg[0];\r
+//                                             return;\r
+//                                     }\r
+//                                     \r
+//                                     String modelUri = modelV.getURI(g);\r
+\r
+                                       if (childUri.startsWith(resUri)){\r
+                                               uri[0] = childUri.substring(resUri.length());\r
+                                               uri[0] = "pre:/" + predefined + uri[0];\r
+//                                             System.err.println(uri);\r
+                                       }\r
+                               }\r
+                       });\r
+                       graph.syncRequest(new WriteRequest() {\r
+                               @Override\r
+                               public void perform(WriteGraph g) throws DatabaseException {\r
+                                       if (uri == null || parent == null){\r
+                                               msg[0] = errmsg;\r
+                                               return;\r
+                                       }\r
+                                       DiagramResource DIA = DiagramResource.getInstance(g);\r
+                    Template2dResource TEMPLATE2D = Template2dResource.getInstance(g);\r
+\r
+                                       Resource var = g.getPossibleObject(parent, DIA.Scenegraph_SVGImage_document);\r
+                                       if (var == null){\r
+                                               msg[0] = "Creation of image reference failed. Only SVG images are supported for now.";\r
+                                               return;\r
+                                       }\r
+                                       \r
+                                       g.claimLiteral(var, TEMPLATE2D.Profiles_VariableReference_path, uri[0] + "#data", Bindings.STRING);\r
+                               }\r
+                       });\r
+                       if (msg[0] != null){\r
+                               ShowMessage.showWarning("Warning", msg[0]);\r
+                       }\r
+               } catch (Exception e) {\r
+                       // TODO: handle exception\r
+                       e.printStackTrace();\r
+               }\r
+       \r
+       }\r
+       \r
+\r
+}
\ No newline at end of file