]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.diagram/src/org/simantics/diagram/content/DiagramContentChanges.java
Robustness fixes for inline SVG text editing on diagrams
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / content / DiagramContentChanges.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.diagram.content;
13
14 import gnu.trove.map.hash.THashMap;
15
16 import java.util.Collections;
17 import java.util.HashSet;
18 import java.util.Map;
19 import java.util.Set;
20
21 import org.simantics.db.Resource;
22
23 /**
24  * @author Tuukka Lehtonen
25  */
26 public class DiagramContentChanges {
27
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;
33
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;
38
39     public boolean                            elementOrderChanged = false;
40
41     public static final DiagramContentChanges EMPTY;
42
43     static {
44         EMPTY = new DiagramContentChanges(true);
45     }
46
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();
57     }
58
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>();
69     }
70
71     public void markElementOrderChanged() {
72         elementOrderChanged = true;
73     }
74
75     public boolean isEmpty() {
76         return elements.isEmpty()
77         && nodes.isEmpty()
78         && connections.isEmpty()
79         && connectionSegments.isEmpty()
80         && branchPoints.isEmpty()
81         && routeGraphConnections.isEmpty()
82         && routeLinks.isEmpty()
83         && routeLines.isEmpty()
84         && routePoints.isEmpty()
85         && !elementOrderChanged;
86     }
87
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());
93         }
94         return result;
95     }
96
97     @Override
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);
121         sb.append("]");
122         return sb.toString();
123     }
124
125     private static String toString(int[] changes) {
126         return
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()] +
130         "]";
131     }
132
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()]++;
137         }
138         return result;
139     }
140
141 }