]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.model/src/org/simantics/browsing/ui/model/sorters/LinkedListSorter.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.model / src / org / simantics / browsing / ui / model / sorters / LinkedListSorter.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.browsing.ui.model.sorters;
13
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.List;
17 import java.util.Map;
18 import java.util.Set;
19
20 import org.simantics.browsing.ui.BuiltinKeys;
21 import org.simantics.browsing.ui.NodeContext;
22 import org.simantics.browsing.ui.model.browsecontexts.BrowseContext;
23 import org.simantics.db.ReadGraph;
24 import org.simantics.db.Resource;
25 import org.simantics.db.common.utils.ListUtils;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.layer0.Layer0;
28
29 public class LinkedListSorter implements Sorter {
30
31     public static final LinkedListSorter INSTANCE = new LinkedListSorter();
32     
33     private LinkedListSorter() {}
34
35         @Override
36         public void sort(ReadGraph graph, BrowseContext context, List<NodeContext> nodes) throws DatabaseException {
37
38                 Layer0 L0 = Layer0.getInstance(graph);
39                 Set<Resource> parents = new HashSet<Resource>();
40                 Map<Resource, NodeContext> map = new HashMap<Resource, NodeContext>();
41                 for(NodeContext node : nodes) {
42                         Resource r = (Resource)node.getConstant(BuiltinKeys.INPUT);
43                         Resource parent = graph.getPossibleObject(r, L0.PartOf);
44                         parents.add(parent);
45                         map.put(r, node);
46                 }
47                 
48                 if(parents.contains(null)) return;
49                 if(parents.size() != 1) return;
50
51                 Resource parent = parents.iterator().next();
52                 nodes.clear();
53                 for(Resource r : ListUtils.toList(graph, parent))  {
54                         NodeContext ctx = map.get(r);
55                         if(ctx != null) nodes.add(ctx);
56                 }
57         
58     }
59
60 }