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