forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxSharedState.java
More file actions
187 lines (161 loc) · 3.95 KB
/
mxSharedState.java
File metadata and controls
187 lines (161 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* $Id: mxSharedState.java,v 1.3 2012/01/13 12:35:44 david Exp $
* Copyright (c) 2007-2012, JGraph Ltd
*/
package com.mxgraph.sharing;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.w3c.dom.Node;
import com.mxgraph.util.mxEventSource;
import com.mxgraph.util.mxXmlUtils;
/**
* Implements a diagram that may be shared among multiple sessions. This
* implementation is based only on string, it does not have a model instance.
* The diagram is represented by its initial state and the sequence of edits
* as applied to the diagram.
*/
public class mxSharedState extends mxEventSource
{
/**
* Defines the requirements for an object that listens to changes on the
* shared diagram.
*/
public interface mxDiagramChangeListener
{
/**
* Fires when the shared diagram was changed.
*
* @param sender Session where the change was received from.
* @param edits String that represents the edits.
*/
void diagramChanged(Object sender, String edits);
}
/**
* Holds a list of diagram change listeners.
*/
protected List<mxDiagramChangeListener> diagramChangeListeners;
/**
* Holds the initial state of the diagram.
*/
protected String state;
/**
* Holds the history of all changes of initial state.
*/
protected StringBuffer delta = new StringBuffer();
/**
* Constructs a new diagram with the given state.
*
* @param state Initial state of the diagram.
*/
public mxSharedState(String state)
{
this.state = state;
}
/**
* Returns the initial state of the diagram.
*/
public String getState()
{
return state;
}
/**
* Returns the history of all changes as a string.
*/
public synchronized String getDelta()
{
return delta.toString();
}
/**
* Appends the given string to the history and dispatches the change to all
* sessions that are listening to this shared diagram.
*
* @param sender Session where the change originated from.
* @param delta XML that represents the change.
*/
public void processDelta(Object sender, Node delta)
{
StringBuffer edits = new StringBuffer();
synchronized (this)
{
Node edit = delta.getFirstChild();
while (edit != null)
{
if (edit.getNodeName().equals("edit"))
{
edits.append(processEdit(edit));
}
edit = edit.getNextSibling();
}
}
String xml = edits.toString();
addDelta(xml);
dispatchDiagramChangeEvent(sender, xml);
}
/**
*
*/
protected String processEdit(Node node)
{
return mxXmlUtils.getXml(node);
}
/**
*
*/
public synchronized void addDelta(String xml)
{
// TODO: Clear delta if xml contains mxRootChange
delta.append(xml);
}
/**
* Clears the history of all changes.
*/
public synchronized void resetDelta()
{
delta = new StringBuffer();
}
/**
* Adds the given listener to the list of diagram change listeners.
*
* @param listener Diagram change listener to be added.
*/
public void addDiagramChangeListener(mxDiagramChangeListener listener)
{
if (diagramChangeListeners == null)
{
diagramChangeListeners = new ArrayList<mxDiagramChangeListener>();
}
diagramChangeListeners.add(listener);
}
/**
* Removes the given listener from the list of diagram change listeners.
*
* @param listener Diagram change listener to be removed.
*/
public void removeDiagramChangeListener(mxDiagramChangeListener listener)
{
if (diagramChangeListeners != null)
{
diagramChangeListeners.remove(listener);
}
}
/**
* Dispatches the given event information to all diagram change listeners.
*
* @param sender Session where the change was received from.
* @param xml XML string that represents the change.
*/
void dispatchDiagramChangeEvent(Object sender, String edits)
{
if (diagramChangeListeners != null)
{
Iterator<mxDiagramChangeListener> it = diagramChangeListeners
.iterator();
while (it.hasNext())
{
mxDiagramChangeListener listener = it.next();
listener.diagramChanged(sender, edits);
}
}
}
}