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