]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/labeldecorators/LabelDecorationContribution.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / labeldecorators / LabelDecorationContribution.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.labeldecorators;
13
14 import org.simantics.browsing.ui.BuiltinKeys;
15 import org.simantics.browsing.ui.NodeContext;
16 import org.simantics.browsing.ui.content.LabelDecorator;
17 import org.simantics.browsing.ui.model.InvalidContribution;
18 import org.simantics.browsing.ui.model.nodetypes.NodeType;
19 import org.simantics.browsing.ui.model.tests.Test;
20 import org.simantics.browsing.ui.model.visuals.VisualsContribution;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.exception.DatabaseException;
23
24 /**
25  * Produces label decorators for nodes of given node type.
26  * @author Hannu Niemistö
27  */
28 public class LabelDecorationContribution extends VisualsContribution {
29     LabelDecorationRule labelDecorationRule;
30     
31     public LabelDecorationContribution(NodeType nodeType, Test test, LabelDecorationRule labelDecorationRule, double priority) throws InvalidContribution {
32         super(nodeType, test, priority);
33         if(!labelDecorationRule.isCompatible(
34                 nodeType.getContentType()
35                 ))
36             throw new InvalidContribution("Label decoration rule is not compatible with the content type.");
37         this.labelDecorationRule = labelDecorationRule;
38     }
39        
40     /**
41      * Returns a label decorator for the node or null, if contribution is
42      * not suitable for the input.
43      */
44
45     public LabelDecorator getLabelDecorator(ReadGraph graph, NodeContext context) {
46         Object content = context.getConstant(BuiltinKeys.INPUT);
47         try {
48             if(test == null || test.test(graph, content))
49                 return labelDecorationRule.getLabelDecorator(graph, content);
50             else
51                 return null;
52         } catch(DatabaseException e) {
53             // TODO reconsider
54             return null;
55         }
56     }    
57 }