| 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.Collections; |
| 18 | import java.util.HashMap; |
| 19 | import java.util.List; |
| 20 | import java.util.Map; |
| 21 | |
| 22 | import org.eclipse.core.resources.IFolder; |
| 23 | import org.eclipse.core.resources.IResource; |
| 24 | import org.eclipse.core.resources.ResourcesPlugin; |
| 25 | import org.eclipse.core.runtime.IPath; |
| 26 | import org.eclipse.jdt.annotation.NonNull; |
| 27 | import org.eclipse.jface.viewers.StyledString.Styler; |
| 28 | import org.eclipse.swt.graphics.Image; |
| 29 | import org.eclipse.swt.graphics.TextStyle; |
| 30 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule; |
| 31 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModuleHelper; |
| 32 | import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisOutput; |
| 33 | import org.eclipse.tracecompass.tmf.core.project.model.ITmfPropertiesProvider; |
| 34 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 35 | import org.eclipse.tracecompass.tmf.ui.properties.ReadOnlyTextPropertyDescriptor; |
| 36 | import org.eclipse.ui.views.properties.IPropertyDescriptor; |
| 37 | import org.eclipse.ui.views.properties.IPropertySource2; |
| 38 | import org.osgi.framework.Bundle; |
| 39 | |
| 40 | /** |
| 41 | * Class for project elements of type analysis modules |
| 42 | * |
| 43 | * @author Geneviève Bastien |
| 44 | * @since 2.0 |
| 45 | */ |
| 46 | public class TmfAnalysisElement extends TmfProjectModelElement implements ITmfStyledProjectModelElement, IPropertySource2 { |
| 47 | |
| 48 | private static final Styler ANALYSIS_CANT_EXECUTE_STYLER = new Styler() { |
| 49 | @Override |
| 50 | public void applyStyles(TextStyle textStyle) { |
| 51 | textStyle.strikeout = true; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | private final @NonNull IAnalysisModuleHelper fAnalysisHelper; |
| 56 | private volatile boolean fCanExecute = true; |
| 57 | |
| 58 | private static final String ANALYSIS_PROPERTIES_CATEGORY = Messages.TmfAnalysisElement_AnalysisProperties; |
| 59 | private static final String HELPER_PROPERTIES_CATEGORY = Messages.TmfAnalysisElement_HelperProperties; |
| 60 | |
| 61 | /** |
| 62 | * Constructor |
| 63 | * |
| 64 | * @param name |
| 65 | * Name of the analysis |
| 66 | * @param resource |
| 67 | * The resource |
| 68 | * @param parent |
| 69 | * Parent of the analysis |
| 70 | * @param module |
| 71 | * The analysis module helper |
| 72 | * @since 2.0 |
| 73 | */ |
| 74 | protected TmfAnalysisElement(String name, IResource resource, |
| 75 | TmfViewsElement parent, @NonNull IAnalysisModuleHelper module) { |
| 76 | super(name, resource, parent); |
| 77 | fAnalysisHelper = module; |
| 78 | } |
| 79 | |
| 80 | // ------------------------------------------------------------------------ |
| 81 | // TmfProjectModelElement |
| 82 | // ------------------------------------------------------------------------ |
| 83 | |
| 84 | /** |
| 85 | * @since 2.0 |
| 86 | */ |
| 87 | @Override |
| 88 | public TmfViewsElement getParent() { |
| 89 | /* Type enforced at constructor */ |
| 90 | return (TmfViewsElement) super.getParent(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @since 2.0 |
| 95 | */ |
| 96 | @Override |
| 97 | protected void refreshChildren() { |
| 98 | fCanExecute = true; |
| 99 | |
| 100 | /* Refresh the outputs of this analysis */ |
| 101 | Map<String, TmfAnalysisOutputElement> childrenMap = new HashMap<>(); |
| 102 | for (TmfAnalysisOutputElement output : getAvailableOutputs()) { |
| 103 | childrenMap.put(output.getName(), output); |
| 104 | } |
| 105 | |
| 106 | /** Get base path for resource */ |
| 107 | final TmfTraceFolder tracesFolder = getProject().getTracesFolder(); |
| 108 | if (tracesFolder == null) { |
| 109 | return; |
| 110 | } |
| 111 | IPath path = tracesFolder.getPath(); |
| 112 | IResource resource = getResource(); |
| 113 | if (resource instanceof IFolder) { |
| 114 | path = ((IFolder) resource).getFullPath(); |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * We can get a list of available outputs once the analysis is |
| 119 | * instantiated when the trace is opened |
| 120 | */ |
| 121 | TmfCommonProjectElement parentTraceElement = getParent().getParent(); |
| 122 | |
| 123 | ITmfTrace trace = parentTraceElement.getTrace(); |
| 124 | if (trace == null) { |
| 125 | deleteOutputs(); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | IAnalysisModule module = trace.getAnalysisModule(fAnalysisHelper.getId()); |
| 130 | if (module == null) { |
| 131 | deleteOutputs(); |
| 132 | /* |
| 133 | * Trace is opened, but the analysis is null, so it does not |
| 134 | * apply |
| 135 | */ |
| 136 | fCanExecute = false; |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | for (IAnalysisOutput output : module.getOutputs()) { |
| 141 | TmfAnalysisOutputElement outputElement = childrenMap.remove(output.getName()); |
| 142 | if (outputElement == null) { |
| 143 | IFolder newresource = ResourcesPlugin.getWorkspace().getRoot().getFolder(path.append(output.getName())); |
| 144 | outputElement = new TmfAnalysisOutputElement(output.getName(), newresource, this, output); |
| 145 | addChild(outputElement); |
| 146 | } |
| 147 | outputElement.refreshChildren(); |
| 148 | } |
| 149 | |
| 150 | /* Remove outputs that are not children of this analysis anymore */ |
| 151 | for (TmfAnalysisOutputElement output : childrenMap.values()) { |
| 152 | removeChild(output); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @since 2.0 |
| 158 | */ |
| 159 | @Override |
| 160 | public Image getIcon() { |
| 161 | String iconFile = getIconFile(); |
| 162 | if (iconFile != null) { |
| 163 | Bundle bundle = getBundle(); |
| 164 | if (bundle != null) { |
| 165 | Image icon = TmfProjectModelIcons.loadIcon(bundle, iconFile); |
| 166 | if (icon != null) { |
| 167 | return icon; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | return TmfProjectModelIcons.DEFAULT_ANALYSIS_ICON; |
| 172 | } |
| 173 | |
| 174 | // ------------------------------------------------------------------------ |
| 175 | // TmfProjectModelElement |
| 176 | // ------------------------------------------------------------------------ |
| 177 | |
| 178 | @Override |
| 179 | public Styler getStyler() { |
| 180 | if (!canExecute()) { |
| 181 | return ANALYSIS_CANT_EXECUTE_STYLER; |
| 182 | } |
| 183 | return null; |
| 184 | } |
| 185 | |
| 186 | // ------------------------------------------------------------------------ |
| 187 | // Operations |
| 188 | // ------------------------------------------------------------------------ |
| 189 | |
| 190 | /** |
| 191 | * Get the list of analysis output model elements under this analysis |
| 192 | * |
| 193 | * @return Array of analysis output elements |
| 194 | */ |
| 195 | public List<TmfAnalysisOutputElement> getAvailableOutputs() { |
| 196 | List<ITmfProjectModelElement> children = getChildren(); |
| 197 | List<TmfAnalysisOutputElement> outputs = new ArrayList<>(); |
| 198 | for (ITmfProjectModelElement child : children) { |
| 199 | if (child instanceof TmfAnalysisOutputElement) { |
| 200 | outputs.add((TmfAnalysisOutputElement) child); |
| 201 | } |
| 202 | } |
| 203 | return outputs; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Gets the analysis id of this module |
| 208 | * |
| 209 | * @return The analysis id |
| 210 | */ |
| 211 | public String getAnalysisId() { |
| 212 | return fAnalysisHelper.getId(); |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Gets the help message for this analysis |
| 217 | * |
| 218 | * @return The help message |
| 219 | */ |
| 220 | public String getHelpMessage() { |
| 221 | TmfCommonProjectElement parentTrace = getParent().getParent(); |
| 222 | |
| 223 | ITmfTrace trace = null; |
| 224 | if (parentTrace instanceof TmfTraceElement) { |
| 225 | TmfTraceElement traceElement = (TmfTraceElement) parentTrace; |
| 226 | trace = traceElement.getTrace(); |
| 227 | if (trace != null) { |
| 228 | IAnalysisModule module = trace.getAnalysisModule(fAnalysisHelper.getId()); |
| 229 | if (module != null) { |
| 230 | return module.getHelpText(trace); |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | if (trace != null) { |
| 236 | return fAnalysisHelper.getHelpText(trace); |
| 237 | } |
| 238 | |
| 239 | return fAnalysisHelper.getHelpText(); |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Gets the icon file name for the analysis |
| 244 | * |
| 245 | * @return The analysis icon file name |
| 246 | */ |
| 247 | public String getIconFile() { |
| 248 | return fAnalysisHelper.getIcon(); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Gets the bundle this analysis is from |
| 253 | * |
| 254 | * @return The analysis bundle |
| 255 | */ |
| 256 | public Bundle getBundle() { |
| 257 | return fAnalysisHelper.getBundle(); |
| 258 | } |
| 259 | |
| 260 | /** Delete all outputs under this analysis element */ |
| 261 | private void deleteOutputs() { |
| 262 | for (TmfAnalysisOutputElement output : getAvailableOutputs()) { |
| 263 | removeChild(output); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * Make sure the trace this analysis is associated to is the currently |
| 269 | * selected one |
| 270 | * @since 2.0 |
| 271 | */ |
| 272 | public void activateParentTrace() { |
| 273 | TmfCommonProjectElement parentTrace = getParent().getParent(); |
| 274 | |
| 275 | if (parentTrace instanceof TmfTraceElement) { |
| 276 | TmfTraceElement traceElement = (TmfTraceElement) parentTrace; |
| 277 | TmfOpenTraceHelper.openTraceFromElement(traceElement); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Checks whether the analysis can be executed or not. |
| 283 | * |
| 284 | * @return <code>true</code> if analysis can be executed else |
| 285 | * <code>false</code> |
| 286 | * @since 3.0 |
| 287 | */ |
| 288 | public boolean canExecute() { |
| 289 | return fCanExecute; |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Gets the analysis helper for this analysis. |
| 294 | * |
| 295 | * @return the analysis module helper |
| 296 | * @since 3.0 |
| 297 | */ |
| 298 | @NonNull protected IAnalysisModuleHelper getAnalysisHelper() { |
| 299 | return fAnalysisHelper; |
| 300 | } |
| 301 | |
| 302 | // ------------------------------------------------------------------------ |
| 303 | // IPropertySource2 |
| 304 | // ------------------------------------------------------------------------ |
| 305 | |
| 306 | /** |
| 307 | * @since 2.0 |
| 308 | */ |
| 309 | @Override |
| 310 | public Object getEditableValue() { |
| 311 | return null; |
| 312 | } |
| 313 | |
| 314 | /** |
| 315 | * Get the analysis properties of this analysisElement if the corresponding |
| 316 | * analysis exists for the current trace |
| 317 | * |
| 318 | * @return a map with the names and values of the trace properties |
| 319 | * respectively as keys and values |
| 320 | */ |
| 321 | private Map<String, String> getAnalysisProperties() { |
| 322 | ITmfProjectModelElement parent = getParent(); |
| 323 | if (!(parent instanceof TmfViewsElement)) { |
| 324 | return Collections.EMPTY_MAP; |
| 325 | } |
| 326 | parent = parent.getParent(); |
| 327 | if (parent instanceof TmfCommonProjectElement) { |
| 328 | ITmfTrace trace = ((TmfCommonProjectElement) parent).getTrace(); |
| 329 | if (trace == null) { |
| 330 | return Collections.EMPTY_MAP; |
| 331 | } |
| 332 | IAnalysisModule module = trace.getAnalysisModule(fAnalysisHelper.getId()); |
| 333 | if (module instanceof ITmfPropertiesProvider) { |
| 334 | return ((ITmfPropertiesProvider) module).getProperties(); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | return Collections.EMPTY_MAP; |
| 339 | } |
| 340 | |
| 341 | private Map<String, String> getAnalysisHelperProperties() { |
| 342 | if (fAnalysisHelper instanceof ITmfPropertiesProvider) { |
| 343 | ITmfPropertiesProvider analysisProperties = (ITmfPropertiesProvider) fAnalysisHelper; |
| 344 | return analysisProperties.getProperties(); |
| 345 | } |
| 346 | return Collections.EMPTY_MAP; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * @since 2.0 |
| 351 | */ |
| 352 | @Override |
| 353 | public IPropertyDescriptor[] getPropertyDescriptors() { |
| 354 | Map<String, String> helperProperties = getAnalysisHelperProperties(); |
| 355 | Map<String, String> analysisProperties = getAnalysisProperties(); |
| 356 | if (!analysisProperties.isEmpty() || !helperProperties.isEmpty()) { |
| 357 | List<IPropertyDescriptor> propertyDescriptorArray = new ArrayList<>(analysisProperties.size() + helperProperties.size()); |
| 358 | for (Map.Entry<String, String> varName : helperProperties.entrySet()) { |
| 359 | ReadOnlyTextPropertyDescriptor descriptor = new ReadOnlyTextPropertyDescriptor(this.getName() + '_' + varName.getKey(), varName.getKey()); |
| 360 | descriptor.setCategory(HELPER_PROPERTIES_CATEGORY); |
| 361 | propertyDescriptorArray.add(descriptor); |
| 362 | } |
| 363 | for (Map.Entry<String, String> varName : analysisProperties.entrySet()) { |
| 364 | ReadOnlyTextPropertyDescriptor descriptor = new ReadOnlyTextPropertyDescriptor(this.getName() + '_' + varName.getKey(), varName.getKey()); |
| 365 | descriptor.setCategory(ANALYSIS_PROPERTIES_CATEGORY); |
| 366 | propertyDescriptorArray.add(descriptor); |
| 367 | } |
| 368 | return propertyDescriptorArray.toArray(new IPropertyDescriptor[analysisProperties.size() + helperProperties.size()]); |
| 369 | } |
| 370 | return new IPropertyDescriptor[0]; |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * @since 2.0 |
| 375 | */ |
| 376 | @Override |
| 377 | public Object getPropertyValue(Object id) { |
| 378 | if (id == null) { |
| 379 | return null; |
| 380 | } |
| 381 | Map<String, String> properties = getAnalysisHelperProperties(); |
| 382 | String key = (String) id; |
| 383 | /* Remove name from key */ |
| 384 | key = key.substring(this.getName().length() + 1); |
| 385 | if (properties.containsKey(key)) { |
| 386 | String value = properties.get(key); |
| 387 | return value; |
| 388 | } |
| 389 | |
| 390 | properties = getAnalysisProperties(); |
| 391 | if (properties.containsKey(key)) { |
| 392 | String value = properties.get(key); |
| 393 | return value; |
| 394 | } |
| 395 | |
| 396 | return null; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * @since 2.0 |
| 401 | */ |
| 402 | @Override |
| 403 | public final void resetPropertyValue(Object id) { |
| 404 | } |
| 405 | |
| 406 | /** |
| 407 | * @since 2.0 |
| 408 | */ |
| 409 | @Override |
| 410 | public final void setPropertyValue(Object id, Object value) { |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * @since 2.0 |
| 415 | */ |
| 416 | @Override |
| 417 | public final boolean isPropertyResettable(Object id) { |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | /** |
| 422 | * @since 2.0 |
| 423 | */ |
| 424 | @Override |
| 425 | public final boolean isPropertySet(Object id) { |
| 426 | return false; |
| 427 | } |
| 428 | |
| 429 | } |