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