]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/visuals/VisualsContribution.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / visuals / VisualsContribution.java
1 /*******************************************************************************
2  * Copyright (c) 2010, 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.model.visuals;
13
14 import org.simantics.browsing.ui.model.InvalidContribution;
15 import org.simantics.browsing.ui.model.check.CheckedStateContribution;
16 import org.simantics.browsing.ui.model.check.CheckedStateRule;
17 import org.simantics.browsing.ui.model.imagedecorators.ImageDecorationContribution;
18 import org.simantics.browsing.ui.model.imagedecorators.ImageDecorationRule;
19 import org.simantics.browsing.ui.model.images.ImageContribution;
20 import org.simantics.browsing.ui.model.images.ImageRule;
21 import org.simantics.browsing.ui.model.labeldecorators.LabelDecorationContribution;
22 import org.simantics.browsing.ui.model.labeldecorators.LabelDecorationRule;
23 import org.simantics.browsing.ui.model.labels.LabelContribution;
24 import org.simantics.browsing.ui.model.labels.LabelRule;
25 import org.simantics.browsing.ui.model.modifiers.ModifierContribution;
26 import org.simantics.browsing.ui.model.modifiers.ModifierRule;
27 import org.simantics.browsing.ui.model.nodetypes.NodeType;
28 import org.simantics.browsing.ui.model.nodetypes.OrderedNodeTypeMultiMap;
29 import org.simantics.browsing.ui.model.sorters.SorterContribution;
30 import org.simantics.browsing.ui.model.sorters.SorterRule;
31 import org.simantics.browsing.ui.model.tests.Test;
32 import org.simantics.browsing.ui.model.tooltips.TooltipContribution;
33 import org.simantics.browsing.ui.model.tooltips.TooltipRule;
34 import org.simantics.db.ReadGraph;
35 import org.simantics.db.Resource;
36 import org.simantics.db.exception.AdaptionException;
37 import org.simantics.db.exception.DatabaseException;
38 import org.simantics.viewpoint.ontology.ViewpointResource;
39
40 /**
41  * Produces labels for nodes of given node type.
42  * @author Hannu Niemistö
43  */
44 public class VisualsContribution implements Comparable<VisualsContribution> {
45     protected NodeType nodeType;
46     protected Test test;
47     protected double priority;
48     
49     public VisualsContribution(NodeType nodeType, Test test, double priority) throws InvalidContribution {
50         if(test != null && !test.isCompatible(
51                 nodeType.getContentType()
52                 ))
53             throw new InvalidContribution("Test is not compatible with the content type.");
54         this.nodeType = nodeType;
55         this.test = test;
56         this.priority = priority;
57     }
58     
59     public static void load(ReadGraph g, Resource visualsContributionResource,
60             OrderedNodeTypeMultiMap<LabelContribution> labelContributions,
61             OrderedNodeTypeMultiMap<ImageContribution> imageContributions,
62             OrderedNodeTypeMultiMap<CheckedStateContribution> checkedStateContributions,
63             OrderedNodeTypeMultiMap<LabelDecorationContribution> labelDecorationContributions,
64             OrderedNodeTypeMultiMap<ImageDecorationContribution> imageDecorationContributions,
65             OrderedNodeTypeMultiMap<ModifierContribution> modifierContributions,
66             OrderedNodeTypeMultiMap<SorterContribution> sorterContributions,
67             OrderedNodeTypeMultiMap<FlatNodeContribution> flatNodeContributions,
68             OrderedNodeTypeMultiMap<TooltipContribution> tooltipContributions)
69     throws DatabaseException, InvalidContribution {
70         ViewpointResource vr = ViewpointResource.getInstance(g);
71         
72         Resource testResource = g.getPossibleObject(visualsContributionResource, vr.VisualsContribution_HasCondition);
73         Test test = testResource == null ? null : g.adapt(testResource, Test.class);
74         
75         Double mpriority = g.getPossibleRelatedValue(visualsContributionResource, vr.VisualsContribution_HasPriority);
76         double priority = mpriority == null ? 0.0 : mpriority.doubleValue();         
77         
78         for(Resource nodeTypeResource : g.getObjects(visualsContributionResource, vr.VisualsContribution_HasNodeType)) {
79             NodeType nodeType = g.adapt(nodeTypeResource, NodeType.class);
80
81             for(Resource ruleResource : g.getObjects(visualsContributionResource, vr.VisualsContribution_HasRule)) {
82                 if(ruleResource.equals(vr.FlatNodeRule)) 
83                     flatNodeContributions.put(nodeType, FlatNodeContribution.INSTANCE);
84                 else {          
85                     VisualsRule rule;
86                     try {
87                         rule = g.adapt(ruleResource, VisualsRule.class);
88                     } catch(AdaptionException e) {
89                         e.printStackTrace();
90                         continue;
91                     }                    
92                     try {
93                         // Note: the rule may be an instance of multiple interfaces
94                         if(rule instanceof LabelRule)
95                             labelContributions.put(nodeType, new LabelContribution(nodeType, test, (LabelRule)rule, priority));
96                         if(rule instanceof ModifierRule)
97                             modifierContributions.put(nodeType, new ModifierContribution(nodeType, test, (ModifierRule)rule, priority));
98                         if(rule instanceof ImageRule)
99                             imageContributions.put(nodeType, new ImageContribution(nodeType, test, (ImageRule)rule, priority));
100                         if(rule instanceof CheckedStateRule)
101                             checkedStateContributions.put(nodeType, new CheckedStateContribution(nodeType, test, (CheckedStateRule)rule, priority));
102                         if(rule instanceof LabelDecorationRule)
103                             labelDecorationContributions.put(nodeType, new LabelDecorationContribution(nodeType, test, (LabelDecorationRule)rule, priority));
104                         if(rule instanceof ImageDecorationRule)
105                             imageDecorationContributions.put(nodeType, new ImageDecorationContribution(nodeType, test, (ImageDecorationRule)rule, priority));
106                         if(rule instanceof SorterRule)
107                             sorterContributions.put(nodeType, new SorterContribution(nodeType, test, (SorterRule)rule, priority));
108                         if(rule instanceof TooltipRule)
109                             tooltipContributions.put(nodeType, new TooltipContribution(nodeType, test, (TooltipRule)rule, priority));
110                     } catch(InvalidContribution e) {
111                         e.printStackTrace();
112                         continue;
113                     }
114                 }
115             }
116         }
117     }   
118     
119     public NodeType getNodeType() {
120         return nodeType;
121     }
122
123     @Override
124     public int compareTo(VisualsContribution o) {
125         return Double.compare(o.priority, priority);
126     }
127 }