]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.charts/src/org/simantics/charts/ui/ChartSorterRule.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.charts / src / org / simantics / charts / ui / ChartSorterRule.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 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.charts.ui;
13
14 import java.util.Collections;
15 import java.util.Comparator;
16 import java.util.List;
17
18 import org.simantics.browsing.ui.BuiltinKeys;
19 import org.simantics.browsing.ui.NodeContext;
20 import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;
21 import org.simantics.browsing.ui.model.sorters.Sorter;
22 import org.simantics.browsing.ui.model.sorters.SorterRule;
23 import org.simantics.charts.query.TrendItemQuery;
24 import org.simantics.db.ReadGraph;
25 import org.simantics.db.Resource;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.exception.RuntimeDatabaseException;
28 import org.simantics.trend.configuration.TrendItem;
29
30 public class ChartSorterRule implements SorterRule, Sorter {
31
32         @Override
33         public boolean isCompatible(Class<?> contentType) {
34                 return contentType.equals(Resource.class);
35         }
36
37         @Override
38         public Sorter getSorter(ReadGraph graph, Object content) throws DatabaseException {
39                 return this;
40         }
41         
42         @Override
43         public void sort(ReadGraph graph, BrowseContext context, List<NodeContext> nodes) throws DatabaseException 
44         {
45                 try {
46                         ChartItemComparator cic = new ChartItemComparator();
47                         cic.graph = graph;
48                         Collections.sort(nodes, cic);
49                 } catch (RuntimeDatabaseException e) {
50                         if (e.getCause()!=null && e.getCause() instanceof DatabaseException) throw (DatabaseException) e.getCause();
51                         throw e;
52                 }
53                 
54         }
55         
56         static class ChartItemComparator implements Comparator<NodeContext> {
57                 ReadGraph graph;
58
59                 @Override
60                 public int compare(NodeContext nc1, NodeContext nc2) {
61                         Resource r1 = (Resource) nc1.getConstant(BuiltinKeys.INPUT);
62                         Resource r2 = (Resource) nc2.getConstant(BuiltinKeys.INPUT);
63                         
64                         try {
65                                 TrendItem ti1 = graph.sync( new TrendItemQuery(r1) );
66                                 TrendItem ti2 = graph.sync( new TrendItemQuery(r2) );
67                                 return ti1.compareTo(ti2);
68                         } catch (DatabaseException e) {
69                                 throw new RuntimeDatabaseException(e);
70                         }
71                 }
72         }
73
74         
75 }