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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.diagram.content;
\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
19 import org.simantics.db.Resource;
\r
20 import org.simantics.utils.datastructures.MapList;
\r
23 * @author Tuukka Lehtonen
\r
25 public class DiagramContents {
\r
28 * Z-ordered list of ALL direct diagram elements.
\r
30 public List<Resource> elements = Collections.emptyList();
\r
33 * The set of normal elements in the diagram contents list.
\r
35 public Set<Resource> nodeSet = Collections.emptySet();
\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
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
46 public Map<Object, Resource> partToConnection = Collections.emptyMap();
\r
47 public MapList<Resource, Object> connectionToParts = new MapList<Resource, Object>();
\r
49 private <T> void calculateChanges(Set<T> from, Set<T> to, Map<T, Change> result) {
\r
51 if (!from.contains(e))
\r
52 result.put(e, Change.ADDED);
\r
54 // result.put(e, Change.POSSIBLY_MODIFIED);
\r
56 if (!to.contains(e))
\r
57 result.put(e, Change.REMOVED);
\r
60 private <T> void addAll(Set<T> to, Map<T, Change> result) {
\r
62 result.put(e, Change.ADDED);
\r
65 public DiagramContentChanges differenceFrom(DiagramContents from) {
\r
66 DiagramContentChanges result = new DiagramContentChanges();
\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
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
96 if (!from.elements.equals(elements))
\r
97 result.markElementOrderChanged();
\r
103 public int hashCode() {
\r
104 final int prime = 31;
\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
113 public boolean equals(Object obj) {
\r
118 if (getClass() != obj.getClass())
\r
120 DiagramContents other = (DiagramContents) obj;
\r
121 if (!elements.equals(other.elements))
\r
123 if (!connectionSegments.equals(other.connectionSegments))
\r
125 if (!branchPoints.equals(other.branchPoints))
\r
127 if (!routeLinks.equals(other.routeLinks))
\r
129 if (!routeLines.equals(other.routeLines))
\r
131 if (!routePoints.equals(other.routePoints))
\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