]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/content/DiagramContents.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / content / DiagramContents.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.content;
13
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18
19 import org.simantics.db.Resource;
20 import org.simantics.utils.datastructures.MapList;
21
22 /**
23  * @author Tuukka Lehtonen
24  */
25 public class DiagramContents {
26
27     /**
28      * Z-ordered list of ALL direct diagram elements.
29      */
30     public List<Resource>            elements                = Collections.emptyList();
31
32     /**
33      * The set of normal elements in the diagram contents list.
34      */
35     public Set<Resource>             nodeSet                 = Collections.emptySet();
36
37     public Set<Resource>             connectionSet           = Collections.emptySet();
38     public Set<EdgeResource>         connectionSegments      = Collections.emptySet();
39     public Set<Resource>             branchPoints            = Collections.emptySet();
40
41     public Set<Resource>             routeGraphConnectionSet = Collections.emptySet();
42     public Set<EdgeResource>         routeLinks              = Collections.emptySet();
43     public Set<Resource>             routeLines              = Collections.emptySet();
44     public Set<Resource>             routePoints             = Collections.emptySet();
45
46     public Map<Object, Resource>     partToConnection        = Collections.emptyMap();
47     public MapList<Resource, Object> connectionToParts       = new MapList<Resource, Object>();
48
49     private <T> void calculateChanges(Set<T> from, Set<T> to, Map<T, Change> result) {
50         for (T e : to)
51             if (!from.contains(e))
52                 result.put(e, Change.ADDED);
53 //            else
54 //                result.put(e, Change.POSSIBLY_MODIFIED);
55         for (T e : from)
56             if (!to.contains(e))
57                 result.put(e, Change.REMOVED);
58     }
59
60     private <T> void addAll(Set<T> to, Map<T, Change> result) {
61         for (T e : to)
62             result.put(e, Change.ADDED);
63     }
64
65     public DiagramContentChanges differenceFrom(DiagramContents from) {
66         DiagramContentChanges result = new DiagramContentChanges();
67         if (this == from)
68             return result;
69         if (from == null) {
70             // Both nodes and connections are considered elements of the diagram.
71             addAll(nodeSet, result.elements);
72             addAll(connectionSet, result.elements);
73             addAll(routeGraphConnectionSet, result.elements);
74             addAll(nodeSet, result.nodes);
75             addAll(connectionSet, result.connections);
76             addAll(connectionSegments, result.connectionSegments);
77             addAll(branchPoints, result.branchPoints);
78             addAll(routeGraphConnectionSet, result.routeGraphConnections);
79             addAll(routeLinks, result.routeLinks);
80             addAll(routeLines, result.routeLines);
81             addAll(routePoints, result.routePoints);
82         } else {
83             // Both nodes and connections are considered elements of the diagram.
84             calculateChanges(from.nodeSet, nodeSet, result.elements);
85             calculateChanges(from.connectionSet, connectionSet, result.elements);
86             calculateChanges(from.routeGraphConnectionSet, routeGraphConnectionSet, result.elements);
87             calculateChanges(from.nodeSet, nodeSet, result.nodes);
88             calculateChanges(from.connectionSet, connectionSet, result.connections);
89             calculateChanges(from.connectionSegments, connectionSegments, result.connectionSegments);
90             calculateChanges(from.branchPoints, branchPoints, result.branchPoints);
91             calculateChanges(from.routeGraphConnectionSet, routeGraphConnectionSet, result.routeGraphConnections);
92             calculateChanges(from.routeLinks, routeLinks, result.routeLinks);
93             calculateChanges(from.routeLines, routeLines, result.routeLines);
94             calculateChanges(from.routePoints, routePoints, result.routePoints);
95
96             if (!from.elements.equals(elements))
97                 result.markElementOrderChanged();
98         }
99         return result;
100     }
101
102     @Override
103     public int hashCode() {
104         final int prime = 31;
105         int result = 1;
106         result = prime * result + ((routeGraphConnectionSet == null) ? 0 : routeGraphConnectionSet.hashCode());
107         result = prime * result + ((connectionSet == null) ? 0 : connectionSet.hashCode());
108         result = prime * result + ((elements == null) ? 0 : elements.hashCode());
109         return result;
110     }
111
112     @Override
113     public boolean equals(Object obj) {
114         if (this == obj)
115             return true;
116         if (obj == null)
117             return false;
118         if (getClass() != obj.getClass())
119             return false;
120         DiagramContents other = (DiagramContents) obj;
121         if (!elements.equals(other.elements))
122             return false;
123         if (!connectionSegments.equals(other.connectionSegments))
124             return false;
125         if (!branchPoints.equals(other.branchPoints))
126             return false;
127         if (!routeLinks.equals(other.routeLinks))
128             return false;
129         if (!routeLines.equals(other.routeLines))
130             return false;
131         if (!routePoints.equals(other.routePoints))
132             return false;
133         return true;
134     }
135
136     @Override
137     public String toString() {
138         return getClass().getSimpleName() + "[elements=" + elements.size()
139         + ", nodes=" + nodeSet.size()
140         + ", connections=" + connectionSet.size()
141         + ", branch points=" + branchPoints.size()
142         + ", connection segments=" + connectionSegments.size()
143         + ", routegraph connections=" + routeGraphConnectionSet.size()
144         + ", routegraph links=" + routeLinks.size()
145         + ", route lines=" + routeLines.size()
146         + ", route points=" + routePoints.size()
147         + "]";
148     }
149
150 }