tmf: Create/Open default project if necessary when opening a trace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfAnalysisElement.java
CommitLineData
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
13package org.eclipse.linuxtools.tmf.ui.project.model;
14
15import java.util.ArrayList;
94227c30 16import java.util.HashMap;
c068a752 17import java.util.List;
94227c30 18import java.util.Map;
c068a752
GB
19
20import org.eclipse.core.resources.IFolder;
21import org.eclipse.core.resources.IResource;
22import org.eclipse.core.resources.ResourcesPlugin;
23import org.eclipse.core.runtime.IPath;
24import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModule;
25import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModuleHelper;
26import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisOutput;
27import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisManager;
28import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
29import 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 */
37public 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);
ff3f02c8
AM
163 if (module != null) {
164 return module.getHelpText();
165 }
4bc53929
GB
166 }
167 }
168
c068a752
GB
169 IAnalysisModuleHelper helper = TmfAnalysisManager.getAnalysisModule(fAnalysisId);
170 if (helper == null) {
171 return new String();
172 }
173
174 return helper.getHelpText();
175 }
176
177 /**
178 * Gets the icon file name for the analysis
179 *
180 * @return The analysis icon file name
181 */
182 public String getIconFile() {
183 IAnalysisModuleHelper helper = TmfAnalysisManager.getAnalysisModule(fAnalysisId);
184 if (helper == null) {
185 return null;
186 }
187 return helper.getIcon();
188 }
189
190 /**
191 * Gets the bundle this analysis is from
192 *
193 * @return The analysis bundle
194 */
195 public Bundle getBundle() {
196 IAnalysisModuleHelper helper = TmfAnalysisManager.getAnalysisModule(fAnalysisId);
197 if (helper == null) {
198 return null;
199 }
200 return helper.getBundle();
201 }
202
94227c30
GB
203 /** Delete all outputs under this analysis element */
204 private void deleteOutputs() {
205 for (TmfAnalysisOutputElement output : getAvailableOutputs()) {
206 removeChild(output);
207 }
208 }
209
c068a752 210 /**
94227c30
GB
211 * Make sure the trace this analysis is associated to is the currently
212 * selected one
c068a752
GB
213 */
214 public void activateParent() {
215 ITmfProjectModelElement parent = getParent();
216
217 if (parent instanceof TmfTraceElement) {
218 TmfTraceElement traceElement = (TmfTraceElement) parent;
219 TmfOpenTraceHelper.openTraceFromElement(traceElement);
220 }
221 }
222}
This page took 0.038468 seconds and 5 git commands to generate.