]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/modifiers/ModifierContribution.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / modifiers / ModifierContribution.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.modifiers;
13
14 import org.simantics.browsing.ui.BuiltinKeys;
15 import org.simantics.browsing.ui.NodeContext;
16 import org.simantics.browsing.ui.content.Labeler.Modifier;
17 import org.simantics.browsing.ui.content.Labeler.DeniedModifier;
18 import org.simantics.browsing.ui.model.InvalidContribution;
19 import org.simantics.browsing.ui.model.nodetypes.NodeType;
20 import org.simantics.browsing.ui.model.tests.Test;
21 import org.simantics.browsing.ui.model.visuals.VisualsContribution;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.exception.DatabaseException;
24
25 public class ModifierContribution extends VisualsContribution {
26     ModifierRule modifierRule;
27     
28     public ModifierContribution(NodeType nodeType, Test test, ModifierRule modifierRule, double priority) throws InvalidContribution {
29         super(nodeType, test, priority);
30         if(!modifierRule.isCompatible(
31                 nodeType.getContentType()
32                 ))
33             throw new InvalidContribution("Label rule is not compatible with the content type.");
34         this.modifierRule = modifierRule;
35     }
36        
37     public Modifier getModifier(ReadGraph graph, NodeContext context, String columnKey) {
38         Object content = context.getConstant(BuiltinKeys.INPUT);
39         try {
40             if(test == null || test.test(graph, content))            
41                 return modifierRule.getModifier(graph, content, columnKey);
42             else
43                 return null;
44         } catch(DatabaseException e) {
45                 return new DeniedModifier(e.getMessage());
46         }
47     }    
48 }