lttng: Temporary lttng-luna annotation update
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / views / histogram / HistogramTextControl.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 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 * 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 *******************************************************************************/
16
17 package org.eclipse.linuxtools.tmf.ui.views.histogram;
18
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;
34
35 /**
36 * This control provides a group containing a text control.
37 *
38 * @version 1.1
39 * @author Francois Chouinard
40 */
41 public abstract class HistogramTextControl implements FocusListener, KeyListener {
42
43 // ------------------------------------------------------------------------
44 // Attributes
45 // ------------------------------------------------------------------------
46
47 /**
48 * The parent histogram view.
49 */
50 protected final HistogramView fParentView;
51
52 // Controls data
53 private final Composite fParent;
54 private Font fFont;
55 private final Group fGroup;
56
57 /**
58 * The text value field.
59 */
60 protected final Text fTextValue;
61
62 private long fValue;
63
64 // ------------------------------------------------------------------------
65 // Constructors
66 // ------------------------------------------------------------------------
67
68 /**
69 * Constructor with given group and text values.
70 *
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
75 * @since 2.0
76 */
77 public HistogramTextControl(HistogramView parentView, Composite parent, String label, long value)
78 {
79 fParentView = parentView;
80 fParent = parent;
81
82 // --------------------------------------------------------------------
83 // Reduce font size for a more pleasing rendering
84 // --------------------------------------------------------------------
85
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());
90
91 // --------------------------------------------------------------------
92 // Create the group
93 // --------------------------------------------------------------------
94
95 // Re-used layout variables
96 GridLayout gridLayout;
97 GridData gridData;
98
99 // Group control
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);
107
108 // Group control
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);
115
116 // --------------------------------------------------------------------
117 // Add listeners
118 // --------------------------------------------------------------------
119
120 fTextValue.addFocusListener(this);
121 fTextValue.addKeyListener(this);
122
123 TmfSignalManager.register(this);
124 }
125
126 /**
127 * Dispose of the widget
128 * @since 2.0
129 */
130 public void dispose() {
131 TmfSignalManager.deregister(this);
132 }
133
134 // ------------------------------------------------------------------------
135 // Accessors
136 // ------------------------------------------------------------------------
137
138 /**
139 * Returns if widget isDisposed or not
140 * @return <code>true</code> if widget is disposed else <code>false</code>
141 */
142 public boolean isDisposed() {
143 return fGroup.isDisposed();
144 }
145
146 // ------------------------------------------------------------------------
147 // Operations
148 // ------------------------------------------------------------------------
149
150 /**
151 * Updates the text value.
152 */
153 protected abstract void updateValue();
154
155 /**
156 * Set the Grid Layout Data for the group.
157 * @param layoutData A GridData to set.
158 */
159 public void setLayoutData(GridData layoutData) {
160 fGroup.setLayoutData(layoutData);
161 }
162
163 /**
164 * The time value in to set in the text field.
165 *
166 * @param time the time to set
167 * @param displayTime the display value
168 * @since 2.0
169 */
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) {
174 if (!isDisposed()) {
175 fValue = time;
176 fTextValue.setText(displayTime);
177 fParent.getParent().layout();
178 }
179 return;
180 }
181
182 // Call self from the UI thread
183 if (!isDisposed()) {
184 Display.getDefault().asyncExec(new Runnable() {
185 @Override
186 public void run() {
187 if (!isDisposed()) {
188 setValue(time, displayTime);
189 }
190 }
191 });
192 }
193 }
194
195 /**
196 * @param time the time value to display
197 */
198 public abstract void setValue(long time);
199
200 /**
201 * Returns the time value.
202 * @return time value.
203 */
204 public long getValue() {
205 return fValue;
206 }
207
208 /**
209 * Add a mouse wheel listener to the text control
210 * @param listener the mouse wheel listener
211 * @since 2.0
212 */
213 public void addMouseWheelListener(MouseWheelListener listener) {
214 fTextValue.addMouseWheelListener(listener);
215 }
216
217 /**
218 * Remove a mouse wheel listener from the text control
219 * @param listener the mouse wheel listener
220 * @since 2.0
221 */
222 public void removeMouseWheelListener(MouseWheelListener listener) {
223 fTextValue.removeMouseWheelListener(listener);
224 }
225
226 // ------------------------------------------------------------------------
227 // FocusListener
228 // ------------------------------------------------------------------------
229
230 @Override
231 public void focusGained(FocusEvent event) {
232 }
233
234 @Override
235 public void focusLost(FocusEvent event) {
236 updateValue();
237 }
238
239 // ------------------------------------------------------------------------
240 // KeyListener
241 // ------------------------------------------------------------------------
242
243 @Override
244 public void keyPressed(KeyEvent event) {
245 switch (event.keyCode) {
246 case SWT.CR:
247 updateValue();
248 break;
249 default:
250 break;
251 }
252 }
253
254 @Override
255 public void keyReleased(KeyEvent e) {
256 }
257
258 }
This page took 0.036464 seconds and 5 git commands to generate.