Merge branch 'master' into lttng-luna
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / histogram / HistogramZoom.java
index b42c807ccb00644f1c0536c104fbd5c7c66bec2a..8c5ae0dd25bdf0022af619af9ac21563914cfae2 100644 (file)
@@ -1,25 +1,25 @@
 /*******************************************************************************
- * 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
+ *   Patrick Tasse - Update for mouse wheel zoom
  *******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.ui.views.histogram;
 
 import org.eclipse.swt.events.MouseEvent;
 import org.eclipse.swt.events.MouseWheelListener;
-import org.eclipse.swt.widgets.Canvas;
 
 /**
  * Class to handle zooming within histogram windows..
- * 
+ *
  * @version 1.0
  * @author Francois Chouinard
  * <p>
@@ -37,7 +37,6 @@ public class HistogramZoom implements MouseWheelListener {
     // ------------------------------------------------------------------------
 
     private final Histogram fHistogram;
-    private final Canvas fCanvas;
 
     private long fAbsoluteStartTime;
     private long fAbsoluteEndTime;
@@ -46,23 +45,29 @@ public class HistogramZoom implements MouseWheelListener {
     private long fRangeStartTime;
     private long fRangeDuration;
 
-    private MouseScrollCounter fScrollCounter;
-
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
 
-    public HistogramZoom(Histogram histogram, Canvas canvas, long start, long end) {
+    /**
+     * Standard constructor.
+     *
+     * @param histogram
+     *            The parent histogram object
+     * @param start
+     *            The start time of the zoom area
+     * @param end
+     *            The end time of the zoom area
+     * @since 2.0
+     */
+    public HistogramZoom(Histogram histogram, long start, long end) {
         fHistogram = histogram;
-        fCanvas = canvas;
         fAbsoluteStartTime = start;
         fAbsoluteEndTime = end;
-        fMinWindowSize = fCanvas.getBounds().x;
+        fMinWindowSize = 0;
 
         fRangeStartTime = fAbsoluteStartTime;
         fRangeDuration = fAbsoluteStartTime + fMinWindowSize;
-
-        canvas.addMouseWheelListener(this);
     }
 
     // ------------------------------------------------------------------------
@@ -97,19 +102,9 @@ public class HistogramZoom implements MouseWheelListener {
     // Operations
     // ------------------------------------------------------------------------
 
-    /**
-     * Stops the zooming (multiple consecutive execution)
-     */
-    public synchronized void stop() {
-        if (fScrollCounter != null) {
-            fScrollCounter.interrupt();
-            fScrollCounter = null;
-        }
-    }
-
     /**
      * The the full time range of the histogram
-     *   
+     *
      * @param startTime the start time the histogram
      * @param endTime the end time of the histogram
      */
@@ -124,49 +119,41 @@ public class HistogramZoom implements MouseWheelListener {
      * @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);
+        zoom(event.count);
     }
 
     private synchronized void zoom(int nbClicks) {
-        // The job is finished
-        fScrollCounter = null;
-
         // 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);
 
@@ -174,86 +161,26 @@ public class HistogramZoom implements MouseWheelListener {
     }
 
     private long validateStart(long start) {
-        if (start < fAbsoluteStartTime)
-            start = fAbsoluteStartTime;
-        if (start > fAbsoluteEndTime)
-            start = fAbsoluteEndTime - fMinWindowSize;
-        return start;
-    }
-
-    private long validateEnd(long start, long end) {
-        if (end > fAbsoluteEndTime)
-            end = fAbsoluteEndTime;
-        if (end < start + fMinWindowSize)
-            end = start + fMinWindowSize;
-        return end;
-    }
-
-    // ------------------------------------------------------------------------
-    // DelayedMouseScroll
-    // ------------------------------------------------------------------------
-
-    private static class MouseScrollCounter extends Thread {
-
-        // --------------------------------------------------------------------
-        // Constants
-        // --------------------------------------------------------------------
-
-        private final static long QUIET_TIME = 100L;
-        private final static long POLLING_INTERVAL = 10L;
-
-        // --------------------------------------------------------------------
-        // Attributes
-        // --------------------------------------------------------------------
-
-        private HistogramZoom fZoom = null;
+        long realStart = start;
 
-        private long fLastPoolTime = 0L;
-        private int nbScrollClick = 0;
-
-        // --------------------------------------------------------------------
-        // 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();
+        if (realStart < fAbsoluteStartTime) {
+            realStart = fAbsoluteStartTime;
         }
-
-        // --------------------------------------------------------------------
-        // 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;
+        if (realStart > fAbsoluteEndTime) {
+            realStart = fAbsoluteEndTime - fMinWindowSize;
         }
+        return realStart;
+    }
 
-        // --------------------------------------------------------------------
-        // Thread
-        // --------------------------------------------------------------------
+    private long validateEnd(long start, long end) {
+        long realEnd = end;
 
-        @Override
-        public void run() {
-            while ((System.currentTimeMillis() - fLastPoolTime) < QUIET_TIME) {
-                try {
-                    Thread.sleep(POLLING_INTERVAL);
-                } catch (Exception e) {
-                    return;
-                }
-            }
-            // Done waiting. Notify the histogram.
-            if (!isInterrupted())
-                fZoom.zoom(nbScrollClick);
+        if (realEnd > fAbsoluteEndTime) {
+            realEnd = fAbsoluteEndTime;
         }
+        if (realEnd < start + fMinWindowSize) {
+            realEnd = start + fMinWindowSize;
+        }
+        return realEnd;
     }
-
 }
This page took 0.03063 seconds and 5 git commands to generate.