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