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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
|
/* XLogo4Schools - A Logo Interpreter specialized for use in schools, based on XLogo by Loic Le Coq
* Copyright (C) 2013 Marko Zivkovic
*
* Contact Information: marko88zivkovic at gmail dot com
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version. This program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the
* GNU General Public License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
* This Java source code belongs to XLogo4Schools, written by Marko Zivkovic
* during his Bachelor thesis at the computer science department of ETH Zurich,
* in the year 2013 and/or during future work.
*
* It is a reengineered version of XLogo written by Loic Le Coq, published
* under the GPL License at http://xlogo.tuxfamily.org/
*
* Contents of this file were entirely written by Marko Zivkovic
*/
package xlogo.kernel.userspace.context;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Stack;
import xlogo.interfaces.MessageBroadcaster;
import xlogo.interfaces.X4SModeSwitcher;
/**
* One of the four main roles in the {@link xlogo.kernel.userspace.UserSpace} <br>
* <b> The Contexts and Priorities and multiplicity</b>
* 1. UserContext [1] : default context, lowest priority, cannot be killed.
* 2. RecordContext [0..1] : medium priority, killing it will kill all networking contexts too.
* 3. NetworkContext [0..*] : highest priority, can be stacked.
*
* @author Marko
*/
public class ContextManager implements X4SModeSwitcher, ContextSwitcher, MessageBroadcaster
{
private LogoContext currentContext;
private final UserContext userContext = new UserContext();
private RecordContext recordContext;
private final Stack<NetworkContext> networkStack = new Stack<NetworkContext>();
private MessageListener contextMessageListener;
public ContextManager()
{
currentContext = userContext;
initContextMessageListener();
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* CONTEXT PROVIDER
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
/**
* After a context has been removed by stopXXXMode(), contextSwitch() will determine the next current context.
* It only peeks at the context collections, but does not modify them. Calling this does never harm,
* but after one of the context collections has been changed, it should be called.<br>
* It notifies ContextProviderListeners if the context has actually changed after calling this.
* <p>
* The policy for context switches is defines in the interface here : {@link X4SModeSwitcher}
* <p>
* Note, this should be called before the mode switch events, because we first want to completely context switch, before we change the mode.
* Generally always publish events after the event has actually occurred.
*
*/
protected void contextSwitch()
{
LogoContext old = currentContext;
if (networkStack.size() > 0)
currentContext = networkStack.peek();
else
currentContext = recordContext != null ? recordContext : userContext;
if(old != currentContext)
notifyContextSwitched(currentContext);
}
/**
* returns the current context after the policy described in the in the interface {@link X4SModeSwitcher}
*/
@Override
public LogoContext getContext()
{
return currentContext;
}
// Context switch : internal communication : no direct gui update, must not run on event dispatcher thread
private final ArrayList<ContextSwitchListener> contextSwitchListeners = new ArrayList<ContextSwitcher.ContextSwitchListener>();
@Override
public void addContextSwitchListener(ContextSwitchListener listener)
{
contextSwitchListeners.add(listener);
}
@Override
public void removeContextSwitchListener(ContextSwitchListener listener)
{
contextSwitchListeners.remove(listener);
}
public void notifyContextSwitched(LogoContext newContext)
{
for (ContextSwitchListener listener : contextSwitchListeners)
listener.contextSwitched(newContext);
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* MODE SWITCHER
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
@Override
public String getSerializedContext()
{
return currentContext.toString();
}
@Override
public boolean isUserMode()
{
return currentContext instanceof UserContext;
}
@Override
public boolean isRecordMode()
{
return currentContext instanceof RecordContext;
}
@Override
public boolean isNetworkMode()
{
return currentContext instanceof NetworkContext;
}
/**
* A record mode can only be started from user mode. In other words,
* a record mode cannot be started, if there is another <b>record or network</b> mode active.
* @throws IllegalStateException If current mode is not user mode.
* @throws IllegalArgumentException If the fileNames are not well formed, null, or ambiguous
* @throws IOException If the record files could not be created => recording is impossible => record mode is impossible
*/
@Override
public void startRecordMode(String[] fileNames) throws IllegalArgumentException, IOException
{
if (!isUserMode())
throw new IllegalStateException();
recordContext = new RecordContext(fileNames);
recordContext.addBroadcastListener(contextMessageListener);
contextSwitch();
notifyContestModeStarted();
}
/**
* This will first regularly kill all network contexts, one after the other. Listeners will be notified for every kill. <br>
* Afterwards it will kill the current record context. <br>
* When this call returns, current mode is user mode.
*
* @throws IllegalStateException If there is no recordContext available
*/
@Override
public void stopRecordMode() throws IllegalStateException
{
if (recordContext == null)
return; // might be a quick double klick that causes this
while(isNetworkMode())
popNetworkMode();
recordContext = null;
contextSwitch();
notifyRecordModeStopped();
}
/**
* @throws IllegalArgumentException If serializedContext is corrupted
*/
@Override
public void pushNetworkMode(String serializedContext) throws IllegalArgumentException
{
NetworkContext nc = new NetworkContext(serializedContext);
networkStack.push(nc);
contextSwitch();
if (networkStack.size() == 1)
notifyNetworkModeStarted();
}
/**
* This will kill the current network context.
* @throws IllegalStateException - if there is no network context to kill.
*/
@Override
public void popNetworkMode() throws IllegalStateException
{
if (!isNetworkMode())
throw new IllegalStateException("There is no network context to kill.");
networkStack.pop();
contextSwitch();
if (!isNetworkMode())
notifyNetworkModeStopped();
}
// Mode Change Listeners : update GUI => run on event dispatcher thread
private ArrayList<ModeChangeListener> modeChangeListeners = new ArrayList<ModeChangeListener>();
@Override
public void addModeChangedListener(ModeChangeListener listener)
{
modeChangeListeners.add(listener);
}
@Override
public void removeModeChangedListener(ModeChangeListener listener)
{
modeChangeListeners.remove(listener);
}
private void notifyContestModeStarted()
{
for (ModeChangeListener listener : modeChangeListeners)
listener.recordModeStarted();
}
private void notifyRecordModeStopped()
{
for (ModeChangeListener listener : modeChangeListeners)
listener.recordModeStopped();
}
private void notifyNetworkModeStarted()
{
for (ModeChangeListener listener : modeChangeListeners)
listener.networkModeStarted();
}
private void notifyNetworkModeStopped()
{
for (ModeChangeListener listener : modeChangeListeners)
listener.networkModeStopped();
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* CONTEXT MESSAGE LISTENERS (used to broadcast clock events from record mode)
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
private void initContextMessageListener()
{
contextMessageListener = new MessageListener(){
@Override
public void messageEvent(String source, String message)
{
for (MessageListener listener : broadcastListeners)
listener.messageEvent(source, message);
}
};
}
private final ArrayList<MessageListener> broadcastListeners = new ArrayList<MessageListener>();
@Override
public void addBroadcastListener(MessageListener listener)
{
broadcastListeners.add(listener);
}
@Override
public void removeBroadcastListener(MessageListener listener)
{
broadcastListeners.remove(listener);
}
}
|