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
CommitLineData
c392540b 1/*******************************************************************************
ed902a2b 2 * Copyright (c) 2009, 2014 Ericsson
f8177ba2 3 *
c392540b
FC
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
f8177ba2 8 *
c392540b
FC
9 * Contributors:
10 * Wiliam Bourque - Adapted from SpinnerGroup (in TimeFrameView)
11 * Francois Chouinard - Cleanup and refactoring
e0752744 12 * Francois Chouinard - Moved from LTTng to TMF
f8177ba2 13 * Francois Chouinard - Better handling of control display, support for signals
65cdf787 14 * Patrick Tasse - Update for mouse wheel zoom
f8177ba2 15 *******************************************************************************/
c392540b 16
2bdf0193 17package org.eclipse.tracecompass.tmf.ui.views.histogram;
c392540b
FC
18
19import org.eclipse.swt.SWT;
20import org.eclipse.swt.events.FocusEvent;
21import org.eclipse.swt.events.FocusListener;
22import org.eclipse.swt.events.KeyEvent;
23import org.eclipse.swt.events.KeyListener;
65cdf787 24import org.eclipse.swt.events.MouseWheelListener;
c392540b
FC
25import org.eclipse.swt.graphics.Font;
26import org.eclipse.swt.graphics.FontData;
27import org.eclipse.swt.layout.GridData;
28import org.eclipse.swt.layout.GridLayout;
29import org.eclipse.swt.widgets.Composite;
30import org.eclipse.swt.widgets.Display;
720d67cb 31import org.eclipse.swt.widgets.Label;
c392540b 32import org.eclipse.swt.widgets.Text;
2bdf0193 33import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
c392540b
FC
34
35/**
c392540b 36 * This control provides a group containing a text control.
f8177ba2
FC
37 *
38 * @version 1.1
b544077e 39 * @author Francois Chouinard
c392540b
FC
40 */
41public abstract class HistogramTextControl implements FocusListener, KeyListener {
42
43 // ------------------------------------------------------------------------
44 // Attributes
45 // ------------------------------------------------------------------------
46
b544077e
BH
47 /**
48 * The parent histogram view.
49 */
c392540b 50 protected final HistogramView fParentView;
c392540b 51
f8177ba2
FC
52 // Controls data
53 private final Composite fParent;
54 private Font fFont;
720d67cb
PT
55 private final Composite fComposite;
56 private final Label fLabel;
f8177ba2 57
b544077e
BH
58 /**
59 * The text value field.
60 */
c392540b 61 protected final Text fTextValue;
f8177ba2 62
c392540b
FC
63 private long fValue;
64
65 // ------------------------------------------------------------------------
66 // Constructors
67 // ------------------------------------------------------------------------
68
b544077e
BH
69 /**
70 * Constructor with given group and text values.
f8177ba2 71 *
b544077e
BH
72 * @param parentView The parent histogram view.
73 * @param parent The parent composite
f8177ba2
FC
74 * @param label The text label
75 * @param value The initial value
b544077e 76 */
f8177ba2
FC
77 public HistogramTextControl(HistogramView parentView, Composite parent, String label, long value)
78 {
c392540b
FC
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];
f8177ba2 89 fFont = new Font(font.getDevice(), fontData.getName(), fontData.getHeight() + fontSizeAdjustment, fontData.getStyle());
c392540b
FC
90
91 // --------------------------------------------------------------------
92 // Create the group
93 // --------------------------------------------------------------------
94
95 // Re-used layout variables
96 GridLayout gridLayout;
97 GridData gridData;
98
720d67cb
PT
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);
f8177ba2 119 fTextValue.setFont(fFont);
c392540b
FC
120 fTextValue.setLayoutData(gridData);
121
122 // --------------------------------------------------------------------
123 // Add listeners
124 // --------------------------------------------------------------------
125
126 fTextValue.addFocusListener(this);
127 fTextValue.addKeyListener(this);
f8177ba2
FC
128
129 TmfSignalManager.register(this);
130 }
131
132 /**
133 * Dispose of the widget
f8177ba2
FC
134 */
135 public void dispose() {
2fea16bc 136 fFont.dispose();
f8177ba2 137 TmfSignalManager.deregister(this);
c392540b
FC
138 }
139
140 // ------------------------------------------------------------------------
141 // Accessors
142 // ------------------------------------------------------------------------
143
b544077e
BH
144 /**
145 * Returns if widget isDisposed or not
f8177ba2 146 * @return <code>true</code> if widget is disposed else <code>false</code>
b544077e 147 */
c392540b 148 public boolean isDisposed() {
720d67cb 149 return fComposite.isDisposed();
c392540b
FC
150 }
151
152 // ------------------------------------------------------------------------
153 // Operations
154 // ------------------------------------------------------------------------
155
b544077e
BH
156 /**
157 * Updates the text value.
158 */
c392540b
FC
159 protected abstract void updateValue();
160
b544077e
BH
161 /**
162 * Set the Grid Layout Data for the group.
163 * @param layoutData A GridData to set.
164 */
c392540b 165 public void setLayoutData(GridData layoutData) {
720d67cb
PT
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
720d67cb
PT
176 */
177 public void setEnabled(boolean enabled) {
178 fTextValue.setEnabled(enabled);
c392540b
FC
179 }
180
b544077e 181 /**
f8177ba2
FC
182 * The time value in to set in the text field.
183 *
b544077e 184 * @param time the time to set
f8177ba2 185 * @param displayTime the display value
b544077e 186 */
f8177ba2 187 protected void setValue(final long time, final String displayTime) {
c392540b
FC
188 // If this is the UI thread, process now
189 Display display = Display.getCurrent();
190 if (display != null) {
65cdf787
PT
191 if (!isDisposed()) {
192 fValue = time;
193 fTextValue.setText(displayTime);
720d67cb 194 fComposite.layout();
65cdf787
PT
195 fParent.getParent().layout();
196 }
c392540b
FC
197 return;
198 }
199
f8177ba2 200 // Call self from the UI thread
c392540b
FC
201 if (!isDisposed()) {
202 Display.getDefault().asyncExec(new Runnable() {
203 @Override
204 public void run() {
205 if (!isDisposed()) {
f8177ba2 206 setValue(time, displayTime);
c392540b
FC
207 }
208 }
209 });
210 }
211 }
f8177ba2
FC
212
213 /**
214 * @param time the time value to display
215 */
216 public abstract void setValue(long time);
217
b544077e
BH
218 /**
219 * Returns the time value.
220 * @return time value.
221 */
c392540b
FC
222 public long getValue() {
223 return fValue;
224 }
225
65cdf787
PT
226 /**
227 * Add a mouse wheel listener to the text control
228 * @param listener the mouse wheel listener
65cdf787
PT
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
65cdf787
PT
237 */
238 public void removeMouseWheelListener(MouseWheelListener listener) {
239 fTextValue.removeMouseWheelListener(listener);
240 }
241
c392540b
FC
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) {
720d67cb 261 switch (event.character) {
c392540b
FC
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.158425 seconds and 5 git commands to generate.