1 /*******************************************************************************
2 * Copyright (c) 2010, 2011 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.model.sorters;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.List;
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;
29 public class LinkedListSorter implements Sorter {
31 public static final LinkedListSorter INSTANCE = new LinkedListSorter();
33 private LinkedListSorter() {}
36 public void sort(ReadGraph graph, BrowseContext context, List<NodeContext> nodes) throws DatabaseException {
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);
48 if(parents.contains(null)) return;
49 if(parents.size() != 1) return;
51 Resource parent = parents.iterator().next();
53 for(Resource r : ListUtils.toList(graph, parent)) {
54 NodeContext ctx = map.get(r);
55 if(ctx != null) nodes.add(ctx);