analysis: Move plugins to their own sub-directory
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / histogram / HistogramTextControl.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 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.tracecompass.tmf.ui.views.histogram;
18
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;
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 Composite fComposite;
56 private final Label fLabel;
57
58 /**
59 * The text value field.
60 */
61 protected final Text fTextValue;
62
63 private long fValue;
64
65 // ------------------------------------------------------------------------
66 // Constructors
67 // ------------------------------------------------------------------------
68
69 /**
70 * Constructor with given group and text values.
71 *
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
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 // Composite
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);
105
106 gridData = new GridData(SWT.FILL, SWT.CENTER, true, false);
107 Label filler = new Label(fComposite, SWT.NONE);
108 filler.setLayoutData(gridData);
109
110 // Label
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);
115
116 // Text control
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);
121
122 // --------------------------------------------------------------------
123 // Add listeners
124 // --------------------------------------------------------------------
125
126 fTextValue.addFocusListener(this);
127 fTextValue.addKeyListener(this);
128
129 TmfSignalManager.register(this);
130 }
131
132 /**
133 * Dispose of the widget
134 */
135 public void dispose() {
136 fFont.dispose();
137 TmfSignalManager.deregister(this);
138 }
139
140 // ------------------------------------------------------------------------
141 // Accessors
142 // ------------------------------------------------------------------------
143
144 /**
145 * Returns if widget isDisposed or not
146 * @return <code>true</code> if widget is disposed else <code>false</code>
147 */
148 public boolean isDisposed() {
149 return fComposite.isDisposed();
150 }
151
152 // ------------------------------------------------------------------------
153 // Operations
154 // ------------------------------------------------------------------------
155
156 /**
157 * Updates the text value.
158 */
159 protected abstract void updateValue();
160
161 /**
162 * Set the Grid Layout Data for the group.
163 * @param layoutData A GridData to set.
164 */
165 public void setLayoutData(GridData layoutData) {
166 fComposite.setLayoutData(layoutData);
167 }
168
169 /**
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.
174 *
175 * @param enabled the new enabled state
176 */
177 public void setEnabled(boolean enabled) {
178 fTextValue.setEnabled(enabled);
179 }
180
181 /**
182 * The time value in to set in the text field.
183 *
184 * @param time the time to set
185 * @param displayTime the display value
186 */
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) {
191 if (!isDisposed()) {
192 fValue = time;
193 fTextValue.setText(displayTime);
194 fComposite.layout();
195 fParent.getParent().layout();
196 }
197 return;
198 }
199
200 // Call self from the UI thread
201 if (!isDisposed()) {
202 Display.getDefault().asyncExec(new Runnable() {
203 @Override
204 public void run() {
205 if (!isDisposed()) {
206 setValue(time, displayTime);
207 }
208 }
209 });
210 }
211 }
212
213 /**
214 * @param time the time value to display
215 */
216 public abstract void setValue(long time);
217
218 /**
219 * Returns the time value.
220 * @return time value.
221 */
222 public long getValue() {
223 return fValue;
224 }
225
226 /**
227 * Add a mouse wheel listener to the text control
228 * @param listener the mouse wheel listener
229 */
230 public void addMouseWheelListener(MouseWheelListener listener) {
231 fTextValue.addMouseWheelListener(listener);
232 }
233
234 /**
235 * Remove a mouse wheel listener from the text control
236 * @param listener the mouse wheel listener
237 */
238 public void removeMouseWheelListener(MouseWheelListener listener) {
239 fTextValue.removeMouseWheelListener(listener);
240 }
241
242 // ------------------------------------------------------------------------
243 // FocusListener
244 // ------------------------------------------------------------------------
245
246 @Override
247 public void focusGained(FocusEvent event) {
248 }
249
250 @Override
251 public void focusLost(FocusEvent event) {
252 updateValue();
253 }
254
255 // ------------------------------------------------------------------------
256 // KeyListener
257 // ------------------------------------------------------------------------
258
259 @Override
260 public void keyPressed(KeyEvent event) {
261 switch (event.character) {
262 case SWT.CR:
263 updateValue();
264 break;
265 default:
266 break;
267 }
268 }
269
270 @Override
271 public void keyReleased(KeyEvent e) {
272 }
273
274 }
This page took 0.061079 seconds and 5 git commands to generate.