Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ef59e705a973b60f84680a3b8c56ed86a1d2b768 (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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
/*******************************************************************************
 * 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 java.util.List;

import org.eclipse.emf.common.notify.Notification;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.layers.stackmodel.BadStateException;
import org.eclipse.papyrus.layers.stackmodel.LayersException;
import org.eclipse.papyrus.layers.stackmodel.NotFoundException;
import org.eclipse.papyrus.layers.stackmodel.command.ComputePropertyValueCommand;
import org.eclipse.papyrus.layers.stackmodel.layers.AbstractLayer;
import org.eclipse.papyrus.layers.stackmodel.layers.LayersPackage;
import org.eclipse.papyrus.layers.stackmodel.layers.LayersStack;
import org.eclipse.papyrus.layers.stackmodel.layers.LayersStackApplication;
import org.eclipse.papyrus.layers.stackmodel.layers.Property;
import org.eclipse.papyrus.layers.stackmodel.layers.PropertySetter;
import org.eclipse.papyrus.layers.stackmodel.notifier.DiagramViewEventNotifier;
import org.eclipse.papyrus.layers.stackmodel.notifier.IDiagramViewEventListener;
import static org.eclipse.papyrus.layers.runtime.Activator.log;


/**
 * This class is used to synchronize the diagram's properties when something
 * change in the LayerStack.
 * It listen on events from {@link LayersStack} and {@link Diagram}.
 *  
 * @author cedric dumoulin
 *
 */
public class LayerStackSynchronizer implements IDiagramViewEventListener, ILayersModelEventListener {

	/**
	 * The observed diagram.
	 */
	protected Diagram diagram;
	
	/**
	 * The observed LayerStack.
	 */
	protected LayersStack layersStack;
	
	/**
	 * The application object. Required by some methods.
	 * <br>
	 * Method requiring this object must check it before any use, 
	 * as this object can be null.
	 * 
	 */
	protected LayersStackApplication application;
	
	/**
	 * The notifier of layer model events.
	 */
	protected LayersModelEventNotifier layersModelEventNotifier;
	
	/**
	 * The notifier of layer model events.
	 */
	protected DiagramViewEventNotifier diagramViewEventNotifier;
	
	/**
	 * Constructor.
	 *
	 * @param layersStack The layerStack to listen to. It should have an associated diagram.
	 * 
	 */
	public LayerStackSynchronizer(LayersStack layersStack) {
		this.diagram = layersStack.getDiagram();
		this.layersStack = layersStack;
		
		// try to get the application object
		// it is the container of the layerStack
		application = (LayersStackApplication)layersStack.eContainer();
		
		
		activate();
//		System.err.println("LayerStackSynchronizer( " +diagram.getName()+ " ) started");
	}


	
	/**
	 * @return the diagram
	 */
	public Diagram getDiagram() {
		return diagram;
	}


	
	/**
	 * @return the layersStack
	 */
	public LayersStack getLayersStack() {
		return layersStack;
	}



	/**
	 * Activate the listeners.
	 * 
	 */
	protected void activate() {
		
		diagramViewEventNotifier = new DiagramViewEventNotifier(diagram);
		diagramViewEventNotifier.addEventListener(this);
		
		layersModelEventNotifier = new LayersModelEventNotifier(layersStack);
		layersModelEventNotifier.addLayersModelEventListener(this);
		
	}

	/**
	 * Deactivate listeners
	 */
	protected void deactivate() {
		diagramViewEventNotifier.removeEventListener(this);
		diagramViewEventNotifier = null;
		
		layersModelEventNotifier.removeLayersModelEventListener(this);
		layersModelEventNotifier = null;
	}
	
	/**
	 * Dispose the synchronizer
	 */
	protected void dispose() {
		// Deactivate listeners
		deactivate();
		diagramViewEventNotifier = null;
		layersModelEventNotifier = null;
	}
	
	/**
	 * Return true if the object is disposed.
	 * @return
	 */
	protected boolean isDisposed() {
		return diagramViewEventNotifier == null;
	}
	
	/**
	 * Check if the application object is not null.
	 * Throw an exception if the application object is null.
	 * <br>
	 * In normal use, the Application is the container of the LayerStack.
	 * It is set in the constructor of this class.
	 * 
	 * @throws BadStateException
	 */
	protected void checkApplication() throws BadStateException {
		if( application == null) {
			throw new BadStateException("Attempt to call a method requireing the 'application object, but the Application object is not set. You must provide a LayerStack contained in its Application.");
		}
	}
	/**
	 * 
	 * @see org.eclipse.papyrus.layers.runtime.ILayersModelEventListener#propertyValueAdded(org.eclipse.emf.common.notify.Notification)
	 *
	 * @param notification
	 */
	@Override
	public void propertyValueAdded(Notification notification) {
		
		if(log.isDebugEnabled()) {
			log.debug(this.getClass().getSimpleName() + ".propertyValueAdded " + notification.getNewValue());
		}

		PropertySetter setter = null;
		try {
			// Name of the property
			String propertyName = LayersModelEventUtils.PropertyEvents.getPropertyNameFromValueAdd(notification);

			// Need to recompute the associated views
			AbstractLayer layer = LayersModelEventUtils.PropertyEvents.getAbstractLayer(notification);
			List<View> views = layer.getViews();
			if( views.size() == 0) {
				return;
			}
			
			checkApplication();
			Property property  = application.getPropertyRegistry().getProperty(propertyName);
			
			List<ComputePropertyValueCommand> commands = layersStack.getViewsComputePropertyValueCommand(views, property);
			
			setter = application.getPropertySetterRegistry().getPropertySetter(property);
			
			// Walk each view and set the property
			for( int i=0; i<views.size(); i++) {
				
				// set the value from the provided cmds.
				// Do it if the cmd is not null
				if( commands != null && commands.get(i) != null ) {
				  setter.setValue(views.get(i), commands.get(i).getCmdValue() );
				} 
				else {
					log.info(this.getClass().getSimpleName() + "ERROR - a cmd is null " + commands);
				}
			}
		} catch (NotFoundException e) {
			log.error(e);
		}	catch (LayersException e) {
			log.error(e);
		}	catch (UnsupportedOperationException e) {
			// A setter or getter is not implemented
			throw new UnsupportedOperationException("setter='" + setter.getClass().getName()+"'", e);
		}		
	}



	@Override
	public void propertyValueRemoved(Notification notification) {
		if(log.isDebugEnabled()) {
			log.debug(this.getClass().getSimpleName() + ".propertyValueRemoved " + notification.getOldValue());
		}

		try {
			// Name of the property
			String propertyName = LayersModelEventUtils.PropertyEvents.getPropertyNameFromValueRemove(notification);

			// Need to recompute the associated views
			AbstractLayer layer = LayersModelEventUtils.PropertyEvents.getAbstractLayer(notification);
			List<View> views = layer.getViews();
			if( views.size() == 0) {
				return;
			}

			checkApplication();
			Property property  = application.getPropertyRegistry().getProperty(propertyName);

			List<ComputePropertyValueCommand> commands = layersStack.getViewsComputePropertyValueCommand(views, property);
			if( commands == null) {
				// no command
				return;
			}

			PropertySetter setter = application.getPropertySetterRegistry().getPropertySetter(property);

			// Walk each view and set the property
			for( int i=0; i<views.size(); i++) {
				// A command can be null
				ComputePropertyValueCommand cmd = commands.get(i).getCmdValue();
				if( cmd != null ) {
					setter.setValue(views.get(i), cmd.getCmdValue() );
				}

			}
		} catch (NotFoundException e) {
			log.error(e);
		} catch (LayersException e) {
			log.error(e);
		}		
	}



	@Override
	public void propertyValueChanged(Notification notification) {
		if(log.isDebugEnabled()) {
			log.debug(this.getClass().getSimpleName() + ".propertyValueChanged " + notification.getNewValue());
		}

		// If LayerExpression::IsLayerEnabled() is changed, treat it separately.
		if(notification.getFeature() == LayersPackage.eINSTANCE.getLayerExpression_IsLayerEnabled()) {
			propertyValueChangedIsLayerEnabled(notification);
			return;
		}
		
		// Here, this should be a regular property that is modified
		try {
			// Name of the property
			String propertyName = LayersModelEventUtils.PropertyEvents.getPropertyNameFromValueSet(notification);

			// Need to recompute the associated views
			AbstractLayer layer = LayersModelEventUtils.PropertyEvents.getAbstractLayer(notification);
			List<View> views = layer.getViews();
			if( views.size() == 0) {
				return;
			}
			
			checkApplication();
			Property property  = application.getPropertyRegistry().getProperty(propertyName);
			
			List<ComputePropertyValueCommand> commands = layersStack.getViewsComputePropertyValueCommand(views, property);
			if(commands == null) {
				return;
			}
			
			PropertySetter setter = application.getPropertySetterRegistry().getPropertySetter(property);
			
			// Walk each view and set the property
			for( int i=0; i<views.size(); i++) {
				ComputePropertyValueCommand getValueCmd = commands.get(i);
				if(getValueCmd != null) {
					setter.setValue(views.get(i), getValueCmd.getCmdValue() );
				}
			}
		} catch (NotFoundException e) {
			log.error(e);
		} catch (LayersException e) {
			log.error(e);
		}
	}



	/**
	 * The LayerExpression::IsLayerEnabled() is changed. Refresh views and properties of the layer.
	 * 
	 * 
	 * @param notification
	 */
	private void propertyValueChangedIsLayerEnabled(Notification notification) {
		
		if(log.isDebugEnabled()) {
			log.debug(this.getClass().getSimpleName() + ".propertyValueChangedIsLayerEnabled " + notification.getNewValue());
			
		}

		try {
			
			checkApplication();
			
			// Extract the affected layer
//			AbstractLayer layer = LayersModelEventUtils.PropertyEvents.getAbstractLayer(notification);
			AbstractLayer layer = (AbstractLayer)notification.getNotifier();
			
			recomputePropertiesForAllViewsOf(layer);
		} catch (NotFoundException e) {
			log.error(e);
		} catch (LayersException e) {
			log.error(e);
		}
	}



	/**
	 * Recompute the specified properties for the specified views.
	 * 
	 * @param views
	 * @param properties
	 * @throws LayersException 
	 */
	private void recompute(List<View> views, List<Property> properties) throws LayersException {

		// Iterate on views
		for( View view : views) {
			recompute( view, properties);
		}
		
	}



	/**
	 * Recompute the specified properties for the specified view.
	 * 
	 * @param view
	 * @param properties
	 * @throws LayersException 
	 */
	private void recompute(View view, List<Property> properties) throws LayersException {

		List<ComputePropertyValueCommand> commands = layersStack.getPropertiesComputePropertyValueCommand(view, properties);
		if(commands == null) {
			// No property to set
			return;
		}

		PropertySetter setter;
		// Walk each cmd and set the property
		for( int i=0; i<commands.size(); i++) {
			try {
				Property property = properties.get(i);
				setter = application.getPropertySetterRegistry().getPropertySetter(property);
				// Set the value only if we have a getter command.
				ComputePropertyValueCommand getterCmd = commands.get(i);
				if(getterCmd != null) {
					setter.setValue(view, getterCmd.getCmdValue() );
				}

			} catch (NotFoundException e) {
				// No setter found
				log.error(e);
			} catch (NullPointerException e) {
				// A command is null
			}
		}
		
	}



	@Override
	public void layerAdded(Notification notification) {
		if(log.isDebugEnabled()) {
			log.debug(this.getClass().getSimpleName() + ".layerAdded() " + notification.getNewValue());
		}

		if( ! (notification.getNewValue() instanceof AbstractLayer) ) {
			log.info( "TODO: " + this.getClass().getSimpleName() + ".layerAdded() " + notification.getOldValue() + " - recompute for LayerOperator not implemented yet.");;
            return;
		}

		// Extract the affected layer
		AbstractLayer layer = (AbstractLayer)notification.getNewValue();

		try {
			checkApplication();
			recomputePropertiesForAllViewsOf(layer);
		} catch (LayersException e) {
			log.error(e);
		}

	}



	@Override
	public void layerRemoved(Notification notification) {
		if(log.isDebugEnabled()) {
			log.debug(this.getClass().getSimpleName() + " layerRemoved() " + notification.getOldValue());
		}

		if( ! (notification.getOldValue() instanceof AbstractLayer) ) {
			log.info( "TODO: " + this.getClass().getSimpleName() + ".layerRemoved() " + notification.getOldValue() + " - recompute() after removing layerOperator not implemented yet.");;
            return;
		}
		// Extract the affected layer
		AbstractLayer layer = (AbstractLayer)notification.getOldValue();

		try {
			checkApplication();
			recomputePropertiesForAllViewsOf(layer);
		} catch (LayersException e) {
			log.error(e);
		}

	}



	/**
	 * Recompute the properties of the views attached to the specified layers.
	 * 
	 * @param layer
	 * @throws LayersException
	 */
	private void recomputePropertiesForAllViewsOf(AbstractLayer layer) throws LayersException {
		// We need the list of affected properties
		List<Property> properties  = layer.getAttachedProperties();

		// We need the list of affected Views.
		List<View> views = layer.getViews();
		if( views.size() == 0) {
			return;
		}
		
		// For each view, recompute the Properties
		recompute( views, properties );
	}



	/**
	 * 
	 * @see org.eclipse.papyrus.layers.runtime.ILayersModelEventListener#layerMoved(org.eclipse.emf.common.notify.Notification)
	 *
	 * @param notification
	 */
	@Override
	public void layerMoved(Notification notification) {
		if(log.isDebugEnabled()) {
			log.debug(this.getClass().getSimpleName() + " layerMoved(not tested) " + notification.getNewValue());
		}

		// Extract the affected layer
		AbstractLayer layer = (AbstractLayer)notification.getNewValue();

		try {
			checkApplication();
			recomputePropertiesForAllViewsOf(layer);
		} catch (LayersException e) {
			log.error(e);
		}

	}

	/**
	 * 
	 * @see org.eclipse.papyrus.layers.runtime.ILayersModelEventListener#layerSet(org.eclipse.emf.common.notify.Notification)
	 *
	 * @param notification
	 */
	@Override
	public void layerSet(Notification notification) {
		log.info(this.getClass().getSimpleName() + " layerSet(not implemented) " + notification.getNewValue());

	}



	/**
	 * 
	 * @see org.eclipse.papyrus.layers.runtime.ILayersModelEventListener#viewAddedToLayer(org.eclipse.emf.common.notify.Notification)
	 *
	 * @param notification
	 */
	@Override
	public void viewAddedToLayer(Notification notification) {
		
		if(log.isDebugEnabled()) {
			log.debug(this.getClass().getSimpleName() + " viewAddedToLayer( " + notification.getNewValue() + " )");
		}

		// We need to find the view, the layer in which it is added,
		// and the properties attached to this layer.
		// Then, we compute this property and set it to the view.
		try {			
			AbstractLayer layer = LayersModelEventUtils.PropertyEvents.getAbstractLayer(notification);
			View view = LayersModelEventUtils.ViewEvents.getViewAdded(notification);
			
			
			checkApplication();
			List<Property> properties  = layer.getAttachedProperties();
			
			List<ComputePropertyValueCommand> commands = layersStack.getPropertiesComputePropertyValueCommand(view, properties);
			if(commands == null) {
				// No property to set
				return;
			}
			
			PropertySetter setter;
			// Walk each cmd and set the property
			for( int i=0; i<commands.size(); i++) {
				try {
					Property property = properties.get(i);
					setter = application.getPropertySetterRegistry().getPropertySetter(property);
					setter.setValue(view, commands.get(i).getCmdValue() );
				} catch (NotFoundException e) {
					// No setter found
					log.error("No setter found", e);
				} catch (NullPointerException e) {
					// A command is null
				}
			}
		} catch (LayersException e) {
			log.error(e);
		}
	}


	@Override
	public void multiViewsAddedToLayer(Notification notification) {
		
		if(log.isDebugEnabled()) {
			log.debug(this.getClass().getSimpleName() + ".multiViewsAddedToLayer( " + notification.getNewValue() + " )");
		}

		// We need to find the view, the layer in which it is added,
		// and the properties attached to this layer.
		// Then, we compute this property and set it to the view.
		try {
			AbstractLayer layer = LayersModelEventUtils.PropertyEvents.getAbstractLayer(notification);
			List<View> views = LayersModelEventUtils.ViewEvents.getViewsAdded(notification);
			
			checkApplication();
			List<Property> properties  = layer.getAttachedProperties();
			recompute(views, properties);
			
		} catch (LayersException e) {
			log.error(e);
		}
		
	}



	/**
	 * 
	 * @see org.eclipse.papyrus.layers.runtime.ILayersModelEventListener#viewRemovedFromLayer(org.eclipse.emf.common.notify.Notification)
	 *
	 * @param notification
	 */
	@Override
	public void viewRemovedFromLayer(Notification notification) {
		if(log.isDebugEnabled()) {
			log.debug(this.getClass().getSimpleName() + " viewRemovedFromLayer( " + notification.getOldValue() + " )");
		}
		
		// We need to find the view, the layer in which it is added,
		// and the properties attached to this layer.
		// Then, we compute this property and set it to the view.
		try {
			AbstractLayer layer = LayersModelEventUtils.PropertyEvents.getAbstractLayer(notification);
			View view = LayersModelEventUtils.ViewEvents.getViewRemoved(notification);
			
			checkApplication();
			List<Property> properties  = layer.getAttachedProperties();
			
			List<ComputePropertyValueCommand> commands = layersStack.getPropertiesComputePropertyValueCommand(view, properties);
			if(commands == null) {
				// No property to set
				return;
			}
			
			PropertySetter setter;
			// Walk each cmd and set the property
			for( int i=0; i<commands.size(); i++) {
				try {
					Property property = properties.get(i);
					setter = application.getPropertySetterRegistry().getPropertySetter(property);
					setter.setValue(view, commands.get(i).getCmdValue() );
				} catch (NotFoundException e) {
					// No setter found
					log.error("No setter found", e);
				} catch (NullPointerException e) {
					// A command is null
				}
			}
		} catch (LayersException e) {
			// TODO Auto-generated catch block
			log.error(e);
		}
	}

	@Override
	public void multiViewsRemovedFromLayer(Notification notification) {
		
		if(log.isDebugEnabled()) {
			log.debug(this.getClass().getSimpleName() + " multiViewsRemovedFromLayer( " + notification.getOldValue() + " )");
		}
		
		// We need to find the view, the layer in which it is added,
		// and the properties attached to this layer.
		// Then, we compute this property and set it to the view.
		try {
			AbstractLayer layer = LayersModelEventUtils.PropertyEvents.getAbstractLayer(notification);
			List<View> views = LayersModelEventUtils.ViewEvents.getViewsRemoved(notification);
			
			checkApplication();
			List<Property> properties  = layer.getAttachedProperties();
			recompute(views, properties);
			
		} catch (LayersException e) {
			log.error(e);
		}
		
	}




	/**
	 * 
	 * @see org.eclipse.papyrus.layers.runtime.ILayersModelEventListener#viewMovedBetweenLayer(org.eclipse.emf.common.notify.Notification)
	 *
	 * @param notification
	 */
	@Override
	public void viewMovedBetweenLayer(Notification notification) {
		log.info(this.getClass().getSimpleName() + " viewMovedBetweenLayer(not implemented) " + notification.getNewValue());

	}



	/**
	 * 
	 * @see org.eclipse.papyrus.layers.stackmodel.notifier.IDiagramViewEventListener#diagramViewAdded(org.eclipse.emf.common.notify.Notification)
	 *
	 * @param msg
	 */
	@Override
	public void diagramViewAdded(Notification msg) {
		log.info(this.getClass().getSimpleName() + " diagramViewAdded(not implemented) " + msg.getNewValue());

		// WARNING !!!
		// Some filtering should be done on the event. 
		// When a View is removed, the event is sent for each of its parts. This
		// result in multiple events receiving.
		// Only events on Shape and Links are useful. So, we need to 
		// do some filtering (in the DiagramViewEventNotifier ?).
	}



	/**
	 * 
	 * @see org.eclipse.papyrus.layers.stackmodel.notifier.IDiagramViewEventListener#diagramViewRemoved(org.eclipse.emf.common.notify.Notification)
	 *
	 * @param msg
	 */
	@Override
	public void diagramViewRemoved(Notification msg) {
		log.info(this.getClass().getSimpleName() + " diagramViewRemoved(not implemented) " + msg.getOldValue());

		// WARNING !!!
		// Some filtering should be done on the event. 
		// When a View is removed, the event is sent for each of its parts. This
		// result in multiple events receiving.
		// Only events on Shape and Links are useful. So, we need to 
		// do some filtering (in the DiagramViewEventNotifier ?).
	}





}

Back to the top