]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.document.linking.ui/src/org/simantics/document/linking/report/evaluator/PredefinedVariables.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.document.linking.ui / src / org / simantics / document / linking / report / evaluator / PredefinedVariables.java
1 package org.simantics.document.linking.report.evaluator;\r
2 \r
3 import java.util.Collection;\r
4 \r
5 import org.simantics.annotation.ontology.AnnotationResource;\r
6 import org.simantics.db.ReadGraph;\r
7 import org.simantics.db.Resource;\r
8 import org.simantics.db.exception.DatabaseException;\r
9 import org.simantics.db.layer0.variable.Variable;\r
10 import org.simantics.db.layer0.variable.Variables;\r
11 import org.simantics.layer0.Layer0;\r
12 import org.simantics.ui.SimanticsUI;\r
13 \r
14 /**\r
15  * Copy-paste from org.simantics.diagram.function.PredefinedVariables (removed diagram specific stuff)\r
16  * \r
17  * \r
18  * @author Marko Luukkainen <marko.luukkainen@vtt.fi>\r
19  *\r
20  */\r
21 public class PredefinedVariables {\r
22         public static final String root = "root";\r
23         public static final String project = "project";\r
24         public static final String model = "model";\r
25         public static final String template = "template";\r
26         public static final String current = ".";\r
27 \r
28 \r
29         private static PredefinedVariables factory = new PredefinedVariables();\r
30         \r
31         public static PredefinedVariables getInstance(){\r
32                 return factory;\r
33         }\r
34         \r
35         public static void setFactory(PredefinedVariables factory){\r
36                 PredefinedVariables.factory = factory;\r
37         }\r
38         \r
39     Resource connectedComponent = null;\r
40     \r
41 \r
42 \r
43 \r
44 \r
45         public static Resource getAnnotation(ReadGraph graph, Resource res) throws DatabaseException {\r
46                 Layer0 L0 = Layer0.getInstance(graph);\r
47         Collection<Resource> children = graph.getObjects(res, L0.ConsistsOf);\r
48         Resource anno = null;\r
49         AnnotationResource ANNO = AnnotationResource.getInstance(graph);\r
50         for (Resource child:children){\r
51                 if (graph.isInstanceOf(child, ANNO.Annotation)){\r
52                         anno = child;\r
53                         break;\r
54                 }\r
55         }\r
56         return anno;\r
57         } \r
58 \r
59 \r
60 \r
61         public Resource getPredefinedResource(ReadGraph graph, Variable selection, String id) throws DatabaseException {\r
62                 Resource predefined = null;\r
63         if (id.equals(root))\r
64                 predefined = graph.getRootLibrary();\r
65         else if (id.equals(project))\r
66                 predefined = SimanticsUI.getProject().get();\r
67         else if (id.equals(model))\r
68                 predefined = Variables.getModel(graph, selection);\r
69         else if (id.equals(template)){\r
70 //                      Resource composite = selection.getRepresents(graph);\r
71 //                      if (composite == null)\r
72 //                              return null;\r
73 //                      ModelingResources MOD = ModelingResources.getInstance(graph);\r
74 //            Template2dResource TEPLATE2D = Template2dResource.getInstance(graph);\r
75 //                      Resource diagram = graph.getPossibleObject(composite, MOD.CompositeToDiagram);\r
76 //                      if (diagram == null)\r
77 //                              return null;\r
78 //                      predefined = graph.getPossibleObject(diagram, TEPLATE2D.HasDrawingTemplate);\r
79         }\r
80         else if (id.equals(current))\r
81                 predefined =  selection.getRepresents(graph);\r
82         return predefined;\r
83         }\r
84         \r
85         public Variable getPredefinedVariable(ReadGraph graph, Variable selection, String id) throws DatabaseException {\r
86                 Resource predefined = this.getPredefinedResource(graph, selection, id);\r
87                 if (predefined == null)\r
88                         return null;\r
89                 \r
90         Variable v = selection;\r
91         //Variable test = Variables.getPossibleVariable(graph, "http://Projects/Development%20Project/Model/Configuration/NewAutomationDiagram2/__CONTAINER__/__DIAGRAM__/4");\r
92         if (predefined != null)\r
93             v = graph.adapt(predefined, Variable.class);\r
94         return v;\r
95         }\r
96         \r
97         public Variable getVariable(ReadGraph graph, String path, Resource converter, Variable selection) throws DatabaseException {\r
98         if (path == null)\r
99                 return null;\r
100         int colonInx = path.indexOf(':');\r
101         int firstSlash = path.indexOf('/');\r
102         int firstHash = path.indexOf('#');\r
103         int firstFlashInx = firstSlash;\r
104         if(firstFlashInx == -1) firstFlashInx = firstHash;\r
105         else {\r
106                 if(firstHash > -1 && firstHash < firstFlashInx) firstFlashInx = firstHash;\r
107         }\r
108 \r
109         String predefined = null;\r
110         String relativePath = null;\r
111 \r
112         // if scheme is followed by absolute uri\r
113         // for examples:\r
114         // pre:/model/Library/logo.svg#data -- get under model\r
115         // pre:/./Library/logo.svg#data-- get under composite which corresponds the diagram\r
116         // pre:/project/Library/FORTUM_LOGO_CMYK.svg#data -- get under project\r
117         // pre:/root/Library/FORTUM_LOGO_CMYK.svg#data -- get under root\r
118         \r
119         if (colonInx != -1 && firstFlashInx != -1 && colonInx+1 == firstFlashInx && path.length() > firstFlashInx+1){\r
120             String scheme = path.substring(0, colonInx);\r
121             String absPath = path.substring(firstFlashInx+1);\r
122             if (scheme.equals("pre")){\r
123                 int endOfPredefined1 = absPath.indexOf('/');\r
124                 int endOfPredefined2 = absPath.indexOf('#');\r
125                 if (endOfPredefined1 == -1 && endOfPredefined2 == -1)\r
126                     predefined = absPath;\r
127                 if (endOfPredefined1 == -1 && endOfPredefined2 != -1)\r
128                     predefined = absPath.substring(0, endOfPredefined2);\r
129                 if (endOfPredefined1 != -1 && endOfPredefined2 == -1)\r
130                     predefined = absPath.substring(0, endOfPredefined1);\r
131                 if (endOfPredefined1 != -1 && endOfPredefined2 != -1){\r
132                     if (endOfPredefined2 < endOfPredefined1)\r
133                         endOfPredefined1 = endOfPredefined2;\r
134                     predefined = absPath.substring(0, endOfPredefined1);\r
135                 }\r
136                 relativePath = absPath.substring(predefined.length());\r
137             }\r
138         }\r
139 \r
140         //Variable activeVariable = ScenegraphLoaderUtils.getPossibleVariableSelection(graph, context);\r
141         if (selection == null)\r
142                 return null;\r
143         \r
144         Variable v = selection;\r
145         if (predefined != null)\r
146                 v = getPredefinedVariable(graph, selection, predefined);\r
147 \r
148         if (v == null)\r
149             return null;\r
150 \r
151         Variable property = null;\r
152         if (relativePath != null){\r
153                 if (relativePath.startsWith("/."))\r
154                         relativePath = relativePath.substring(1);\r
155             property = v.browsePossible(graph, relativePath);\r
156         }\r
157         else\r
158             property = v.browsePossible(graph, path);\r
159         return property;\r
160     }\r
161 \r
162 }\r