Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f87140becceef6948479d13db4e1cec769121b41 (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
package org.eclipse.papyrus.navigator.internal.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.gef.EditPart;
import org.eclipse.gmf.runtime.diagram.ui.parts.IDiagramGraphicalViewer;
import org.eclipse.gmf.runtime.emf.core.util.EMFCoreUtil;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.NotationPackage;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.papyrus.infra.core.sasheditor.contentprovider.IPageMngr;
import org.eclipse.papyrus.infra.core.utils.EditorUtils;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IViewReference;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

/**
 * Utility method for Model Navigator.
 * 
 * @author <a href="mailto:jerome.benois@obeo.fr">Jerome Benois</a>
 * @author <a href="mailto:fjcano@prodevelop.es">Francisco Javier Cano Muñoz</a>
 * @author <a href="mailto:thomas.szadel@atosorigin.com">Thomas Szadel</a>: Remove Backbone
 *         dependency
 **/
public class NavigatorUtils {

	/**
	 * Find a <IViewPart> by it's id string.
	 * 
	 * @param viewID
	 *        the view id
	 * 
	 * @return the i view part
	 */
	public static IViewPart findViewPart(String viewID) {
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		if(page == null) {
			return null;
		}
		IViewReference reference = page.findViewReference(viewID);
		if(reference == null) {
			return null;
		}
		IWorkbenchPart part = reference.getPart(false);
		if(part instanceof IViewPart) {
			return (IViewPart)part;
		} else {
			return null;
		}
	}

	/**
	 * Unwraps selection. Gets <EObject>s from <EditPart>s, from <View>s or from <EObject>s
	 * 
	 * @param selection
	 *        the selection
	 * 
	 * @return the i selection
	 */
	public static ISelection unwrapSelection(ISelection selection) {
		if(selection instanceof StructuredSelection && !selection.isEmpty()) {
			List<EObject> selectionList = new ArrayList<EObject>();
			StructuredSelection structuredSelection = (StructuredSelection)selection;
			for(Iterator<?> iterator = structuredSelection.iterator(); iterator.hasNext();) {
				Object next = iterator.next();
				if(next instanceof EditPart) {
					Object model = ((EditPart)next).getModel();
					EObject element = null;
					if(model instanceof View) {
						element = ((View)model).getElement();
					} else if(model instanceof EObject) {
						element = (EObject)model;
					}
					if(element != null) {
						selectionList.add(element);
					}
				} else if(next instanceof View) {
					EObject element = ((View)next).getElement();
					if(element != null) {
						selectionList.add(element);
					}
				} else if(next instanceof EObject) {
					selectionList.add((EObject)next);
				}
			}
			return new StructuredSelection(selectionList);
		} else {
			return selection;
		}
	}

	/**
	 * Finds the <EditPart>s for the <EObject>s in the selection.
	 * 
	 * @param selection
	 *        the selection
	 * @param viewer
	 *        the viewer
	 * 
	 * @return the edits the parts from selection
	 */
	public static List<EditPart> getEditPartsFromSelection(ISelection selection, IDiagramGraphicalViewer viewer) {
		if(selection instanceof StructuredSelection && !selection.isEmpty()) {
			StructuredSelection structuredSelection = (StructuredSelection)selection;
			// look for Views of the EObjects in the selection
			List<View> views = new ArrayList<View>();
			for(Object o : structuredSelection.toList()) {
				if(o instanceof EObject) {
					List<Object> referencerViews = getEObjectViews((EObject)o);
					for(Object ro : referencerViews) {
						if(ro instanceof View) {
							views.add((View)ro);
						}
					}
				}
			}
			if(!views.isEmpty()) {
				List<EditPart> editParts = new ArrayList<EditPart>();
				for(View view : views) {
					Object ep = viewer.getEditPartRegistry().get(view);
					if(ep instanceof EditPart) {
						editParts.add((EditPart)ep);
					}
				}
				if(!editParts.isEmpty()) {
					return editParts;
				}
			}
		}
		return Collections.emptyList();
	}

	/**
	 * Gets the given <EObject> views.
	 * 
	 * @param element
	 *        the element
	 * 
	 * @return the e object views
	 */
	// @unused
	public static List<Object> getEObjectViews(EObject element) {
		List<Object> views = new ArrayList<Object>();
		if(element != null) {
			EReference[] features = { NotationPackage.eINSTANCE.getView_Element() };
			Collection<?> referencers = EMFCoreUtil.getReferencers(element, features);
			views.addAll(referencers);
		}
		return views;
	}

	// //
	// get an object name
	// //

	/**
	 * Gets the object name or empty string.
	 * 
	 * @param object
	 *        the object
	 * 
	 * @return the object name or empty string
	 */
	// @unused
	public static String getObjectNameOrEmptyString(Object object) {
		String name = getObjectName(object);
		return name == null ? "" : name;
	}

	/** The Constant getNameNames. */
	private static final String[] getNameNames = { "getName", "getname" };

	/**
	 * Gets the object name.
	 * 
	 * @param object
	 *        the object
	 * 
	 * @return the object name
	 */
	// @unused
	public static String getObjectName(Object object) {
		if(object == null) {
			return null;
		}
		Method method = null;
		Object o = null;
		for(String methodName : getNameNames) {
			try {
				method = object.getClass().getMethod(methodName, (Class[])null);
			} catch (NoSuchMethodException e) {
				method = null;
			}
			if(method != null) {
				break;
			}
		}
		if(method != null) {
			try {
				o = method.invoke(object, (Object[])null);
			} catch (IllegalAccessException ex) {
				return null;
			} catch (InvocationTargetException ex) {
				return null;
			}
			if(o instanceof String) {
				return (String)o;
			}
		}
		return null;
	}

	/**
	 * Opens a {@link Diagram} in the sash editor.
	 * 
	 * @param diagram
	 */
	// @unused
	public static void openDiagram(Diagram diagram) {
		IPageMngr pageManager = EditorUtils.getIPageMngr();
		if(pageManager != null) {
			pageManager.openPage(diagram);
		}
	}

	/**
	 * Opens a view part in the workbench with the specified ID.
	 * 
	 * @param viewPartID
	 */
	// @unused
	public static void openViewPart(String viewPartID) {
		if(viewPartID == null) {
			return;
		}
		try {
			PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().showView(viewPartID);
		} catch (PartInitException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Opens the property sheets view.
	 */
	public static void openPropertySheetsView() {
		openViewPart("org.eclipse.ui.views.PropertySheet");
	}
}

Back to the top