--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.diagram.content;\r
+\r
+import java.util.Collections;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.utils.datastructures.MapList;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class DiagramContents {\r
+\r
+ /**\r
+ * Z-ordered list of ALL direct diagram elements.\r
+ */\r
+ public List<Resource> elements = Collections.emptyList();\r
+\r
+ /**\r
+ * The set of normal elements in the diagram contents list.\r
+ */\r
+ public Set<Resource> nodeSet = Collections.emptySet();\r
+\r
+ public Set<Resource> connectionSet = Collections.emptySet();\r
+ public Set<EdgeResource> connectionSegments = Collections.emptySet();\r
+ public Set<Resource> branchPoints = Collections.emptySet();\r
+\r
+ public Set<Resource> routeGraphConnectionSet = Collections.emptySet();\r
+ public Set<EdgeResource> routeLinks = Collections.emptySet();\r
+ public Set<Resource> routeLines = Collections.emptySet();\r
+ public Set<Resource> routePoints = Collections.emptySet();\r
+\r
+ public Map<Object, Resource> partToConnection = Collections.emptyMap();\r
+ public MapList<Resource, Object> connectionToParts = new MapList<Resource, Object>();\r
+\r
+ private <T> void calculateChanges(Set<T> from, Set<T> to, Map<T, Change> result) {\r
+ for (T e : to)\r
+ if (!from.contains(e))\r
+ result.put(e, Change.ADDED);\r
+// else\r
+// result.put(e, Change.POSSIBLY_MODIFIED);\r
+ for (T e : from)\r
+ if (!to.contains(e))\r
+ result.put(e, Change.REMOVED);\r
+ }\r
+\r
+ private <T> void addAll(Set<T> to, Map<T, Change> result) {\r
+ for (T e : to)\r
+ result.put(e, Change.ADDED);\r
+ }\r
+\r
+ public DiagramContentChanges differenceFrom(DiagramContents from) {\r
+ DiagramContentChanges result = new DiagramContentChanges();\r
+ if (this == from)\r
+ return result;\r
+ if (from == null) {\r
+ // Both nodes and connections are considered elements of the diagram.\r
+ addAll(nodeSet, result.elements);\r
+ addAll(connectionSet, result.elements);\r
+ addAll(routeGraphConnectionSet, result.elements);\r
+ addAll(nodeSet, result.nodes);\r
+ addAll(connectionSet, result.connections);\r
+ addAll(connectionSegments, result.connectionSegments);\r
+ addAll(branchPoints, result.branchPoints);\r
+ addAll(routeGraphConnectionSet, result.routeGraphConnections);\r
+ addAll(routeLinks, result.routeLinks);\r
+ addAll(routeLines, result.routeLines);\r
+ addAll(routePoints, result.routePoints);\r
+ } else {\r
+ // Both nodes and connections are considered elements of the diagram.\r
+ calculateChanges(from.nodeSet, nodeSet, result.elements);\r
+ calculateChanges(from.connectionSet, connectionSet, result.elements);\r
+ calculateChanges(from.routeGraphConnectionSet, routeGraphConnectionSet, result.elements);\r
+ calculateChanges(from.nodeSet, nodeSet, result.nodes);\r
+ calculateChanges(from.connectionSet, connectionSet, result.connections);\r
+ calculateChanges(from.connectionSegments, connectionSegments, result.connectionSegments);\r
+ calculateChanges(from.branchPoints, branchPoints, result.branchPoints);\r
+ calculateChanges(from.routeGraphConnectionSet, routeGraphConnectionSet, result.routeGraphConnections);\r
+ calculateChanges(from.routeLinks, routeLinks, result.routeLinks);\r
+ calculateChanges(from.routeLines, routeLines, result.routeLines);\r
+ calculateChanges(from.routePoints, routePoints, result.routePoints);\r
+\r
+ if (!from.elements.equals(elements))\r
+ result.markElementOrderChanged();\r
+ }\r
+ return result;\r
+ }\r
+\r
+ @Override\r
+ public int hashCode() {\r
+ final int prime = 31;\r
+ int result = 1;\r
+ result = prime * result + ((routeGraphConnectionSet == null) ? 0 : routeGraphConnectionSet.hashCode());\r
+ result = prime * result + ((connectionSet == null) ? 0 : connectionSet.hashCode());\r
+ result = prime * result + ((elements == null) ? 0 : elements.hashCode());\r
+ return result;\r
+ }\r
+\r
+ @Override\r
+ public boolean equals(Object obj) {\r
+ if (this == obj)\r
+ return true;\r
+ if (obj == null)\r
+ return false;\r
+ if (getClass() != obj.getClass())\r
+ return false;\r
+ DiagramContents other = (DiagramContents) obj;\r
+ if (!elements.equals(other.elements))\r
+ return false;\r
+ if (!connectionSegments.equals(other.connectionSegments))\r
+ return false;\r
+ if (!branchPoints.equals(other.branchPoints))\r
+ return false;\r
+ if (!routeLinks.equals(other.routeLinks))\r
+ return false;\r
+ if (!routeLines.equals(other.routeLines))\r
+ return false;\r
+ if (!routePoints.equals(other.routePoints))\r
+ return false;\r
+ return true;\r
+ }\r
+\r
+ @Override\r
+ public String toString() {\r
+ return getClass().getSimpleName() + "[elements=" + elements.size()\r
+ + ", nodes=" + nodeSet.size()\r
+ + ", connections=" + connectionSet.size()\r
+ + ", branch points=" + branchPoints.size()\r
+ + ", connection segments=" + connectionSegments.size()\r
+ + ", routegraph connections=" + routeGraphConnectionSet.size()\r
+ + ", routegraph links=" + routeLinks.size()\r
+ + ", route lines=" + routeLines.size()\r
+ + ", route points=" + routePoints.size()\r
+ + "]";\r
+ }\r
+\r
+}
\ No newline at end of file