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