]> gerrit.simantics Code Review - simantics/platform.git/blob
a426efd1f7f3bb51f2aa2cdd926d4d952adbbe55
[simantics/platform.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2010, 2011 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.browsing.ui.model.visuals;\r
13 \r
14 import org.simantics.browsing.ui.model.InvalidContribution;\r
15 import org.simantics.browsing.ui.model.check.CheckedStateContribution;\r
16 import org.simantics.browsing.ui.model.check.CheckedStateRule;\r
17 import org.simantics.browsing.ui.model.imagedecorators.ImageDecorationContribution;\r
18 import org.simantics.browsing.ui.model.imagedecorators.ImageDecorationRule;\r
19 import org.simantics.browsing.ui.model.images.ImageContribution;\r
20 import org.simantics.browsing.ui.model.images.ImageRule;\r
21 import org.simantics.browsing.ui.model.labeldecorators.LabelDecorationContribution;\r
22 import org.simantics.browsing.ui.model.labeldecorators.LabelDecorationRule;\r
23 import org.simantics.browsing.ui.model.labels.LabelContribution;\r
24 import org.simantics.browsing.ui.model.labels.LabelRule;\r
25 import org.simantics.browsing.ui.model.modifiers.ModifierContribution;\r
26 import org.simantics.browsing.ui.model.modifiers.ModifierRule;\r
27 import org.simantics.browsing.ui.model.nodetypes.NodeType;\r
28 import org.simantics.browsing.ui.model.nodetypes.OrderedNodeTypeMultiMap;\r
29 import org.simantics.browsing.ui.model.sorters.SorterContribution;\r
30 import org.simantics.browsing.ui.model.sorters.SorterRule;\r
31 import org.simantics.browsing.ui.model.tests.Test;\r
32 import org.simantics.db.ReadGraph;\r
33 import org.simantics.db.Resource;\r
34 import org.simantics.db.exception.AdaptionException;\r
35 import org.simantics.db.exception.DatabaseException;\r
36 import org.simantics.viewpoint.ontology.ViewpointResource;\r
37 \r
38 /**\r
39  * Produces labels for nodes of given node type.\r
40  * @author Hannu Niemistö\r
41  */\r
42 public class VisualsContribution implements Comparable<VisualsContribution> {\r
43     protected NodeType nodeType;\r
44     protected Test test;\r
45     protected double priority;\r
46     \r
47     public VisualsContribution(NodeType nodeType, Test test, double priority) throws InvalidContribution {\r
48         if(test != null && !test.isCompatible(\r
49                 nodeType.getContentType()\r
50                 ))\r
51             throw new InvalidContribution("Test is not compatible with the content type.");\r
52         this.nodeType = nodeType;\r
53         this.test = test;\r
54         this.priority = priority;\r
55     }\r
56     \r
57     public static void load(ReadGraph g, Resource visualsContributionResource,\r
58             OrderedNodeTypeMultiMap<LabelContribution> labelContributions,\r
59             OrderedNodeTypeMultiMap<ImageContribution> imageContributions,\r
60             OrderedNodeTypeMultiMap<CheckedStateContribution> checkedStateContributions,\r
61             OrderedNodeTypeMultiMap<LabelDecorationContribution> labelDecorationContributions,\r
62             OrderedNodeTypeMultiMap<ImageDecorationContribution> imageDecorationContributions,\r
63             OrderedNodeTypeMultiMap<ModifierContribution> modifierContributions,\r
64             OrderedNodeTypeMultiMap<SorterContribution> sorterContributions,\r
65             OrderedNodeTypeMultiMap<FlatNodeContribution> flatNodeContributions) \r
66     throws DatabaseException, InvalidContribution {\r
67         ViewpointResource vr = ViewpointResource.getInstance(g);\r
68         \r
69         Resource testResource = g.getPossibleObject(visualsContributionResource, vr.VisualsContribution_HasCondition);\r
70         Test test = testResource == null ? null : g.adapt(testResource, Test.class);\r
71         \r
72         Double mpriority = g.getPossibleRelatedValue(visualsContributionResource, vr.VisualsContribution_HasPriority);\r
73         double priority = mpriority == null ? 0.0 : mpriority.doubleValue();         \r
74         \r
75         for(Resource nodeTypeResource : g.getObjects(visualsContributionResource, vr.VisualsContribution_HasNodeType)) {\r
76             NodeType nodeType = g.adapt(nodeTypeResource, NodeType.class);\r
77 \r
78             for(Resource ruleResource : g.getObjects(visualsContributionResource, vr.VisualsContribution_HasRule)) {\r
79                 if(ruleResource.equals(vr.FlatNodeRule)) \r
80                     flatNodeContributions.put(nodeType, FlatNodeContribution.INSTANCE);\r
81                 else {          \r
82                     VisualsRule rule;\r
83                     try {\r
84                         rule = g.adapt(ruleResource, VisualsRule.class);\r
85                     } catch(AdaptionException e) {\r
86                         e.printStackTrace();\r
87                         continue;\r
88                     }                    \r
89                     try {\r
90                         // Note: the rule may be an instance of multiple interfaces\r
91                         if(rule instanceof LabelRule)\r
92                             labelContributions.put(nodeType, new LabelContribution(nodeType, test, (LabelRule)rule, priority));\r
93                         if(rule instanceof ModifierRule)\r
94                             modifierContributions.put(nodeType, new ModifierContribution(nodeType, test, (ModifierRule)rule, priority));\r
95                         if(rule instanceof ImageRule)\r
96                             imageContributions.put(nodeType, new ImageContribution(nodeType, test, (ImageRule)rule, priority));\r
97                         if(rule instanceof CheckedStateRule)\r
98                             checkedStateContributions.put(nodeType, new CheckedStateContribution(nodeType, test, (CheckedStateRule)rule, priority));\r
99                         if(rule instanceof LabelDecorationRule)\r
100                             labelDecorationContributions.put(nodeType, new LabelDecorationContribution(nodeType, test, (LabelDecorationRule)rule, priority));\r
101                         if(rule instanceof ImageDecorationRule)\r
102                             imageDecorationContributions.put(nodeType, new ImageDecorationContribution(nodeType, test, (ImageDecorationRule)rule, priority));\r
103                         if(rule instanceof SorterRule)\r
104                             sorterContributions.put(nodeType, new SorterContribution(nodeType, test, (SorterRule)rule, priority));\r
105                     } catch(InvalidContribution e) {\r
106                         e.printStackTrace();\r
107                         continue;\r
108                     }\r
109                 }\r
110             }\r
111         }\r
112     }   \r
113     \r
114     public NodeType getNodeType() {\r
115         return nodeType;\r
116     }\r
117 \r
118     @Override\r
119     public int compareTo(VisualsContribution o) {\r
120         return Double.compare(o.priority, priority);\r
121     }\r
122 }\r