ss: Move plugins to Trace Compass namespace
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / histogram / HistogramDataModel.java
index 7f4a9b6ff4730e7168963da74a0b1bee49fe0275..f708d5ecde55d56d52f3b2d75237ae77d75bebe3 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2011, 2013 Ericsson
+ * Copyright (c) 2011, 2014 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
  *   Francois Chouinard - Added support for empty initial buckets
  *   Patrick Tasse - Support selection range
  *   Jean-Christian Kouamé, Simon Delisle - Added support to manage lost events
+ *   Xavier Raynaud - Support multi-trace coloring
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.ui.views.histogram;
 
 import java.util.Arrays;
+import java.util.LinkedHashMap;
+import java.util.Map;
 
 import org.eclipse.core.runtime.ListenerList;
 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
+import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
+import org.eclipse.linuxtools.tmf.core.trace.TmfTraceManager;
 
 /**
  * Histogram-independent data model.
@@ -85,9 +90,13 @@ public class HistogramDataModel implements IHistogramDataModel {
     // Attributes
     // ------------------------------------------------------------------------
 
+    // Trace management
+    private ITmfTrace fTrace = null;
+    private final Map<ITmfTrace, Integer> fTraceMap = new LinkedHashMap<>();
+
     // Bucket management
     private final int fNbBuckets;
-    private final long[] fBuckets;
+    private final HistogramBucket[] fBuckets;
     private final long[] fLostEventsBuckets;
     private long fBucketDuration;
     private long fNbEvents;
@@ -148,7 +157,7 @@ public class HistogramDataModel implements IHistogramDataModel {
     public HistogramDataModel(long startTime, int nbBuckets) {
         fFirstBucketTime = fFirstEventTime = fEndTime = startTime;
         fNbBuckets = nbBuckets;
-        fBuckets = new long[nbBuckets];
+        fBuckets = new HistogramBucket[nbBuckets];
         fLostEventsBuckets = new long[nbBuckets];
         fModelListeners = new ListenerList();
         clear();
@@ -162,7 +171,10 @@ public class HistogramDataModel implements IHistogramDataModel {
      */
     public HistogramDataModel(HistogramDataModel other) {
         fNbBuckets = other.fNbBuckets;
-        fBuckets = Arrays.copyOf(other.fBuckets, fNbBuckets);
+        fBuckets = new HistogramBucket[fNbBuckets];
+        for (int i = 0; i < fNbBuckets; i++) {
+            fBuckets[i] = new HistogramBucket(other.fBuckets[i]);
+        }
         fLostEventsBuckets = Arrays.copyOf(other.fLostEventsBuckets, fNbBuckets);
         fBucketDuration = Math.max(other.fBucketDuration, 1);
         fNbEvents = other.fNbEvents;
@@ -180,6 +192,16 @@ public class HistogramDataModel implements IHistogramDataModel {
         }
     }
 
+
+    /**
+     * Disposes the data model
+     * @since 3.0
+     */
+    public void dispose() {
+        fTraceMap.clear();
+        fTrace = null;
+    }
+
     // ------------------------------------------------------------------------
     // Accessors
     // ------------------------------------------------------------------------
@@ -229,6 +251,65 @@ public class HistogramDataModel implements IHistogramDataModel {
         return fFirstEventTime;
     }
 
+    /**
+     * Sets the trace of this model.
+     * @param trace - a {@link ITmfTrace}
+     * @since 3.0
+     */
+    public void setTrace(ITmfTrace trace) {
+        this.fTrace = trace;
+        fTraceMap.clear();
+        ITmfTrace[] traces = TmfTraceManager.getTraceSet(fTrace);
+        if (traces != null) {
+            int i = 0;
+            for (ITmfTrace tr : traces) {
+                fTraceMap.put(tr, i);
+                i++;
+            }
+        }
+    }
+
+    /**
+     * Gets the trace of this model.
+     * @return a {@link ITmfTrace}
+     * @since 3.0
+     */
+    public ITmfTrace getTrace() {
+        return this.fTrace;
+    }
+
+    /**
+     * Gets the traces names of this model.
+     * @return an array of trace names
+     * @since 3.0
+     */
+    public String[] getTraceNames() {
+        ITmfTrace[] traces = TmfTraceManager.getTraceSet(fTrace);
+        if (traces == null) {
+            return new String[0];
+        }
+        String[] traceNames = new String[traces.length];
+        int i = 0;
+        for (ITmfTrace tr : traces) {
+            traceNames[i] = tr.getName();
+            i++;
+        }
+        return traceNames;
+    }
+
+    /**
+     * Gets the number of traces of this model.
+     * @return the number of traces of this model.
+     * @since 3.0
+     */
+    public int getNbTraces() {
+        ITmfTrace[] traces = TmfTraceManager.getTraceSet(fTrace);
+        if (traces == null) {
+            return 1; //
+        }
+        return traces.length;
+    }
+
     /**
      * Sets the model start time
      *
@@ -269,18 +350,6 @@ public class HistogramDataModel implements IHistogramDataModel {
         return fEndTime;
     }
 
-    /**
-     * Returns the time of the current event in the model.
-     *
-     * @return the time of the current event.
-     * @deprecated As of 2.1, use {@link #getSelectionBegin()} and
-     *             {@link #getSelectionEnd()}
-     */
-    @Deprecated
-    public long getCurrentEventTime() {
-        return fSelectionBegin;
-    }
-
     /**
      * Returns the begin time of the current selection in the model.
      *
@@ -366,7 +435,7 @@ public class HistogramDataModel implements IHistogramDataModel {
      */
     @Override
     public void clear() {
-        Arrays.fill(fBuckets, 0);
+        Arrays.fill(fBuckets, null);
         Arrays.fill(fLostEventsBuckets, 0);
         fNbEvents = 0;
         fFirstBucketTime = 0;
@@ -379,34 +448,6 @@ public class HistogramDataModel implements IHistogramDataModel {
         fireModelUpdateNotification();
     }
 
-    /**
-     * Sets the current event time (no notification of listeners)
-     *
-     * @param timestamp
-     *            A time stamp to set.
-     * @deprecated As of 2.1, use {@link #setSelection(long, long)}
-     */
-    @Deprecated
-    public void setCurrentEvent(long timestamp) {
-        fSelectionBegin = timestamp;
-        fSelectionEnd = timestamp;
-    }
-
-    /**
-     * Sets the current event time with notification of listeners
-     *
-     * @param timestamp
-     *            A time stamp to set.
-     * @deprecated As of 2.1, use
-     *             {@link #setSelectionNotifyListeners(long, long)}
-     */
-    @Deprecated
-    public void setCurrentEventNotifyListeners(long timestamp) {
-        fSelectionBegin = timestamp;
-        fSelectionEnd = timestamp;
-        fireModelUpdateNotification();
-    }
-
     /**
      * Sets the current selection time range (no notification of listeners)
      *
@@ -443,10 +484,12 @@ public class HistogramDataModel implements IHistogramDataModel {
      *            The current event Count (for notification purposes)
      * @param timestamp
      *            The timestamp of the event to count
-     *
+     * @param trace
+     *            The event trace
+     * @since 3.0
      */
     @Override
-    public void countEvent(long eventCount, long timestamp) {
+    public void countEvent(long eventCount, long timestamp, ITmfTrace trace) {
 
         // Validate
         if (timestamp < 0) {
@@ -454,7 +497,7 @@ public class HistogramDataModel implements IHistogramDataModel {
         }
 
         // Set the start/end time if not already done
-        if ((fFirstBucketTime == 0) && (fLastBucket == 0) && (fBuckets[0] == 0) && (timestamp > 0)) {
+        if ((fFirstBucketTime == 0) && (fLastBucket == 0) && (fBuckets[0] == null) && (timestamp > 0)) {
             fFirstBucketTime = timestamp;
             fFirstEventTime = timestamp;
             updateEndTime();
@@ -496,7 +539,14 @@ public class HistogramDataModel implements IHistogramDataModel {
 
         // Increment the right bucket
         int index = (int) ((timestamp - fFirstBucketTime) / fBucketDuration);
-        fBuckets[index]++;
+        if (fBuckets[index] == null) {
+            fBuckets[index] = new HistogramBucket(getNbTraces());
+        }
+        Integer traceIndex = fTraceMap.get(trace);
+        if (traceIndex == null) {
+            traceIndex = 0;
+        }
+        fBuckets[index].addEvent(traceIndex);
         fNbEvents++;
         if (fLastBucket < index) {
             fLastBucket = index;
@@ -586,14 +636,17 @@ public class HistogramDataModel implements IHistogramDataModel {
         for (int i = 0; i < nbBars; i++) {
             int count = 0;
             int countLostEvent = 0;
+            result.fData[i] = new HistogramBucket(getNbTraces());
             for (int j = i * bucketsPerBar; j < ((i + 1) * bucketsPerBar); j++) {
                 if (fNbBuckets <= j) {
                     break;
                 }
-                count += fBuckets[j];
+                if (fBuckets[j] != null) {
+                    count += fBuckets[j].getNbEvents();
+                    result.fData[i].add(fBuckets[j]);
+                }
                 countLostEvent += fLostEventsBuckets[j];
             }
-            result.fData[i] = count;
             result.fLostEventsData[i] = countLostEvent;
             result.fLastBucket = i;
             if (result.fMaxValue < count) {
@@ -614,20 +667,8 @@ public class HistogramDataModel implements IHistogramDataModel {
 
         fBucketDuration = Math.max(fBucketDuration, 1);
         // Set selection begin and end index in the scaled histogram
-        if (fSelectionBegin < fFirstBucketTime) {
-            result.fSelectionBeginBucket = -1;
-        } else if (fSelectionBegin > fEndTime) {
-            result.fSelectionBeginBucket = fLastBucket;
-        } else {
-            result.fSelectionBeginBucket = (int) ((fSelectionBegin - fFirstBucketTime) / fBucketDuration) / bucketsPerBar;
-        }
-        if (fSelectionEnd < fFirstBucketTime) {
-            result.fSelectionEndBucket = -1;
-        } else if (fSelectionEnd > fEndTime) {
-            result.fSelectionEndBucket = fLastBucket;
-        } else {
-            result.fSelectionEndBucket = (int) ((fSelectionEnd - fFirstBucketTime) / fBucketDuration) / bucketsPerBar;
-        }
+        result.fSelectionBeginBucket = (int) ((fSelectionBegin - fFirstBucketTime) / fBucketDuration) / bucketsPerBar;
+        result.fSelectionEndBucket = (int) ((fSelectionEnd - fFirstBucketTime) / fBucketDuration) / bucketsPerBar;
 
         result.fFirstBucketTime = fFirstBucketTime;
         result.fFirstEventTime = fFirstEventTime;
@@ -644,10 +685,10 @@ public class HistogramDataModel implements IHistogramDataModel {
 
     private void mergeBuckets() {
         for (int i = 0; i < (fNbBuckets / 2); i++) {
-            fBuckets[i] = fBuckets[2 * i] + fBuckets[(2 * i) + 1];
+            fBuckets[i] = new HistogramBucket(fBuckets[2 * i], fBuckets[(2 * i) + 1]);
             fLostEventsBuckets[i] = fLostEventsBuckets[2 * i] + fLostEventsBuckets[(2 * i) + 1];
         }
-        Arrays.fill(fBuckets, fNbBuckets / 2, fNbBuckets, 0);
+        Arrays.fill(fBuckets, fNbBuckets / 2, fNbBuckets, null);
         Arrays.fill(fLostEventsBuckets, fNbBuckets / 2, fNbBuckets, 0);
         fBucketDuration *= 2;
         updateEndTime();
@@ -656,12 +697,12 @@ public class HistogramDataModel implements IHistogramDataModel {
 
     private void moveBuckets(int offset) {
         for (int i = fNbBuckets - 1; i >= offset; i--) {
-            fBuckets[i] = fBuckets[i - offset];
+            fBuckets[i] = new HistogramBucket(fBuckets[i - offset]);
             fLostEventsBuckets[i] = fLostEventsBuckets[i - offset];
         }
 
         for (int i = 0; i < offset; i++) {
-            fBuckets[i] = 0;
+            fBuckets[i] = null;
             fLostEventsBuckets[i] = 0;
         }
     }
This page took 0.031345 seconds and 5 git commands to generate.