]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/ElementReorder.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / synchronization / graph / ElementReorder.java
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
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.diagram.synchronization.graph;\r
13 \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
20 \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
29 \r
30 /**\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
33  * \r
34  * <p>\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
37  * \r
38  * @author Tuukka Lehtonen\r
39  */\r
40 public class ElementReorder extends ModificationAdapter {\r
41 \r
42     IDiagram diagram;\r
43     List<IElement> order;\r
44 \r
45     public ElementReorder(IDiagram diagram, List<IElement> order) {\r
46         super(LOW_PRIORITY);\r
47         this.diagram = diagram;\r
48         this.order = order;\r
49     }\r
50 \r
51     @Override\r
52     public void perform(WriteGraph g) throws Exception {\r
53         Resource l = diagram.getHint(DiagramModelHints.KEY_DIAGRAM_RESOURCE);\r
54 \r
55         List<Resource> graphOrder = OrderedSetUtils.toList(g, l);\r
56         Set<Resource> graphContents = new HashSet<Resource>(graphOrder);\r
57 \r
58         List<Resource> diagramOrder = new ArrayList<Resource>(order.size());\r
59         Map<Resource, Integer> diagramOrderIndex = new HashMap<Resource, Integer>(order.size());\r
60         int i = 0;\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
71                     ++i;\r
72                 }\r
73             }\r
74         }\r
75 \r
76         // Reorder the backend list according to diagramOrder\r
77         i = 0;\r
78         for (Resource r : graphOrder) {\r
79             Integer di = diagramOrderIndex.get(r);\r
80             if (di != null) {\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
89                         after = l;\r
90                         if (l.equals(graphPrev))\r
91                             continue;\r
92                     } else {\r
93                         after = diagramOrder.get(targetIndex - 1);\r
94                         if (after.equals(graphPrev))\r
95                             continue;\r
96                     }\r
97 \r
98                     // r needs to be repositioned.\r
99                     OrderedSetUtils.remove(g, l, r);\r
100                     OrderedSetUtils.addAfter(g, l, after, r);\r
101                 }\r
102             }\r
103         }\r
104     }\r
105 \r
106 }\r