]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.selectionview/src/org/simantics/selectionview/StandardPropertySorterRule.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.selectionview / src / org / simantics / selectionview / StandardPropertySorterRule.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.selectionview;
13
14 import org.simantics.browsing.ui.BuiltinKeys;
15 import org.simantics.browsing.ui.NodeContext;
16 import org.simantics.browsing.ui.common.ColumnKeys;
17 import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;
18 import org.simantics.browsing.ui.model.sorters.AbstractSorter;
19 import org.simantics.browsing.ui.model.sorters.Sorter;
20 import org.simantics.browsing.ui.model.sorters.SorterRule;
21 import org.simantics.databoard.Bindings;
22 import org.simantics.db.ReadGraph;
23 import org.simantics.db.Resource;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.variable.Variable;
26 import org.simantics.utils.datastructures.ComparablePair;
27
28 public class StandardPropertySorterRule implements SorterRule {    
29     
30         final private Sorter sorter;
31         
32         public StandardPropertySorterRule(Resource rule) {
33                 this.sorter = new StandardSorter(rule);
34         }
35         
36         static class StandardSorter extends AbstractSorter<ComparablePair<String, String>> {
37                 
38                 final private Resource rule;
39                 
40                 public StandardSorter(Resource rule) {
41                         this.rule = rule;
42                 }
43
44                 @Override
45                 public ComparablePair<String, String> getSortingCriterion(ReadGraph graph, BrowseContext context, NodeContext node) throws DatabaseException {
46
47                         Object content = node.getConstant(BuiltinKeys.INPUT);
48                         if(content instanceof Variable) {
49                                 Variable var = (Variable)content;
50                         Variable info = var.getPossibleProperty(graph, SelectionVariables.PROPERTY_INFO);
51                         // FIXME: what is this block ??
52                         if(info == null) { 
53 //                              System.err.println("null info for " + ((Variable)content).getURI(graph));
54 //                              info = ((Variable)content).getPossiblePropertyValue(graph, SelectionVariables.PROPERTY_INFO);
55                                 return ComparablePair.make("", ((Variable)content).getName(graph));
56                         }
57                         
58                         Variable special = SelectionViewUtils.getSpecialCategory(graph, rule, info);
59                         if(special != null) info = special;
60                         
61                         String cat = info.getPossiblePropertyValue(graph, SelectionVariables.CATEGORY_SORTING_NAME, Bindings.STRING);
62                         if(cat == null) {
63                                 cat = info.getPossiblePropertyValue(graph, SelectionVariables.CATEGORY_NAME, Bindings.STRING);
64                                 if(cat == null) cat = "";
65                         }
66                         String label = ((Variable)content).getPossiblePropertyValue(graph, SelectionVariables.PROPERTY_SORTING_NAME, Bindings.STRING);
67                         if(label == null) label = ((Variable)content).getPossiblePropertyValue(graph, ColumnKeys.DISPLAY_PROPERTY, Bindings.STRING);
68                         if(label == null) label = "Invalid label for " + ((Variable)content).getURI(graph); 
69                         return ComparablePair.make(cat,label);
70                                 
71                         } else if (content instanceof CategoryNode) {
72                                 return ComparablePair.make(((CategoryNode)content).getSortingName(), "");
73                         }
74                         
75                         throw new IllegalArgumentException("Content type should be Variable or CategoryNode");
76                         
77                 }
78
79                 @Override
80                 public int compare(ComparablePair<String, String> a, ComparablePair<String, String> b) {
81                         return a.compareTo(b);
82                 }
83                 
84         };
85         
86         
87     @Override
88     public boolean isCompatible(Class<?> contentType) {
89         return contentType.equals(Variable.class);
90     }
91
92     @Override
93     public Sorter getSorter(ReadGraph graph, Object content)
94             throws DatabaseException {
95         return sorter;
96     }
97
98 }