TMF: Fix a bug in UI when refreshing analysis elements under traces
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfAnalysisElement.java
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;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
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;
56 parent.addChild(this);
57 }
58
59 // ------------------------------------------------------------------------
60 // TmfProjectModelElement
61 // ------------------------------------------------------------------------
62
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);
69 }
70
71 IAnalysisModuleHelper helper = TmfAnalysisManager.getAnalysisModule(fAnalysisId);
72 if (helper == null) {
73 deleteOutputs();
74 return;
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
83 /*
84 * We can get a list of available outputs once the analysis is
85 * instantiated when the trace is opened
86 */
87 ITmfProjectModelElement parent = getParent();
88 if (parent instanceof TmfTraceElement) {
89 ITmfTrace trace = ((TmfTraceElement) parent).getTrace();
90 if (trace == null) {
91 deleteOutputs();
92 return;
93 }
94
95 IAnalysisModule module = trace.getAnalysisModule(fAnalysisId);
96 if (module == null) {
97 deleteOutputs();
98 return;
99 }
100
101 for (IAnalysisOutput output : module.getOutputs()) {
102 TmfAnalysisOutputElement outputElement = childrenMap.remove(output.getName());
103 if (outputElement == null) {
104 IFolder newresource = ResourcesPlugin.getWorkspace().getRoot().getFolder(path.append(output.getName()));
105 outputElement = new TmfAnalysisOutputElement(output.getName(), newresource, this, output);
106 }
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);
131 }
132 }
133 return outputs;
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() {
156 IAnalysisModuleHelper helper = TmfAnalysisManager.getAnalysisModule(fAnalysisId);
157 if (helper == null) {
158 return new String();
159 }
160
161 return helper.getHelpText();
162 }
163
164 /**
165 * Gets the icon file name for the analysis
166 *
167 * @return The analysis icon file name
168 */
169 public String getIconFile() {
170 IAnalysisModuleHelper helper = TmfAnalysisManager.getAnalysisModule(fAnalysisId);
171 if (helper == null) {
172 return null;
173 }
174 return helper.getIcon();
175 }
176
177 /**
178 * Gets the bundle this analysis is from
179 *
180 * @return The analysis bundle
181 */
182 public Bundle getBundle() {
183 IAnalysisModuleHelper helper = TmfAnalysisManager.getAnalysisModule(fAnalysisId);
184 if (helper == null) {
185 return null;
186 }
187 return helper.getBundle();
188 }
189
190 /** Delete all outputs under this analysis element */
191 private void deleteOutputs() {
192 for (TmfAnalysisOutputElement output : getAvailableOutputs()) {
193 removeChild(output);
194 }
195 }
196
197 /**
198 * Make sure the trace this analysis is associated to is the currently
199 * selected one
200 */
201 public void activateParent() {
202 ITmfProjectModelElement parent = getParent();
203
204 if (parent instanceof TmfTraceElement) {
205 TmfTraceElement traceElement = (TmfTraceElement) parent;
206 TmfOpenTraceHelper.openTraceFromElement(traceElement);
207 }
208 }
209 }
This page took 0.035909 seconds and 6 git commands to generate.