| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011, 2013 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 | * Francois Chouinard - Moved from LTTng to TMF |
| 12 | * Francois Chouinard - Simplified constructor, handle interval format change |
| 13 | * Patrick Tasse - Update value handling |
| 14 | *******************************************************************************/ |
| 15 | |
| 16 | package org.eclipse.linuxtools.tmf.ui.views.histogram; |
| 17 | |
| 18 | import java.text.ParseException; |
| 19 | |
| 20 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler; |
| 21 | import org.eclipse.linuxtools.tmf.core.signal.TmfTimestampFormatUpdateSignal; |
| 22 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; |
| 23 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; |
| 24 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp; |
| 25 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestampFormat; |
| 26 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
| 27 | import org.eclipse.swt.widgets.Composite; |
| 28 | |
| 29 | /** |
| 30 | * This control provides a group containing a text control. |
| 31 | * |
| 32 | * @version 1.1 |
| 33 | * @author Francois Chouinard |
| 34 | */ |
| 35 | public class HistogramCurrentTimeControl extends HistogramTextControl { |
| 36 | |
| 37 | // ------------------------------------------------------------------------ |
| 38 | // Construction |
| 39 | // ------------------------------------------------------------------------ |
| 40 | |
| 41 | /** |
| 42 | * Standard constructor |
| 43 | * |
| 44 | * @param parentView A parent histogram view |
| 45 | * @param parent A parent composite to draw in |
| 46 | * @param label A label |
| 47 | * @param value A value |
| 48 | * @since 2.0 |
| 49 | */ |
| 50 | public HistogramCurrentTimeControl(HistogramView parentView, Composite parent, |
| 51 | String label, long value) |
| 52 | { |
| 53 | super(parentView, parent, label, value); |
| 54 | } |
| 55 | |
| 56 | // ------------------------------------------------------------------------ |
| 57 | // Operations |
| 58 | // ------------------------------------------------------------------------ |
| 59 | |
| 60 | @Override |
| 61 | protected void updateValue() { |
| 62 | if (getValue() == Long.MIN_VALUE) { |
| 63 | fTextValue.setText(""); //$NON-NLS-1$ |
| 64 | return; |
| 65 | } |
| 66 | String string = fTextValue.getText(); |
| 67 | long value = getValue(); |
| 68 | try { |
| 69 | value = TmfTimestampFormat.getDefaulTimeFormat().parseValue(string, getValue()); |
| 70 | } catch (ParseException e) { |
| 71 | } |
| 72 | if (getValue() != value) { |
| 73 | // Make sure that the new time is within range |
| 74 | ITmfTrace trace = fParentView.getTrace(); |
| 75 | if (trace != null) { |
| 76 | TmfTimeRange range = trace.getTimeRange(); |
| 77 | long startTime = range.getStartTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(); |
| 78 | long endTime = range.getEndTime().normalize(0, ITmfTimestamp.NANOSECOND_SCALE).getValue(); |
| 79 | if (value < startTime) { |
| 80 | value = startTime; |
| 81 | } else if (value > endTime) { |
| 82 | value = endTime; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // Set and propagate |
| 87 | setValue(value); |
| 88 | updateSelectionTime(value); |
| 89 | } else { |
| 90 | setValue(value); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Update the selection time |
| 96 | * |
| 97 | * @param time |
| 98 | * the new selected time |
| 99 | * @since 2.2 |
| 100 | */ |
| 101 | protected void updateSelectionTime(long time) { |
| 102 | fParentView.updateSelectionTime(time, time); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void setValue(long time) { |
| 107 | if (time != Long.MIN_VALUE) { |
| 108 | super.setValue(time, new TmfTimestamp(time, ITmfTimestamp.NANOSECOND_SCALE).toString()); |
| 109 | } else { |
| 110 | super.setValue(time, ""); //$NON-NLS-1$ |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // ------------------------------------------------------------------------ |
| 115 | // Signal Handlers |
| 116 | // ------------------------------------------------------------------------ |
| 117 | |
| 118 | /** |
| 119 | * Format the timestamp and update the display. Compute the new text size, |
| 120 | * adjust the text and group widgets and then refresh the view layout. |
| 121 | * |
| 122 | * @param signal the incoming signal |
| 123 | * @since 2.0 |
| 124 | */ |
| 125 | @TmfSignalHandler |
| 126 | public void timestampFormatUpdated(TmfTimestampFormatUpdateSignal signal) { |
| 127 | setValue(getValue()); |
| 128 | } |
| 129 | |
| 130 | } |