Commit | Line | Data |
---|---|---|
c392540b | 1 | /******************************************************************************* |
11252342 | 2 | * Copyright (c) 2009, 2013 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 | |
e0752744 | 17 | package org.eclipse.linuxtools.tmf.ui.views.histogram; |
c392540b | 18 | |
f8177ba2 | 19 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager; |
c392540b FC |
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; | |
65cdf787 | 25 | import org.eclipse.swt.events.MouseWheelListener; |
c392540b FC |
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; | |
720d67cb | 32 | import org.eclipse.swt.widgets.Label; |
c392540b FC |
33 | import org.eclipse.swt.widgets.Text; |
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 | */ |
41 | public 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 | |
76 | * @since 2.0 | |
b544077e | 77 | */ |
f8177ba2 FC |
78 | public HistogramTextControl(HistogramView parentView, Composite parent, String label, long value) |
79 | { | |
c392540b FC |
80 | fParentView = parentView; |
81 | fParent = parent; | |
82 | ||
83 | // -------------------------------------------------------------------- | |
84 | // Reduce font size for a more pleasing rendering | |
85 | // -------------------------------------------------------------------- | |
86 | ||
87 | final int fontSizeAdjustment = -1; | |
88 | final Font font = parent.getFont(); | |
89 | final FontData fontData = font.getFontData()[0]; | |
f8177ba2 | 90 | fFont = new Font(font.getDevice(), fontData.getName(), fontData.getHeight() + fontSizeAdjustment, fontData.getStyle()); |
c392540b FC |
91 | |
92 | // -------------------------------------------------------------------- | |
93 | // Create the group | |
94 | // -------------------------------------------------------------------- | |
95 | ||
96 | // Re-used layout variables | |
97 | GridLayout gridLayout; | |
98 | GridData gridData; | |
99 | ||
720d67cb PT |
100 | // Composite |
101 | gridLayout = new GridLayout(3, false); | |
102 | gridLayout.marginHeight = 0; | |
103 | gridLayout.marginWidth = 0; | |
104 | fComposite = new Composite(fParent, SWT.NONE); | |
105 | fComposite.setLayout(gridLayout); | |
106 | ||
107 | gridData = new GridData(SWT.FILL, SWT.CENTER, true, false); | |
108 | Label filler = new Label(fComposite, SWT.NONE); | |
109 | filler.setLayoutData(gridData); | |
110 | ||
111 | // Label | |
112 | gridData = new GridData(SWT.CENTER, SWT.CENTER, false, false); | |
113 | fLabel = new Label(fComposite, SWT.NONE); | |
114 | fLabel.setText(label); | |
115 | fLabel.setFont(fFont); | |
116 | ||
117 | // Text control | |
118 | gridData = new GridData(SWT.CENTER, SWT.CENTER, false, false); | |
119 | fTextValue = new Text(fComposite, SWT.BORDER); | |
f8177ba2 | 120 | fTextValue.setFont(fFont); |
c392540b FC |
121 | fTextValue.setLayoutData(gridData); |
122 | ||
123 | // -------------------------------------------------------------------- | |
124 | // Add listeners | |
125 | // -------------------------------------------------------------------- | |
126 | ||
127 | fTextValue.addFocusListener(this); | |
128 | fTextValue.addKeyListener(this); | |
f8177ba2 FC |
129 | |
130 | TmfSignalManager.register(this); | |
131 | } | |
132 | ||
133 | /** | |
134 | * Dispose of the widget | |
135 | * @since 2.0 | |
136 | */ | |
137 | public void dispose() { | |
2fea16bc | 138 | fFont.dispose(); |
f8177ba2 | 139 | TmfSignalManager.deregister(this); |
c392540b FC |
140 | } |
141 | ||
142 | // ------------------------------------------------------------------------ | |
143 | // Accessors | |
144 | // ------------------------------------------------------------------------ | |
145 | ||
b544077e BH |
146 | /** |
147 | * Returns if widget isDisposed or not | |
f8177ba2 | 148 | * @return <code>true</code> if widget is disposed else <code>false</code> |
b544077e | 149 | */ |
c392540b | 150 | public boolean isDisposed() { |
720d67cb | 151 | return fComposite.isDisposed(); |
c392540b FC |
152 | } |
153 | ||
154 | // ------------------------------------------------------------------------ | |
155 | // Operations | |
156 | // ------------------------------------------------------------------------ | |
157 | ||
b544077e BH |
158 | /** |
159 | * Updates the text value. | |
160 | */ | |
c392540b FC |
161 | protected abstract void updateValue(); |
162 | ||
b544077e BH |
163 | /** |
164 | * Set the Grid Layout Data for the group. | |
165 | * @param layoutData A GridData to set. | |
166 | */ | |
c392540b | 167 | public void setLayoutData(GridData layoutData) { |
720d67cb PT |
168 | fComposite.setLayoutData(layoutData); |
169 | } | |
170 | ||
171 | /** | |
172 | * Enables the receiver if the argument is <code>true</code>, | |
173 | * and disables it otherwise. A disabled control is typically | |
174 | * not selectable from the user interface and draws with an | |
175 | * inactive or "grayed" look. | |
176 | * | |
177 | * @param enabled the new enabled state | |
178 | * @since 2.2 | |
179 | */ | |
180 | public void setEnabled(boolean enabled) { | |
181 | fTextValue.setEnabled(enabled); | |
c392540b FC |
182 | } |
183 | ||
b544077e | 184 | /** |
f8177ba2 FC |
185 | * The time value in to set in the text field. |
186 | * | |
b544077e | 187 | * @param time the time to set |
f8177ba2 FC |
188 | * @param displayTime the display value |
189 | * @since 2.0 | |
b544077e | 190 | */ |
f8177ba2 | 191 | protected void setValue(final long time, final String displayTime) { |
c392540b FC |
192 | // If this is the UI thread, process now |
193 | Display display = Display.getCurrent(); | |
194 | if (display != null) { | |
65cdf787 PT |
195 | if (!isDisposed()) { |
196 | fValue = time; | |
197 | fTextValue.setText(displayTime); | |
720d67cb | 198 | fComposite.layout(); |
65cdf787 PT |
199 | fParent.getParent().layout(); |
200 | } | |
c392540b FC |
201 | return; |
202 | } | |
203 | ||
f8177ba2 | 204 | // Call self from the UI thread |
c392540b FC |
205 | if (!isDisposed()) { |
206 | Display.getDefault().asyncExec(new Runnable() { | |
207 | @Override | |
208 | public void run() { | |
209 | if (!isDisposed()) { | |
f8177ba2 | 210 | setValue(time, displayTime); |
c392540b FC |
211 | } |
212 | } | |
213 | }); | |
214 | } | |
215 | } | |
f8177ba2 FC |
216 | |
217 | /** | |
218 | * @param time the time value to display | |
219 | */ | |
220 | public abstract void setValue(long time); | |
221 | ||
b544077e BH |
222 | /** |
223 | * Returns the time value. | |
224 | * @return time value. | |
225 | */ | |
c392540b FC |
226 | public long getValue() { |
227 | return fValue; | |
228 | } | |
229 | ||
65cdf787 PT |
230 | /** |
231 | * Add a mouse wheel listener to the text control | |
232 | * @param listener the mouse wheel listener | |
233 | * @since 2.0 | |
234 | */ | |
235 | public void addMouseWheelListener(MouseWheelListener listener) { | |
236 | fTextValue.addMouseWheelListener(listener); | |
237 | } | |
238 | ||
239 | /** | |
240 | * Remove a mouse wheel listener from the text control | |
241 | * @param listener the mouse wheel listener | |
242 | * @since 2.0 | |
243 | */ | |
244 | public void removeMouseWheelListener(MouseWheelListener listener) { | |
245 | fTextValue.removeMouseWheelListener(listener); | |
246 | } | |
247 | ||
c392540b FC |
248 | // ------------------------------------------------------------------------ |
249 | // FocusListener | |
250 | // ------------------------------------------------------------------------ | |
251 | ||
252 | @Override | |
253 | public void focusGained(FocusEvent event) { | |
254 | } | |
255 | ||
256 | @Override | |
257 | public void focusLost(FocusEvent event) { | |
258 | updateValue(); | |
259 | } | |
260 | ||
261 | // ------------------------------------------------------------------------ | |
262 | // KeyListener | |
263 | // ------------------------------------------------------------------------ | |
264 | ||
265 | @Override | |
266 | public void keyPressed(KeyEvent event) { | |
720d67cb | 267 | switch (event.character) { |
c392540b FC |
268 | case SWT.CR: |
269 | updateValue(); | |
270 | break; | |
271 | default: | |
272 | break; | |
273 | } | |
274 | } | |
275 | ||
276 | @Override | |
277 | public void keyReleased(KeyEvent e) { | |
278 | } | |
279 | ||
280 | } |