Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9c18040619f700bc2e4f155c5bfc9d5938cd595a (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
/*****************************************************************************
 * Copyright (c) 2008 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:
 *  Chokri Mraidha (CEA LIST) Chokri.Mraidha@cea.fr - Initial API and implementation
 *  Patrick Tessier (CEA LIST) Patrick.Tessier@cea.fr - modification
 *
 *****************************************************************************/
package org.eclipse.papyrus.uml.profile.ui.actions;

import java.util.Iterator;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.uml2.uml.Element;

/**
 * Action delegate default implementation for this plugin. Is is based on a
 * selection of a {@link Element}.
 */
public abstract class AbstractViewActionDelegate implements IViewActionDelegate {

	/** current selection */
	protected Element selectedElement;

	/**
	 * {@inheritDoc}
	 */
	public void init(IViewPart view) {
	}

	/**
	 * {@inheritDoc}
	 */
	public abstract void run(IAction action);

	/**
	 * Returns <code>true</code> if the element is a selectableElement, i.e. if the action
	 * should be enabled
	 * 
	 * @param element
	 *        the element to test
	 * @return <code>true</code> if the element can be selected
	 */
	protected boolean isSelectableElement(Object o) {
		return (o instanceof Element);
	}

	/**
	 * {@inheritDoc}
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		IStructuredSelection structSelection = null;
		if(selection instanceof IStructuredSelection) {
			structSelection = (IStructuredSelection)selection;
			// iterate the selection to update the selected element
			@SuppressWarnings("unchecked")
			Iterator<Object> it = structSelection.iterator();
			while(it.hasNext()) {
				Object o = (Object)it.next();
				if (o instanceof IAdaptable) {
					EObject eObject = (EObject) ((IAdaptable) o)
							.getAdapter(EObject.class);
					if (eObject != null) {
						setSelectedElement(eObject);
					}
				}
				if(isSelectableElement(o)) {
					setSelectedElement(o);
					return;
				}
			}
		}
	}

	/**
	 * Sets the current selected element
	 * 
	 * @param selectedElement
	 *        the new selected element
	 */
	protected void setSelectedElement(Object selectedElement) {
		if( selectedElement instanceof Element){
			this.selectedElement = (Element)selectedElement;
		}
	}

	/**
	 * Returns current selected element
	 * 
	 * @return current selected element may be null
	 */
	protected Element getSelectedElement() {
		return selectedElement;
	}

}

Back to the top