tmf: Update copyright headers in tmf.ui
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / histogram / HistogramZoom.java
index a928b6b592824a123111c8de4f4353b92753c115..dc4c806dc2e5ce839ee16c47c81252def52bfe19 100644 (file)
@@ -1,11 +1,11 @@
 /*******************************************************************************
- * Copyright (c) 2011, 2012 Ericsson
- * 
+ * Copyright (c) 2011, 2013 Ericsson
+ *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
  * accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/epl-v10.html
- * 
+ *
  * Contributors:
  *   Francois Chouinard - Initial API and implementation
  *   Francois Chouinard - Moved from LTTng to TMF
@@ -18,7 +18,10 @@ import org.eclipse.swt.events.MouseWheelListener;
 import org.eclipse.swt.widgets.Canvas;
 
 /**
- * <b><u>HistogramZoom</u></b>
+ * Class to handle zooming within histogram windows..
+ *
+ * @version 1.0
+ * @author Francois Chouinard
  * <p>
  */
 public class HistogramZoom implements MouseWheelListener {
@@ -49,7 +52,20 @@ public class HistogramZoom implements MouseWheelListener {
     // Constructors
     // ------------------------------------------------------------------------
 
-    public HistogramZoom(Histogram histogram, Canvas canvas, long start, long end) {
+    /**
+     * Standard constructor.
+     *
+     * @param histogram
+     *            The parent histogram object
+     * @param canvas
+     *            The canvas
+     * @param start
+     *            The start time of the zoom area
+     * @param end
+     *            The end time of the zoom area
+     */
+    public HistogramZoom(Histogram histogram, Canvas canvas, long start,
+            long end) {
         fHistogram = histogram;
         fCanvas = canvas;
         fAbsoluteStartTime = start;
@@ -66,15 +82,27 @@ public class HistogramZoom implements MouseWheelListener {
     // Accessors
     // ------------------------------------------------------------------------
 
-    public long getStartTime() {
+    /**
+     * Get start time of the zoom window.
+     * @return the start time.
+     */
+    public synchronized long getStartTime() {
         return fRangeStartTime;
     }
 
-    public long getEndTime() {
+    /**
+     * Get the end time of the zoom window.
+     * @return the end time
+     */
+    public synchronized long getEndTime() {
         return fRangeStartTime + fRangeDuration;
     }
 
-    public long getDuration() {
+    /**
+     * Get the duration of the zoom window.
+     * @return the duration of the zoom window.
+     */
+    public synchronized long getDuration() {
         return fRangeDuration;
     }
 
@@ -82,6 +110,9 @@ public class HistogramZoom implements MouseWheelListener {
     // Operations
     // ------------------------------------------------------------------------
 
+    /**
+     * Stops the zooming (multiple consecutive execution)
+     */
     public synchronized void stop() {
         if (fScrollCounter != null) {
             fScrollCounter.interrupt();
@@ -89,41 +120,52 @@ public class HistogramZoom implements MouseWheelListener {
         }
     }
 
+    /**
+     * The the full time range of the histogram
+     *
+     * @param startTime the start time the histogram
+     * @param endTime the end time of the histogram
+     */
     public synchronized void setFullRange(long startTime, long endTime) {
         fAbsoluteStartTime = startTime;
         fAbsoluteEndTime = endTime;
     }
 
+    /**
+     * Sets the new zoom window
+     * @param startTime the start time
+     * @param duration the duration
+     */
     public synchronized void setNewRange(long startTime, long duration) {
-        if (startTime < fAbsoluteStartTime)
-            startTime = fAbsoluteStartTime;
+        long realStart = startTime;
 
-        long endTime = startTime + duration;
+        if (realStart < fAbsoluteStartTime) {
+            realStart = fAbsoluteStartTime;
+        }
+
+        long endTime = realStart + duration;
         if (endTime > fAbsoluteEndTime) {
             endTime = fAbsoluteEndTime;
-            if (endTime - duration > fAbsoluteStartTime)
-                startTime = endTime - duration;
-            else {
-                startTime = fAbsoluteStartTime;
+            if (endTime - duration > fAbsoluteStartTime) {
+                realStart = endTime - duration;
+            else {
+                realStart = fAbsoluteStartTime;
             }
         }
 
-        fRangeStartTime = startTime;
-        fRangeDuration = endTime - startTime;
+        fRangeStartTime = realStart;
+        fRangeDuration = endTime - realStart;
     }
 
     // ------------------------------------------------------------------------
     // MouseWheelListener
     // ------------------------------------------------------------------------
 
-    private long fMouseTimestamp = 0;
-
     @Override
     public synchronized void mouseScrolled(MouseEvent event) {
         if (fScrollCounter == null) {
             fScrollCounter = new MouseScrollCounter(this);
             fScrollCounter.start();
-            fMouseTimestamp = fHistogram.getTimestamp(event.x);
         }
         fScrollCounter.incrementMouseScroll(event.count);
     }
@@ -135,9 +177,8 @@ public class HistogramZoom implements MouseWheelListener {
         // Compute the new time range
         long requestedRange = (nbClicks > 0) ? Math.round(ZOOM_FACTOR * fRangeDuration) : (long) Math.ceil(fRangeDuration * (1.0 / ZOOM_FACTOR));
 
-        // Perform a proportional zoom wrt the mouse position
-        double ratio = ((double) (fMouseTimestamp - fRangeStartTime)) / fRangeDuration;
-        long requestedStart = validateStart(fRangeStartTime + (long) ((fRangeDuration - requestedRange) * ratio));
+        // Distribute delta and adjust for boundaries
+        long requestedStart = validateStart(fRangeStartTime + (fRangeDuration - requestedRange) / 2);
         long requestedEnd = validateEnd(requestedStart, requestedStart + requestedRange);
         requestedStart = validateStart(requestedEnd - requestedRange);
 
@@ -145,26 +186,34 @@ public class HistogramZoom implements MouseWheelListener {
     }
 
     private long validateStart(long start) {
-        if (start < fAbsoluteStartTime)
-            start = fAbsoluteStartTime;
-        if (start > fAbsoluteEndTime)
-            start = fAbsoluteEndTime - fMinWindowSize;
-        return start;
+        long realStart = start;
+
+        if (realStart < fAbsoluteStartTime) {
+            realStart = fAbsoluteStartTime;
+        }
+        if (realStart > fAbsoluteEndTime) {
+            realStart = fAbsoluteEndTime - fMinWindowSize;
+        }
+        return realStart;
     }
 
     private long validateEnd(long start, long end) {
-        if (end > fAbsoluteEndTime)
-            end = fAbsoluteEndTime;
-        if (end < start + fMinWindowSize)
-            end = start + fMinWindowSize;
-        return end;
+        long realEnd = end;
+
+        if (realEnd > fAbsoluteEndTime) {
+            realEnd = fAbsoluteEndTime;
+        }
+        if (realEnd < start + fMinWindowSize) {
+            realEnd = start + fMinWindowSize;
+        }
+        return realEnd;
     }
 
     // ------------------------------------------------------------------------
     // DelayedMouseScroll
     // ------------------------------------------------------------------------
 
-    private class MouseScrollCounter extends Thread {
+    private static class MouseScrollCounter extends Thread {
 
         // --------------------------------------------------------------------
         // Constants
@@ -186,6 +235,10 @@ public class HistogramZoom implements MouseWheelListener {
         // Constructors
         // --------------------------------------------------------------------
 
+        /**
+         * Constructor of inner class to handle consecutive scrolls of mouse wheel.
+         * @param zoom the histogram zoom reference
+         */
         public MouseScrollCounter(HistogramZoom zoom) {
             fZoom = zoom;
             fLastPoolTime = System.currentTimeMillis();
@@ -195,6 +248,10 @@ public class HistogramZoom implements MouseWheelListener {
         // Operation
         // --------------------------------------------------------------------
 
+        /**
+         * Increments the number of scroll clicks.
+         * @param nbScrolls the number to add to the current value
+         */
         public void incrementMouseScroll(int nbScrolls) {
             fLastPoolTime = System.currentTimeMillis();
             nbScrollClick += nbScrolls;
@@ -214,8 +271,9 @@ public class HistogramZoom implements MouseWheelListener {
                 }
             }
             // Done waiting. Notify the histogram.
-            if (!isInterrupted())
+            if (!isInterrupted()) {
                 fZoom.zoom(nbScrollClick);
+            }
         }
     }
 
This page took 0.026287 seconds and 5 git commands to generate.