]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/sorters/SorterContribution.java
7dc674df0344ef10001566df0808850c4a50e8de
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / sorters / SorterContribution.java
1 /*******************************************************************************\r
2  * Copyright (c) 2010, 2011 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.model.sorters;\r
13 \r
14 import org.simantics.browsing.ui.BuiltinKeys;\r
15 import org.simantics.browsing.ui.NodeContext;\r
16 import org.simantics.browsing.ui.model.InvalidContribution;\r
17 import org.simantics.browsing.ui.model.nodetypes.NodeType;\r
18 import org.simantics.browsing.ui.model.tests.Test;\r
19 import org.simantics.browsing.ui.model.visuals.VisualsContribution;\r
20 import org.simantics.db.ReadGraph;\r
21 import org.simantics.db.exception.DatabaseException;\r
22 \r
23 public class SorterContribution extends VisualsContribution {\r
24     SorterRule modifierRule;\r
25     \r
26     public SorterContribution(NodeType nodeType, Test test, SorterRule modifierRule, double priority) throws InvalidContribution {\r
27         super(nodeType, test, priority);\r
28         if(!modifierRule.isCompatible(\r
29                 nodeType.getContentType()\r
30                 ))\r
31             throw new InvalidContribution("Sorter rule is not compatible with the content type.");\r
32         this.modifierRule = modifierRule;\r
33     }\r
34        \r
35     public Sorter getSorter(ReadGraph graph, NodeContext context) {\r
36         Object content = context.getConstant(BuiltinKeys.INPUT);\r
37         try {\r
38             if(test == null || test.test(graph, content))            \r
39                 return modifierRule.getSorter(graph, content);\r
40             else\r
41                 return null;\r
42         } catch(DatabaseException e) {\r
43             // TODO reconsider\r
44             return null;\r
45         }\r
46     }    \r
47 }\r