Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7f7cd36d9b798510ff820a90fba640b39dc7904a (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
/*****************************************************************************
 * Copyright (c) 2013 CEA
 *
 *    
 * 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:
 *   Soyatec - Initial API and implementation
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.diagram.sequence.providers;

import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.action.ContributionItem;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.BehaviorExecutionSpecificationBehaviorEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.edit.parts.BehaviorExecutionSpecificationEditPart;
import org.eclipse.papyrus.uml.diagram.sequence.util.BehaviorDisplayHelper;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.ui.ISelectionService;
import org.eclipse.ui.menus.IWorkbenchContribution;
import org.eclipse.ui.services.IServiceLocator;


/**
 * @author Jin Liu (jin.liu@soyatec.com)
 */
public class DisplayBehaviorContributionItem extends ContributionItem implements IWorkbenchContribution {

	private IServiceLocator serviceLocator;

	/**
	 * @see org.eclipse.ui.menus.IWorkbenchContribution#initialize(org.eclipse.ui.services.IServiceLocator)
	 * 
	 * @param serviceLocator
	 */
	public void initialize(IServiceLocator serviceLocator) {
		this.serviceLocator = serviceLocator;
	}

	@Override
	public boolean isDynamic() {
		return true;
	}

	private ISelection getSelection() {
		if(serviceLocator == null) {
			return null;
		}
		ISelectionService selectionService = (ISelectionService)serviceLocator.getService(ISelectionService.class);
		if(selectionService != null) {
			return selectionService.getSelection();
		}
		return null;
	}

	/**
	 * @see org.eclipse.jface.action.ContributionItem#fill(org.eclipse.swt.widgets.Menu, int)
	 * 
	 * @param menu
	 * @param index
	 */
	@Override
	public void fill(Menu menu, int index) {
		super.fill(menu, index);
		ISelection selection = getSelection();
		if(selection == null || selection.isEmpty()) {
			return;
		}
		if(selection instanceof IStructuredSelection) {
			Object firstElement = ((IStructuredSelection)selection).getFirstElement();
			if(firstElement instanceof BehaviorExecutionSpecificationEditPart) {
				final View view = ((BehaviorExecutionSpecificationEditPart)firstElement).getNotationView();
				final TransactionalEditingDomain domain = ((BehaviorExecutionSpecificationEditPart)firstElement).getEditingDomain();
				boolean displayBehavior = BehaviorDisplayHelper.shouldDisplayBehavior(view);
				if(displayBehavior) {
					MenuItem hide = new MenuItem(menu, SWT.NONE, 0);
					hide.setText("Hide Behavior Label");
					hide.addListener(SWT.Selection, new Listener() {

						public void handleEvent(Event event) {
							Command cmd = BehaviorDisplayHelper.getChangeDisplayBehaviorCommand(domain, view, false);
							domain.getCommandStack().execute(cmd);
						}
					});
					new MenuItem(menu, SWT.SEPARATOR, 1);
				} else {
					MenuItem show = new MenuItem(menu, SWT.NONE, 0);
					show.setText("Display Behavior Label");
					show.addListener(SWT.Selection, new Listener() {

						public void handleEvent(Event event) {
							Command cmd = BehaviorDisplayHelper.getChangeDisplayBehaviorCommand(domain, view, true);
							domain.getCommandStack().execute(cmd);
						}
					});
					new MenuItem(menu, SWT.SEPARATOR, 1);
				}
			}
		}
	}
}

Back to the top