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; | |
32 | import org.eclipse.swt.widgets.Group; | |
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; | |
c392540b | 55 | private final Group fGroup; |
f8177ba2 | 56 | |
b544077e BH |
57 | /** |
58 | * The text value field. | |
59 | */ | |
c392540b | 60 | protected final Text fTextValue; |
f8177ba2 | 61 | |
c392540b FC |
62 | private long fValue; |
63 | ||
64 | // ------------------------------------------------------------------------ | |
65 | // Constructors | |
66 | // ------------------------------------------------------------------------ | |
67 | ||
b544077e BH |
68 | /** |
69 | * Constructor with given group and text values. | |
f8177ba2 | 70 | * |
b544077e BH |
71 | * @param parentView The parent histogram view. |
72 | * @param parent The parent composite | |
f8177ba2 FC |
73 | * @param label The text label |
74 | * @param value The initial value | |
75 | * @since 2.0 | |
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 | ||
99 | // Group control | |
f8177ba2 | 100 | gridLayout = new GridLayout(1, true); |
c392540b FC |
101 | gridLayout.horizontalSpacing = 0; |
102 | gridLayout.verticalSpacing = 0; | |
9a83a5f5 | 103 | fGroup = new Group(fParent, SWT.SHADOW_NONE); |
f8177ba2 FC |
104 | fGroup.setText(label); |
105 | fGroup.setFont(fFont); | |
c392540b FC |
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; | |
f8177ba2 FC |
112 | fTextValue = new Text(fGroup, SWT.BORDER); |
113 | fTextValue.setFont(fFont); | |
c392540b FC |
114 | fTextValue.setLayoutData(gridData); |
115 | ||
116 | // -------------------------------------------------------------------- | |
117 | // Add listeners | |
118 | // -------------------------------------------------------------------- | |
119 | ||
120 | fTextValue.addFocusListener(this); | |
121 | fTextValue.addKeyListener(this); | |
f8177ba2 FC |
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); | |
c392540b FC |
132 | } |
133 | ||
134 | // ------------------------------------------------------------------------ | |
135 | // Accessors | |
136 | // ------------------------------------------------------------------------ | |
137 | ||
b544077e BH |
138 | /** |
139 | * Returns if widget isDisposed or not | |
f8177ba2 | 140 | * @return <code>true</code> if widget is disposed else <code>false</code> |
b544077e | 141 | */ |
c392540b FC |
142 | public boolean isDisposed() { |
143 | return fGroup.isDisposed(); | |
144 | } | |
145 | ||
146 | // ------------------------------------------------------------------------ | |
147 | // Operations | |
148 | // ------------------------------------------------------------------------ | |
149 | ||
b544077e BH |
150 | /** |
151 | * Updates the text value. | |
152 | */ | |
c392540b FC |
153 | protected abstract void updateValue(); |
154 | ||
b544077e BH |
155 | /** |
156 | * Set the Grid Layout Data for the group. | |
157 | * @param layoutData A GridData to set. | |
158 | */ | |
c392540b FC |
159 | public void setLayoutData(GridData layoutData) { |
160 | fGroup.setLayoutData(layoutData); | |
161 | } | |
162 | ||
b544077e | 163 | /** |
f8177ba2 FC |
164 | * The time value in to set in the text field. |
165 | * | |
b544077e | 166 | * @param time the time to set |
f8177ba2 FC |
167 | * @param displayTime the display value |
168 | * @since 2.0 | |
b544077e | 169 | */ |
f8177ba2 | 170 | protected void setValue(final long time, final String displayTime) { |
c392540b FC |
171 | // If this is the UI thread, process now |
172 | Display display = Display.getCurrent(); | |
173 | if (display != null) { | |
65cdf787 PT |
174 | if (!isDisposed()) { |
175 | fValue = time; | |
176 | fTextValue.setText(displayTime); | |
177 | fParent.getParent().layout(); | |
178 | } | |
c392540b FC |
179 | return; |
180 | } | |
181 | ||
f8177ba2 | 182 | // Call self from the UI thread |
c392540b FC |
183 | if (!isDisposed()) { |
184 | Display.getDefault().asyncExec(new Runnable() { | |
185 | @Override | |
186 | public void run() { | |
187 | if (!isDisposed()) { | |
f8177ba2 | 188 | setValue(time, displayTime); |
c392540b FC |
189 | } |
190 | } | |
191 | }); | |
192 | } | |
193 | } | |
f8177ba2 FC |
194 | |
195 | /** | |
196 | * @param time the time value to display | |
197 | */ | |
198 | public abstract void setValue(long time); | |
199 | ||
b544077e BH |
200 | /** |
201 | * Returns the time value. | |
202 | * @return time value. | |
203 | */ | |
c392540b FC |
204 | public long getValue() { |
205 | return fValue; | |
206 | } | |
207 | ||
65cdf787 PT |
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 | ||
c392540b FC |
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 | } |