Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 26f840a10e380d5db2cbedafe1d6437c0305b00e (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
/*****************************************************************************
 * Copyright (c) 2015 CEA LIST 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:
 *   CEA LIST - Initial API and implementation
 *
 *****************************************************************************/

package org.eclipse.papyrus.infra.gmfdiag.navigation.menu;

import java.util.LinkedList;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.SelectionRequest;
import org.eclipse.gmf.runtime.diagram.ui.editparts.IGraphicalEditPart;
import org.eclipse.gmf.runtime.draw2d.ui.figures.WrappingLabel;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.papyrus.infra.core.services.BadStateException;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.core.services.ServiceNotFoundException;
import org.eclipse.papyrus.infra.core.services.ServicesRegistry;
import org.eclipse.papyrus.infra.emf.utils.EMFHelper;
import org.eclipse.papyrus.infra.gmfdiag.navigation.Activator;
import org.eclipse.papyrus.infra.gmfdiag.navigation.menu.listener.NavigationMenuKeyListener;
import org.eclipse.papyrus.infra.gmfdiag.navigation.menu.listener.SelectionMenuSelectionChangedListener;
import org.eclipse.papyrus.infra.gmfdiag.navigation.menu.provider.SelectionMenuLabelProvider;
import org.eclipse.papyrus.infra.services.navigation.service.ExtendedNavigableElement;
import org.eclipse.papyrus.infra.services.navigation.service.NavigableElement;
import org.eclipse.papyrus.infra.services.navigation.service.NavigationMenu;
import org.eclipse.papyrus.infra.services.navigation.service.NavigationMenuButton;
import org.eclipse.papyrus.infra.services.navigation.service.NavigationService;
import org.eclipse.papyrus.infra.services.openelement.service.OpenElementService;
import org.eclipse.papyrus.infra.services.viewersearch.impl.ViewerSearchService;
import org.eclipse.papyrus.infra.widgets.editors.SelectionMenu;
import org.eclipse.papyrus.infra.widgets.providers.CollectionContentProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TreeItem;

// A Single ViewerContext for each diagram (Root EditPartViewer)
public class DefaultNavigationMenu implements NavigationMenu {
	private ServicesRegistry servicesRegistry;

	private Shell parentShell;

	private NavigationService navigationService;

	private ViewerSearchService viewerSearchService;

	private EObject currentModel;

	private List<Object> prependObjects;

	private List<Object> appendObjects;

	private SelectionMenu selectionMenu;

	private List<SelectionMenu> subMenus;

	private boolean wasUnderlined;

	private WrappingLabel lastWrappingLabel;

	private boolean altReleasedPostNavigation = true;

	private View selectedView;

	public class NavigationMenuInitializationException extends Exception {
		private static final long serialVersionUID = 1094797103244873186L;

		private Object source;

		public NavigationMenuInitializationException(Object source) {
			this.source = source;
		}

		@Override
		public String getMessage() {
			String error = "";

			if (source instanceof ServicesRegistry) {
				error = "services registry is not set";
			} else if (source instanceof Shell) {
				error = "parent shell is not set";
			} else if (source instanceof NavigationService) {
				error = "navigation service could not be initialized";
			}

			return "Navigation menu initialization error: " + error;
		}
	}

	public DefaultNavigationMenu() {
		this.appendObjects = new LinkedList<Object>();
		this.prependObjects = new LinkedList<Object>();
	}

	public void handleRequest(SelectionRequest request, EditPart targetEditPart) {
		if (targetEditPart != null) {
			EObject model = EMFHelper.getEObject(targetEditPart);

			if (isExitState(request, model)) {
				exitItem();
			}

			if (isEnterState(request, model)) {
				enterItem(targetEditPart);
			}
		}
	}

	public void handleRequest(MouseEvent e, TreeItem treeItem) {
		if (treeItem != null) {
			EObject model = EMFHelper.getEObject(treeItem.getData());

			if (isExitState(e, model)) {
				exitItem();
			}

			if (isEnterState(e, model)) {
				enterItem(model);
			}
		}
	}

	protected boolean isExitState(SelectionRequest request, EObject model) {
		if (!request.isAltKeyPressed()) {
			altReleasedPostNavigation = true;
			return true;
		}

		return isExitState(model);
	}

	private boolean isExitState(MouseEvent e, EObject model) {
		if ((e.stateMask & SWT.ALT) == 0) {
			altReleasedPostNavigation = true;
			return true;
		}

		return isExitState(model);
	}

	protected boolean isExitState(EObject model) {
		if (currentModel == null) {
			return false;
		}

		if (model == null) {
			return true;
		}

		if (model != currentModel) {
			return true;
		}

		return false;
	}

	protected boolean isEnterState(SelectionRequest request, EObject model) {
		if (!request.isAltKeyPressed() || !altReleasedPostNavigation) {
			return false;
		}

		return isEnterState(model);
	}

	protected boolean isEnterState(MouseEvent e, EObject model) {
		if ((e.stateMask & SWT.ALT) == 0 || !altReleasedPostNavigation) {
			return false;
		}

		return isEnterState(model);
	}

	protected boolean isEnterState(EObject model) {
		if (model == currentModel) {
			return false;
		}

		if (model == null) {
			return false;
		}

		currentModel = model;

		return true;
	}

	public boolean willEnter(SelectionRequest request, EditPart targetEditPart) {
		if (!request.isAltKeyPressed()) {
			return false;
		}

		EObject model = null;
		if (targetEditPart != null) {
			model = EMFHelper.getEObject(targetEditPart);

		}

		return willEnter(model);
	}

	public boolean willEnter(EObject model) {
		if (model == currentModel) {
			return false;
		}

		if (model == null) {
			return false;
		}

		return true;
	}

	private void disposeCurrentMenu() {
		if (selectionMenu != null) {
			selectionMenu.dispose();
			selectionMenu = null;
		}
	}

	public void exitItem() {
		if (lastWrappingLabel != null) {
			lastWrappingLabel.setTextUnderline(wasUnderlined);
		}

		wasUnderlined = false;
		lastWrappingLabel = null;
		currentModel = null;

		disposeCurrentMenu();
	}

	private void enterItem(Object source) {
		disposeCurrentMenu();

		Shell shell;
		try {
			shell = getParentShell();
		} catch (NavigationMenuInitializationException e) {
			Activator.log.error(e);
			return;
		}

		// The list that contains items of the Navigation Menu
		final List<Object> navigationMenuElements = new LinkedList<Object>();

		// Add objects to prepend
		if (prependObjects != null && prependObjects.size() > 0) {
			navigationMenuElements.addAll(prependObjects);
		}

		// Add navigable objects
		try {
			navigationMenuElements.addAll(getNavigationService().getNavigableElements(source));
		} catch (NavigationMenuInitializationException e) {
			Activator.log.error(e);
			return;
		}

		// Add objects to append
		if (appendObjects != null && appendObjects.size() > 0) {
			navigationMenuElements.addAll(appendObjects);
		}

		// Nothing to display...
		if (navigationMenuElements.isEmpty()) {
			return;
		}

		// Add "More..." button
		// navigationMenuElements.add(new MoreButton());

		// The semantic element
		EObject umlElement = EMFHelper.getEObject(source);

		// Create the selection menu and subMenus list
		selectionMenu = new SelectionMenu(shell);
		subMenus = new LinkedList<SelectionMenu>();

		selectionMenu.setLabelProvider(new SelectionMenuLabelProvider());

		selectionMenu.setContentProvider(CollectionContentProvider.instance);
		selectionMenu.setInput(navigationMenuElements);
		selectionMenu.open();

		wasUnderlined = false;
		if (source instanceof IGraphicalEditPart) {
			selectedView = ((IGraphicalEditPart) source).getPrimaryView();
			IGraphicalEditPart graphicalEditPart = (IGraphicalEditPart) source;
			IFigure figure = graphicalEditPart.getFigure();
			if (figure instanceof WrappingLabel) {
				lastWrappingLabel = ((WrappingLabel) figure);
				wasUnderlined = lastWrappingLabel.isTextUnderlined();
				lastWrappingLabel.setTextUnderline(!wasUnderlined);
			}
		} else {
			selectedView = null;
		}

		selectionMenu.addSelectionChangedListener(new SelectionMenuSelectionChangedListener(DefaultNavigationMenu.this, selectionMenu, navigationMenuElements, umlElement, subMenus));
		selectionMenu.addKeyListener(new NavigationMenuKeyListener(this));
		// selectionMenu.addMouseTrackListener(new SelectionMenuMouseTrackListener(DefaultNavigationMenu.this, selectionMenu, subMenus, umlElement));
	}

	public void addContextualMenus(List<Object> navigationMenuElements, Object umlElement) {
		List<NavigationMenuButton> buttons;
		try {
			buttons = getNavigationService().getButtons(umlElement);
			navigationMenuElements.addAll(buttons);
		} catch (NavigationMenuInitializationException e) {
			Activator.log.error(e);
		}
	}

	public List<Object> getViewsToSelect(NavigableElement navElement, boolean onlyOpened) {
		if (navElement == null) {
			return null;
		}

		EObject element = null;
		if (navElement instanceof ExtendedNavigableElement
				&& ((ExtendedNavigableElement) navElement).getSemanticElement() instanceof EObject) {
			element = (EObject) ((ExtendedNavigableElement) navElement).getSemanticElement();
		}

		if (element != null) {
			ViewerSearchService viewerSearchService = null;

			try {
				viewerSearchService = getViewerSearchService();
			} catch (NavigationMenuInitializationException e) {
				Activator.log.error(e);
			}

			if (viewerSearchService != null) {
				return viewerSearchService.getViewersInCurrentModel(element, null, false, onlyOpened);
			}
		}

		return new LinkedList<Object>();
	}

	public void showInModelExplorer(NavigableElement navigableElement) {
		Object semanticElement = null;
		if (navigableElement instanceof ExtendedNavigableElement) {
			semanticElement = ((ExtendedNavigableElement) navigableElement).getSemanticElement();
		}

		if (semanticElement != null) {
			try {
				getNavigationService().navigate(semanticElement, "org.eclipse.papyrus.views.modelexplorer.navigation.target");
			} catch (NavigationMenuInitializationException e) {
				Activator.log.error(e);
			}
		}

		altReleasedPostNavigation = false;
		exitItem();
	}

	public void revealObject(Object object) {
		ServicesRegistry registry = null;
		try {
			registry = getServicesRegistry();
		} catch (NavigationMenuInitializationException e) {
			Activator.log.error(e);
			exitItem();
			return;
		}

		try {
			OpenElementService service = registry.getService(OpenElementService.class);
			if (object instanceof EObject) {
				service.openElement((EObject) object);
			}
		} catch (Exception e) {
			Activator.log.error(e);
		}

		altReleasedPostNavigation = false;
		exitItem();
	}

	private ServicesRegistry getServicesRegistry() throws NavigationMenuInitializationException {
		if (servicesRegistry == null) {
			throw new NavigationMenuInitializationException(servicesRegistry);
		}
		return servicesRegistry;
	}

	private NavigationService getNavigationService() throws NavigationMenuInitializationException {
		if (navigationService == null) {
			throw new NavigationMenuInitializationException(navigationService);
		}
		return navigationService;
	}

	public ViewerSearchService getViewerSearchService() throws NavigationMenuInitializationException {
		if (viewerSearchService == null) {
			throw new NavigationMenuInitializationException(viewerSearchService);
		}
		return viewerSearchService;
	}

	private Shell getParentShell() throws NavigationMenuInitializationException {
		if (parentShell == null) {
			throw new NavigationMenuInitializationException(parentShell);
		}
		return parentShell;
	}


	public List<Object> getAppendObjects() {
		return appendObjects;
	}

	public void setAppendObjects(List<Object> appendObjects) {
		this.appendObjects = appendObjects;
	}

	public List<Object> getPrependObjects() {
		return prependObjects;
	}

	public void setPrependObjects(List<Object> prependObjects) {
		this.prependObjects = prependObjects;
	}

	/* These are not used but they are necessary for the getCommand method of the NavigationEditPolicy */

	public Command navigate(final SelectionRequest request, final EditPart host) {
		if (!request.isAltKeyPressed()) {
			return null;
		}

		EditPart targetEditPart = host.getViewer().findObjectAt(request.getLocation());

		final NavigableElement element = getElementToNavigate(targetEditPart);
		if (element == null) {
			return null;
		}

		return new Command() {

			@Override
			public void execute() {
				try {
					getNavigationService().navigate(element);
				} catch (NavigationMenuInitializationException e) {
					Activator.log.error(e);
				}
				exitItem();
			}
		};
	}

	private NavigableElement getElementToNavigate(EditPart target) {
		List<NavigableElement> navigableElements;
		try {
			navigableElements = getNavigationService().getNavigableElements(target);
		} catch (NavigationMenuInitializationException e) {
			Activator.log.error(e);
			return null;
		}

		if (navigableElements.isEmpty()) {
			return null;
		}

		for (NavigableElement element : navigableElements) {
			if (element.isEnabled()) {
				return element;
			}
		}
		return null;
	}

	/**
	 * @see org.eclipse.papyrus.infra.services.navigation.service.NavigationMenu#handleRequest(java.lang.Object, java.lang.Object)
	 *
	 * @param request
	 * @param target
	 */
	public void handleRequest(Object request, Object target) {
		if (request instanceof SelectionRequest && target instanceof EditPart) {
			handleRequest((SelectionRequest) request, (EditPart) target);
		}

		if (request instanceof MouseEvent && target instanceof TreeItem) {
			handleRequest((MouseEvent) request, (TreeItem) target);
		}
	}

	/**
	 * @see org.eclipse.papyrus.infra.services.navigation.service.NavigationMenu#willEnter(java.lang.Object, java.lang.Object)
	 *
	 * @param request
	 * @param target
	 * @return
	 */
	public boolean willEnter(Object request, Object target) {
		if (request instanceof SelectionRequest && target instanceof EditPart) {
			return willEnter((SelectionRequest) request, (EditPart) target);
		}

		return false;
	}

	/**
	 * @see org.eclipse.papyrus.infra.services.navigation.service.NavigationMenu#navigate(java.lang.Object, java.lang.Object)
	 *
	 * @param request
	 * @param host
	 * @return
	 */
	public Object navigate(Object request, Object host) {
		if (request instanceof SelectionRequest && host instanceof EditPart) {
			return navigate((SelectionRequest) request, (EditPart) host);
		}

		return null;
	}

	/**
	 * @see org.eclipse.papyrus.infra.services.navigation.service.NavigationMenu#setServicesRegistry(org.eclipse.papyrus.infra.core.services.ServicesRegistry)
	 *
	 * @param registry
	 */
	public void setServicesRegistry(ServicesRegistry registry) {
		this.servicesRegistry = registry;
		try {
			this.navigationService = getServicesRegistry().getService(NavigationService.class);
			this.viewerSearchService = getServicesRegistry().getService(ViewerSearchService.class);
		} catch (ServiceException e) {
			if (e instanceof ServiceNotFoundException) {
				viewerSearchService = new ViewerSearchService();
				try {
					viewerSearchService.startService();
					getServicesRegistry().add(ViewerSearchService.class, 1, viewerSearchService);
				} catch (Exception e1) {
					Activator.log.error(e1);
				}
			} else if (e instanceof BadStateException) {
				try {
					getServicesRegistry().startRegistry();
					viewerSearchService = getServicesRegistry().getService(ViewerSearchService.class);
				} catch (Exception e1) {
					Activator.log.error(e1);
				}
			}
		} catch (NavigationMenuInitializationException e) {
			Activator.log.error(e);
		}
	}

	public View getSelectedView() {
		return selectedView;
	}

	/**
	 * @see org.eclipse.papyrus.infra.services.navigation.service.NavigationMenu#setParentShell(org.eclipse.swt.widgets.Shell)
	 *
	 * @param parentShell
	 */
	public void setParentShell(Shell parentShell) {
		this.parentShell = parentShell;
	}

	public void altReleased() {
		altReleasedPostNavigation = true;
	}
}

Back to the top