Remove all existing @since annotations
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / 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.tracecompass.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.jdt.annotation.NonNull;
26 import org.eclipse.jface.viewers.StyledString.Styler;
27 import org.eclipse.swt.graphics.TextStyle;
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;
31 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
32 import org.osgi.framework.Bundle;
33
34 /**
35 * Class for project elements of type analysis modules
36 *
37 * @author Geneviève Bastien
38 */
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 };
47
48 private final @NonNull IAnalysisModuleHelper fAnalysisHelper;
49 private boolean fCanExecute = true;
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
60 * @param module
61 * The analysis module helper
62 */
63 protected TmfAnalysisElement(String name, IResource resource, ITmfProjectModelElement parent, @NonNull IAnalysisModuleHelper module) {
64 super(name, resource, parent);
65 fAnalysisHelper = module;
66 parent.addChild(this);
67 }
68
69 // ------------------------------------------------------------------------
70 // TmfProjectModelElement
71 // ------------------------------------------------------------------------
72
73 @Override
74 void refreshChildren() {
75 fCanExecute = true;
76
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);
81 }
82
83 /** Get base path for resource */
84 IPath path = getProject().getTracesFolder().getPath();
85 if (fResource instanceof IFolder) {
86 path = ((IFolder) fResource).getFullPath();
87 }
88
89 /*
90 * We can get a list of available outputs once the analysis is
91 * instantiated when the trace is opened
92 */
93 ITmfProjectModelElement parent = getParent();
94 if (parent instanceof TmfCommonProjectElement) {
95 ITmfTrace trace = ((TmfCommonProjectElement) parent).getTrace();
96 if (trace == null) {
97 deleteOutputs();
98 return;
99 }
100
101 IAnalysisModule module = trace.getAnalysisModule(fAnalysisHelper.getId());
102 if (module == null) {
103 deleteOutputs();
104 /*
105 * Trace is opened, but the analysis is null, so it does not
106 * apply
107 */
108 fCanExecute = false;
109 return;
110 }
111
112 for (IAnalysisOutput output : module.getOutputs()) {
113 TmfAnalysisOutputElement outputElement = childrenMap.remove(output.getName());
114 if (outputElement == null) {
115 IFolder newresource = ResourcesPlugin.getWorkspace().getRoot().getFolder(path.append(output.getName()));
116 outputElement = new TmfAnalysisOutputElement(output.getName(), newresource, this, output);
117 }
118 outputElement.refreshChildren();
119 }
120
121 }
122 /* Remove outputs that are not children of this analysis anymore */
123 for (TmfAnalysisOutputElement output : childrenMap.values()) {
124 removeChild(output);
125 }
126 }
127
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
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);
155 }
156 }
157 return outputs;
158 }
159
160 /**
161 * Gets the analysis id of this module
162 *
163 * @return The analysis id
164 */
165 public String getAnalysisId() {
166 return fAnalysisHelper.getId();
167 }
168
169 /**
170 * Gets the help message for this analysis
171 *
172 * @return The help message
173 */
174 public String getHelpMessage() {
175 ITmfProjectModelElement parent = getParent();
176
177 ITmfTrace trace = null;
178 if (parent instanceof TmfTraceElement) {
179 TmfTraceElement traceElement = (TmfTraceElement) parent;
180 trace = traceElement.getTrace();
181 if (trace != null) {
182 IAnalysisModule module = trace.getAnalysisModule(fAnalysisHelper.getId());
183 if (module != null) {
184 return module.getHelpText(trace);
185 }
186 }
187 }
188
189 if (trace != null) {
190 return fAnalysisHelper.getHelpText(trace);
191 }
192
193 return fAnalysisHelper.getHelpText();
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() {
202 return fAnalysisHelper.getIcon();
203 }
204
205 /**
206 * Gets the bundle this analysis is from
207 *
208 * @return The analysis bundle
209 */
210 public Bundle getBundle() {
211 return fAnalysisHelper.getBundle();
212 }
213
214 /** Delete all outputs under this analysis element */
215 private void deleteOutputs() {
216 for (TmfAnalysisOutputElement output : getAvailableOutputs()) {
217 removeChild(output);
218 }
219 }
220
221 /**
222 * Make sure the trace this analysis is associated to is the currently
223 * selected one
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 }
233
234 }
This page took 0.041903 seconds and 6 git commands to generate.