]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
919b02d1572b54cd16ad0d10bb710cd13de64dc6
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.browser.contributions;\r
13 \r
14 import java.util.Collection;\r
15 import java.util.HashSet;\r
16 \r
17 import org.eclipse.jface.resource.ImageDescriptor;\r
18 import org.eclipse.jface.viewers.IDecoration;\r
19 import org.simantics.browsing.ui.common.node.AbstractNode;\r
20 import org.simantics.browsing.ui.swt.ImagerContributor;\r
21 import org.simantics.db.ReadGraph;\r
22 import org.simantics.db.Resource;\r
23 import org.simantics.db.common.request.ObjectsWithType;\r
24 import org.simantics.db.exception.DatabaseException;\r
25 import org.simantics.db.request.Read;\r
26 import org.simantics.issues.Severity;\r
27 import org.simantics.issues.common.MaxIssueSeverityRecursive;\r
28 import org.simantics.issues.ontology.IssueResource;\r
29 import org.simantics.layer0.Layer0;\r
30 import org.simantics.simulation.ontology.SimulationResource;\r
31 import org.simantics.structural.stubs.StructuralResource2;\r
32 import org.simantics.sysdyn.SysdynResource;\r
33 import org.simantics.sysdyn.ui.Activator;\r
34 import org.simantics.sysdyn.ui.browser.nodes.BookNode;\r
35 import org.simantics.sysdyn.ui.browser.nodes.ConfigurationNode;\r
36 import org.simantics.sysdyn.ui.browser.nodes.ExperimentNode;\r
37 import org.simantics.sysdyn.ui.browser.nodes.ExperimentsFolder;\r
38 import org.simantics.sysdyn.ui.browser.nodes.FunctionLibraryNode;\r
39 import org.simantics.sysdyn.ui.browser.nodes.FunctionNode;\r
40 import org.simantics.sysdyn.ui.browser.nodes.FunctionsFolder;\r
41 import org.simantics.sysdyn.ui.browser.nodes.InputNode;\r
42 import org.simantics.sysdyn.ui.browser.nodes.ModelNode;\r
43 import org.simantics.sysdyn.ui.browser.nodes.ModuleNode;\r
44 import org.simantics.sysdyn.ui.browser.nodes.ModuleTypeNode;\r
45 import org.simantics.sysdyn.ui.browser.nodes.ModulesNode;\r
46 import org.simantics.sysdyn.ui.browser.nodes.SharedFunctionLibraryNode;\r
47 import org.simantics.sysdyn.ui.browser.nodes.SharedFunctionsFolder;\r
48 import org.simantics.sysdyn.ui.browser.nodes.SheetNode;\r
49 import org.simantics.sysdyn.ui.browser.nodes.VariableNode;\r
50 import org.simantics.utils.ui.gfx.DecorationOverlayIcon;\r
51 \r
52 /**\r
53  * Imager for nodes in old sysdyn model browser\r
54  * @author Teemu Lempinen\r
55  *\r
56  */\r
57 public class AbstractNodeImager extends ImagerContributor<AbstractNode<Resource>> {\r
58 \r
59     @Override\r
60     public ImageDescriptor getDescriptor(ReadGraph graph, AbstractNode<Resource> node) throws DatabaseException {\r
61 \r
62         String image = null;\r
63         if (node instanceof SharedFunctionsFolder)\r
64             image = "icons/folder_link.png";\r
65         else if (node instanceof ExperimentsFolder || \r
66                 node instanceof ModulesNode || \r
67                 node instanceof FunctionsFolder ||\r
68                 node instanceof FunctionLibraryNode ||\r
69                 node instanceof SharedFunctionLibraryNode)\r
70             image = "icons/folder.png";\r
71         else if (node instanceof ModuleTypeNode || node instanceof ModuleNode || node instanceof ConfigurationNode)\r
72             image = "icons/bricks.png";\r
73         else if (node instanceof ExperimentNode) {\r
74             SysdynResource sr = SysdynResource.getInstance(graph);\r
75             if(graph.isInstanceOf(node.data, sr.PlaybackExperiment))\r
76                 image = "icons/timeline_marker.png";\r
77             else if(graph.isInstanceOf(node.data, sr.GameExperiment))\r
78                 image = "icons/time_go.png";\r
79             else if(graph.isInstanceOf(node.data, sr.SensitivityAnalysisExperiment))\r
80                 image = "icons/rainbow.png";\r
81             else\r
82                 image = "icons/time.png";\r
83         } else if (node instanceof InputNode)\r
84             image = "icons/brick_link.png";\r
85         else if (node instanceof ModelNode)\r
86             image = "icons/chart_organisation.png";\r
87         else if (node instanceof FunctionNode) {\r
88             Layer0 l0 = Layer0.getInstance(graph);\r
89             SysdynResource sr = SysdynResource.getInstance(graph);\r
90             if (graph.hasStatement(node.data, l0.PartOf, sr.Built$in_Functions)) {\r
91                 // Sysdyn functions\r
92                 image =  "icons/sysdynFunction.png";\r
93             } else if (graph.hasStatement(node.data, l0.PartOf, sr.Built$in_Functions_Modelica_Functions)) {\r
94                 image =  "icons/modelicaFunction.png";\r
95             } else if (graph.hasStatement(node.data, l0.PartOf, sr.Built$in_Functions_Modelica_Array_Functions)) {\r
96                 image =  "icons/modelicaArrayFunction.png";\r
97             } else if (graph.hasStatement(node.data, l0.PartOf, sr.Built$in_Functions_Vensim_Functions)) {\r
98                 image =  "icons/vensimFunction.png";\r
99             } else {\r
100                 Resource r = graph.getPossibleObject(node.data, l0.PartOf);\r
101                 if (r != null) {\r
102                     boolean shared = false;\r
103                     do {\r
104                         shared = graph.isInstanceOf(r, sr.SharedFunctionOntology);\r
105                         r = graph.getPossibleObject(r, l0.PartOf);\r
106                     } while (!shared && (r != null));\r
107                     if (shared) {\r
108                         image =  "icons/functionLink.png";\r
109                     } else {\r
110                         image =  "icons/function.png";\r
111                     }\r
112                 }\r
113             }\r
114         } else if (node instanceof VariableNode)\r
115             // Must be queried after FunctionNode\r
116             image =  "icons/brick.png";\r
117         else if (node instanceof BookNode)\r
118             image =  "icons/table_multiple.png";\r
119         else if (node instanceof SheetNode)\r
120             image =  "icons/table.png";\r
121 \r
122         if (image != null) {\r
123 \r
124             ImageDescriptor id = ImageDescriptor.createFromURL(Activator.getDefault().getBundle().getResource(image));\r
125 \r
126             Severity maxSeverity = getMaxSeverity(graph, node);\r
127             \r
128             if (maxSeverity == null)\r
129                 return id;\r
130             else\r
131                 return getDecoration(id, maxSeverity);\r
132         }\r
133         return null;\r
134     }\r
135     \r
136     /**\r
137      * \r
138      * @param graph\r
139      * @param node\r
140      * @return\r
141      * @throws DatabaseException\r
142      */\r
143     private Severity getMaxSeverity(ReadGraph graph, AbstractNode<Resource> node) throws DatabaseException {\r
144         Layer0 L0 = Layer0.getInstance(graph);\r
145         IssueResource.getInstance(graph);\r
146         SysdynResource SR = SysdynResource.getInstance(graph);\r
147 \r
148         HashSet<Resource> issueTypes = new HashSet<Resource>();\r
149         issueTypes.add(SR.Variable);\r
150         issueTypes.add(SR.Configuration);\r
151         issueTypes.add(SR.Module);\r
152         \r
153         Resource data = node.data;\r
154         if(node instanceof ModuleTypeNode) {\r
155             Resource symbol = node.data;\r
156             Resource moduleType = graph.getPossibleObject(symbol, L0.PartOf);\r
157             data = graph.getPossibleObject(moduleType, StructuralResource2.getInstance(graph).IsDefinedBy);\r
158         } else if(node instanceof ModuleNode) {\r
159             Resource moduleType = graph.getPossibleObject(node.data, L0.InstanceOf);\r
160             data = graph.getPossibleObject(moduleType, StructuralResource2.getInstance(graph).IsDefinedBy);\r
161         } else if(node instanceof ModelNode) {\r
162             data = graph.getPossibleObject(node.data, SimulationResource.getInstance(graph).HasConfiguration);\r
163         }\r
164 \r
165         Severity maxSeverity = getMaxSeverityStructural(graph, data, issueTypes, null);  \r
166         return maxSeverity;\r
167     }\r
168     \r
169     /**\r
170      * Recursive function for obtaining maximum severity of a configuration and its module\r
171      * children\r
172      * \r
173      * @param graph readGraph\r
174      * @param resource Resource from which issues are searched for. If the resource is configuraiton, issues are searched also from its module children.\r
175      * @param issueTypes Issue type resources\r
176      * @param severity Maximum current severity\r
177      * @return\r
178      * @throws DatabaseException\r
179      */\r
180     private Severity getMaxSeverityStructural(ReadGraph graph, Resource resource, HashSet<Resource> issueTypes, Severity severity) throws DatabaseException {\r
181         if(resource == null)\r
182             return severity;\r
183         \r
184         Layer0 L0 = Layer0.getInstance(graph);\r
185         SysdynResource SR = SysdynResource.getInstance(graph);\r
186         \r
187         Resource data = resource;\r
188         Severity maxSeverity = graph.syncRequest(new MaxIssueSeverityRecursive(data, L0.ConsistsOf, issueTypes));\r
189 \r
190         // Try structural\r
191         if(graph.isInstanceOf(data, SR.Configuration)) {\r
192             Collection<Resource> moduleChildren =  graph.syncRequest(new ModuleChildren(data));\r
193             for(Resource r : moduleChildren) {\r
194                 Resource moduleType = graph.getPossibleObject(r, L0.InstanceOf);\r
195                 Resource configuration = graph.getPossibleObject(moduleType, StructuralResource2.getInstance(graph).IsDefinedBy);\r
196                 if(configuration != null)\r
197                     maxSeverity = getMaxSeverityStructural(graph, configuration, issueTypes, maxSeverity);\r
198             }\r
199         }\r
200 \r
201         return Severity.moreSevere(maxSeverity, severity);\r
202     }\r
203 \r
204     /**\r
205      * Request for obtaining all module children of a configuration\r
206      * @author Teemu Lempinen\r
207      *\r
208      */\r
209     private class ModuleChildren implements Read<Collection<Resource>> {\r
210         \r
211         Resource configuration; \r
212         \r
213         public ModuleChildren(Resource configuration) {\r
214             this.configuration = configuration;\r
215         }\r
216 \r
217         @Override\r
218         public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {\r
219             return graph.syncRequest(new ObjectsWithType(configuration, Layer0.getInstance(graph).ConsistsOf, SysdynResource.getInstance(graph).Module));\r
220         }\r
221     }\r
222 \r
223     private ImageDescriptor getDecoration(ImageDescriptor original, Severity severity) {\r
224         ImageDescriptor img = Activator.getDefault().getImageRegistry().getDescriptor(severity.toString());\r
225         if (original == null || original.getImageData() == null) \r
226             return img;\r
227         else\r
228             return new DecorationOverlayIcon(original, img, IDecoration.BOTTOM_LEFT);\r
229     }\r
230 \r
231 }\r