]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/synchronization/graph/ElementReorder.java
Fixed multiple issues causing dangling references to discarded queries
[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> newGraphOrder = new ArrayList<>();
59         for (IElement e : order) {
60             Object obj = ElementUtils.getObject(e);
61             if (obj instanceof Resource) {
62                 Resource r = (Resource) obj;
63                 // Only consider resources that still are in the diagram.
64                 // This prevents errors in situations where #order contains
65                 // elements that no longer exist in the diagram.
66                 if (graphContents.contains(r)) {
67                         newGraphOrder.add(r);
68                 }
69             }
70         }
71
72         // Safety measure for possible missing elements
73         if (graphOrder.size() != newGraphOrder.size()) {
74             Set<Resource> added = new HashSet<Resource>(newGraphOrder);
75             for (Resource r : graphOrder) {
76                 if (!added.contains(r)) {
77                     newGraphOrder.add(r);
78                 }
79             }
80         }
81
82         OrderedSetUtils.reorder(g, l, newGraphOrder);
83     }
84
85 }