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