forked from jgraph/mxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmxEventSource.java
More file actions
185 lines (161 loc) · 3.59 KB
/
mxEventSource.java
File metadata and controls
185 lines (161 loc) · 3.59 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
/**
* Copyright (c) 2007, Gaudenz Alder
*/
package com.mxgraph.util;
import java.util.ArrayList;
import java.util.List;
/**
* Base class for objects that dispatch named events.
*/
public class mxEventSource
{
/**
* Defines the requirements for an object that listens to an event source.
*/
public interface mxIEventListener
{
/**
* Called when the graph model has changed.
*
* @param sender Reference to the source of the event.
* @param evt Event object to be dispatched.
*/
void invoke(Object sender, mxEventObject evt);
}
/**
* Holds the event names and associated listeners in an array. The array
* contains the event name followed by the respective listener for each
* registered listener.
*/
protected transient List<Object> eventListeners = null;
/**
* Holds the source object for this event source.
*/
protected Object eventSource;
/**
* Specifies if events can be fired. Default is true.
*/
protected boolean eventsEnabled = true;
/**
* Constructs a new event source using this as the source object.
*/
public mxEventSource()
{
this(null);
}
/**
* Constructs a new event source for the given source object.
*/
public mxEventSource(Object source)
{
setEventSource(source);
}
/**
*
*/
public Object getEventSource()
{
return eventSource;
}
/**
*
*/
public void setEventSource(Object value)
{
this.eventSource = value;
}
/**
*
*/
public boolean isEventsEnabled()
{
return eventsEnabled;
}
/**
*
*/
public void setEventsEnabled(boolean eventsEnabled)
{
this.eventsEnabled = eventsEnabled;
}
/**
* Binds the specified function to the given event name. If no event name
* is given, then the listener is registered for all events.
*/
public void addListener(String eventName, mxIEventListener listener)
{
if (eventListeners == null)
{
eventListeners = new ArrayList<Object>();
}
eventListeners.add(eventName);
eventListeners.add(listener);
}
/**
* Function: removeListener
*
* Removes all occurances of the given listener from the list of listeners.
*/
public void removeListener(mxIEventListener listener)
{
removeListener(listener, null);
}
/**
* Function: removeListener
*
* Removes all occurances of the given listener from the list of listeners.
*/
public void removeListener(mxIEventListener listener, String eventName)
{
if (eventListeners != null)
{
for (int i = eventListeners.size() - 2; i > -1; i -= 2)
{
if (eventListeners.get(i + 1) == listener
&& (eventName == null || String.valueOf(
eventListeners.get(i)).equals(eventName)))
{
eventListeners.remove(i + 1);
eventListeners.remove(i);
}
}
}
}
/**
* Dispatches the given event name with this object as the event source.
* <code>fireEvent(new mxEventObject("eventName", key1, val1, .., keyN, valN))</code>
*
*/
public void fireEvent(mxEventObject evt)
{
fireEvent(evt, null);
}
/**
* Dispatches the given event name, passing all arguments after the given
* name to the registered listeners for the event.
*/
public void fireEvent(mxEventObject evt, Object sender)
{
if (eventListeners != null && !eventListeners.isEmpty()
&& isEventsEnabled())
{
if (sender == null)
{
sender = getEventSource();
}
if (sender == null)
{
sender = this;
}
for (int i = 0; i < eventListeners.size(); i += 2)
{
String listen = (String) eventListeners.get(i);
if (listen == null || listen.equals(evt.getName()))
{
((mxIEventListener) eventListeners.get(i + 1)).invoke(
sender, evt);
}
}
}
}
}