tmf: Support folders in tracing projects
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfTraceTypeUIUtils.java
index cc282d26d828e73297ec9e3b21143f5afffe6119..fa0bd8291666ba3cfc6d5f77d5b62c8bafe40818 100644 (file)
@@ -8,32 +8,34 @@
  *
  * Contributors:
  *   Alexandre Montplaisir - Initial API and implementation
+ *   Patrick Tasse - Add support for folder elements
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.ui.project.model;
 
-import java.io.File;
 import java.util.ArrayList;
 import java.util.Comparator;
 import java.util.HashMap;
+import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.TreeSet;
 
 import org.eclipse.core.resources.IResource;
-import org.eclipse.core.resources.ResourcesPlugin;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IConfigurationElement;
-import org.eclipse.core.runtime.IPath;
 import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Platform;
 import org.eclipse.core.runtime.Status;
-import org.eclipse.linuxtools.internal.tmf.ui.Activator;
+import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
 import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceImportException;
 import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType;
+import org.eclipse.linuxtools.tmf.core.project.model.TmfTraceType.TraceElementType;
 import org.eclipse.linuxtools.tmf.core.project.model.TraceTypeHelper;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
 import org.eclipse.linuxtools.tmf.core.util.Pair;
+import org.eclipse.osgi.util.NLS;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.SelectionEvent;
 import org.eclipse.swt.events.SelectionListener;
@@ -50,11 +52,37 @@ import org.eclipse.swt.widgets.Shell;
  */
 public final class TmfTraceTypeUIUtils {
 
-    private static final String DEFAULT_TRACE_ICON_PATH = "icons" + File.separator + "elcl16" + File.separator + "trace.gif"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
+    /** Extension point ID */
+    public static final String TMF_TRACE_TYPE_UI_ID = "org.eclipse.linuxtools.tmf.ui.tracetypeui"; //$NON-NLS-1$
+
+    /** Extension point element 'type' (should match the type in TmfTraceType) */
+    public static final String TYPE_ELEM = "type"; //$NON-NLS-1$
+
+    /**
+     * Extension point element 'experiment' (should match the type in
+     * TmfTraceType)
+     */
+    public static final String EXPERIMENT_ELEM = "experiment"; //$NON-NLS-1$
+
+    /** Extension point element 'Default editor' */
+    public static final String DEFAULT_EDITOR_ELEM = "defaultEditor"; //$NON-NLS-1$
+
+    /** Extension point element 'Events table type' */
+    public static final String EVENTS_TABLE_TYPE_ELEM = "eventsTableType"; //$NON-NLS-1$
+
+    /** Extension point attribute 'tracetype' */
+    public static final String TRACETYPE_ATTR = "tracetype"; //$NON-NLS-1$
+
+    /** Extension point attribute 'icon' */
+    public static final String ICON_ATTR = "icon"; //$NON-NLS-1$
+
+    /** Extension point attribute 'class' (attribute of eventsTableType) */
+    public static final String CLASS_ATTR = "class"; //$NON-NLS-1$
 
     private static final char SEPARATOR = ':';
 
-    private TmfTraceTypeUIUtils() {}
+    private TmfTraceTypeUIUtils() {
+    }
 
     private static List<Pair<Integer, TraceTypeHelper>> reduce(List<Pair<Integer, TraceTypeHelper>> candidates) {
         List<Pair<Integer, TraceTypeHelper>> retVal = new ArrayList<>();
@@ -89,7 +117,6 @@ public final class TmfTraceTypeUIUtils {
         return count == 0;
     }
 
-
     /**
      * Is the trace type id a custom (user-defined) trace type. These are the
      * traces like : text and xml defined by the custom trace wizard.
@@ -179,6 +206,9 @@ public final class TmfTraceTypeUIUtils {
         TreeSet<Pair<Integer, TraceTypeHelper>> validCandidates = new TreeSet<>(comparator);
         final Iterable<TraceTypeHelper> traceTypeHelpers = type.getTraceTypeHelpers();
         for (TraceTypeHelper traceTypeHelper : traceTypeHelpers) {
+            if (traceTypeHelper.isExperimentType()) {
+                continue;
+            }
             int confidence = traceTypeHelper.validateWithConfidence(path);
             if (confidence >= 0) {
                 // insert in the tree map, ordered by confidence (highest confidence first) then name
@@ -189,7 +219,7 @@ public final class TmfTraceTypeUIUtils {
 
         TraceTypeHelper traceTypeToSet = null;
         if (validCandidates.isEmpty()) {
-            final String errorMsg = Messages.TmfOpenTraceHelper_NoTraceTypeMatch + path;
+            final String errorMsg = NLS.bind(Messages.TmfOpenTraceHelper_NoTraceTypeMatch, path);
             throw new TmfTraceImportException(errorMsg);
         } else if (validCandidates.size() != 1) {
             List<Pair<Integer, TraceTypeHelper>> candidates = new ArrayList<>(validCandidates);
@@ -222,45 +252,99 @@ public final class TmfTraceTypeUIUtils {
         return traceTypeToSet;
     }
 
-
     /**
      * Set the trace type of a {@Link TraceTypeHelper}. Should only be
      * used internally by this project.
      *
-     * @param path
-     *            the {@link IPath} path of the resource to set
+     * @param resource
+     *            the resource to set
      * @param traceType
      *            the {@link TraceTypeHelper} to set the trace type to.
      * @return Status.OK_Status if successful, error is otherwise.
      * @throws CoreException
      *             An exception caused by accessing eclipse project items.
      */
-    public static IStatus setTraceType(IPath path, TraceTypeHelper traceType) throws CoreException {
-        IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
-        String traceBundle = null, traceTypeId = traceType.getCanonicalName(), traceIcon = null;
-        if (isCustomTraceId(traceTypeId)) {
-            traceBundle = Activator.getDefault().getBundle().getSymbolicName();
-            traceIcon = DEFAULT_TRACE_ICON_PATH;
-        } else {
-            IConfigurationElement ce = TmfTraceType.getInstance().getTraceAttributes(traceTypeId);
-            traceBundle = ce.getContributor().getName();
-            traceIcon = ce.getAttribute(TmfTraceType.ICON_ATTR);
-        }
+    public static IStatus setTraceType(IResource resource, TraceTypeHelper traceType) throws CoreException {
+        String traceTypeId = traceType.getCanonicalName();
 
-        resource.setPersistentProperty(TmfCommonConstants.TRACEBUNDLE, traceBundle);
         resource.setPersistentProperty(TmfCommonConstants.TRACETYPE, traceTypeId);
-        resource.setPersistentProperty(TmfCommonConstants.TRACEICON, traceIcon);
 
         TmfProjectElement tmfProject = TmfProjectRegistry.getProject(resource.getProject(), true);
-        final TmfTraceFolder tracesFolder = tmfProject.getTracesFolder();
-        List<TmfTraceElement> traces = tracesFolder.getTraces();
-        for (TmfTraceElement traceElement : traces) {
-            if (traceElement.getName().equals(resource.getName())) {
-                traceElement.refreshTraceType();
-                break;
+        if (tmfProject.getTracesFolder().getPath().isPrefixOf(resource.getFullPath())) {
+            String elementPath = resource.getFullPath().makeRelativeTo(tmfProject.getTracesFolder().getPath()).toString();
+            refreshTraceElement(tmfProject.getTracesFolder().getTraces(), elementPath);
+        } else if (resource.getParent().equals(tmfProject.getExperimentsFolder().getResource())) {
+            /* The trace type to set is for an experiment */
+            for (TmfExperimentElement experimentElement : tmfProject.getExperimentsFolder().getExperiments()) {
+                if (resource.equals(experimentElement.getResource())) {
+                    experimentElement.refreshTraceType();
+                    break;
+                }
+            }
+        } else {
+            for (TmfExperimentElement experimentElement : tmfProject.getExperimentsFolder().getExperiments()) {
+                if (experimentElement.getPath().isPrefixOf(resource.getFullPath())) {
+                    String elementPath = resource.getFullPath().makeRelativeTo(experimentElement.getPath()).toString();
+                    refreshTraceElement(experimentElement.getTraces(), elementPath);
+                    break;
+                }
             }
         }
         tmfProject.refresh();
         return Status.OK_STATUS;
     }
+
+    private static void refreshTraceElement(List<TmfTraceElement> traceElements, String elementPath) {
+        for (TmfTraceElement traceElement : traceElements) {
+            if (traceElement.getElementPath().equals(elementPath)) {
+                traceElement.refreshTraceType();
+                break;
+            }
+        }
+    }
+
+    /**
+     * Retrieves all configuration elements from the platform extension registry
+     * for the trace type UI extension.
+     *
+     * @param elType
+     *            The type of trace type requested, either TRACE or EXPERIMENT
+     * @return An array of trace type configuration elements
+     */
+    public static IConfigurationElement[] getTypeUIElements(TraceElementType elType) {
+        String elementName = TYPE_ELEM;
+        if (elType == TraceElementType.EXPERIMENT) {
+            elementName = EXPERIMENT_ELEM;
+        }
+        IConfigurationElement[] elements =
+                Platform.getExtensionRegistry().getConfigurationElementsFor(TMF_TRACE_TYPE_UI_ID);
+        List<IConfigurationElement> typeElements = new LinkedList<>();
+        for (IConfigurationElement element : elements) {
+            if (element.getName().equals(elementName)) {
+                typeElements.add(element);
+            }
+        }
+        return typeElements.toArray(new IConfigurationElement[typeElements.size()]);
+    }
+
+    /**
+     * Get the UI elements for the given trace type
+     *
+     * @param traceType
+     *            The tracetype ID
+     * @param elType
+     *            The type of trace type requested, either TRACE or EXPERIMENT
+     * @return The top-level configuration element (access its children with
+     *         .getChildren()). Or null if there is no such element.
+     */
+    @Nullable
+    public static IConfigurationElement getTraceUIAttributes(String traceType, TraceElementType elType) {
+        IConfigurationElement[] elements = getTypeUIElements(elType);
+        for (IConfigurationElement ce : elements) {
+            if (traceType.equals(ce.getAttribute(TRACETYPE_ATTR))) {
+                return ce;
+            }
+        }
+        return null;
+    }
 }
This page took 0.025689 seconds and 5 git commands to generate.