1 /*******************************************************************************
2 * Copyright (c) 2009, 2013 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
.linuxtools
.tmf
.ui
.views
.histogram
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
20 import org
.eclipse
.swt
.SWT
;
21 import org
.eclipse
.swt
.events
.FocusEvent
;
22 import org
.eclipse
.swt
.events
.FocusListener
;
23 import org
.eclipse
.swt
.events
.KeyEvent
;
24 import org
.eclipse
.swt
.events
.KeyListener
;
25 import org
.eclipse
.swt
.events
.MouseWheelListener
;
26 import org
.eclipse
.swt
.graphics
.Font
;
27 import org
.eclipse
.swt
.graphics
.FontData
;
28 import org
.eclipse
.swt
.layout
.GridData
;
29 import org
.eclipse
.swt
.layout
.GridLayout
;
30 import org
.eclipse
.swt
.widgets
.Composite
;
31 import org
.eclipse
.swt
.widgets
.Display
;
32 import org
.eclipse
.swt
.widgets
.Group
;
33 import org
.eclipse
.swt
.widgets
.Text
;
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 Group fGroup
;
58 * The text value field.
60 protected final Text fTextValue
;
64 // ------------------------------------------------------------------------
66 // ------------------------------------------------------------------------
69 * Constructor with given group and text values.
71 * @param parentView The parent histogram view.
72 * @param parent The parent composite
73 * @param label The text label
74 * @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(1, true);
101 gridLayout
.horizontalSpacing
= 0;
102 gridLayout
.verticalSpacing
= 0;
103 fGroup
= new Group(fParent
, SWT
.SHADOW_NONE
);
104 fGroup
.setText(label
);
105 fGroup
.setFont(fFont
);
106 fGroup
.setLayout(gridLayout
);
109 gridData
= new GridData(SWT
.LEFT
, SWT
.CENTER
, true, false);
110 gridData
.horizontalIndent
= 0;
111 gridData
.verticalIndent
= 0;
112 fTextValue
= new Text(fGroup
, SWT
.BORDER
);
113 fTextValue
.setFont(fFont
);
114 fTextValue
.setLayoutData(gridData
);
116 // --------------------------------------------------------------------
118 // --------------------------------------------------------------------
120 fTextValue
.addFocusListener(this);
121 fTextValue
.addKeyListener(this);
123 TmfSignalManager
.register(this);
127 * Dispose of the widget
130 public void dispose() {
131 TmfSignalManager
.deregister(this);
134 // ------------------------------------------------------------------------
136 // ------------------------------------------------------------------------
139 * Returns if widget isDisposed or not
140 * @return <code>true</code> if widget is disposed else <code>false</code>
142 public boolean isDisposed() {
143 return fGroup
.isDisposed();
146 // ------------------------------------------------------------------------
148 // ------------------------------------------------------------------------
151 * Updates the text value.
153 protected abstract void updateValue();
156 * Set the Grid Layout Data for the group.
157 * @param layoutData A GridData to set.
159 public void setLayoutData(GridData layoutData
) {
160 fGroup
.setLayoutData(layoutData
);
164 * The time value in to set in the text field.
166 * @param time the time to set
167 * @param displayTime the display value
170 protected void setValue(final long time
, final String displayTime
) {
171 // If this is the UI thread, process now
172 Display display
= Display
.getCurrent();
173 if (display
!= null) {
176 fTextValue
.setText(displayTime
);
177 fParent
.getParent().layout();
182 // Call self from the UI thread
184 Display
.getDefault().asyncExec(new Runnable() {
188 setValue(time
, displayTime
);
196 * @param time the time value to display
198 public abstract void setValue(long time
);
201 * Returns the time value.
202 * @return time value.
204 public long getValue() {
209 * Add a mouse wheel listener to the text control
210 * @param listener the mouse wheel listener
213 public void addMouseWheelListener(MouseWheelListener listener
) {
214 fTextValue
.addMouseWheelListener(listener
);
218 * Remove a mouse wheel listener from the text control
219 * @param listener the mouse wheel listener
222 public void removeMouseWheelListener(MouseWheelListener listener
) {
223 fTextValue
.removeMouseWheelListener(listener
);
226 // ------------------------------------------------------------------------
228 // ------------------------------------------------------------------------
231 public void focusGained(FocusEvent event
) {
235 public void focusLost(FocusEvent event
) {
239 // ------------------------------------------------------------------------
241 // ------------------------------------------------------------------------
244 public void keyPressed(KeyEvent event
) {
245 switch (event
.keyCode
) {
255 public void keyReleased(KeyEvent e
) {