tmf: Support folders in tracing projects
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfAnalysisElement.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
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 * Geneviève Bastien - Initial API and implementation
11 * Patrick Tasse - Add support for folder elements
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.ui.project.model;
15
16 import java.util.ArrayList;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20
21 import org.eclipse.core.resources.IFolder;
22 import org.eclipse.core.resources.IResource;
23 import org.eclipse.core.resources.ResourcesPlugin;
24 import org.eclipse.core.runtime.IPath;
25 import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModule;
26 import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModuleHelper;
27 import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisOutput;
28 import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisManager;
29 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
30 import org.osgi.framework.Bundle;
31
32 /**
33 * Class for project elements of type analysis modules
34 *
35 * @author Geneviève Bastien
36 * @since 3.0
37 */
38 public class TmfAnalysisElement extends TmfProjectModelElement {
39
40 private final String fAnalysisId;
41
42 /**
43 * Constructor
44 *
45 * @param name
46 * Name of the analysis
47 * @param resource
48 * The resource
49 * @param parent
50 * Parent of the analysis
51 * @param id
52 * The analysis module id
53 */
54 protected TmfAnalysisElement(String name, IResource resource, ITmfProjectModelElement parent, String id) {
55 super(name, resource, parent);
56 fAnalysisId = id;
57 parent.addChild(this);
58 }
59
60 // ------------------------------------------------------------------------
61 // TmfProjectModelElement
62 // ------------------------------------------------------------------------
63
64 @Override
65 void refreshChildren() {
66 /* Refresh the outputs of this analysis */
67 Map<String, TmfAnalysisOutputElement> childrenMap = new HashMap<>();
68 for (TmfAnalysisOutputElement output : getAvailableOutputs()) {
69 childrenMap.put(output.getName(), output);
70 }
71
72 IAnalysisModuleHelper helper = TmfAnalysisManager.getAnalysisModule(fAnalysisId);
73 if (helper == null) {
74 deleteOutputs();
75 return;
76 }
77
78 /** Get base path for resource */
79 IPath path = getProject().getTracesFolder().getPath();
80 if (fResource instanceof IFolder) {
81 path = ((IFolder) fResource).getFullPath();
82 }
83
84 /*
85 * We can get a list of available outputs once the analysis is
86 * instantiated when the trace is opened
87 */
88 ITmfProjectModelElement parent = getParent();
89 if (parent instanceof TmfCommonProjectElement) {
90 ITmfTrace trace = ((TmfCommonProjectElement) parent).getTrace();
91 if (trace == null) {
92 deleteOutputs();
93 return;
94 }
95
96 IAnalysisModule module = trace.getAnalysisModule(fAnalysisId);
97 if (module == null) {
98 deleteOutputs();
99 return;
100 }
101
102 for (IAnalysisOutput output : module.getOutputs()) {
103 TmfAnalysisOutputElement outputElement = childrenMap.remove(output.getName());
104 if (outputElement == null) {
105 IFolder newresource = ResourcesPlugin.getWorkspace().getRoot().getFolder(path.append(output.getName()));
106 outputElement = new TmfAnalysisOutputElement(output.getName(), newresource, this, output);
107 }
108 outputElement.refreshChildren();
109 }
110 }
111 /* Remove outputs that are not children of this analysis anymore */
112 for (TmfAnalysisOutputElement output : childrenMap.values()) {
113 removeChild(output);
114 }
115 }
116
117 // ------------------------------------------------------------------------
118 // Operations
119 // ------------------------------------------------------------------------
120
121 /**
122 * Get the list of analysis output model elements under this analysis
123 *
124 * @return Array of analysis output elements
125 */
126 public List<TmfAnalysisOutputElement> getAvailableOutputs() {
127 List<ITmfProjectModelElement> children = getChildren();
128 List<TmfAnalysisOutputElement> outputs = new ArrayList<>();
129 for (ITmfProjectModelElement child : children) {
130 if (child instanceof TmfAnalysisOutputElement) {
131 outputs.add((TmfAnalysisOutputElement) child);
132 }
133 }
134 return outputs;
135 }
136
137 /**
138 * Gets the analysis id of this module
139 *
140 * @return The analysis id
141 */
142 public String getAnalysisId() {
143 return fAnalysisId;
144 }
145
146 /**
147 * Gets the help message for this analysis
148 *
149 * @return The help message
150 */
151 public String getHelpMessage() {
152 ITmfProjectModelElement parent = getParent();
153
154 if (parent instanceof TmfTraceElement) {
155 TmfTraceElement traceElement = (TmfTraceElement) parent;
156 ITmfTrace trace = traceElement.getTrace();
157 if (trace != null) {
158 IAnalysisModule module = trace.getAnalysisModule(fAnalysisId);
159 if (module != null) {
160 return module.getHelpText();
161 }
162 }
163 }
164
165 IAnalysisModuleHelper helper = TmfAnalysisManager.getAnalysisModule(fAnalysisId);
166 if (helper == null) {
167 return new String();
168 }
169
170 return helper.getHelpText();
171 }
172
173 /**
174 * Gets the icon file name for the analysis
175 *
176 * @return The analysis icon file name
177 */
178 public String getIconFile() {
179 IAnalysisModuleHelper helper = TmfAnalysisManager.getAnalysisModule(fAnalysisId);
180 if (helper == null) {
181 return null;
182 }
183 return helper.getIcon();
184 }
185
186 /**
187 * Gets the bundle this analysis is from
188 *
189 * @return The analysis bundle
190 */
191 public Bundle getBundle() {
192 IAnalysisModuleHelper helper = TmfAnalysisManager.getAnalysisModule(fAnalysisId);
193 if (helper == null) {
194 return null;
195 }
196 return helper.getBundle();
197 }
198
199 /** Delete all outputs under this analysis element */
200 private void deleteOutputs() {
201 for (TmfAnalysisOutputElement output : getAvailableOutputs()) {
202 removeChild(output);
203 }
204 }
205
206 /**
207 * Make sure the trace this analysis is associated to is the currently
208 * selected one
209 */
210 public void activateParent() {
211 ITmfProjectModelElement parent = getParent();
212
213 if (parent instanceof TmfTraceElement) {
214 TmfTraceElement traceElement = (TmfTraceElement) parent;
215 TmfOpenTraceHelper.openTraceFromElement(traceElement);
216 }
217 }
218 }
This page took 0.039358 seconds and 6 git commands to generate.