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