]> gerrit.simantics Code Review - simantics/platform.git/blob - 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
1 package org.simantics.modeling.template2d.ui.function;\r
2 \r
3 import java.util.Vector;\r
4 \r
5 import org.simantics.databoard.Bindings;\r
6 import org.simantics.db.ReadGraph;\r
7 import org.simantics.db.Resource;\r
8 import org.simantics.db.WriteGraph;\r
9 import org.simantics.db.common.request.ReadRequest;\r
10 import org.simantics.db.common.request.WriteRequest;\r
11 import org.simantics.db.exception.DatabaseException;\r
12 import org.simantics.db.layer0.variable.Variable;\r
13 import org.simantics.db.layer0.variable.Variables;\r
14 import org.simantics.diagram.function.PredefinedVariables;\r
15 import org.simantics.diagram.stubs.DiagramResource;\r
16 import org.simantics.image2.ontology.ImageResource;\r
17 import org.simantics.modeling.template2d.ontology.Template2dResource;\r
18 import org.simantics.project.ontology.ProjectResource;\r
19 import org.simantics.simulation.ontology.SimulationResource;\r
20 import org.simantics.utils.ui.dialogs.ShowMessage;\r
21 \r
22 public class LinkSVGImage {\r
23         \r
24     public static void linkSVGImage(WriteGraph graph, final Resource parent, final Resource child) {\r
25         \r
26         final String[] uri = {null};\r
27         final String[] msg = {null};\r
28         final String errmsg = "Creation of image reference failed. Activate an experiment and try to copy and paste an image again.";\r
29         \r
30         try {\r
31                         graph.syncRequest(new ReadRequest() {\r
32                                 @Override\r
33                                 public void run(ReadGraph g) throws DatabaseException {\r
34                                         if (parent == null || child == null) {\r
35                                                 msg[0] = errmsg; \r
36                                                 return;\r
37                                         }\r
38                                         //Layer0 L0 = Layer0.getInstance(g);\r
39                                         ImageResource IMG = ImageResource.getInstance(g);\r
40                                         if (!g.isInstanceOf(child, IMG.SvgImage)){\r
41                                                 msg[0] = errmsg; \r
42                                                 return;\r
43                                         }\r
44                                         \r
45                                         Variable parentV = Variables.getVariable(g, parent);\r
46                                         if (parentV == null){\r
47                                                 msg[0] = errmsg; \r
48                                                 return;\r
49                                         }\r
50                                         \r
51                                         Vector<Variable> grandOfParent = new Vector<Variable>();\r
52                                         Variable tmp = parentV;\r
53                                         do {\r
54                                                 grandOfParent.add(0, tmp);\r
55                                                 tmp = tmp.getParent(g);\r
56                                         }\r
57                                         while (tmp != null);\r
58                                         \r
59 //                                      System.err.println(parentV.getURI(g));\r
60 //                                      String parentUri = parentV.getURI(g);\r
61                                         \r
62                                         Variable childV = Variables.getVariable(g, child);\r
63                                         String childUri = null;\r
64                                         if (childV == null){\r
65                                                 msg[0] = errmsg; \r
66                                                 return;\r
67                                         }\r
68                                         \r
69                                         Vector<Variable> grandOfChild = new Vector<Variable>();\r
70                                         tmp = childV;\r
71                                         do {\r
72                                                 grandOfChild.add(0, tmp);\r
73                                                 tmp = tmp.getParent(g);\r
74                                         }\r
75                                         while (tmp != null);\r
76                                         \r
77                                         int k = -1; // index of the most nearest common parent\r
78                                         int maxLen = (grandOfChild.size() < grandOfParent.size())? grandOfChild.size() : grandOfParent.size(); \r
79                                         for (int i = 0;i < maxLen; i++){\r
80                                                 if (grandOfChild.get(i).equals(grandOfParent.get(i)))\r
81                                                         k++;\r
82                                                 else\r
83                                                         break;\r
84                                         }\r
85                                         if (k == -1) // no common parent\r
86                                         {\r
87                                                 msg[0] = errmsg; \r
88                                                 return;\r
89                                         }\r
90                                         //StructuralResource2 STR = StructuralResource2.getInstance(g);\r
91                                         SimulationResource SIMU = SimulationResource.getInstance(g);\r
92                                         ProjectResource PROJ = ProjectResource.getInstance(g);\r
93                     Template2dResource TEPLATE2D = Template2dResource.getInstance(g);\r
94                                         Resource root = g.getRootLibrary();;\r
95                                         Resource type = null;\r
96                                         String predefined = null;\r
97                                         Resource res = null;\r
98                                         for (;k >= 0;k--){\r
99                                                 tmp = grandOfChild.get(k);\r
100                                                 res = tmp.getRepresents(g);\r
101                                                 if (res.equals(root)){\r
102                                                         predefined = PredefinedVariables.root;\r
103                                                         break;\r
104                                                 }\r
105                                                 type = g.getPossibleType(res, SIMU.Model);\r
106                                                 if (type != null){\r
107                                                         predefined = PredefinedVariables.model;\r
108                                                         break;\r
109                                                 }\r
110                                                 type = g.getPossibleType(res, PROJ.Project);\r
111                                                 if (type != null){\r
112                                                         predefined = PredefinedVariables.project;\r
113                                                         break;\r
114                                                 }\r
115                                                 type = g.getPossibleType(res, TEPLATE2D.DrawingTemplate);\r
116                                                 if (type != null){\r
117                                                         predefined = PredefinedVariables.template;\r
118                                                         break;\r
119                                                 }\r
120                                         }\r
121                                         if (predefined == null) // no predefined common parent\r
122                                         {\r
123                                                 msg[0] = errmsg; \r
124                                                 return;\r
125                                         }\r
126                                         Variable resV = Variables.getVariable(g, res);\r
127                                         String resUri = resV.getURI(g);\r
128                                         \r
129                                         childUri = childV.getURI(g);\r
130 //                                      System.err.println(childUri);\r
131 \r
132                                         Resource childModel = Variables.getModel(g, childV);\r
133                                         Resource parentModel = Variables.getModel(g, parentV);\r
134                                         if (childModel == null || parentModel == null){\r
135                                                 msg[0] = errmsg; \r
136                                                 return;\r
137                                         }\r
138                                         \r
139                                         if (!parentModel.equals(childModel)){\r
140                                                 msg[0] = errmsg;\r
141                                                 return;\r
142                                         }\r
143 \r
144 //                                      Variable modelV = Variables.getVariable(g, childModel);\r
145 //                                      if (modelV == null){\r
146 //                                              msg[0] = errmsg[0];\r
147 //                                              return;\r
148 //                                      }\r
149 //                                      \r
150 //                                      String modelUri = modelV.getURI(g);\r
151 \r
152                                         if (childUri.startsWith(resUri)){\r
153                                                 uri[0] = childUri.substring(resUri.length());\r
154                                                 uri[0] = "pre:/" + predefined + uri[0];\r
155 //                                              System.err.println(uri);\r
156                                         }\r
157                                 }\r
158                         });\r
159                         graph.syncRequest(new WriteRequest() {\r
160                                 @Override\r
161                                 public void perform(WriteGraph g) throws DatabaseException {\r
162                                         if (uri == null || parent == null){\r
163                                                 msg[0] = errmsg;\r
164                                                 return;\r
165                                         }\r
166                                         DiagramResource DIA = DiagramResource.getInstance(g);\r
167                     Template2dResource TEMPLATE2D = Template2dResource.getInstance(g);\r
168 \r
169                                         Resource var = g.getPossibleObject(parent, DIA.Scenegraph_SVGImage_document);\r
170                                         if (var == null){\r
171                                                 msg[0] = "Creation of image reference failed. Only SVG images are supported for now.";\r
172                                                 return;\r
173                                         }\r
174                                         \r
175                                         g.claimLiteral(var, TEMPLATE2D.Profiles_VariableReference_path, uri[0] + "#data", Bindings.STRING);\r
176                                 }\r
177                         });\r
178                         if (msg[0] != null){\r
179                                 ShowMessage.showWarning("Warning", msg[0]);\r
180                         }\r
181                 } catch (Exception e) {\r
182                         // TODO: handle exception\r
183                         e.printStackTrace();\r
184                 }\r
185         \r
186         }\r
187         \r
188 \r
189 }