1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 Ericsson
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
10 * Wiliam Bourque - Adapted from SpinnerGroup (in TimeFrameView)
11 * Francois Chouinard - Cleanup and refactoring
12 * Francois Chouinard - Moved from LTTng to TMF
13 * Francois Chouinard - Better handling of control display, support for signals
14 * Patrick Tasse - Update for mouse wheel zoom
15 *******************************************************************************/
17 package org
.eclipse
.tracecompass
.tmf
.ui
.views
.histogram
;
19 import org
.eclipse
.swt
.SWT
;
20 import org
.eclipse
.swt
.events
.FocusEvent
;
21 import org
.eclipse
.swt
.events
.FocusListener
;
22 import org
.eclipse
.swt
.events
.KeyEvent
;
23 import org
.eclipse
.swt
.events
.KeyListener
;
24 import org
.eclipse
.swt
.events
.MouseWheelListener
;
25 import org
.eclipse
.swt
.graphics
.Font
;
26 import org
.eclipse
.swt
.graphics
.FontData
;
27 import org
.eclipse
.swt
.layout
.GridData
;
28 import org
.eclipse
.swt
.layout
.GridLayout
;
29 import org
.eclipse
.swt
.widgets
.Composite
;
30 import org
.eclipse
.swt
.widgets
.Display
;
31 import org
.eclipse
.swt
.widgets
.Label
;
32 import org
.eclipse
.swt
.widgets
.Text
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalManager
;
36 * This control provides a group containing a text control.
39 * @author Francois Chouinard
41 public abstract class HistogramTextControl
implements FocusListener
, KeyListener
{
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
48 * The parent histogram view.
50 protected final HistogramView fParentView
;
53 private final Composite fParent
;
55 private final Composite fComposite
;
56 private final Label fLabel
;
59 * The text value field.
61 protected final Text fTextValue
;
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
70 * Constructor with given group and text values.
72 * @param parentView The parent histogram view.
73 * @param parent The parent composite
74 * @param label The text label
75 * @param value The initial value
77 public HistogramTextControl(HistogramView parentView
, Composite parent
, String label
, long value
)
79 fParentView
= parentView
;
82 // --------------------------------------------------------------------
83 // Reduce font size for a more pleasing rendering
84 // --------------------------------------------------------------------
86 final int fontSizeAdjustment
= -1;
87 final Font font
= parent
.getFont();
88 final FontData fontData
= font
.getFontData()[0];
89 fFont
= new Font(font
.getDevice(), fontData
.getName(), fontData
.getHeight() + fontSizeAdjustment
, fontData
.getStyle());
91 // --------------------------------------------------------------------
93 // --------------------------------------------------------------------
95 // Re-used layout variables
96 GridLayout gridLayout
;
100 gridLayout
= new GridLayout(3, false);
101 gridLayout
.marginHeight
= 0;
102 gridLayout
.marginWidth
= 0;
103 fComposite
= new Composite(fParent
, SWT
.NONE
);
104 fComposite
.setLayout(gridLayout
);
106 gridData
= new GridData(SWT
.FILL
, SWT
.CENTER
, true, false);
107 Label filler
= new Label(fComposite
, SWT
.NONE
);
108 filler
.setLayoutData(gridData
);
111 gridData
= new GridData(SWT
.CENTER
, SWT
.CENTER
, false, false);
112 fLabel
= new Label(fComposite
, SWT
.NONE
);
113 fLabel
.setText(label
);
114 fLabel
.setFont(fFont
);
117 gridData
= new GridData(SWT
.CENTER
, SWT
.CENTER
, false, false);
118 fTextValue
= new Text(fComposite
, SWT
.BORDER
);
119 fTextValue
.setFont(fFont
);
120 fTextValue
.setLayoutData(gridData
);
122 // --------------------------------------------------------------------
124 // --------------------------------------------------------------------
126 fTextValue
.addFocusListener(this);
127 fTextValue
.addKeyListener(this);
129 TmfSignalManager
.register(this);
133 * Dispose of the widget
135 public void dispose() {
137 TmfSignalManager
.deregister(this);
140 // ------------------------------------------------------------------------
142 // ------------------------------------------------------------------------
145 * Returns if widget isDisposed or not
146 * @return <code>true</code> if widget is disposed else <code>false</code>
148 public boolean isDisposed() {
149 return fComposite
.isDisposed();
152 // ------------------------------------------------------------------------
154 // ------------------------------------------------------------------------
157 * Updates the text value.
159 protected abstract void updateValue();
162 * Set the Grid Layout Data for the group.
163 * @param layoutData A GridData to set.
165 public void setLayoutData(GridData layoutData
) {
166 fComposite
.setLayoutData(layoutData
);
170 * Enables the receiver if the argument is <code>true</code>,
171 * and disables it otherwise. A disabled control is typically
172 * not selectable from the user interface and draws with an
173 * inactive or "grayed" look.
175 * @param enabled the new enabled state
177 public void setEnabled(boolean enabled
) {
178 fTextValue
.setEnabled(enabled
);
182 * The time value in to set in the text field.
184 * @param time the time to set
185 * @param displayTime the display value
187 protected void setValue(final long time
, final String displayTime
) {
188 // If this is the UI thread, process now
189 Display display
= Display
.getCurrent();
190 if (display
!= null) {
193 fTextValue
.setText(displayTime
);
195 fParent
.getParent().layout();
200 // Call self from the UI thread
202 Display
.getDefault().asyncExec(new Runnable() {
206 setValue(time
, displayTime
);
214 * @param time the time value to display
216 public abstract void setValue(long time
);
219 * Returns the time value.
220 * @return time value.
222 public long getValue() {
227 * Add a mouse wheel listener to the text control
228 * @param listener the mouse wheel listener
230 public void addMouseWheelListener(MouseWheelListener listener
) {
231 fTextValue
.addMouseWheelListener(listener
);
235 * Remove a mouse wheel listener from the text control
236 * @param listener the mouse wheel listener
238 public void removeMouseWheelListener(MouseWheelListener listener
) {
239 fTextValue
.removeMouseWheelListener(listener
);
242 // ------------------------------------------------------------------------
244 // ------------------------------------------------------------------------
247 public void focusGained(FocusEvent event
) {
251 public void focusLost(FocusEvent event
) {
255 // ------------------------------------------------------------------------
257 // ------------------------------------------------------------------------
260 public void keyPressed(KeyEvent event
) {
261 switch (event
.character
) {
271 public void keyReleased(KeyEvent e
) {