Skip to main content
summaryrefslogtreecommitdiffstats
blob: ba4b6ed3ae65228995d19761d3a9260b26b1b152 (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
/*****************************************************************************
 * 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:
 *  CEA LIST - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.moka.ui.presentation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.resources.IMarker;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.debug.core.model.IThread;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature.Setting;
import org.eclipse.emf.ecore.EValidator;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.RootEditPart;
import org.eclipse.gef.ui.parts.GraphicalEditor;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.core.Activator;
import org.eclipse.papyrus.infra.core.resource.ModelSet;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IPageManager;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.core.utils.ServiceUtils;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.services.markerlistener.IPapyrusMarker;
import org.eclipse.papyrus.infra.services.markerlistener.PapyrusMarkerAdapter;
import org.eclipse.papyrus.moka.MokaConstants;
import org.eclipse.papyrus.moka.debug.MokaBreakpoint;
import org.eclipse.papyrus.moka.launch.EditorUtils;
import org.eclipse.ui.IEditorPart;

/**
 * Some facilities to manage animation in diagrams.
 *
 */
public class AnimationUtils {

	/**
	 * The static instance for this class
	 */
	protected static AnimationUtils eInstance = null ;

	/**
	 * An EObject -> 'Suspended' marker map
	 */
	protected static Map<EObject, IPapyrusMarker> eObjectToSuspendedMarker = new HashMap<EObject, IPapyrusMarker>() ;

	/**
	 * An EObject -> 'Animation' marker map
	 */
	protected static Map<EObject, IPapyrusMarker> eObjectToAnimationMarker = new HashMap<EObject, IPapyrusMarker>() ;

	/**
	 * An EObject -> List<Diagram> map
	 */
	protected static Map<EObject, List<Diagram>> eObjectToDiagrams = new HashMap<EObject, List<Diagram>>() ;

	private AnimationUtils() {

	}

	public static void init() {
		// Reinitializes hash maps
		eObjectToAnimationMarker = new HashMap<EObject, IPapyrusMarker>() ;
		eObjectToSuspendedMarker = new HashMap<EObject, IPapyrusMarker>() ;
		eObjectToDiagrams = new HashMap<EObject, List<Diagram> >() ;
	}

	/**
	 * Returns an instance of AnimationUtils. Guarantees that AnimationUtils is instantiated only once.
	 * 
	 * @return An instance of AnimationUtils
	 */
	public static AnimationUtils getInstance() {
		if (eInstance == null)
			eInstance = new AnimationUtils() ;
		return eInstance ;
	}

	/**
	 * Retrieves all diagrams in which the given model element has a graphical representation
	 * 
	 * @param modelElement The model element for which diagrams where it appears have to be retrieved
	 * @return all diagrams in which the given model element has a graphical representation
	 */
	public List<Diagram> getDiagrams(EObject modelElement) {
		List<Diagram> matchingDiagrams = eObjectToDiagrams.get(modelElement) ;
		if (matchingDiagrams != null) {
			return matchingDiagrams ;
		}

		Resource resource = modelElement.eResource() ;
		if (! resource.getURI().isPlatformResource()) {
			matchingDiagrams = new ArrayList<Diagram>() ;
			eObjectToDiagrams.put(modelElement, new ArrayList<Diagram>()) ;
			return matchingDiagrams ;
		}
		ResourceSet resourceSet = resource.getResourceSet() ;

		final String  resourceNotationURI = modelElement.eResource().getURI().toString().replaceAll(".uml", ".notation") ;
		@SuppressWarnings("unused")
		Resource notationResource = resourceSet.getResource(URI.createURI(resourceNotationURI), true) ;
		List<EObject> matchingObjects = new ArrayList<EObject>() ;
		matchingObjects.add(modelElement) ;

		Map<EObject, Collection<Setting>> maps = EcoreUtil.UsageCrossReferencer.findAll(matchingObjects, resourceSet);
		matchingDiagrams = new ArrayList<Diagram>() ;

		for(Object key : maps.keySet()) {
			for(Setting setting : maps.get(key)) {
				if(setting.getEObject() instanceof View) {
					Diagram diagram = ((View)setting.getEObject()).getDiagram();
					if(! matchingDiagrams.contains(diagram)) {
						matchingDiagrams.add(diagram) ;
					}
				}
			}
		}

		eObjectToDiagrams.put(modelElement, matchingDiagrams) ;

		return matchingDiagrams ;
	}

	/**
	 * Returns true if some diagrams with graphical representation of the given model element exist, false otherwise 
	 * 
	 * @param modelElement
	 * @return true if some diagrams with graphical representation of the given model element exist, false otherwise
	 */
	public boolean diagramsExistFor(EObject modelElement) {
		return ! this.getDiagrams(modelElement).isEmpty() ;
	}
	
	/**
	 * In the case where the list of diagrams for the given modelElement has already been retrieved,
	 * resets this list 
	 * 
	 * @param modelElement The model element for which the list of diagrams has to be reset
	 */
	public void resetDiagrams(EObject modelElement) {
		eObjectToDiagrams.remove(modelElement) ;
	}

	/**
	 * Opens the given diagram (or puts focus on diagram if this diagram is already opened) in the current active papyrus editor.
	 * By construction, the active papyrus editor is supposed to be opened with the appropriate .di file.
	 * openDiagram is called by MokaDebugModelPresentation.addAnnotations(), which is always called after MokaDebugModelPresentation.getEditorInput() is called.
	 * MokaDebugModelPresentation.getEditorInput() returns a FileEditorInput for the .di model to which the given Diagram belongs.
	 * 
	 * @param diagram The diagram to be opened.
	 */
	public void openDiagram(final Diagram diagram, boolean closeOpenedDiagrams) {
		IEditorPart part = EditorUtils.getEditorPart(diagram) ;
		ServicesRegistry servicesRegistry =  (ServicesRegistry)part.getAdapter(ServicesRegistry.class);
		try {
			final IPageManager pageMngr = ServiceUtils.getInstance().getIPageManager(servicesRegistry) ;
			try {						
				if (pageMngr.isOpen(diagram)) {
					pageMngr.selectPage(diagram) ;
				}
				else {
					TransactionalEditingDomain domain = (TransactionalEditingDomain)EMFHelper.resolveEditingDomain(diagram) ;
					OpenDiagramCommand updateCommand = new OpenDiagramCommand(diagram, pageMngr, domain);
					domain.getCommandStack().execute(updateCommand) ;
				} 
			} catch (Exception e) {
				Activator.log.error(e) ;
			}
		} catch (ServiceException e1) {
			Activator.log.error(e1);
		}
	}

	/**
	 * A convenience method for retrieving the diagram edit part corresponding to a graphical editor.
	 * 
	 * @return the diagram edit part corresponding to the given editor
	 */
	public RootEditPart getRootEditPart(GraphicalEditor editor) { 
		return (RootEditPart)editor.getAdapter(EditPart.class);
	}

	/**
	 * Adds a "Suspended" marker to the given semantic element
	 * 
	 * @param semanticElement The semantic element to which a "Suspended" marker has to be attached
	 * @param thread The context thread in which this marker is attached
	 */
	public void addSuspendedMarker(EObject semanticElement) {
		if (eObjectToSuspendedMarker.get(semanticElement) != null)
			// Marker already exists
			return ;
		if (semanticElement.eIsProxy())
			semanticElement = resolve(semanticElement) ;
		IResource iresource = MokaBreakpoint.getIResource(semanticElement.eResource()) ;
		try {
			if (iresource != null) {
				String uriOfSemanticElement = EcoreUtil.getURI(semanticElement).toString() ;
				IMarker marker = iresource.createMarker(MokaConstants.MOKA_SUSPENDED_MARKER_ID);
				marker.setAttribute(EValidator.URI_ATTRIBUTE, uriOfSemanticElement) ;
				eObjectToSuspendedMarker.put(semanticElement, PapyrusMarkerAdapter.wrap(semanticElement.eResource(), marker)) ;
			}
		} catch (CoreException ce) {
			Activator.log.error(ce) ;
		}
	}

	/**
	 * Removes the "Suspended" marker for all semantic elements suspended in the context of the given thread
	 * 
	 * @param thread The thread for which "Suspend" markers have to be removed from suspended semantic elements
	 */
	public void removeSuspendedMarker(IThread thread) {
		for (EObject key : eObjectToSuspendedMarker.keySet()) {
			IPapyrusMarker stackFrameMarker = eObjectToSuspendedMarker.get(key) ;
			try {
				stackFrameMarker.delete() ;
			} catch (CoreException e) {
				Activator.log.error(e);
			}
		}
		eObjectToSuspendedMarker.clear() ;
	}

	/**
	 * Adds an "Animation" marker to the given semantic element
	 * 
	 * @param semanticElement The semantic element to which an "Animation" marker has to be attached
	 */
	public void addAnimationMarker(EObject semanticElement) {
		if (eObjectToAnimationMarker.get(semanticElement) != null)
			// Marker already exists
			return ;
		if (semanticElement.eIsProxy())
			semanticElement = resolve(semanticElement) ;
		IResource iresource = MokaBreakpoint.getIResource(semanticElement.eResource()) ;
		try {
			if (iresource != null) {
				String uriOfSemanticElement = EcoreUtil.getURI(semanticElement).toString() ;
				if (MokaConstants.MOKA_OPEN_DIAGRAM_IN_AUTOMATIC_ANIMATION) {
					List<Diagram> diagrams = getDiagrams(semanticElement) ;
					for (Diagram d : diagrams) {
						openDiagram(d, false) ;
					}
				}
				IMarker marker = iresource.createMarker(MokaConstants.MOKA_ANIMATION_MARKER_ID);
				marker.setAttribute(EValidator.URI_ATTRIBUTE, uriOfSemanticElement) ;
				eObjectToAnimationMarker.put(semanticElement, PapyrusMarkerAdapter.wrap(semanticElement.eResource(), marker)) ;
			}
		} catch (CoreException ce) {
			Activator.log.error(ce) ;
		}
	}

	/**
	 * Removes the "Animation" marker associated with the given semantic element, if any
	 * 
	 * @param semanticElement The semantic element to which an "Animation" marker has to be attached
	 */
	public void removeAnimationMarker(EObject semanticElement) {
		if (semanticElement.eIsProxy())
			semanticElement = resolve(semanticElement) ;
		IPapyrusMarker animationFrameMarker = eObjectToAnimationMarker.get(semanticElement) ;
		if (animationFrameMarker != null) {
			try {
				animationFrameMarker.delete() ;
				eObjectToAnimationMarker.remove(semanticElement) ;
			} catch (CoreException e) {
				Activator.log.error(e) ;
			}
		}
	}

	/**
	 * Resolve the given semantic element in the case where it is a proxy.
	 * This is supposed to happen only when, in the course of execution,
	 * the editor for the model being executed is closed.
	 * This may imply re-creation of a new IEditorPart.
	 * 
	 * @param semanticElement The semantic element that needs to be resolved
	 * @return the resolved semantic element
	 */
	public static EObject resolve(EObject semanticElement) {
		if (semanticElement.eIsProxy()) {
			getInstance().resetDiagrams(semanticElement) ;
			IEditorPart part = EditorUtils.getEditorPart(semanticElement) ;
			ServicesRegistry registry = (ServicesRegistry) part.getAdapter(ServicesRegistry.class) ;
			ResourceSet resourceSet = null ;
			try {
				resourceSet = registry.getService(ModelSet.class) ;
			} catch (ServiceException e) {
				resourceSet = new ResourceSetImpl() ;
			}
			semanticElement = EcoreUtil.resolve(semanticElement, resourceSet) ;
		}
		return semanticElement ;
	}

	public boolean isAnimationMarkerApplied(EObject element) {
		return eObjectToAnimationMarker.get(element) != null ;
	}

	/**
	 * A command for opening a diagram
	 *
	 */
	protected class OpenDiagramCommand extends RecordingCommand {

		protected Diagram diagram ;
		protected IPageManager pageMngr ;

		/**
		 * Constructor for the OpenDiagramCommand.
		 * 
		 * @param diagram The diagram to be opened
		 * @param pageMngr The page manager to be used for opening the diagram
		 * @param domain The editing domain in which the command will be executed
		 */
		public OpenDiagramCommand(Diagram diagram, IPageManager pageMngr, TransactionalEditingDomain domain) {
			super(domain) ;
			this.diagram = diagram ;
			this.pageMngr = pageMngr ;
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see
		 * org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand#doExecuteWithResult(org.eclipse.core.runtime.IProgressMonitor
		 * , org.eclipse.core.runtime.IAdaptable)
		 */
		@Override
		protected void doExecute() {
			this.pageMngr.openPage(diagram) ;
			this.pageMngr.selectPage(diagram) ;
		}

	}

}

Back to the top