Skip to main content
summaryrefslogtreecommitdiffstats
blob: a93321213d0c40c41b3fa89af88c7be67b57533c (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
/*****************************************************************************
 * Copyright (c) 2010 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.customization.properties.util;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;

/**
 * Utility class for Actions
 * 
 * @author Camille Letavernier
 * 
 */
public class ActionUtil {

	/**
	 * EMF can only handle collections of EObjects. However, as the
	 * customization plugin relies a lot on EMF Facet, we often have to handle
	 * objects that can be adapted to EObjects, but are not EObjects
	 * themselves. This method adapts a collections of such objects to their
	 * underlying EObjects, so that EMF can handle them.
	 * Objects that cannot be adapted remain as-is in the collection.
	 * 
	 * @param selection
	 *        The collection to adapt
	 * @return
	 *         The adapted selection
	 */
	public static Collection<Object> getAdaptedSelection(Collection<? extends Object> selection) {
		Collection<Object> newSelection = new LinkedList<Object>();
		for(Object o : selection) {
			if(o instanceof IAdaptable) {
				EObject eObject = (EObject)((IAdaptable)o).getAdapter(EObject.class);
				newSelection.add(eObject);
			} else {
				newSelection.add(o);
			}
		}
		return newSelection;
	}

	/**
	 * EMF can only handle ISelection containing EObjects. However, as the
	 * customization plugin relies a lot on EMF Facet, we often have to handle
	 * objects that can be adapted to EObjects, but are not EObjects
	 * themselves. This method adapts a ISelection of such objects to their
	 * underlying EObjects, so that EMF can handle them.
	 * Objects that cannot be adapted remain as-is in the selection.
	 * 
	 * @param sourceSelection
	 *        The selection to adapt
	 * @return
	 *         The adapted selection
	 */
	public static ISelection getAdaptedSelection(ISelection sourceSelection) {
		if(sourceSelection instanceof StructuredSelection) {
			StructuredSelection currentSelection = (StructuredSelection)sourceSelection;
			List<Object> newSelection = new LinkedList<Object>();

			Iterator<?> it = currentSelection.iterator();
			while(it.hasNext()) {
				Object object = it.next();
				if(object instanceof IAdaptable) {
					EObject eObject = (EObject)((IAdaptable)object).getAdapter(EObject.class);
					if(eObject != null)
						newSelection.add(eObject);
					else
						newSelection.add(object);
				}
			}

			StructuredSelection selection = new StructuredSelection(newSelection);
			return selection;
		} else {
			return sourceSelection;
		}
	}
}

Back to the top