Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b406f9544597b07fb2cd4bf83b9965d9bd3564fb (plain) (blame)
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
/*******************************************************************************
 * Copyright (c) 2013 CEA LIST.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Cedric Dumoulin - cedric.dumoulin@lifl.fr
 ******************************************************************************/
package org.eclipse.papyrus.layers.runtime;

import static org.eclipse.papyrus.layers.runtime.Activator.log;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.papyrus.infra.gmfdiag.common.model.NotationModel;
import org.eclipse.papyrus.layers.runtime.model.ILayersModelRootEventListener;
import org.eclipse.papyrus.layers.runtime.model.LayersModel;
import org.eclipse.papyrus.layers.runtime.model.LayersModelEventRootNotifier;
import org.eclipse.papyrus.layers.stackmodel.layers.LayersStack;
import org.eclipse.papyrus.layers.stackmodel.layers.LayersStackApplication;


/**
 *
 * Listen to:
 * - LayerStack added
 * - application added/removed in LayerModel
 *
 * <br>
 * The class has an internal state machine with two states: ApplicationExist and NoApplication. <br>
 * In the ApplicationExist state, the class listen to {@link LayersStack} added/removed and to the Diagrams deletion. <br>
 * In the NoApplication state, the class listen on the LayerModel to be informed when the application is created.
 * When application is created, we change the state to ApplicationExist.
 *
 * There is an UML sm diagram of this in architecture.di.
 *
 *
 *
 * @author cedric dumoulin
 *
 */
public class LayerStackApplicationSynchronizer implements ILayersModelRootEventListener, ILayersStackApplicationEventListener, INotationDiagramRemovedEventListener {

	/**
	 * States used in the state machine.
	 */
	protected enum State {
		NoApplication, ApplicationCreated, disposed
	};

	protected State state;

	protected LayersModel layersModel;
	protected NotationModel notationModel;

	/**
	 * All registered synchronizers.
	 */
	protected Map<Diagram, LayerStackSynchronizer> synchronizers = new HashMap<Diagram, LayerStackSynchronizer>();

	/**
	 * Notifier on Diagram removed event.
	 */
	protected NotationDiagramRemovedFromResourceEventNotifier notationDiagramRemovedNotifier;

	/**
	 * Notifier on LayerSTackApplication events
	 */
	protected LayersStackApplicationEventNotifier layersStackApplicationEventNotifier;

	/**
	 * Notifier on {@link LayersModel} events.
	 */
	protected LayersModelEventRootNotifier layersModelEventRootNotifier;

	/**
	 * Constructor.
	 *
	 */
	public LayerStackApplicationSynchronizer(LayersModel layersModel, NotationModel notationModel) {

		this.layersModel = layersModel;
		this.notationModel = notationModel;

		initState();
	}

	/**
	 * Init the state of the object
	 */
	private void initState() {

		if (layersModel.lookupLayerStackApplication() != null) {
			transitionInitToApplicationCreatedState();
		}
		else {
			transitionInitToNoApplicationState();
		}
	}

	/**
	 * Initialize the class for the NoApplication state:
	 * - listen on LayersModel notifiers for application creation
	 */
	private void transitionInitToApplicationCreatedState() {
		activateLayersModelAndDiagramDeletionNotifiers();
		activateLayersModelRootNotifier();
		createSynchronizerForExistingStacks();
		state = State.ApplicationCreated;
	}

	/**
	 * Initialize the class for the NoApplication state:
	 * - listen on LayersModel notifiers for application creation
	 */
	private void transitionNoApplicationToApplicationCreatedState() {
		activateLayersModelAndDiagramDeletionNotifiers();
		activateLayersModelRootNotifier();
		state = State.ApplicationCreated;
	}

	/**
	 * Initialize the class for the ApplicationCreated state:
	 * - listen on appropriate notifiers
	 * - check if some LayerStack are already there.
	 */
	private void transitionInitToNoApplicationState() {
		activateLayersModelRootNotifier();
		state = State.NoApplication;
	}

	/**
	 * Initialize the class for the ApplicationCreated state:
	 * - listen on appropriate notifiers
	 * - check if some LayerStack are already there.
	 */
	private void transitionApplicationCreatedToNoApplicationState() {
		deactivateLayersModelAndDiagramDeletionNotifiers();
		state = State.NoApplication;
	}

	/**
	 * Create synchronizer for already created stack.
	 * The application object must exist.
	 */
	private void createSynchronizerForExistingStacks() {

		LayersStackApplication application = layersModel.lookupLayerStackApplication();
		if (application == null) {
			// Application is not set, skip.
			return;
		}

		for (LayersStack stack : application.getLayersStacks()) {
			if (stack.getDiagram() != null) {
				addLayerStackSynchronizer(stack);
			}
		}

	}



	/**
	 * @return the synchronizers
	 */
	public Map<Diagram, LayerStackSynchronizer> getSynchronizers() {
		return synchronizers;
	}


	/**
	 * @return the layersModel
	 */
	public LayersModel getLayersModel() {
		return layersModel;
	}


	/**
	 * @return the notationModel
	 */
	public NotationModel getNotationModel() {
		return notationModel;
	}

	/**
	 * Activate the listeners.
	 *
	 */
	protected void activate() {
		initState();
	}

	/**
	 * Activate the listeners.
	 *
	 */
	private void activateLayersModelRootNotifier() {
		/**
		 * Listen on layersModel to be inform when the application object is created.
		 * When it is created, we can listen on it.
		 */

		layersModelEventRootNotifier = new LayersModelEventRootNotifier(layersModel);
		layersModelEventRootNotifier.addEventListener(this);
	}


	/**
	 * Activate the listeners.
	 *
	 */
	private void deactivateLayersModelRootNotifier() {
		/**
		 * Listen on layersModel to be inform when the application object is created.
		 * When it is created, we can listen on it.
		 */
		if (layersModelEventRootNotifier != null) {
			layersModelEventRootNotifier.removeEventListener(this);
			layersModelEventRootNotifier = null;
		}
	}

	/**
	 * Deactivate the listeners.
	 *
	 */
	private void activateLayersModelAndDiagramDeletionNotifiers() {
		/**
		 * Listen on layersModel to be inform when the application object is created.
		 * When it is created, we can listen on it.
		 */
		notationDiagramRemovedNotifier = new NotationDiagramRemovedFromResourceEventNotifier(notationModel);
		notationDiagramRemovedNotifier.addLayersModelEventListener(this);

		layersStackApplicationEventNotifier = new LayersStackApplicationEventNotifier(layersModel);
		layersStackApplicationEventNotifier.addLayersModelEventListener(this);
	}

	/**
	 * Deactivate listeners
	 */
	protected void deactivateLayersModelAndDiagramDeletionNotifiers() {
		if (notationDiagramRemovedNotifier != null) {
			notationDiagramRemovedNotifier.removeLayersModelEventListener(this);
			notationDiagramRemovedNotifier = null;
		}

		if (layersStackApplicationEventNotifier != null) {
			layersStackApplicationEventNotifier.removeLayersModelEventListener(this);
			layersStackApplicationEventNotifier = null;
		}
	}

	/**
	 * Deactivate listeners
	 */
	protected void deactivate() {
		deactivateLayersModelAndDiagramDeletionNotifiers();
		deactivateLayersModelRootNotifier();
	}

	/**
	 * Dispose the synchronizer
	 */
	public void dispose() {
		// Deactivate listeners
		deactivate();
		layersModel = null;
		notationModel = null;
		state = State.disposed;
	}

	/**
	 * Return true if the object is disposed.
	 *
	 * @return
	 */
	protected boolean isDisposed() {
		return state == State.disposed;
	}

	/**
	 * Called by events when a {@link LayersStack} is added to the {@link LayersStackApplication}
	 *
	 * @param msg
	 */
	@Override
	public void layerStackAdded(Notification msg) {

		if (log.isDebugEnabled()) {
			log.debug("LayerStackApplicationSynchronizer.layerStackAdded : " + msg.getNewValue());
		}

		// Find the LayerStack
		LayersStack stack = LayersStackApplicationEventNotifier.getAddedLayerStack(msg);

		addLayerStackSynchronizer(stack);
	}


	/**
	 * Create and add a {@link LayerStackSynchronizer} for the stack.
	 * Do nothing if the stack has already a synchronizer.
	 *
	 * @param stack
	 */
	private void addLayerStackSynchronizer(LayersStack stack) {
		// Check if already exist
		if (lookupLayersStackSynchronizer(stack.getDiagram()) != null) {
			return;
		}

		// Create a new synchronizer
		LayerStackSynchronizer layerStackSynchronizer = new LayerStackSynchronizer(stack);
		putLayersStackSynchronizer(layerStackSynchronizer);
	}

	/**
	 * Lookup a {@link LayerStackSynchronizer} by its diagram.
	 *
	 * @param diagram
	 * @return
	 */
	protected LayerStackSynchronizer lookupLayersStackSynchronizer(Diagram diagram) {
		return synchronizers.get(diagram);
	}

	/**
	 * Lookup a {@link LayerStackSynchronizer} by its diagram.
	 *
	 * @param diagram
	 * @return
	 */
	protected void putLayersStackSynchronizer(LayerStackSynchronizer layerStackSynchronizer) {

		synchronizers.put(layerStackSynchronizer.getDiagram(), layerStackSynchronizer);
	}


	/**
	 * Called by events when a {@link LayersStack} is removed from the {@link LayersStackApplication}
	 *
	 * @param msg
	 */
	@Override
	public void layerStackRemoved(Notification msg) {
		if (log.isDebugEnabled()) {
			log.debug("LayerStack is removed" + msg.getOldValue());
		}

		// Find the LayerStack
		LayersStack stack = LayersStackApplicationEventNotifier.getRemovedLayerStack(msg);

		LayerStackSynchronizer synchronizer = synchronizers.remove(stack.getDiagram());
		synchronizer.dispose();

		// Also remove from application

	}

	/**
	 * Called by events when a {@link Diagram} is removed from the {@link NotationModel}
	 *
	 * @param msg
	 */
	@Override
	public void diagramRemoved(Notification msg) {

		if (log.isDebugEnabled()) {
			log.debug(this.getClass().getSimpleName() + ".diagramRemoved() - " + msg.getOldValue());
		}

		if (state != State.ApplicationCreated) {
			return;
		}

		// Find the diagram
		Diagram diagram = NotationDiagramRemovedFromResourceEventNotifier.getRemovedDiagram(msg);

		// Remove from application
		// This should in turn remove the LayerStackSynchronizer from
		// this class (LayerStackApplicationSynchronizer).
		getLayersModel().lookupLayerStackApplication().removeLayersStackFor(diagram);

		// check if really removed from this object
		LayerStackSynchronizer synchronizer = synchronizers.remove(diagram);
		if (synchronizer != null) {
			// Should not happen
			log.info(this.getClass().getSimpleName() + ".diagramRemoved() - synchronizer not completly cleaned. Finish the job.");
			synchronizer.dispose();
		}
	}


	@Override
	public void layersModelRootAdded(Notification msg) {
		if (state == State.NoApplication) {
			transitionNoApplicationToApplicationCreatedState();
		}

	}


	@Override
	public void layersModelRootRemoved(Notification msg) {
		if (state == State.ApplicationCreated) {
			transitionApplicationCreatedToNoApplicationState();
		}
	}
}

Back to the top