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