1 /*******************************************************************************
2 * Copyright (c) 2009, 2011, 2012 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 *******************************************************************************/
16 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.histogram
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
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
.graphics
.Font
;
25 import org
.eclipse
.swt
.graphics
.FontData
;
26 import org
.eclipse
.swt
.layout
.GridData
;
27 import org
.eclipse
.swt
.layout
.GridLayout
;
28 import org
.eclipse
.swt
.widgets
.Composite
;
29 import org
.eclipse
.swt
.widgets
.Display
;
30 import org
.eclipse
.swt
.widgets
.Group
;
31 import org
.eclipse
.swt
.widgets
.Text
;
34 * This control provides a group containing a text control.
37 * @author Francois Chouinard
39 public abstract class HistogramTextControl
implements FocusListener
, KeyListener
{
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
46 * The parent histogram view.
48 protected final HistogramView fParentView
;
51 private final Composite fParent
;
53 private final Group fGroup
;
56 * The text value field.
58 protected final Text fTextValue
;
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
67 * Constructor with given group and text values.
69 * @param parentView The parent histogram view.
70 * @param parent The parent composite
71 * @param label The text label
72 * @param value The initial value
75 public HistogramTextControl(HistogramView parentView
, Composite parent
, String label
, long value
)
77 fParentView
= parentView
;
80 // --------------------------------------------------------------------
81 // Reduce font size for a more pleasing rendering
82 // --------------------------------------------------------------------
84 final int fontSizeAdjustment
= -1;
85 final Font font
= parent
.getFont();
86 final FontData fontData
= font
.getFontData()[0];
87 fFont
= new Font(font
.getDevice(), fontData
.getName(), fontData
.getHeight() + fontSizeAdjustment
, fontData
.getStyle());
89 // --------------------------------------------------------------------
91 // --------------------------------------------------------------------
93 // Re-used layout variables
94 GridLayout gridLayout
;
98 gridLayout
= new GridLayout(1, true);
99 gridLayout
.horizontalSpacing
= 0;
100 gridLayout
.verticalSpacing
= 0;
101 fGroup
= new Group(fParent
, SWT
.SHADOW_NONE
);
102 fGroup
.setText(label
);
103 fGroup
.setFont(fFont
);
104 fGroup
.setLayout(gridLayout
);
107 gridData
= new GridData(SWT
.LEFT
, SWT
.CENTER
, true, false);
108 gridData
.horizontalIndent
= 0;
109 gridData
.verticalIndent
= 0;
110 fTextValue
= new Text(fGroup
, SWT
.BORDER
);
111 fTextValue
.setFont(fFont
);
112 fTextValue
.setLayoutData(gridData
);
114 // --------------------------------------------------------------------
116 // --------------------------------------------------------------------
118 fTextValue
.addFocusListener(this);
119 fTextValue
.addKeyListener(this);
121 TmfSignalManager
.register(this);
125 * Dispose of the widget
128 public void dispose() {
129 TmfSignalManager
.deregister(this);
132 // ------------------------------------------------------------------------
134 // ------------------------------------------------------------------------
137 * Returns if widget isDisposed or not
138 * @return <code>true</code> if widget is disposed else <code>false</code>
140 public boolean isDisposed() {
141 return fGroup
.isDisposed();
144 // ------------------------------------------------------------------------
146 // ------------------------------------------------------------------------
149 * Updates the text value.
151 protected abstract void updateValue();
154 * Set the Grid Layout Data for the group.
155 * @param layoutData A GridData to set.
157 public void setLayoutData(GridData layoutData
) {
158 fGroup
.setLayoutData(layoutData
);
162 * The time value in to set in the text field.
164 * @param time the time to set
165 * @param displayTime the display value
168 protected void setValue(final long time
, final String displayTime
) {
169 // If this is the UI thread, process now
170 Display display
= Display
.getCurrent();
171 if (display
!= null) {
173 fTextValue
.setText(displayTime
);
174 fParent
.getParent().layout();
178 // Call self from the UI thread
180 Display
.getDefault().asyncExec(new Runnable() {
184 setValue(time
, displayTime
);
192 * @param time the time value to display
194 public abstract void setValue(long time
);
197 * Returns the time value.
198 * @return time value.
200 public long getValue() {
204 // ------------------------------------------------------------------------
206 // ------------------------------------------------------------------------
210 * @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent)
213 public void focusGained(FocusEvent event
) {
218 * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent)
221 public void focusLost(FocusEvent event
) {
225 // ------------------------------------------------------------------------
227 // ------------------------------------------------------------------------
231 * @see org.eclipse.swt.events.KeyListener#keyPressed(org.eclipse.swt.events.KeyEvent)
234 public void keyPressed(KeyEvent event
) {
235 switch (event
.keyCode
) {
246 * @see org.eclipse.swt.events.KeyListener#keyReleased(org.eclipse.swt.events.KeyEvent)
249 public void keyReleased(KeyEvent e
) {