Commit | Line | Data |
---|---|---|
c392540b | 1 | /******************************************************************************* |
e0752744 | 2 | * Copyright (c) 2009, 2011, 2012 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 FC |
13 | * Francois Chouinard - Better handling of control display, support for signals |
14 | *******************************************************************************/ | |
c392540b | 15 | |
e0752744 | 16 | package org.eclipse.linuxtools.tmf.ui.views.histogram; |
c392540b | 17 | |
f8177ba2 | 18 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager; |
c392540b FC |
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.graphics.Font; | |
25 | import org.eclipse.swt.graphics.FontData; | |
26 | import org.eclipse.swt.layout.GridData; | |
27 | import org.eclipse.swt.layout.GridLayout; | |
28 | import org.eclipse.swt.widgets.Composite; | |
29 | import org.eclipse.swt.widgets.Display; | |
30 | import org.eclipse.swt.widgets.Group; | |
31 | import org.eclipse.swt.widgets.Text; | |
32 | ||
33 | /** | |
c392540b | 34 | * This control provides a group containing a text control. |
f8177ba2 FC |
35 | * |
36 | * @version 1.1 | |
b544077e | 37 | * @author Francois Chouinard |
c392540b FC |
38 | */ |
39 | public abstract class HistogramTextControl implements FocusListener, KeyListener { | |
40 | ||
41 | // ------------------------------------------------------------------------ | |
42 | // Attributes | |
43 | // ------------------------------------------------------------------------ | |
44 | ||
b544077e BH |
45 | /** |
46 | * The parent histogram view. | |
47 | */ | |
c392540b | 48 | protected final HistogramView fParentView; |
c392540b | 49 | |
f8177ba2 FC |
50 | // Controls data |
51 | private final Composite fParent; | |
52 | private Font fFont; | |
c392540b | 53 | private final Group fGroup; |
f8177ba2 | 54 | |
b544077e BH |
55 | /** |
56 | * The text value field. | |
57 | */ | |
c392540b | 58 | protected final Text fTextValue; |
f8177ba2 | 59 | |
c392540b FC |
60 | private long fValue; |
61 | ||
62 | // ------------------------------------------------------------------------ | |
63 | // Constructors | |
64 | // ------------------------------------------------------------------------ | |
65 | ||
b544077e BH |
66 | /** |
67 | * Constructor with given group and text values. | |
f8177ba2 | 68 | * |
b544077e BH |
69 | * @param parentView The parent histogram view. |
70 | * @param parent The parent composite | |
f8177ba2 FC |
71 | * @param label The text label |
72 | * @param value The initial value | |
73 | * @since 2.0 | |
b544077e | 74 | */ |
f8177ba2 FC |
75 | public HistogramTextControl(HistogramView parentView, Composite parent, String label, long value) |
76 | { | |
c392540b FC |
77 | fParentView = parentView; |
78 | fParent = parent; | |
79 | ||
80 | // -------------------------------------------------------------------- | |
81 | // Reduce font size for a more pleasing rendering | |
82 | // -------------------------------------------------------------------- | |
83 | ||
84 | final int fontSizeAdjustment = -1; | |
85 | final Font font = parent.getFont(); | |
86 | final FontData fontData = font.getFontData()[0]; | |
f8177ba2 | 87 | fFont = new Font(font.getDevice(), fontData.getName(), fontData.getHeight() + fontSizeAdjustment, fontData.getStyle()); |
c392540b FC |
88 | |
89 | // -------------------------------------------------------------------- | |
90 | // Create the group | |
91 | // -------------------------------------------------------------------- | |
92 | ||
93 | // Re-used layout variables | |
94 | GridLayout gridLayout; | |
95 | GridData gridData; | |
96 | ||
97 | // Group control | |
f8177ba2 | 98 | gridLayout = new GridLayout(1, true); |
c392540b FC |
99 | gridLayout.horizontalSpacing = 0; |
100 | gridLayout.verticalSpacing = 0; | |
9a83a5f5 | 101 | fGroup = new Group(fParent, SWT.SHADOW_NONE); |
f8177ba2 FC |
102 | fGroup.setText(label); |
103 | fGroup.setFont(fFont); | |
c392540b FC |
104 | fGroup.setLayout(gridLayout); |
105 | ||
106 | // Group control | |
107 | gridData = new GridData(SWT.LEFT, SWT.CENTER, true, false); | |
108 | gridData.horizontalIndent = 0; | |
109 | gridData.verticalIndent = 0; | |
f8177ba2 FC |
110 | fTextValue = new Text(fGroup, SWT.BORDER); |
111 | fTextValue.setFont(fFont); | |
c392540b FC |
112 | fTextValue.setLayoutData(gridData); |
113 | ||
114 | // -------------------------------------------------------------------- | |
115 | // Add listeners | |
116 | // -------------------------------------------------------------------- | |
117 | ||
118 | fTextValue.addFocusListener(this); | |
119 | fTextValue.addKeyListener(this); | |
f8177ba2 FC |
120 | |
121 | TmfSignalManager.register(this); | |
122 | } | |
123 | ||
124 | /** | |
125 | * Dispose of the widget | |
126 | * @since 2.0 | |
127 | */ | |
128 | public void dispose() { | |
129 | TmfSignalManager.deregister(this); | |
c392540b FC |
130 | } |
131 | ||
132 | // ------------------------------------------------------------------------ | |
133 | // Accessors | |
134 | // ------------------------------------------------------------------------ | |
135 | ||
b544077e BH |
136 | /** |
137 | * Returns if widget isDisposed or not | |
f8177ba2 | 138 | * @return <code>true</code> if widget is disposed else <code>false</code> |
b544077e | 139 | */ |
c392540b FC |
140 | public boolean isDisposed() { |
141 | return fGroup.isDisposed(); | |
142 | } | |
143 | ||
144 | // ------------------------------------------------------------------------ | |
145 | // Operations | |
146 | // ------------------------------------------------------------------------ | |
147 | ||
b544077e BH |
148 | /** |
149 | * Updates the text value. | |
150 | */ | |
c392540b FC |
151 | protected abstract void updateValue(); |
152 | ||
b544077e BH |
153 | /** |
154 | * Set the Grid Layout Data for the group. | |
155 | * @param layoutData A GridData to set. | |
156 | */ | |
c392540b FC |
157 | public void setLayoutData(GridData layoutData) { |
158 | fGroup.setLayoutData(layoutData); | |
159 | } | |
160 | ||
b544077e | 161 | /** |
f8177ba2 FC |
162 | * The time value in to set in the text field. |
163 | * | |
b544077e | 164 | * @param time the time to set |
f8177ba2 FC |
165 | * @param displayTime the display value |
166 | * @since 2.0 | |
b544077e | 167 | */ |
f8177ba2 | 168 | protected void setValue(final long time, final String displayTime) { |
c392540b FC |
169 | // If this is the UI thread, process now |
170 | Display display = Display.getCurrent(); | |
171 | if (display != null) { | |
172 | fValue = time; | |
f8177ba2 FC |
173 | fTextValue.setText(displayTime); |
174 | fParent.getParent().layout(); | |
c392540b FC |
175 | return; |
176 | } | |
177 | ||
f8177ba2 | 178 | // Call self from the UI thread |
c392540b FC |
179 | if (!isDisposed()) { |
180 | Display.getDefault().asyncExec(new Runnable() { | |
181 | @Override | |
182 | public void run() { | |
183 | if (!isDisposed()) { | |
f8177ba2 | 184 | setValue(time, displayTime); |
c392540b FC |
185 | } |
186 | } | |
187 | }); | |
188 | } | |
189 | } | |
f8177ba2 FC |
190 | |
191 | /** | |
192 | * @param time the time value to display | |
193 | */ | |
194 | public abstract void setValue(long time); | |
195 | ||
b544077e BH |
196 | /** |
197 | * Returns the time value. | |
198 | * @return time value. | |
199 | */ | |
c392540b FC |
200 | public long getValue() { |
201 | return fValue; | |
202 | } | |
203 | ||
204 | // ------------------------------------------------------------------------ | |
205 | // FocusListener | |
206 | // ------------------------------------------------------------------------ | |
207 | ||
b544077e BH |
208 | /* |
209 | * (non-Javadoc) | |
210 | * @see org.eclipse.swt.events.FocusListener#focusGained(org.eclipse.swt.events.FocusEvent) | |
211 | */ | |
c392540b FC |
212 | @Override |
213 | public void focusGained(FocusEvent event) { | |
214 | } | |
215 | ||
b544077e BH |
216 | /* |
217 | * (non-Javadoc) | |
218 | * @see org.eclipse.swt.events.FocusListener#focusLost(org.eclipse.swt.events.FocusEvent) | |
219 | */ | |
c392540b FC |
220 | @Override |
221 | public void focusLost(FocusEvent event) { | |
222 | updateValue(); | |
223 | } | |
224 | ||
225 | // ------------------------------------------------------------------------ | |
226 | // KeyListener | |
227 | // ------------------------------------------------------------------------ | |
228 | ||
b544077e BH |
229 | /* |
230 | * (non-Javadoc) | |
231 | * @see org.eclipse.swt.events.KeyListener#keyPressed(org.eclipse.swt.events.KeyEvent) | |
232 | */ | |
c392540b FC |
233 | @Override |
234 | public void keyPressed(KeyEvent event) { | |
235 | switch (event.keyCode) { | |
236 | case SWT.CR: | |
237 | updateValue(); | |
238 | break; | |
239 | default: | |
240 | break; | |
241 | } | |
242 | } | |
243 | ||
b544077e BH |
244 | /* |
245 | * (non-Javadoc) | |
246 | * @see org.eclipse.swt.events.KeyListener#keyReleased(org.eclipse.swt.events.KeyEvent) | |
247 | */ | |
c392540b FC |
248 | @Override |
249 | public void keyReleased(KeyEvent e) { | |
250 | } | |
251 | ||
252 | } |