1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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.diagram.synchronization.graph;
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.List;
21 import org.simantics.db.Resource;
22 import org.simantics.db.WriteGraph;
23 import org.simantics.db.common.utils.OrderedSetUtils;
24 import org.simantics.diagram.synchronization.ModificationAdapter;
25 import org.simantics.diagram.ui.DiagramModelHints;
26 import org.simantics.g2d.diagram.IDiagram;
27 import org.simantics.g2d.element.ElementUtils;
28 import org.simantics.g2d.element.IElement;
31 * This modification reorders the specified diagram in the graph backend
32 * according to the order of the elements specified argument element list.
35 * The algorithm is linear with respect to the amount of elements on the
36 * diagram. It tries to minimize the number of ordered set operations.
38 * @author Tuukka Lehtonen
40 public class ElementReorder extends ModificationAdapter {
45 public ElementReorder(IDiagram diagram, List<IElement> order) {
47 this.diagram = diagram;
52 public void perform(WriteGraph g) throws Exception {
53 Resource l = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
55 List<Resource> graphOrder = OrderedSetUtils.toList(g, l);
56 Set<Resource> graphContents = new HashSet<Resource>(graphOrder);
58 List<Resource> diagramOrder = new ArrayList<Resource>(order.size());
59 Map<Resource, Integer> diagramOrderIndex = new HashMap<Resource, Integer>(order.size());
61 for (IElement e : order) {
62 Object obj = ElementUtils.getObject(e);
63 if (obj instanceof Resource) {
64 Resource r = (Resource) obj;
65 // Only consider resources that still are in the diagram.
66 // This prevents errors in situations where #order contains
67 // elements that no longer exist in the diagram.
68 if (graphContents.contains(r)) {
70 diagramOrderIndex.put(r, Integer.valueOf(i));
76 // Reorder the backend list according to diagramOrder
78 for (Resource r : graphOrder) {
79 Integer di = diagramOrderIndex.get(r);
83 if (graphIndex != targetIndex) {
84 // Check if the predecessor of r is already correct.
85 // If it is, we don't have to do anything for r.
86 Resource graphPrev = OrderedSetUtils.prev(g, l, r);
87 Resource after = null;
88 if (targetIndex == 0) {
90 if (l.equals(graphPrev))
93 after = diagramOrder.get(targetIndex - 1);
94 if (after.equals(graphPrev))
98 // r needs to be repositioned.
99 OrderedSetUtils.remove(g, l, r);
100 OrderedSetUtils.addAfter(g, l, after, r);