]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/ElementReorder.java
Some enhancements to GraphLayer-related utilities for Diagram layers
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / synchronization / graph / ElementReorder.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in 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.diagram.synchronization.graph;
13
14 import java.util.ArrayList;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Set;
20
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;
29
30 /**
31  * This modification reorders the specified diagram in the graph backend
32  * according to the order of the elements specified argument element list.
33  * 
34  * <p>
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.
37  * 
38  * @author Tuukka Lehtonen
39  */
40 public class ElementReorder extends ModificationAdapter {
41
42     IDiagram diagram;
43     List<IElement> order;
44
45     public ElementReorder(IDiagram diagram, List<IElement> order) {
46         super(LOW_PRIORITY);
47         this.diagram = diagram;
48         this.order = order;
49     }
50
51     @Override
52     public void perform(WriteGraph g) throws Exception {
53         Resource l = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);
54
55         List<Resource> graphOrder = OrderedSetUtils.toList(g, l);
56         Set<Resource> graphContents = new HashSet<Resource>(graphOrder);
57
58         List<Resource> diagramOrder = new ArrayList<Resource>(order.size());
59         Map<Resource, Integer> diagramOrderIndex = new HashMap<Resource, Integer>(order.size());
60         int i = 0;
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)) {
69                     diagramOrder.add(r);
70                     diagramOrderIndex.put(r, Integer.valueOf(i));
71                     ++i;
72                 }
73             }
74         }
75
76         // Reorder the backend list according to diagramOrder
77         i = 0;
78         for (Resource r : graphOrder) {
79             Integer di = diagramOrderIndex.get(r);
80             if (di != null) {
81                 int targetIndex = di;
82                 int graphIndex = i++;
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) {
89                         after = l;
90                         if (l.equals(graphPrev))
91                             continue;
92                     } else {
93                         after = diagramOrder.get(targetIndex - 1);
94                         if (after.equals(graphPrev))
95                             continue;
96                     }
97
98                     // r needs to be repositioned.
99                     OrderedSetUtils.remove(g, l, r);
100                     OrderedSetUtils.addAfter(g, l, after, r);
101                 }
102             }
103         }
104     }
105
106 }