Internalize lttng.core APIs
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / common / ParamsUpdater.java
1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Alvaro Sanchez-Leon (alvsan09@gmail.com) - Initial API and implementation
11 *******************************************************************************/
12 package org.eclipse.linuxtools.lttng.ui.views.common;
13
14 import org.eclipse.linuxtools.internal.lttng.core.event.LttngTimestamp;
15 import org.eclipse.linuxtools.lttng.ui.TraceDebug;
16 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
17 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
18 import org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.TmfTimeScaleSelectionEvent;
19
20 /**
21 *
22 * Preserve the time and space width parameters applicable to a particular view
23 * in order to facilitate filtering of events and request handling.
24 *
25 * @author alvaro
26 *
27 */
28 public class ParamsUpdater {
29 // ========================================================================
30 // Data
31 // ========================================================================
32
33 private long startTime = 0;
34 private long endTime = Long.MAX_VALUE;
35 private static Long selectedTime = null;
36 private final int DEFAULT_WIDTH = 2000; // number of estimated pixels that
37 // can hold the time range space
38 private int width = DEFAULT_WIDTH; // width in pixels used to represent the
39 // time interval
40 private double pixelsPerNs = 0;
41 private int eventsDiscarded = 0;
42 private int eventsDiscardedOutOfView = 0;
43 private int eventsDiscardedNotVisible = 0;
44 private int eventsDiscardedWrongOrder = 0;
45 private TmfTimeRange trange = null;
46
47 public static final int OUT_OF_VIEWRANGE = 0;
48 public static final int NOT_VISIBLE = 1;
49
50 // ========================================================================
51 // Methods
52 // ========================================================================
53
54 /**
55 * @param event
56 * @return
57 */
58 public synchronized boolean processTimeScaleEvent(
59 TmfTimeScaleSelectionEvent event) {
60
61 boolean updated = false;
62 if (event != null) {
63 long time0 = event.getTime0();
64 long time1 = event.getTime1();
65 int dwidth = event.getWidth();
66
67 updated = update(time0, time1, dwidth);
68
69 // initialization only, otherwise wait for the actual selection
70 // event to update its value. Note that the time must be different
71 // upon selection of a new time in order to trigger an update to all
72 if (selectedTime == null) {
73 setSelectedTime(event.getSelectedTime());
74 }
75
76 }
77
78 return updated;
79
80 }
81
82 /**
83 * Save the selected time
84 * @param selTime
85 */
86 public void setSelectedTime(long selTime) {
87 TraceDebug.debug("Selected time changed from: \n\t" + selectedTime //$NON-NLS-1$
88 + " to: \n\t" + selTime); //$NON-NLS-1$
89 selectedTime = selTime;
90 }
91
92 /**
93 * May return null, if the selected time is invalid
94 *
95 * @return
96 */
97 public Long getSelectedTime() {
98 return selectedTime;
99 }
100
101 /**
102 * Update time range but keep width as is
103 *
104 * @param time0
105 * @param time1
106 * @return
107 */
108 public boolean update(long time0, long time1) {
109 return update(time0, time1, width);
110 }
111
112 /**
113 * Only positive attributes are expected
114 *
115 * @param time0
116 * @param time1
117 * @param dwidth
118 * @return
119 */
120 public boolean update(long time0, long time1, int dwidth) {
121 boolean updated = false;
122
123 if (time0 == startTime && time1 == endTime && dwidth == width) {
124 // No updated needed
125 return updated;
126 }
127
128 // Negatives are invalid
129 time0 = time0 > 0 ? time0 : 0;
130 time1 = time1 > 0 ? time1 : 0;
131 dwidth = dwidth > 0 ? dwidth : 0;
132
133 if (time1 > time0) {
134 // Store the new values as long as they are within range
135 startTime = time0;
136 endTime = time1;
137 width = dwidth;
138
139 pixelsPerNs = (double) width / (double) (endTime - startTime);
140
141 TmfTimestamp fTimeStart = new LttngTimestamp(startTime);
142 TmfTimestamp fTimeEnd = new LttngTimestamp(endTime);
143 trange = new TmfTimeRange(fTimeStart, fTimeEnd);
144
145 // make sure the selected time is within the new range or else set
146 // mark it as invalid
147 if (selectedTime != null) {
148 setSelectedTime(selectedTime);
149 }
150
151 // update succeeded
152 updated = true;
153
154 TraceDebug.debug("Configuration updated to: StartTime: " /* */ //$NON-NLS-1$
155 + fTimeStart /* */
156 + "-" /* */ //$NON-NLS-1$
157 + fTimeEnd /* */
158 + " width: " /* */ //$NON-NLS-1$
159 + width + " k: " + pixelsPerNs); /* */ //$NON-NLS-1$
160 } else {
161 TraceDebug
162 .debug("End time is not greater than start time, start time: " //$NON-NLS-1$
163 + time0 + " end time: " + time1); //$NON-NLS-1$
164 }
165
166 return updated;
167 }
168
169 /**
170 * @return
171 */
172 public long getStartTime() {
173 return startTime;
174 }
175
176 /**
177 * @return
178 */
179 public long getEndTime() {
180 return endTime;
181 }
182
183 /**
184 * @return
185 */
186 public int getWidth() {
187 if (width == 0) {
188 TraceDebug
189 .debug("Unexpected width value of 0 pixels, returning default"); //$NON-NLS-1$
190 return DEFAULT_WIDTH;
191 }
192
193 return width;
194 }
195
196 /**
197 * Return the current constant "K" of pixels per nano second used for the
198 * widest time space widget registered in this instance.
199 *
200 * @return
201 */
202 public double getPixelsPerNs() {
203 return pixelsPerNs;
204 }
205
206 /**
207 * Set the value of pixels per nano second as long as the value is grater
208 * positive
209 *
210 * @return
211 */
212 public void setPixelsPerNs(double pixperNsec) {
213 if (pixperNsec > 0) {
214 pixelsPerNs = pixperNsec;
215 }
216 }
217
218 /**
219 * @param value
220 */
221 public void setEventsDiscarded(int value) {
222 eventsDiscarded = value;
223 if (value == 0) {
224 eventsDiscardedWrongOrder = 0;
225 eventsDiscardedNotVisible = 0;
226 eventsDiscardedOutOfView = 0;
227 }
228 }
229
230 /**
231 *
232 */
233 public void incrementEventsDiscarded(int reason) {
234 if (reason == OUT_OF_VIEWRANGE) {
235 this.eventsDiscardedOutOfView++;
236 }
237
238 if (reason == NOT_VISIBLE) {
239 this.eventsDiscardedNotVisible++;
240 }
241
242 this.eventsDiscarded++;
243 }
244
245 /**
246 * @return
247 */
248 public int getEventsDiscarded() {
249 return eventsDiscarded;
250 }
251
252 /**
253 * increase the number of events discarder since they were not received in a
254 * later time than previous events
255 */
256 public void incrementEventsDiscardedWrongOrder() {
257 this.eventsDiscarded++;
258 this.eventsDiscardedWrongOrder++;
259 }
260
261 /**
262 * @return
263 */
264 public int getEventsDiscardedWrongOrder() {
265 return eventsDiscardedWrongOrder;
266
267 }
268
269 /**
270 * @return
271 */
272 public int getEventsDiscardedNotVisible() {
273 return eventsDiscardedNotVisible;
274
275 }
276
277 /**
278 * @return
279 */
280 public int getEventsDiscardedOutOfViewRange() {
281 return eventsDiscardedOutOfView;
282
283 }
284
285 /**
286 * @return
287 */
288 public TmfTimeRange getTrange() {
289 return trange;
290 }
291
292 }
This page took 0.038336 seconds and 5 git commands to generate.