]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.selectionview/src/org/simantics/selectionview/StandardPropertySorterRule.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.selectionview / src / org / simantics / selectionview / StandardPropertySorterRule.java
diff --git a/bundles/org.simantics.selectionview/src/org/simantics/selectionview/StandardPropertySorterRule.java b/bundles/org.simantics.selectionview/src/org/simantics/selectionview/StandardPropertySorterRule.java
new file mode 100644 (file)
index 0000000..03dae72
--- /dev/null
@@ -0,0 +1,98 @@
+/*******************************************************************************\r
+ * Copyright (c) 2010, 2011 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.selectionview;\r
+\r
+import org.simantics.browsing.ui.BuiltinKeys;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.common.ColumnKeys;\r
+import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;\r
+import org.simantics.browsing.ui.model.sorters.AbstractSorter;\r
+import org.simantics.browsing.ui.model.sorters.Sorter;\r
+import org.simantics.browsing.ui.model.sorters.SorterRule;\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.variable.Variable;\r
+import org.simantics.utils.datastructures.ComparablePair;\r
+\r
+public class StandardPropertySorterRule implements SorterRule {    \r
+    \r
+       final private Sorter sorter;\r
+       \r
+       public StandardPropertySorterRule(Resource rule) {\r
+               this.sorter = new StandardSorter(rule);\r
+       }\r
+       \r
+       static class StandardSorter extends AbstractSorter<ComparablePair<String, String>> {\r
+               \r
+               final private Resource rule;\r
+               \r
+               public StandardSorter(Resource rule) {\r
+                       this.rule = rule;\r
+               }\r
+\r
+               @Override\r
+               public ComparablePair<String, String> getSortingCriterion(ReadGraph graph, BrowseContext context, NodeContext node) throws DatabaseException {\r
+\r
+                       Object content = node.getConstant(BuiltinKeys.INPUT);\r
+                       if(content instanceof Variable) {\r
+                               Variable var = (Variable)content;\r
+                       Variable info = var.getPossibleProperty(graph, SelectionVariables.PROPERTY_INFO);\r
+                       // FIXME: what is this block ??\r
+                       if(info == null) { \r
+//                             System.err.println("null info for " + ((Variable)content).getURI(graph));\r
+//                             info = ((Variable)content).getPossiblePropertyValue(graph, SelectionVariables.PROPERTY_INFO);\r
+                               return ComparablePair.make("", ((Variable)content).getName(graph));\r
+                       }\r
+                       \r
+                       Variable special = SelectionViewUtils.getSpecialCategory(graph, rule, info);\r
+                       if(special != null) info = special;\r
+                       \r
+                       String cat = info.getPossiblePropertyValue(graph, SelectionVariables.CATEGORY_SORTING_NAME, Bindings.STRING);\r
+                       if(cat == null) {\r
+                               cat = info.getPossiblePropertyValue(graph, SelectionVariables.CATEGORY_NAME, Bindings.STRING);\r
+                               if(cat == null) cat = "";\r
+                       }\r
+                       String label = ((Variable)content).getPossiblePropertyValue(graph, SelectionVariables.PROPERTY_SORTING_NAME, Bindings.STRING);\r
+                       if(label == null) label = ((Variable)content).getPossiblePropertyValue(graph, ColumnKeys.DISPLAY_PROPERTY, Bindings.STRING);\r
+                       if(label == null) label = "Invalid label for " + ((Variable)content).getURI(graph); \r
+                       return ComparablePair.make(cat,label);\r
+                               \r
+                       } else if (content instanceof CategoryNode) {\r
+                               return ComparablePair.make(((CategoryNode)content).getSortingName(), "");\r
+                       }\r
+                       \r
+                       throw new IllegalArgumentException("Content type should be Variable or CategoryNode");\r
+                       \r
+               }\r
+\r
+               @Override\r
+               public int compare(ComparablePair<String, String> a, ComparablePair<String, String> b) {\r
+                       return a.compareTo(b);\r
+               }\r
+               \r
+       };\r
+       \r
+       \r
+    @Override\r
+    public boolean isCompatible(Class<?> contentType) {\r
+        return contentType.equals(Variable.class);\r
+    }\r
+\r
+    @Override\r
+    public Sorter getSorter(ReadGraph graph, Object content)\r
+            throws DatabaseException {\r
+        return sorter;\r
+    }\r
+\r
+}\r