1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.diagram.synchronization.graph;
\r
14 import java.util.ArrayList;
\r
15 import java.util.HashMap;
\r
16 import java.util.HashSet;
\r
17 import java.util.List;
\r
18 import java.util.Map;
\r
19 import java.util.Set;
\r
21 import org.simantics.db.Resource;
\r
22 import org.simantics.db.WriteGraph;
\r
23 import org.simantics.db.common.utils.OrderedSetUtils;
\r
24 import org.simantics.diagram.synchronization.ModificationAdapter;
\r
25 import org.simantics.diagram.ui.DiagramModelHints;
\r
26 import org.simantics.g2d.diagram.IDiagram;
\r
27 import org.simantics.g2d.element.ElementUtils;
\r
28 import org.simantics.g2d.element.IElement;
\r
31 * This modification reorders the specified diagram in the graph backend
\r
32 * according to the order of the elements specified argument element list.
\r
35 * The algorithm is linear with respect to the amount of elements on the
\r
36 * diagram. It tries to minimize the number of ordered set operations.
\r
38 * @author Tuukka Lehtonen
\r
40 public class ElementReorder extends ModificationAdapter {
\r
43 List<IElement> order;
\r
45 public ElementReorder(IDiagram diagram, List<IElement> order) {
\r
46 super(LOW_PRIORITY);
\r
47 this.diagram = diagram;
\r
52 public void perform(WriteGraph g) throws Exception {
\r
53 Resource l = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
\r
55 List<Resource> graphOrder = OrderedSetUtils.toList(g, l);
\r
56 Set<Resource> graphContents = new HashSet<Resource>(graphOrder);
\r
58 List<Resource> diagramOrder = new ArrayList<Resource>(order.size());
\r
59 Map<Resource, Integer> diagramOrderIndex = new HashMap<Resource, Integer>(order.size());
\r
61 for (IElement e : order) {
\r
62 Object obj = ElementUtils.getObject(e);
\r
63 if (obj instanceof Resource) {
\r
64 Resource r = (Resource) obj;
\r
65 // Only consider resources that still are in the diagram.
\r
66 // This prevents errors in situations where #order contains
\r
67 // elements that no longer exist in the diagram.
\r
68 if (graphContents.contains(r)) {
\r
69 diagramOrder.add(r);
\r
70 diagramOrderIndex.put(r, Integer.valueOf(i));
\r
76 // Reorder the backend list according to diagramOrder
\r
78 for (Resource r : graphOrder) {
\r
79 Integer di = diagramOrderIndex.get(r);
\r
81 int targetIndex = di;
\r
82 int graphIndex = i++;
\r
83 if (graphIndex != targetIndex) {
\r
84 // Check if the predecessor of r is already correct.
\r
85 // If it is, we don't have to do anything for r.
\r
86 Resource graphPrev = OrderedSetUtils.prev(g, l, r);
\r
87 Resource after = null;
\r
88 if (targetIndex == 0) {
\r
90 if (l.equals(graphPrev))
\r
93 after = diagramOrder.get(targetIndex - 1);
\r
94 if (after.equals(graphPrev))
\r
98 // r needs to be repositioned.
\r
99 OrderedSetUtils.remove(g, l, r);
\r
100 OrderedSetUtils.addAfter(g, l, after, r);
\r