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 gnu.trove.map.hash.THashMap;
16 import java.util.Collections;
17 import java.util.HashSet;
21 import org.simantics.db.Resource;
24 * @author Tuukka Lehtonen
26 public class DiagramContentChanges {
28 public final Map<Resource, Change> elements;
29 public final Map<Resource, Change> nodes;
30 public final Map<Resource, Change> connections;
31 public final Map<EdgeResource, Change> connectionSegments;
32 public final Map<Resource, Change> branchPoints;
34 public final Map<Resource, Change> routeGraphConnections;
35 public final Map<EdgeResource, Change> routeLinks;
36 public final Map<Resource, Change> routeLines;
37 public final Map<Resource, Change> routePoints;
39 public boolean elementOrderChanged = false;
41 public static final DiagramContentChanges EMPTY;
44 EMPTY = new DiagramContentChanges(true);
47 private DiagramContentChanges(boolean empty) {
48 this.elements = Collections.emptyMap();
49 this.nodes = Collections.emptyMap();
50 this.connections = Collections.emptyMap();
51 this.connectionSegments = Collections.emptyMap();
52 this.branchPoints = Collections.emptyMap();
53 this.routeGraphConnections = Collections.emptyMap();
54 this.routeLinks = Collections.emptyMap();
55 this.routeLines = Collections.emptyMap();
56 this.routePoints = Collections.emptyMap();
59 public DiagramContentChanges() {
60 this.elements = new THashMap<Resource, Change>();
61 this.nodes = new THashMap<Resource, Change>();
62 this.connections = new THashMap<Resource, Change>();
63 this.connectionSegments = new THashMap<EdgeResource, Change>();
64 this.branchPoints = new THashMap<Resource, Change>();
65 this.routeGraphConnections = new THashMap<Resource, Change>();
66 this.routeLinks = new THashMap<EdgeResource, Change>();
67 this.routeLines = new THashMap<Resource, Change>();
68 this.routePoints = new THashMap<Resource, Change>();
71 public void markElementOrderChanged() {
72 elementOrderChanged = true;
75 public boolean isEmpty() {
76 return elements.isEmpty()
78 && connections.isEmpty()
79 && connectionSegments.isEmpty()
80 && branchPoints.isEmpty()
81 && routeGraphConnections.isEmpty()
82 && routeLinks.isEmpty()
83 && routeLines.isEmpty()
84 && routePoints.isEmpty()
85 && !elementOrderChanged;
88 public <T> Set<T> pick(Map<T, Change> map, Change change) {
89 Set<T> result = new HashSet<T>();
90 for (Map.Entry<T, Change> entry : map.entrySet()) {
91 if (entry.getValue() == change)
92 result.add(entry.getKey());
98 public String toString() {
99 StringBuilder sb = new StringBuilder();
100 sb.append(getClass().getSimpleName());
101 sb.append("[elements=");
102 sb.append(toString(count(elements)));
103 sb.append(", nodes=");
104 sb.append(toString(count(nodes)));
105 sb.append(", connection=");
106 sb.append(toString(count(connections)));
107 sb.append(", connection segments=");
108 sb.append(toString(count(connectionSegments)));
109 sb.append(", branch points=");
110 sb.append(toString(count(branchPoints)));
111 sb.append(", routegraph connections=");
112 sb.append(toString(count(routeGraphConnections)));
113 sb.append(", route links=");
114 sb.append(toString(count(routeLinks)));
115 sb.append(", route lines=");
116 sb.append(toString(count(routeLines)));
117 sb.append(", route points=");
118 sb.append(toString(count(routePoints)));
119 sb.append(", element order changed=");
120 sb.append(elementOrderChanged);
122 return sb.toString();
125 private static String toString(int[] changes) {
127 "[" + Change.ADDED.toString() + "=" + changes[Change.ADDED.ordinal()] +
128 ", " + Change.REMOVED.toString() + "=" + changes[Change.REMOVED.ordinal()] +
129 //", " + Change.POSSIBLY_MODIFIED.toString() + "=" + changes[Change.POSSIBLY_MODIFIED.ordinal()] +
133 private static int[] count(Map<?, Change> map) {
134 int[] result = new int[Change.values().length];
135 for (Change change : map.values()) {
136 result[change.ordinal()]++;