Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ea6a47d6c45924e54fae664675160ceb5a34522f (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
/*****************************************************************************
 * Copyright (c) 2015 Christian W. Damus and others.
 * 
 * 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:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.infra.gmfdiag.common.sync.tests;

import static org.junit.Assert.fail;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.ui.editparts.DiagramEditPart;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.gmfdiag.canonical.strategy.ISemanticChildrenStrategy;
import org.eclipse.papyrus.infra.gmfdiag.common.sync.ContainerChildrenSyncFeature;
import org.eclipse.papyrus.infra.gmfdiag.common.sync.DiagramEdgesSyncFeature;
import org.eclipse.papyrus.infra.gmfdiag.common.sync.EditPartMasterSlaveSyncBucket;
import org.eclipse.papyrus.infra.gmfdiag.common.sync.EditPartSyncRegistry;
import org.eclipse.papyrus.infra.gmfdiag.common.sync.NodePositionSyncFeature;
import org.eclipse.papyrus.infra.gmfdiag.common.sync.NodeSizeSyncFeature;
import org.eclipse.papyrus.infra.gmfdiag.common.sync.SyncStyles;
import org.eclipse.papyrus.infra.sync.SyncBucket;
import org.eclipse.papyrus.infra.sync.SyncItem;
import org.eclipse.papyrus.infra.sync.SyncRegistry;
import org.eclipse.papyrus.infra.sync.service.ISyncAction;
import org.eclipse.papyrus.infra.sync.service.ISyncService;
import org.eclipse.papyrus.infra.sync.service.ISyncTrigger;
import org.eclipse.papyrus.junit.utils.rules.PapyrusEditorFixture;
import org.eclipse.papyrus.uml.diagram.common.canonical.DefaultUMLSemanticChildrenStrategy;
import org.eclipse.uml2.uml.Association;
import org.eclipse.uml2.uml.Package;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

/**
 * A container of synchronization configurations for the test suite.
 */
public class TestSyncFixture extends TestWatcher {
	private final PapyrusEditorFixture editor;

	private static PapyrusEditorFixture current;

	private TestSyncFixture(PapyrusEditorFixture editor) {
		super();

		this.editor = editor;
	}

	static TestRule compose(PapyrusEditorFixture editor) {
		return RuleChain.outerRule(new TestSyncFixture(editor)).around(editor);
	}

	@Override
	protected void starting(Description description) {
		current = editor;
	}

	@Override
	protected void finished(Description description) {
		current = null;
	}

	public static class TestClassDiagramSyncTrigger implements ISyncTrigger {
		public TestClassDiagramSyncTrigger() {
			super();
		}

		public boolean isTriggeredOn(Object object) {
			boolean result = false;

			if ((current != null) && (object instanceof DiagramEditPart)) {
				DiagramEditPart diagramEP = (DiagramEditPart) object;
				Diagram diagram = (Diagram) diagramEP.getModel();
				if (diagram.getElement() instanceof org.eclipse.uml2.uml.Package) {
					Resource resource = diagram.eResource();
					result = resource.getResourceSet() == current.getResourceSet();
				}
			}

			return result;
		}

		public ISyncAction trigger(ISyncService syncService, Object object) {
			return new ISyncAction() {

				public IStatus perform(ISyncService syncService, Object object) {
					DiagramEditPart diagram = (DiagramEditPart) object;
					org.eclipse.uml2.uml.Package package_ = (org.eclipse.uml2.uml.Package) EMFHelper.getEObject(diagram);

					TestDiagramSyncRegistry registry = syncService.getSyncRegistry(TestDiagramSyncRegistry.class);

					switch (SyncStyles.getSyncKind((View) diagram.getModel())) {
					case MASTER:
						SyncBucket<org.eclipse.uml2.uml.Package, EditPart, Notification> bucket = registry.getBucket(package_);
						if (bucket == null) {
							bucket = new TestPackageSyncBucket(package_, diagram);
							registry.register(bucket);
						}
						break;
					case SLAVE:
						registry.synchronize(diagram);
						break;
					case NONE:
						// Pass
						break;
					default:
						fail("Unsupported synchronization kind in test model");
					}
					return null;
				}
			};
		}
	}

	public static class TestDiagramSyncRegistry extends EditPartSyncRegistry<org.eclipse.uml2.uml.Package, EditPart> {
		public TestDiagramSyncRegistry() {
			super();
		}
	}

	static class TestPackageSyncBucket extends EditPartMasterSlaveSyncBucket<org.eclipse.uml2.uml.Package, EditPart> {
		public TestPackageSyncBucket(org.eclipse.uml2.uml.Package model, DiagramEditPart master) {
			super(model, master);

			add(new ClassNodesSyncFeature(this));
			add(new AssociationEdgesSyncFeature(this));
		}
	}

	static class ClassNodesSyncFeature extends ContainerChildrenSyncFeature<org.eclipse.uml2.uml.Package, org.eclipse.uml2.uml.Class, EditPart> {
		public ClassNodesSyncFeature(SyncBucket<Package, EditPart, Notification> bucket) {
			super(bucket);
		}

		@Override
		protected Class<? extends SyncRegistry<org.eclipse.uml2.uml.Class, EditPart, Notification>> getNestedSyncRegistryType() {
			return ClassNodeSyncRegistry.class;
		}

		@Override
		protected Command onTargetAdded(SyncItem<Package, EditPart> from, EObject source, SyncItem<Package, EditPart> to, final EditPart target) {
			org.eclipse.uml2.uml.Class class_ = (org.eclipse.uml2.uml.Class) source;

			final SyncRegistry<org.eclipse.uml2.uml.Class, EditPart, Notification> registry = getNestedSyncRegistry();
			SyncBucket<org.eclipse.uml2.uml.Class, EditPart, Notification> bucket = registry.getBucket(class_);
			if (bucket == null) {
				EditPart master = findChild(from.getBackend(), source);
				bucket = new ClassNodeSyncBucket(class_, master);
				registry.register(bucket);
			}

			return synchronizingWrapper(registry, target, null);
		}

		@Override
		protected Command onTargetRemoved(SyncItem<Package, EditPart> to, EditPart target) {
			SyncRegistry<org.eclipse.uml2.uml.Class, EditPart, Notification> registry = getNestedSyncRegistry();

			// De-synchronize this edit-part
			registry.desynchronize(target);

			return null;
		}
	}

	public static class ClassNodeSyncRegistry extends EditPartSyncRegistry<org.eclipse.uml2.uml.Class, EditPart> {
		public ClassNodeSyncRegistry() {
			super();
		}
	}

	static class ClassNodeSyncBucket extends EditPartMasterSlaveSyncBucket<org.eclipse.uml2.uml.Class, EditPart> {
		public ClassNodeSyncBucket(org.eclipse.uml2.uml.Class model, EditPart master) {
			super(model, master);

			add(new NodePositionSyncFeature<org.eclipse.uml2.uml.Class, EditPart>(this));
			add(new NodeSizeSyncFeature<org.eclipse.uml2.uml.Class, EditPart>(this));
		}
	}

	static class AssociationEdgesSyncFeature extends DiagramEdgesSyncFeature<org.eclipse.uml2.uml.Package, Association, EditPart> {
		private final ISemanticChildrenStrategy semanticChildren = new DefaultUMLSemanticChildrenStrategy();

		public AssociationEdgesSyncFeature(SyncBucket<Package, EditPart, Notification> bucket) {
			super(bucket);
		}

		@Override
		protected Class<? extends SyncRegistry<Association, EditPart, Notification>> getNestedSyncRegistryType() {
			return AssociationEdgeSyncRegistry.class;
		}

		@Override
		protected EObject getSourceElement(EObject connectionElement) {
			// I am only invoked on associations, so this cast is OK
			return (EObject) semanticChildren.getSource(connectionElement);
		}

		@Override
		protected EObject getTargetElement(EObject connectionElement) {
			// I am only invoked on associations, so this cast is OK
			return (EObject) semanticChildren.getTarget(connectionElement);
		}
	}

	public static class AssociationEdgeSyncRegistry extends EditPartSyncRegistry<Association, EditPart> {
		public AssociationEdgeSyncRegistry() {
			super();
		}
	}

}

Back to the top