| 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 | * Francois Chouinard - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.lttng.ui.views.timeframe; |
| 14 | |
| 15 | import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; |
| 16 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; |
| 17 | import org.eclipse.swt.SWT; |
| 18 | import org.eclipse.swt.events.ModifyEvent; |
| 19 | import org.eclipse.swt.events.ModifyListener; |
| 20 | import org.eclipse.swt.layout.GridData; |
| 21 | import org.eclipse.swt.widgets.Composite; |
| 22 | import org.eclipse.swt.widgets.Group; |
| 23 | import org.eclipse.swt.widgets.Label; |
| 24 | import org.eclipse.swt.widgets.Spinner; |
| 25 | |
| 26 | // ======================================================================== |
| 27 | // SpinnerGroup |
| 28 | // ======================================================================== |
| 29 | |
| 30 | /** |
| 31 | * <b><u>SpinnerGroup</u></b> |
| 32 | * <p> |
| 33 | * A SpinnerGroup holds two coordinated spinners (for seconds and |
| 34 | * nanoseconds) representing the current time within the trace. |
| 35 | * <p> |
| 36 | * The current time can take any value anything within the time range (start |
| 37 | * and end time). |
| 38 | */ |
| 39 | public class SpinnerGroup { |
| 40 | |
| 41 | // The nanosecond scale (10^9) |
| 42 | private static final int NS_PER_SECOND = 1000 * 1000 * 1000; |
| 43 | private static final byte NS_SCALING_FACTOR = -9; |
| 44 | |
| 45 | // Labels |
| 46 | private static final String SECONDS_LABEL = "sec"; //$NON-NLS-1$ |
| 47 | private static final String NANOSEC_LABEL = "ns"; //$NON-NLS-1$ |
| 48 | |
| 49 | // Widgets |
| 50 | private Group group; |
| 51 | private Spinner seconds; |
| 52 | private Spinner nanosec; |
| 53 | |
| 54 | // The valid time range - start time |
| 55 | private TmfTimestamp startTime; |
| 56 | private int startSeconds; |
| 57 | private int startNanosec; |
| 58 | |
| 59 | // The valid time range - end time |
| 60 | private TmfTimestamp endTime; |
| 61 | private int endSeconds; |
| 62 | private int endNanosec; |
| 63 | |
| 64 | // The current time value |
| 65 | private TmfTimestamp currentTime; |
| 66 | private int currentSeconds; |
| 67 | private int currentNanosec; |
| 68 | |
| 69 | @SuppressWarnings("unused") |
| 70 | private TimeFrameView fOwner; |
| 71 | |
| 72 | /** |
| 73 | * <b><u>Constructor</u></b> |
| 74 | * <p> |
| 75 | * <li>Creates the display group and formats it for the grid cell |
| 76 | * <li>Sets the initial values for Start/End/Current time |
| 77 | * </li> |
| 78 | * <p> |
| 79 | * @param parent - the parent Composite |
| 80 | * @param groupName - the group name |
| 81 | * @param range - the valid time range (start/end time) |
| 82 | * @param current - the current time |
| 83 | */ |
| 84 | public SpinnerGroup(TimeFrameView owner, Composite parent, String groupName, TmfTimeRange range, TmfTimestamp current) { |
| 85 | |
| 86 | fOwner = owner; |
| 87 | |
| 88 | // Create the group |
| 89 | group = new Group(parent, SWT.BORDER); |
| 90 | group.setText(groupName); |
| 91 | |
| 92 | // Make it use the whole grid cell |
| 93 | GridData gridData = new GridData(SWT.LEFT, SWT.TOP, true, false); |
| 94 | gridData.horizontalAlignment = SWT.FILL; |
| 95 | group.setLayoutData(gridData); |
| 96 | |
| 97 | // Create and position the widgets |
| 98 | seconds = new Spinner(group, SWT.BORDER); |
| 99 | seconds.addModifyListener(new ModifyListener() { |
| 100 | @Override |
| 101 | public void modifyText(ModifyEvent e) { |
| 102 | currentSeconds = seconds.getSelection(); |
| 103 | refreshCurrentTime(); |
| 104 | } |
| 105 | }); |
| 106 | seconds.setBounds(5, 25, 110, 25); |
| 107 | |
| 108 | Label label = new Label(group, SWT.LEFT); |
| 109 | label.setText(SECONDS_LABEL); |
| 110 | label.setBounds(120, 28, 25, 22); |
| 111 | |
| 112 | nanosec = new Spinner(group, SWT.BORDER); |
| 113 | nanosec.addModifyListener(new ModifyListener() { |
| 114 | @Override |
| 115 | public void modifyText(ModifyEvent e) { |
| 116 | currentNanosec = nanosec.getSelection(); |
| 117 | // Correct for nanosec underflow |
| 118 | if (currentNanosec < 0) { |
| 119 | currentSeconds--; |
| 120 | currentNanosec = NS_PER_SECOND - 1; |
| 121 | } |
| 122 | // Correct for nanosec overflow |
| 123 | if (currentNanosec >= NS_PER_SECOND) { |
| 124 | currentSeconds++; |
| 125 | currentNanosec = 0; |
| 126 | } |
| 127 | refreshCurrentTime(); |
| 128 | } |
| 129 | }); |
| 130 | nanosec.setBounds(150, 25, 110, 25); |
| 131 | |
| 132 | label = new Label(group, SWT.LEFT); |
| 133 | label.setText(NANOSEC_LABEL); |
| 134 | label.setBounds(265, 28, 25, 22); |
| 135 | |
| 136 | setContent(range, current); |
| 137 | } |
| 138 | |
| 139 | private void refreshCurrentTime() { |
| 140 | long newCurrentTime = ((long) currentSeconds) * NS_PER_SECOND + currentNanosec; |
| 141 | TmfTimestamp ts = new TmfTimestamp(newCurrentTime, NS_SCALING_FACTOR, 0); |
| 142 | currentTime = ts; |
| 143 | // fOwner.synchTimeFrameWidgets(this); |
| 144 | } |
| 145 | |
| 146 | // ==================================================================== |
| 147 | // Get/Set |
| 148 | // ==================================================================== |
| 149 | |
| 150 | public TmfTimestamp getStartTime() { |
| 151 | return startTime; |
| 152 | } |
| 153 | |
| 154 | public TmfTimestamp getEndTime() { |
| 155 | return endTime; |
| 156 | } |
| 157 | |
| 158 | public TmfTimestamp getCurrentTime() { |
| 159 | return currentTime; |
| 160 | } |
| 161 | |
| 162 | public TmfTimestamp getSpan() { |
| 163 | TmfTimestamp span = new TmfTimestamp(startTime.getAdjustment(endTime, NS_SCALING_FACTOR), NS_SCALING_FACTOR, 0); |
| 164 | return span; |
| 165 | } |
| 166 | |
| 167 | public TmfTimeRange getTimeRange() { |
| 168 | TmfTimeRange range = new TmfTimeRange(startTime, endTime); |
| 169 | return range; |
| 170 | } |
| 171 | |
| 172 | public void setStartTime(TmfTimestamp ts) { |
| 173 | try { |
| 174 | startTime = ts.synchronize(0, NS_SCALING_FACTOR); |
| 175 | startSeconds = (int) (startTime.getValue() / NS_PER_SECOND); |
| 176 | startNanosec = (int) (startTime.getValue() % NS_PER_SECOND); |
| 177 | } |
| 178 | catch (ArithmeticException e) { |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | public void setEndTime(TmfTimestamp ts) { |
| 183 | try { |
| 184 | endTime = ts.synchronize(0, NS_SCALING_FACTOR); |
| 185 | endSeconds = (int) (endTime.getValue() / NS_PER_SECOND); |
| 186 | endNanosec = (int) (endTime.getValue() % NS_PER_SECOND); |
| 187 | } |
| 188 | catch (ArithmeticException e) { |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | public void setCurrentTime(TmfTimestamp ts) { |
| 193 | try { |
| 194 | currentTime = ts.synchronize(0, NS_SCALING_FACTOR); |
| 195 | currentSeconds = (int) (currentTime.getValue() / NS_PER_SECOND); |
| 196 | currentNanosec = (int) (currentTime.getValue() % NS_PER_SECOND); |
| 197 | } |
| 198 | catch (ArithmeticException e) { |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | // ==================================================================== |
| 203 | // Operators |
| 204 | // ==================================================================== |
| 205 | |
| 206 | /** |
| 207 | * <b><u>setContent</u></b> |
| 208 | * <p> |
| 209 | * <li>validates that [startTime <= currentTime <= endTime] is respected |
| 210 | * <li>sets the start/current/end time and update the spinners |
| 211 | * </li> |
| 212 | * <p> |
| 213 | * |
| 214 | * @param range |
| 215 | * @param current |
| 216 | */ |
| 217 | public void setContent(TmfTimeRange range, TmfTimestamp current) { |
| 218 | |
| 219 | if (range != null) { |
| 220 | // Extract the time range |
| 221 | TmfTimestamp start = range.getStartTime(); |
| 222 | TmfTimestamp end = range.getEndTime(); |
| 223 | |
| 224 | // Assume start time is OK |
| 225 | setStartTime(start); |
| 226 | |
| 227 | // Make sure end time >= start time |
| 228 | if (end.compareTo(start, false) < 0) { |
| 229 | end = start; |
| 230 | } |
| 231 | setEndTime(end); |
| 232 | |
| 233 | // Make sure [start time <= current time <= end time] |
| 234 | // If not: current = min(max(start, current), end); |
| 235 | if (current.compareTo(start, false) < 0) { |
| 236 | current = start; |
| 237 | } |
| 238 | if (current.compareTo(end, false) > 0) { |
| 239 | current = end; |
| 240 | } |
| 241 | } |
| 242 | setCurrentTime(current); |
| 243 | |
| 244 | // And configure the spinners |
| 245 | updateSpinners(); |
| 246 | } |
| 247 | |
| 248 | /** |
| 249 | * <b><u>setValue</u></b> |
| 250 | * <p> |
| 251 | * <li>validates that [startTime <= currentTime <= endTime] is respected |
| 252 | * <li>sets the current time and the spinners |
| 253 | * </li> |
| 254 | * <p> |
| 255 | * |
| 256 | * @param range |
| 257 | * @param current |
| 258 | */ |
| 259 | public void setValue(TmfTimestamp current) { |
| 260 | |
| 261 | // Make sure [start time <= current time <= end time] |
| 262 | // If not: current = min(max(start, current), end); |
| 263 | if (current.compareTo(startTime, false) < 0) { |
| 264 | current = startTime; |
| 265 | } |
| 266 | if (current.compareTo(endTime, false) > 0) { |
| 267 | current = endTime; |
| 268 | } |
| 269 | setCurrentTime(current); |
| 270 | |
| 271 | // And configure the spinners |
| 272 | updateSpinners(); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Update the spinners with the new current time value |
| 277 | * Perform the update on the UI thread |
| 278 | */ |
| 279 | public void updateSpinners() { |
| 280 | // Ignore update if disposed |
| 281 | if (seconds.isDisposed()) return; |
| 282 | |
| 283 | seconds.getDisplay().asyncExec(new Runnable() { |
| 284 | @Override |
| 285 | public void run() { |
| 286 | if (!seconds.isDisposed() && !nanosec.isDisposed()) { |
| 287 | // If we are on the start second, ensure that [currentNS >= startNS] |
| 288 | // If the currentSeconds > startSeconds, set startns to -1 so we can |
| 289 | // "underflow" |
| 290 | int startns = -1; |
| 291 | if (currentSeconds <= startSeconds) { |
| 292 | currentSeconds = startSeconds; |
| 293 | startns = startNanosec; |
| 294 | if (currentNanosec < startns) { |
| 295 | currentNanosec = startns; |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | // If we are on the end second, ensure that [currentNS <= endNS] |
| 300 | // If the currentSeconds < endSeconds, set endns to MAX so we can |
| 301 | // "overflow" |
| 302 | int endns = NS_PER_SECOND; |
| 303 | if (currentSeconds >= endSeconds) { |
| 304 | currentSeconds = endSeconds; |
| 305 | endns = endNanosec; |
| 306 | if (currentNanosec > endns) { |
| 307 | currentNanosec = endns; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | // Refresh the spinners (value, range, increments, ...) |
| 312 | // To ensure that the spinners are properly set, the range has to be > 0 |
| 313 | // seconds.setValues(currentSeconds, startSeconds - 1, endSeconds + 1, 0, 1, 10); |
| 314 | // nanosec.setValues(currentNanosec, startns - 1, endns + 1, 0, 1, 1000000); |
| 315 | seconds.setValues(currentSeconds, startSeconds, endSeconds, 0, 1, 10); |
| 316 | nanosec.setValues(currentNanosec, startns, endns, 0, 100000, 10000000); |
| 317 | |
| 318 | // If start == end (i.e. no range), disable the spinner |
| 319 | // (if start == end, the spinner widget range is set to [0..100] by default) |
| 320 | seconds.setEnabled(startSeconds != endSeconds); |
| 321 | nanosec.setEnabled(startns != endns); |
| 322 | } |
| 323 | } |
| 324 | }); |
| 325 | } |
| 326 | } |