4d556dbbfc709a863de96cd633b8ea1bbad110fb
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / commands / TmfHandlerUtil.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Marc-Andre Laperle - Initial API and implementation
11 * Patrick Tasse - Add support for folder elements
12 *******************************************************************************/
13
14 package org.eclipse.tracecompass.internal.tmf.ui.commands;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.jface.viewers.ISelection;
18 import org.eclipse.jface.viewers.IStructuredSelection;
19 import org.eclipse.tracecompass.tmf.ui.project.model.ITmfProjectModelElement;
20 import org.eclipse.tracecompass.tmf.ui.project.model.TmfTraceFolder;
21
22 /**
23 * Utility methods for handlers
24 *
25 * @author Marc-Andre Laperle
26 */
27 public class TmfHandlerUtil {
28
29 /**
30 * Get the enclosing project from the selection
31 *
32 * @param selection
33 * the selection
34 *
35 * @return the enclosing project or null if selection is no enclosed by a
36 * project
37 */
38 public static IProject getProjectFromSelection(ISelection selection) {
39 if (selection instanceof IStructuredSelection) {
40 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
41 Object firstElement = structuredSelection.getFirstElement();
42 if (firstElement instanceof ITmfProjectModelElement) {
43 ITmfProjectModelElement tmfProjectElement = (ITmfProjectModelElement) firstElement;
44 return tmfProjectElement.getProject().getResource();
45 }
46 }
47
48 return null;
49 }
50
51 /**
52 * Get the trace folder from the selection
53 *
54 * @param selection
55 * the selection
56 *
57 * @return the enclosing project or null if selection is no enclosed by a
58 * project
59 */
60 public static TmfTraceFolder getTraceFolderFromSelection(ISelection selection) {
61 if (selection instanceof IStructuredSelection) {
62 IStructuredSelection structuredSelection = (IStructuredSelection) selection;
63 Object firstElement = structuredSelection.getFirstElement();
64 if (firstElement instanceof ITmfProjectModelElement) {
65 ITmfProjectModelElement element = (ITmfProjectModelElement) firstElement;
66 while (element != null) {
67 if (element instanceof TmfTraceFolder) {
68 return (TmfTraceFolder) element;
69 }
70 element = element.getParent();
71 }
72 }
73 }
74 return null;
75 }
76 }
This page took 0.033245 seconds and 4 git commands to generate.