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