tmf : show environment variables of traces under Experiments
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / project / model / TmfTraceElement.java
index c15a7b19782a1acf4ecb5ac38c071f9174af1699..0abd421b0034831f02542ba994abbbf5b143ea35 100644 (file)
@@ -9,8 +9,11 @@
  * Contributors:
  *   Francois Chouinard - Initial API and implementation
  *   Bernd Hufmann - Added supplementary files handling
- *   Geneviève Bastien - Moved supplementary files handling to parent class, added
- *                      code to copy trace
+ *   Geneviève Bastien - Moved supplementary files handling to parent class,
+ *                       added code to copy trace
+ *   Patrick Tasse - Close editors to release resources
+ *   Jean-Christian Kouame - added trace properties to be shown into
+ *                           the properties view
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.ui.project.model;
@@ -37,17 +40,26 @@ import org.eclipse.linuxtools.internal.tmf.ui.parsers.custom.CustomXmlTraceDefin
 import org.eclipse.linuxtools.tmf.core.TmfCommonConstants;
 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
+import org.eclipse.linuxtools.tmf.core.trace.ITmfTraceProperties;
 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
-import org.eclipse.linuxtools.tmf.core.util.ReadOnlyTextPropertyDescriptor;
+import org.eclipse.linuxtools.tmf.core.trace.TmfTraceManager;
 import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
+import org.eclipse.linuxtools.tmf.ui.properties.ReadOnlyTextPropertyDescriptor;
 import org.eclipse.ui.IActionFilter;
+import org.eclipse.ui.IEditorReference;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.IWorkbenchWindow;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.PlatformUI;
+import org.eclipse.ui.part.FileEditorInput;
 import org.eclipse.ui.views.properties.IPropertyDescriptor;
 import org.eclipse.ui.views.properties.IPropertySource2;
 
 /**
- * Implementation of trace model element representing a trace. It provides methods to instantiate
- * <code>ITmfTrace</code> and <code>ITmfEvent</code> as well as editor ID from the trace type
- * extension definition.
+ * Implementation of trace model element representing a trace. It provides
+ * methods to instantiate <code>ITmfTrace</code> and <code>ITmfEvent</code> as
+ * well as editor ID from the trace type extension definition.
  *
  * @version 1.0
  * @author Francois Chouinard
@@ -69,12 +81,13 @@ public class TmfTraceElement extends TmfWithFolderElement implements IActionFilt
     public static final String IS_LINKED = "isLinked"; //$NON-NLS-1$
 
     // Property View stuff
-    private static final String sfInfoCategory = "Info"; //$NON-NLS-1$
-    private static final String sfName = "name"; //$NON-NLS-1$
-    private static final String sfPath = "path"; //$NON-NLS-1$
-    private static final String sfLocation = "location"; //$NON-NLS-1$
-    private static final String sfEventType = "type"; //$NON-NLS-1$
-    private static final String sfIsLinked = "linked"; //$NON-NLS-1$
+    private static final String sfResourcePropertiesCategory = Messages.TmfTraceElement_ResourceProperties;
+    private static final String sfName = Messages.TmfTraceElement_Name;
+    private static final String sfPath = Messages.TmfTraceElement_Path;
+    private static final String sfLocation = Messages.TmfTraceElement_Location;
+    private static final String sfEventType = Messages.TmfTraceElement_EventType;
+    private static final String sfIsLinked = Messages.TmfTraceElement_IsLinked;
+    private static final String sfTracePropertiesCategory = Messages.TmfTraceElement_TraceProperties;
 
     private static final ReadOnlyTextPropertyDescriptor sfNameDescriptor = new ReadOnlyTextPropertyDescriptor(sfName, sfName);
     private static final ReadOnlyTextPropertyDescriptor sfPathDescriptor = new ReadOnlyTextPropertyDescriptor(sfPath, sfPath);
@@ -86,11 +99,11 @@ public class TmfTraceElement extends TmfWithFolderElement implements IActionFilt
             sfTypeDescriptor, sfIsLinkedDescriptor };
 
     static {
-        sfNameDescriptor.setCategory(sfInfoCategory);
-        sfPathDescriptor.setCategory(sfInfoCategory);
-        sfLocationDescriptor.setCategory(sfInfoCategory);
-        sfTypeDescriptor.setCategory(sfInfoCategory);
-        sfIsLinkedDescriptor.setCategory(sfInfoCategory);
+        sfNameDescriptor.setCategory(sfResourcePropertiesCategory);
+        sfPathDescriptor.setCategory(sfResourcePropertiesCategory);
+        sfLocationDescriptor.setCategory(sfResourcePropertiesCategory);
+        sfTypeDescriptor.setCategory(sfResourcePropertiesCategory);
+        sfIsLinkedDescriptor.setCategory(sfResourcePropertiesCategory);
     }
 
     private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$
@@ -106,12 +119,14 @@ public class TmfTraceElement extends TmfWithFolderElement implements IActionFilt
     // Static initialization
     // ------------------------------------------------------------------------
 
-    // The mapping of available trace type IDs to their corresponding configuration element
+    // The mapping of available trace type IDs to their corresponding
+    // configuration element
     private static final Map<String, IConfigurationElement> sfTraceTypeAttributes = new HashMap<String, IConfigurationElement>();
     private static final Map<String, IConfigurationElement> sfTraceCategories = new HashMap<String, IConfigurationElement>();
 
     /**
-     *  Initialize statically at startup by getting extensions from the platform extension registry.
+     * Initialize statically at startup by getting extensions from the platform
+     * extension registry.
      */
     public static void init() {
         IConfigurationElement[] config = Platform.getExtensionRegistry().getConfigurationElementsFor(TmfTraceType.TMF_TRACE_TYPE_ID);
@@ -131,21 +146,28 @@ public class TmfTraceElement extends TmfWithFolderElement implements IActionFilt
     // Constructors
     // ------------------------------------------------------------------------
     /**
-     * Constructor.
-     * Creates trace model element under the trace folder.
-     * @param name The name of trace
-     * @param trace The trace resource.
-     * @param parent The parent element (trace folder)
+     * Constructor. Creates trace model element under the trace folder.
+     *
+     * @param name
+     *            The name of trace
+     * @param trace
+     *            The trace resource.
+     * @param parent
+     *            The parent element (trace folder)
      */
     public TmfTraceElement(String name, IResource trace, TmfTraceFolder parent) {
         this(name, trace, (TmfProjectModelElement) parent);
     }
+
     /**
-     * Constructor.
-     * Creates trace model element under the experiment folder.
-     * @param name The name of trace
-     * @param trace The trace resource.
-     * @param parent The parent element (experiment folder)
+     * Constructor. Creates trace model element under the experiment folder.
+     *
+     * @param name
+     *            The name of trace
+     * @param trace
+     *            The trace resource.
+     * @param parent
+     *            The parent element (experiment folder)
      */
     public TmfTraceElement(String name, IResource trace, TmfExperimentElement parent) {
         this(name, trace, (TmfProjectModelElement) parent);
@@ -162,6 +184,7 @@ public class TmfTraceElement extends TmfWithFolderElement implements IActionFilt
     // ------------------------------------------------------------------------
     /**
      * Returns the trace type ID.
+     *
      * @return trace type ID.
      */
     public String getTraceType() {
@@ -169,8 +192,8 @@ public class TmfTraceElement extends TmfWithFolderElement implements IActionFilt
     }
 
     /**
-     * Refreshes the trace type filed by reading the trace type persistent property of the resource
-     * referenece.
+     * Refreshes the trace type filed by reading the trace type persistent
+     * property of the resource referenece.
      */
     public void refreshTraceType() {
         try {
@@ -181,7 +204,8 @@ public class TmfTraceElement extends TmfWithFolderElement implements IActionFilt
     }
 
     /**
-     * Instantiate a <code>ITmfTrace</code> object based on the trace type and the corresponding extension.
+     * Instantiate a <code>ITmfTrace</code> object based on the trace type and
+     * the corresponding extension.
      *
      * @return the <code>ITmfTrace</code> or <code>null</code> for an error
      */
@@ -207,6 +231,9 @@ public class TmfTraceElement extends TmfWithFolderElement implements IActionFilt
                     }
                 }
                 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
+                if (ce == null) {
+                    return null;
+                }
                 ITmfTrace trace = (ITmfTrace) ce.createExecutableExtension(TmfTraceType.TRACE_TYPE_ATTR);
                 return trace;
             }
@@ -217,7 +244,8 @@ public class TmfTraceElement extends TmfWithFolderElement implements IActionFilt
     }
 
     /**
-     * Instantiate a <code>ITmfEvent</code> object based on the trace type and the corresponding extension.
+     * Instantiate a <code>ITmfEvent</code> object based on the trace type and
+     * the corresponding extension.
      *
      * @return the <code>ITmfEvent</code> or <code>null</code> for an error
      */
@@ -239,6 +267,9 @@ public class TmfTraceElement extends TmfWithFolderElement implements IActionFilt
                     }
                 }
                 IConfigurationElement ce = sfTraceTypeAttributes.get(fTraceTypeId);
+                if (ce == null) {
+                    return null;
+                }
                 ITmfEvent event = (ITmfEvent) ce.createExecutableExtension(TmfTraceType.EVENT_TYPE_ATTR);
                 return event;
             }
@@ -250,6 +281,7 @@ public class TmfTraceElement extends TmfWithFolderElement implements IActionFilt
 
     /**
      * Returns the optional editor ID from the trace type extension.
+     *
      * @return the editor ID or <code>null</code> if not defined.
      */
     public String getEditorId() {
@@ -270,12 +302,14 @@ public class TmfTraceElement extends TmfWithFolderElement implements IActionFilt
     }
 
     /**
-     * Returns the file resource used to store bookmarks after creating it if necessary.
-     * If the trace resource is a file, it is returned directly.
-     * If the trace resource is a folder, a linked file is returned.
-     * The file will be created if it does not exist.
+     * Returns the file resource used to store bookmarks after creating it if
+     * necessary. If the trace resource is a file, it is returned directly. If
+     * the trace resource is a folder, a linked file is returned. The file will
+     * be created if it does not exist.
+     *
      * @return the bookmarks file
-     * @throws CoreException if the bookmarks file cannot be created
+     * @throws CoreException
+     *             if the bookmarks file cannot be created
      * @since 2.0
      */
     public IFile createBookmarksFile() throws CoreException {
@@ -297,8 +331,9 @@ public class TmfTraceElement extends TmfWithFolderElement implements IActionFilt
     }
 
     /**
-     * Returns the file resource used to store bookmarks.
-     * The file may not exist.
+     * Returns the file resource used to store bookmarks. The file may not
+     * exist.
+     *
      * @return the bookmarks file
      * @since 2.0
      */
@@ -314,15 +349,18 @@ public class TmfTraceElement extends TmfWithFolderElement implements IActionFilt
     }
 
     /**
-     * Returns the <code>TmfTraceElement</code> located under the <code>TmfTracesFolder</code>.
+     * Returns the <code>TmfTraceElement</code> located under the
+     * <code>TmfTracesFolder</code>.
      *
-     * @return <code>this</code> if this element is under the <code>TmfTracesFolder</code>
-     *         else the corresponding <code>TmfTraceElement</code> if this element is under
+     * @return <code>this</code> if this element is under the
+     *         <code>TmfTracesFolder</code> else the corresponding
+     *         <code>TmfTraceElement</code> if this element is under
      *         <code>TmfExperimentElement</code>.
      */
     public TmfTraceElement getElementUnderTraceFolder() {
 
-        // If trace is under an experiment, return original trace from the traces folder
+        // If trace is under an experiment, return original trace from the
+        // traces folder
         if (getParent() instanceof TmfExperimentElement) {
             for (TmfTraceElement aTrace : getProject().getTracesFolder().getTraces()) {
                 if (aTrace.getName().equals(getName())) {
@@ -349,10 +387,7 @@ public class TmfTraceElement extends TmfWithFolderElement implements IActionFilt
     // ------------------------------------------------------------------------
     // TmfTraceElement
     // ------------------------------------------------------------------------
-    /*
-     * (non-Javadoc)
-     * @see org.eclipse.linuxtools.tmf.ui.project.model.ITmfProjectModelElement#getProject()
-     */
+
     @Override
     public TmfProjectElement getProject() {
         if (getParent() instanceof TmfTraceFolder) {
@@ -373,28 +408,53 @@ public class TmfTraceElement extends TmfWithFolderElement implements IActionFilt
     // IPropertySource2
     // ------------------------------------------------------------------------
 
-    /*
-     * (non-Javadoc)
-     * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
-     */
     @Override
     public Object getEditableValue() {
         return null;
     }
 
-    /*
-     * (non-Javadoc)
-     * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
+    /**
+     * Get the trace properties of this traceElement if the corresponding trace
+     * is opened in an editor
+     *
+     * @return a map with the names and values of the trace properties
+     *         respectively as keys and values
      */
+    private Map<String, String> getTraceProperties() {
+        for (ITmfTrace openedTrace : TmfTraceManager.getInstance().getOpenedTraces()) {
+            for (ITmfTrace singleTrace : TmfTraceManager.getTraceSet(openedTrace)) {
+                if (this.getLocation().toString().endsWith(singleTrace.getPath())) {
+                    if (singleTrace instanceof ITmfTraceProperties) {
+                        ITmfTraceProperties traceProperties = (ITmfTraceProperties) singleTrace;
+                        return traceProperties.getTraceProperties();
+                    }
+                }
+            }
+        }
+        return new HashMap<String, String>();
+    }
+
     @Override
     public IPropertyDescriptor[] getPropertyDescriptors() {
+        Map<String, String> traceProperties = getTraceProperties();
+        if (!traceProperties.isEmpty()) {
+            IPropertyDescriptor[] propertyDescriptorArray = new IPropertyDescriptor[traceProperties.size() + sfDescriptors.length];
+            int index = 0;
+            for (Map.Entry<String, String> varName : traceProperties.entrySet()) {
+                ReadOnlyTextPropertyDescriptor descriptor = new ReadOnlyTextPropertyDescriptor(this.getName() + "_" + varName.getKey(), varName.getKey()); //$NON-NLS-1$
+                descriptor.setCategory(sfTracePropertiesCategory);
+                propertyDescriptorArray[index] = descriptor;
+                index++;
+            }
+            for (int i = 0; i < sfDescriptors.length; i++) {
+                propertyDescriptorArray[index] = sfDescriptors[i];
+                index++;
+            }
+            return propertyDescriptorArray;
+        }
         return Arrays.copyOf(sfDescriptors, sfDescriptors.length);
     }
 
-    /*
-     * (non-Javadoc)
-     * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object)
-     */
     @Override
     public Object getPropertyValue(Object id) {
 
@@ -421,6 +481,14 @@ public class TmfTraceElement extends TmfWithFolderElement implements IActionFilt
             }
         }
 
+        Map<String, String> traceProperties = getTraceProperties();
+        if (id != null && !traceProperties.isEmpty()) {
+            String key = (String) id;
+            key = key.replaceFirst(this.getName() + "_", ""); //$NON-NLS-1$ //$NON-NLS-2$
+            String value = traceProperties.get(key);
+            return value;
+        }
+
         return null;
     }
 
@@ -435,35 +503,19 @@ public class TmfTraceElement extends TmfWithFolderElement implements IActionFilt
         return "[no category]"; //$NON-NLS-1$
     }
 
-    /*
-     * (non-Javadoc)
-     * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object)
-     */
     @Override
     public void resetPropertyValue(Object id) {
     }
 
-    /*
-     * (non-Javadoc)
-     * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object, java.lang.Object)
-     */
     @Override
     public void setPropertyValue(Object id, Object value) {
     }
 
-    /*
-     * (non-Javadoc)
-     * @see org.eclipse.ui.views.properties.IPropertySource2#isPropertyResettable(java.lang.Object)
-     */
     @Override
     public boolean isPropertyResettable(Object id) {
         return false;
     }
 
-    /*
-     * (non-Javadoc)
-     * @see org.eclipse.ui.views.properties.IPropertySource2#isPropertySet(java.lang.Object)
-     */
     @Override
     public boolean isPropertySet(Object id) {
         return false;
@@ -484,5 +536,44 @@ public class TmfTraceElement extends TmfWithFolderElement implements IActionFilt
         return new TmfTraceElement(string, res, folder);
     }
 
+    /**
+     * Close opened editors associated with this trace.
+     *
+     * @since 2.0
+     */
+    public void closeEditors() {
+        // Close the trace if open
+        IFile file = getBookmarksFile();
+        FileEditorInput input = new FileEditorInput(file);
+        IWorkbench wb = PlatformUI.getWorkbench();
+        for (IWorkbenchWindow wbWindow : wb.getWorkbenchWindows()) {
+            for (IWorkbenchPage wbPage : wbWindow.getPages()) {
+                for (IEditorReference editorReference : wbPage.getEditorReferences()) {
+                    try {
+                        if (editorReference.getEditorInput().equals(input)) {
+                            wbPage.closeEditor(editorReference.getEditor(false), false);
+                        }
+                    } catch (PartInitException e) {
+                        Activator.getDefault().logError("Error closing editor for trace " + getName(), e); //$NON-NLS-1$
+                    }
+                }
+            }
+        }
 
+        // Close experiments that contain the trace if open
+        if (getParent() instanceof TmfTraceFolder) {
+            TmfExperimentFolder experimentFolder = getProject().getExperimentsFolder();
+            for (ITmfProjectModelElement experiment : experimentFolder.getChildren()) {
+                for (ITmfProjectModelElement child : experiment.getChildren()) {
+                    if (child.getName().equals(getName())) {
+                        ((TmfExperimentElement) experiment).closeEditors();
+                        break;
+                    }
+                }
+            }
+        } else if (getParent() instanceof TmfExperimentElement) {
+            TmfExperimentElement experiment = (TmfExperimentElement) getParent();
+            experiment.closeEditors();
+        }
+    }
 }
This page took 0.033297 seconds and 5 git commands to generate.