]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.charts/src/org/simantics/charts/ui/ChartSorterRule.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / ui / ChartSorterRule.java
diff --git a/bundles/org.simantics.charts/src/org/simantics/charts/ui/ChartSorterRule.java b/bundles/org.simantics.charts/src/org/simantics/charts/ui/ChartSorterRule.java
new file mode 100644 (file)
index 0000000..fedb734
--- /dev/null
@@ -0,0 +1,75 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 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.charts.ui;\r
+\r
+import java.util.Collections;\r
+import java.util.Comparator;\r
+import java.util.List;\r
+\r
+import org.simantics.browsing.ui.BuiltinKeys;\r
+import org.simantics.browsing.ui.NodeContext;\r
+import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;\r
+import org.simantics.browsing.ui.model.sorters.Sorter;\r
+import org.simantics.browsing.ui.model.sorters.SorterRule;\r
+import org.simantics.charts.query.TrendItemQuery;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.exception.RuntimeDatabaseException;\r
+import org.simantics.trend.configuration.TrendItem;\r
+\r
+public class ChartSorterRule implements SorterRule, Sorter {\r
+\r
+       @Override\r
+       public boolean isCompatible(Class<?> contentType) {\r
+               return contentType.equals(Resource.class);\r
+       }\r
+\r
+       @Override\r
+       public Sorter getSorter(ReadGraph graph, Object content) throws DatabaseException {\r
+               return this;\r
+       }\r
+       \r
+       @Override\r
+       public void sort(ReadGraph graph, BrowseContext context, List<NodeContext> nodes) throws DatabaseException \r
+       {\r
+               try {\r
+                       ChartItemComparator cic = new ChartItemComparator();\r
+                       cic.graph = graph;\r
+                       Collections.sort(nodes, cic);\r
+               } catch (RuntimeDatabaseException e) {\r
+                       if (e.getCause()!=null && e.getCause() instanceof DatabaseException) throw (DatabaseException) e.getCause();\r
+                       throw e;\r
+               }\r
+               \r
+       }\r
+       \r
+       static class ChartItemComparator implements Comparator<NodeContext> {\r
+               ReadGraph graph;\r
+\r
+               @Override\r
+               public int compare(NodeContext nc1, NodeContext nc2) {\r
+                       Resource r1 = (Resource) nc1.getConstant(BuiltinKeys.INPUT);\r
+                       Resource r2 = (Resource) nc2.getConstant(BuiltinKeys.INPUT);\r
+                       \r
+                       try {\r
+                               TrendItem ti1 = graph.sync( new TrendItemQuery(r1) );\r
+                               TrendItem ti2 = graph.sync( new TrendItemQuery(r2) );\r
+                               return ti1.compareTo(ti2);\r
+                       } catch (DatabaseException e) {\r
+                               throw new RuntimeDatabaseException(e);\r
+                       }\r
+               }\r
+       }\r
+\r
+       \r
+}\r