Commit | Line | Data |
---|---|---|
c392540b | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2011, 2014 Ericsson |
20ff3b75 | 3 | * |
c392540b FC |
4 | * All rights reserved. This program and the accompanying materials are |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
20ff3b75 | 8 | * |
c392540b FC |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
e0752744 | 11 | * Francois Chouinard - Moved from LTTng to TMF |
65cdf787 | 12 | * Patrick Tasse - Update for mouse wheel zoom |
c392540b FC |
13 | *******************************************************************************/ |
14 | ||
2bdf0193 | 15 | package org.eclipse.tracecompass.tmf.ui.views.histogram; |
c392540b | 16 | |
ba1f5c20 PT |
17 | import org.eclipse.swt.events.KeyEvent; |
18 | import org.eclipse.swt.events.KeyListener; | |
c392540b FC |
19 | import org.eclipse.swt.events.MouseEvent; |
20 | import org.eclipse.swt.events.MouseWheelListener; | |
c392540b FC |
21 | |
22 | /** | |
b544077e | 23 | * Class to handle zooming within histogram windows.. |
20ff3b75 | 24 | * |
b544077e BH |
25 | * @version 1.0 |
26 | * @author Francois Chouinard | |
c392540b | 27 | * <p> |
c392540b | 28 | */ |
ba1f5c20 | 29 | public class HistogramZoom implements MouseWheelListener, KeyListener { |
c392540b FC |
30 | |
31 | // ------------------------------------------------------------------------ | |
32 | // Constants | |
33 | // ------------------------------------------------------------------------ | |
34 | ||
35 | private final static double ZOOM_FACTOR = 0.8; | |
36 | ||
37 | // ------------------------------------------------------------------------ | |
38 | // Attributes | |
39 | // ------------------------------------------------------------------------ | |
40 | ||
41 | private final Histogram fHistogram; | |
c392540b FC |
42 | |
43 | private long fAbsoluteStartTime; | |
44 | private long fAbsoluteEndTime; | |
45 | private final long fMinWindowSize; | |
46 | ||
47 | private long fRangeStartTime; | |
48 | private long fRangeDuration; | |
49 | ||
c392540b | 50 | // ------------------------------------------------------------------------ |
e0752744 | 51 | // Constructors |
c392540b FC |
52 | // ------------------------------------------------------------------------ |
53 | ||
20ff3b75 AM |
54 | /** |
55 | * Standard constructor. | |
56 | * | |
57 | * @param histogram | |
58 | * The parent histogram object | |
20ff3b75 AM |
59 | * @param start |
60 | * The start time of the zoom area | |
61 | * @param end | |
62 | * The end time of the zoom area | |
63 | */ | |
65cdf787 | 64 | public HistogramZoom(Histogram histogram, long start, long end) { |
c392540b | 65 | fHistogram = histogram; |
c392540b FC |
66 | fAbsoluteStartTime = start; |
67 | fAbsoluteEndTime = end; | |
65cdf787 | 68 | fMinWindowSize = 0; |
c392540b FC |
69 | |
70 | fRangeStartTime = fAbsoluteStartTime; | |
71 | fRangeDuration = fAbsoluteStartTime + fMinWindowSize; | |
ba1f5c20 PT |
72 | |
73 | histogram.addMouseWheelListener(this); | |
74 | histogram.addKeyListener(this); | |
c392540b FC |
75 | } |
76 | ||
77 | // ------------------------------------------------------------------------ | |
78 | // Accessors | |
79 | // ------------------------------------------------------------------------ | |
80 | ||
b544077e BH |
81 | /** |
82 | * Get start time of the zoom window. | |
83 | * @return the start time. | |
84 | */ | |
5a5c2fc7 | 85 | public synchronized long getStartTime() { |
c392540b FC |
86 | return fRangeStartTime; |
87 | } | |
88 | ||
b544077e BH |
89 | /** |
90 | * Get the end time of the zoom window. | |
91 | * @return the end time | |
92 | */ | |
5a5c2fc7 | 93 | public synchronized long getEndTime() { |
c392540b FC |
94 | return fRangeStartTime + fRangeDuration; |
95 | } | |
96 | ||
b544077e BH |
97 | /** |
98 | * Get the duration of the zoom window. | |
99 | * @return the duration of the zoom window. | |
100 | */ | |
5a5c2fc7 | 101 | public synchronized long getDuration() { |
c392540b FC |
102 | return fRangeDuration; |
103 | } | |
104 | ||
105 | // ------------------------------------------------------------------------ | |
106 | // Operations | |
107 | // ------------------------------------------------------------------------ | |
108 | ||
b544077e BH |
109 | /** |
110 | * The the full time range of the histogram | |
20ff3b75 | 111 | * |
b544077e BH |
112 | * @param startTime the start time the histogram |
113 | * @param endTime the end time of the histogram | |
114 | */ | |
8edafa7f | 115 | public synchronized void setFullRange(long startTime, long endTime) { |
c392540b FC |
116 | fAbsoluteStartTime = startTime; |
117 | fAbsoluteEndTime = endTime; | |
118 | } | |
119 | ||
b544077e BH |
120 | /** |
121 | * Sets the new zoom window | |
122 | * @param startTime the start time | |
123 | * @param duration the duration | |
124 | */ | |
8edafa7f | 125 | public synchronized void setNewRange(long startTime, long duration) { |
41b5c37f AM |
126 | long realStart = startTime; |
127 | ||
128 | if (realStart < fAbsoluteStartTime) { | |
129 | realStart = fAbsoluteStartTime; | |
20ff3b75 | 130 | } |
c392540b | 131 | |
41b5c37f | 132 | long endTime = realStart + duration; |
c392540b FC |
133 | if (endTime > fAbsoluteEndTime) { |
134 | endTime = fAbsoluteEndTime; | |
20ff3b75 | 135 | if (endTime - duration > fAbsoluteStartTime) { |
41b5c37f | 136 | realStart = endTime - duration; |
20ff3b75 | 137 | } else { |
41b5c37f | 138 | realStart = fAbsoluteStartTime; |
c392540b FC |
139 | } |
140 | } | |
141 | ||
41b5c37f AM |
142 | fRangeStartTime = realStart; |
143 | fRangeDuration = endTime - realStart; | |
c392540b FC |
144 | } |
145 | ||
146 | // ------------------------------------------------------------------------ | |
147 | // MouseWheelListener | |
148 | // ------------------------------------------------------------------------ | |
149 | ||
c392540b | 150 | @Override |
ba1f5c20 | 151 | public void mouseScrolled(MouseEvent event) { |
38df2c82 | 152 | zoom(event.count); |
c392540b FC |
153 | } |
154 | ||
ba1f5c20 PT |
155 | @Override |
156 | public void keyPressed(KeyEvent e) { | |
157 | if (e.character == '+') { | |
158 | zoom(1); | |
159 | } else if (e.character == '-') { | |
160 | zoom(-1); | |
161 | } | |
162 | } | |
163 | ||
ba1f5c20 PT |
164 | @Override |
165 | public void keyReleased(KeyEvent e) { | |
166 | } | |
167 | ||
c392540b | 168 | private synchronized void zoom(int nbClicks) { |
c392540b FC |
169 | // Compute the new time range |
170 | long requestedRange = (nbClicks > 0) ? Math.round(ZOOM_FACTOR * fRangeDuration) : (long) Math.ceil(fRangeDuration * (1.0 / ZOOM_FACTOR)); | |
171 | ||
bd6307ff | 172 | // Distribute delta and adjust for boundaries |
83f4e378 | 173 | long requestedStart = validateStart(fRangeStartTime + (fRangeDuration - requestedRange) / 2); |
c392540b FC |
174 | long requestedEnd = validateEnd(requestedStart, requestedStart + requestedRange); |
175 | requestedStart = validateStart(requestedEnd - requestedRange); | |
176 | ||
177 | fHistogram.updateTimeRange(requestedStart, requestedEnd); | |
178 | } | |
179 | ||
180 | private long validateStart(long start) { | |
41b5c37f AM |
181 | long realStart = start; |
182 | ||
183 | if (realStart < fAbsoluteStartTime) { | |
184 | realStart = fAbsoluteStartTime; | |
20ff3b75 | 185 | } |
41b5c37f AM |
186 | if (realStart > fAbsoluteEndTime) { |
187 | realStart = fAbsoluteEndTime - fMinWindowSize; | |
20ff3b75 | 188 | } |
41b5c37f | 189 | return realStart; |
c392540b FC |
190 | } |
191 | ||
192 | private long validateEnd(long start, long end) { | |
41b5c37f AM |
193 | long realEnd = end; |
194 | ||
195 | if (realEnd > fAbsoluteEndTime) { | |
196 | realEnd = fAbsoluteEndTime; | |
20ff3b75 | 197 | } |
41b5c37f AM |
198 | if (realEnd < start + fMinWindowSize) { |
199 | realEnd = start + fMinWindowSize; | |
20ff3b75 | 200 | } |
41b5c37f | 201 | return realEnd; |
c392540b | 202 | } |
c392540b | 203 | } |