ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfAnalysisElement.java
index 559a655f469d52eee6701734c017759766249e11..0b479a33df930634339326caaa34297089784674 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2013 École Polytechnique de Montréal
+ * Copyright (c) 2013, 2014 École Polytechnique de Montréal
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -8,22 +8,27 @@
  *
  * Contributors:
  *   Geneviève Bastien - Initial API and implementation
+ *   Patrick Tasse - Add support for folder elements
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.ui.project.model;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.eclipse.core.resources.IFolder;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.IPath;
+import org.eclipse.jface.viewers.StyledString.Styler;
 import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModule;
 import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisModuleHelper;
 import org.eclipse.linuxtools.tmf.core.analysis.IAnalysisOutput;
 import org.eclipse.linuxtools.tmf.core.analysis.TmfAnalysisManager;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
+import org.eclipse.swt.graphics.TextStyle;
 import org.osgi.framework.Bundle;
 
 /**
@@ -32,9 +37,17 @@ import org.osgi.framework.Bundle;
  * @author Geneviève Bastien
  * @since 3.0
  */
-public class TmfAnalysisElement extends TmfProjectModelElement {
+public class TmfAnalysisElement extends TmfProjectModelElement implements ITmfStyledProjectModelElement {
+
+    private static final Styler ANALYSIS_CANT_EXECUTE_STYLER = new Styler() {
+        @Override
+        public void applyStyles(TextStyle textStyle) {
+            textStyle.strikeout = true;
+        }
+    };
 
     private final String fAnalysisId;
+    private boolean fCanExecute = true;
 
     /**
      * Constructor
@@ -51,33 +64,27 @@ public class TmfAnalysisElement extends TmfProjectModelElement {
     protected TmfAnalysisElement(String name, IResource resource, ITmfProjectModelElement parent, String id) {
         super(name, resource, parent);
         fAnalysisId = id;
-        refreshOutputs();
+        parent.addChild(this);
     }
 
-    private void refreshOutputs() {
-        List<TmfAnalysisOutputElement> outputs = getAvailableOutputs();
+    // ------------------------------------------------------------------------
+    // TmfProjectModelElement
+    // ------------------------------------------------------------------------
 
-        /* Remove children */
-        getChildren().clear();
+    @Override
+    void refreshChildren() {
+        fCanExecute = true;
 
-        /* Add the children again */
-        for (TmfAnalysisOutputElement module : outputs) {
-            addChild(module);
+        /* Refresh the outputs of this analysis */
+        Map<String, TmfAnalysisOutputElement> childrenMap = new HashMap<>();
+        for (TmfAnalysisOutputElement output : getAvailableOutputs()) {
+            childrenMap.put(output.getName(), output);
         }
 
-    }
-
-    /**
-     * Get the list of analysis elements
-     *
-     * @return Array of analysis elements
-     */
-    public List<TmfAnalysisOutputElement> getAvailableOutputs() {
-        List<TmfAnalysisOutputElement> list = new ArrayList<TmfAnalysisOutputElement>();
-
         IAnalysisModuleHelper helper = TmfAnalysisManager.getAnalysisModule(fAnalysisId);
         if (helper == null) {
-            return list;
+            deleteOutputs();
+            return;
         }
 
         /** Get base path for resource */
@@ -86,33 +93,75 @@ public class TmfAnalysisElement extends TmfProjectModelElement {
             path = ((IFolder) fResource).getFullPath();
         }
 
-        /* We can get a list of available outputs once the analysis is instantiated when the trace is opened */
+        /*
+         * We can get a list of available outputs once the analysis is
+         * instantiated when the trace is opened
+         */
         ITmfProjectModelElement parent = getParent();
-        if (parent instanceof TmfTraceElement) {
-            ITmfTrace trace = ((TmfTraceElement) parent).getTrace();
+        if (parent instanceof TmfCommonProjectElement) {
+            ITmfTrace trace = ((TmfCommonProjectElement) parent).getTrace();
             if (trace == null) {
-                return list;
+                deleteOutputs();
+                return;
             }
 
             IAnalysisModule module = trace.getAnalysisModule(fAnalysisId);
             if (module == null) {
-                return list;
+                deleteOutputs();
+                /*
+                 * Trace is opened, but the analysis is null, so it does not
+                 * apply
+                 */
+                fCanExecute = false;
+                return;
             }
 
             for (IAnalysisOutput output : module.getOutputs()) {
-                if (fResource instanceof IFolder) {
+                TmfAnalysisOutputElement outputElement = childrenMap.remove(output.getName());
+                if (outputElement == null) {
                     IFolder newresource = ResourcesPlugin.getWorkspace().getRoot().getFolder(path.append(output.getName()));
-                    TmfAnalysisOutputElement out = new TmfAnalysisOutputElement(output.getName(), newresource, this, output);
-                    list.add(out);
+                    outputElement = new TmfAnalysisOutputElement(output.getName(), newresource, this, output);
                 }
+                outputElement.refreshChildren();
             }
+
+        }
+        /* Remove outputs that are not children of this analysis anymore */
+        for (TmfAnalysisOutputElement output : childrenMap.values()) {
+            removeChild(output);
         }
-        return list;
     }
 
+    // ------------------------------------------------------------------------
+    // TmfProjectModelElement
+    // ------------------------------------------------------------------------
+
     @Override
-    public TmfProjectElement getProject() {
-        return getParent().getProject();
+    public Styler getStyler() {
+        if (!fCanExecute) {
+            return ANALYSIS_CANT_EXECUTE_STYLER;
+        }
+        return null;
+    }
+
+    // ------------------------------------------------------------------------
+    // Operations
+    // ------------------------------------------------------------------------
+
+    /**
+     * Get the list of analysis output model elements under this analysis
+     *
+     * @return Array of analysis output elements
+     */
+    public List<TmfAnalysisOutputElement> getAvailableOutputs() {
+        List<ITmfProjectModelElement> children = getChildren();
+        List<TmfAnalysisOutputElement> outputs = new ArrayList<>();
+        for (ITmfProjectModelElement child : children) {
+            if (child instanceof TmfAnalysisOutputElement) {
+                outputs.add((TmfAnalysisOutputElement) child);
+            }
+        }
+        return outputs;
     }
 
     /**
@@ -130,11 +179,29 @@ public class TmfAnalysisElement extends TmfProjectModelElement {
      * @return The help message
      */
     public String getHelpMessage() {
+        ITmfProjectModelElement parent = getParent();
+
+        ITmfTrace trace = null;
+        if (parent instanceof TmfTraceElement) {
+            TmfTraceElement traceElement = (TmfTraceElement) parent;
+            trace = traceElement.getTrace();
+            if (trace != null) {
+                IAnalysisModule module = trace.getAnalysisModule(fAnalysisId);
+                if (module != null) {
+                    return module.getHelpText(trace);
+                }
+            }
+        }
+
         IAnalysisModuleHelper helper = TmfAnalysisManager.getAnalysisModule(fAnalysisId);
         if (helper == null) {
             return new String();
         }
 
+        if (trace != null) {
+            return helper.getHelpText(trace);
+        }
+
         return helper.getHelpText();
     }
 
@@ -164,8 +231,16 @@ public class TmfAnalysisElement extends TmfProjectModelElement {
         return helper.getBundle();
     }
 
+    /** Delete all outputs under this analysis element */
+    private void deleteOutputs() {
+        for (TmfAnalysisOutputElement output : getAvailableOutputs()) {
+            removeChild(output);
+        }
+    }
+
     /**
-     * Make sure the trace this analysis is associated to is the currently selected one
+     * Make sure the trace this analysis is associated to is the currently
+     * selected one
      */
     public void activateParent() {
         ITmfProjectModelElement parent = getParent();
@@ -175,4 +250,5 @@ public class TmfAnalysisElement extends TmfProjectModelElement {
             TmfOpenTraceHelper.openTraceFromElement(traceElement);
         }
     }
+
 }
This page took 0.030796 seconds and 5 git commands to generate.