2010-10-15 Francois Chouinard <fchouinard@gmail.com> Fix for Bug327910
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / histogram / TimeTextGroup.java
1 /*******************************************************************************
2 * Copyright (c) 2009 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 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.lttng.ui.views.histogram;
14
15 import org.eclipse.swt.SWT;
16 import org.eclipse.swt.events.FocusEvent;
17 import org.eclipse.swt.events.FocusListener;
18 import org.eclipse.swt.events.KeyEvent;
19 import org.eclipse.swt.events.KeyListener;
20 import org.eclipse.swt.graphics.Font;
21 import org.eclipse.swt.graphics.FontData;
22 import org.eclipse.swt.layout.GridData;
23 import org.eclipse.swt.layout.GridLayout;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Display;
26 import org.eclipse.swt.widgets.Group;
27 import org.eclipse.swt.widgets.Label;
28 import org.eclipse.swt.widgets.Text;
29
30 /**
31 * <b><u>TimeTextGroup</u></b>
32 * <p>
33 * Special control for HistogramView
34 * <p>
35 * This control will give you a group, a text box and a label at once.
36 */
37 public class TimeTextGroup implements FocusListener, KeyListener {
38
39 /*
40 // 2010-06-10 Yuriy: Has been moved to header into HistogramView.java
41 protected static final String NANOSEC_LABEL = "sec";
42 */
43 private static final String LONGEST_STRING_VALUE = "." + Long.MAX_VALUE;
44 private static final int MAX_CHAR_IN_TEXTBOX = LONGEST_STRING_VALUE.length();
45
46 // The "small font" height used to display time will be "default font" minus this constant
47 private static final int VERY_SMALL_FONT_MODIFIER = 2;
48 private static final int SMALL_FONT_MODIFIER = 1;
49
50 // Indentation size
51 // private static final int DEFAULT_INDENT_SIZE = 10;
52
53 private HistogramView parentView = null;
54 private AsyncTimeTextGroupRedrawer asyncRedrawer = null;
55
56 private Group grpName = null;
57 private Text txtNanosec = null;
58 private Label lblNanosec = null;
59
60 private long timeValue = 0L;
61
62 /**
63 * Default Constructor.<p>
64 *
65 * @param newParentView Parent HistogramView
66 * @param parent Parent Composite, used to position the inner controls.
67 * @param textStyle Style of the textbox. Usually SWT.BORDER or SWT.NONE (or anything that suit a Text)
68 * @param groupStyle Style of the group. Anything that suite a Text
69 */
70 public TimeTextGroup(HistogramView newParentView, Composite parent, int textStyle, int groupStyle) {
71 this(newParentView, parent, textStyle, groupStyle, "", HistogramConstant.formatNanoSecondsTime(0L), false);
72 }
73
74 /**
75 * Default Constructor with adjustement for small screen.<p>
76 *
77 * @param newParentView Parent HistogramView
78 * @param parent Parent Composite, used to position the inner controls.
79 * @param textStyle Style of the textbox. Usually SWT.BORDER or SWT.NONE (or anything that suit a Text)
80 * @param groupStyle Style of the group. Anything that suite a Text
81 * @param isSpaceSaverNeeded Value that tell if we try to save some space in the control.
82 */
83 public TimeTextGroup(HistogramView newParentView, Composite parent, int textStyle, int groupStyle, boolean isSpaceSaverNeeded) {
84 this(newParentView, parent, textStyle, groupStyle, "", HistogramConstant.formatNanoSecondsTime(0L), isSpaceSaverNeeded);
85 }
86
87 /**
88 * Default Constructor, allow you to give the groupname and the textbox value.<p>
89 *
90 * @param newParentView Parent HistogramView
91 * @param parent Parent Composite, used to position the inner controls.
92 * @param textStyle Style of the textbox. Usually SWT.BORDER or SWT.NONE (or anything that suit a Text)
93 * @param groupStyle Style of the group. Anything that suite a Text
94 * @param groupValue Value (label) of the group.
95 * @param textValue Value of the textbox.
96 */
97 public TimeTextGroup(HistogramView newParentView, Composite parent, int textStyle, int groupStyle, String groupValue, String textValue) {
98 this(newParentView, parent, textStyle, groupStyle, groupValue, textValue, false);
99 }
100
101 /**
102 * Default Constructor with adjustment for small screen, allow you to give the group name and the text box value.<p>
103 *
104 * @param newParentView Parent HistogramView
105 * @param parent Parent Composite, used to position the inner controls.
106 * @param textStyle Style of the text box. Usually SWT.BORDER or SWT.NONE (or anything that suit a Text)
107 * @param groupStyle Style of the group. Anything that suite a Text
108 * @param groupValue Value (label) of the group.
109 * @param textValue Value of the text box.
110 * @param isSpaceSaverNeeded Value that tell if we try to save some space in the control.
111 */
112 public TimeTextGroup(HistogramView newParentView, Composite parent, int textStyle, int groupStyle, String groupValue, String textValue, boolean isSpaceSaverNeeded) {
113 Font font = parent.getFont();
114 FontData tmpFontData = font.getFontData()[0];
115
116 Font smallFont = null;
117 int textBoxSize = -1;
118 // int indentSize = -1;
119
120 // If we were asked to save size, calculate the correct value here
121 if ( isSpaceSaverNeeded == true ) {
122 smallFont = new Font(font.getDevice(), tmpFontData.getName(), tmpFontData.getHeight()-VERY_SMALL_FONT_MODIFIER, tmpFontData.getStyle());
123
124 // No minimum textBoxSize and no indent size
125 textBoxSize = 0;
126 // indentSize = 0;
127 }
128 else {
129 // We use only a slightly smaller font
130 smallFont = new Font(font.getDevice(), tmpFontData.getName(), tmpFontData.getHeight()-SMALL_FONT_MODIFIER, tmpFontData.getStyle());
131
132 // ** Creation of the textbox
133 // Calculate the optimal size of the textbox already
134 // This will avoid the control to move around and resize when bigger value are given
135 textBoxSize = HistogramConstant.getTextSizeInControl(parent, LONGEST_STRING_VALUE);
136
137 // Default indent
138 // indentSize = DEFAULT_INDENT_SIZE;
139 }
140
141 parentView = newParentView;
142
143 // ** Creation of the group
144 // GridLayout gridLayoutgroup = new GridLayout(2, false);
145 GridLayout gridLayoutgroup = new GridLayout(1, false);
146 gridLayoutgroup.horizontalSpacing = 0;
147 gridLayoutgroup.verticalSpacing = 0;
148 grpName = new Group(parent, groupStyle);
149 grpName.setText(groupValue);
150 grpName.setFont(smallFont);
151 grpName.setLayout(gridLayoutgroup);
152
153 txtNanosec = new Text(grpName, textStyle);
154 txtNanosec.setTextLimit( MAX_CHAR_IN_TEXTBOX );
155 txtNanosec.setText(textValue);
156 txtNanosec.setFont(smallFont);
157 GridData gridDataTextBox = new GridData(SWT.LEFT, SWT.CENTER, true, false);
158 gridDataTextBox.horizontalIndent = 0; // indentSize;
159 gridDataTextBox.verticalIndent = 0;
160 gridDataTextBox.minimumWidth = textBoxSize;
161 txtNanosec.setLayoutData(gridDataTextBox);
162
163 // ** Creation of the label
164 /*
165 lblNanosec = new Label(grpName, SWT.LEFT);
166 lblNanosec.setText(NANOSEC_LABEL);
167 lblNanosec.setFont(smallFont);
168 GridData gridDataLabel = new GridData(SWT.LEFT, SWT.CENTER, false, false);
169 gridDataLabel.horizontalIndent = indentSize;
170 gridDataLabel.verticalIndent = 0;
171 lblNanosec.setLayoutData(gridDataLabel);
172 */
173
174 // Add all listener
175 addNeededListeners();
176 }
177
178 /*
179 * Create and add all listeners needed by our control.<p>
180 */
181 protected void addNeededListeners() {
182
183 // AsyncCanvasRedrawer is an internal class
184 // This is used to redraw the canvas without danger from a different thread
185 asyncRedrawer = new AsyncTimeTextGroupRedrawer(this);
186
187 txtNanosec.addFocusListener(this);
188 txtNanosec.addKeyListener(this);
189 }
190
191 /**
192 * Getter for the layout data currently in use.<p>
193 *
194 * @return the layout
195 */
196 public Object getLayoutData() {
197 return grpName.getLayoutData();
198 }
199
200 /**
201 * Set a new layoutData for our control.<p>
202 *
203 * @param layoutData the new layout data
204 */
205 public void setLayoutData(Object layoutData) {
206 grpName.setLayoutData(layoutData);
207 }
208
209 /**
210 * Get the control's parent.<p>
211 *
212 * @return Currently used parent
213 */
214 public Composite getParent() {
215 return grpName.getParent();
216 }
217
218 /**
219 * Set a new parent for the control.<p>
220 *
221 * @return Currently used parent
222 */
223 public void setParent(Composite newParent) {
224 grpName.setParent(newParent);
225 txtNanosec.setParent(newParent);
226 lblNanosec.setParent(newParent);
227 }
228
229 /**
230 * Getter for the time value of the control.<p>
231 *
232 * @return The nanoseconds time value
233 */
234 public long getValue() {
235 return timeValue;
236 }
237
238 /**
239 * Set a new String value to the control.<p>
240 * Note : The String value will be converted in long before being applied;
241 * if any conversion error occur, 0 will be used. <p>
242 *
243 * @param newTimeAsString The value to convert and set.
244 */
245 public void setValue(String newTimeAsString) {
246 long timeAsLong = HistogramConstant.convertStringToNanoseconds(newTimeAsString);
247 setValue( timeAsLong );
248 }
249
250 /**
251 * Set a new value to the control.<p>
252 * Note : The value will be formatted as nanosecond value,
253 * missing zero will be added if needed.<p>
254 *
255 * @param newTime The value to set.
256 */
257 public void setValue(long newTime) {
258 timeValue = newTime;
259 txtNanosec.setText( HistogramConstant.formatNanoSecondsTime(newTime) );
260 }
261
262 /**
263 * Set a new String value, asynchronously.<p>
264 * This will call setValue(String) in async.Exec to avoid Thread Access problem to UI.<p>
265 *
266 * @param newTimeAsString The value to convert and set.
267 */
268 public void setValueAsynchronously(String newTimeAsString) {
269 long timeAsLong = HistogramConstant.convertStringToNanoseconds(newTimeAsString);
270 setValueAsynchronously( timeAsLong );
271 }
272
273 /**
274 * Set a new String value, asynchronously.<p>
275 * This will call setValue(long) in async.Exec to avoid Thread Access problem to UI.<p>
276 *
277 * @param newTimeAsString The value to set.
278 */
279 public void setValueAsynchronously(long newTime) {
280 // Set the correct value ASAP
281 timeValue = newTime;
282
283 // Create a new redrawer in case it doesn't exist yet (we never know with thread!)
284 if ( asyncRedrawer == null ) {
285 asyncRedrawer = new AsyncTimeTextGroupRedrawer(this);
286 }
287
288 asyncRedrawer.asynchronousSetValue(newTime);
289 }
290
291 /**
292 * Set a new group name (label) for this control.<p>
293 *
294 * @param newName The new name to set.
295 */
296 public void setGroupName(String newName) {
297 grpName.setText(newName);
298 }
299
300 /**
301 * Set a new group name (label) for this control, asynchronously.<p>
302 * This will call setValue(long) in async.Exec to avoid Thread Access problem to UI.<p>
303 *
304 * @param newName The new name to set.
305 */
306 public void setGroupNameAsynchronously(String newGroupName) {
307 // Create a new redrawer in case it doesn't exist yet (we never know with thread!)
308 if ( asyncRedrawer == null ) {
309 asyncRedrawer = new AsyncTimeTextGroupRedrawer(this);
310 }
311
312 asyncRedrawer.asynchronousSetGroupName(newGroupName);
313 }
314
315
316 /**
317 * Method to call the "Asynchronous redrawer" for this time text group<p>
318 * This allow safe redraw from different threads.
319 */
320 public void redrawAsynchronously() {
321 // Create a new redrawer in case it doesn't exist yet (we never know with thread!)
322 if ( asyncRedrawer == null ) {
323 asyncRedrawer = new AsyncTimeTextGroupRedrawer(this);
324 }
325
326 asyncRedrawer.asynchronousRedraw();
327 }
328
329 /**
330 * Redraw the control
331 */
332 public void redraw () {
333 grpName.redraw();
334 txtNanosec.redraw();
335 lblNanosec.redraw();
336 }
337
338 /*
339 * This function is called when an user enter a new string in the control by hand.<p>
340 * It will ensure the format of the String is valid.
341 */
342 protected void handleNewStringValue() {
343 String valueInText = txtNanosec.getText();
344 long valueAsLong = HistogramConstant.convertStringToNanoseconds(valueInText);
345
346 if ( getValue() != valueAsLong ) {
347 setValue(valueAsLong);
348 // Notify our parent that the control was updated
349 notifyParentUpdatedTextGroupValue();
350 }
351 }
352
353 /**
354 * This function notify our parent HistogramView that our value changed.
355 */
356 public void notifyParentUpdatedTextGroupValue() {
357 parentView.timeTextGroupChangeNotification();
358 }
359
360 /**
361 * Function that is called when the canvas get focus.<p>
362 *
363 * Doesn't do anything yet...
364 *
365 * @param event The focus event generated.
366 */
367 public void focusGained(FocusEvent event) {
368 // Nothing to do yet
369 }
370
371 /**
372 * Function that is called when the canvas loose focus.<p>
373 * It will validate that the String entered by the user (if any) is valid.<p>
374 *
375 * @param event The focus event generated.
376 */
377 public void focusLost(FocusEvent event) {
378 handleNewStringValue();
379 }
380
381 /**
382 * Function that is called when a key is pressed.<p>
383 * Possible actions :
384 * - Enter (CR) : Validate the entered String.<p>
385 *
386 * @param event The KeyEvent generated when the key was pressed.
387 */
388 public void keyPressed(KeyEvent event) {
389 switch (event.keyCode) {
390 // SWT.CR is "ENTER" Key
391 case SWT.CR:
392 handleNewStringValue();
393 break;
394 default:
395 break;
396 }
397 }
398
399 /**
400 * Function that is called when a key is released.<p>
401 * Possible actions :
402 * Nothing yet
403 *
404 * @param event The KeyEvent generated when the key was pressed.
405 */
406 public void keyReleased(KeyEvent e) {
407
408 }
409 }
410
411 /**
412 * <b><u>AsyncTimeTextGroupRedrawer Inner Class</u></b>
413 * <p>
414 * Asynchronous redrawer for the TimeTextGroup
415 * <p>
416 * This class role is to call method that update the UI on asynchronously.
417 * This should prevent any "invalid thread access" exception when trying to update UI from a different thread.
418 */
419 class AsyncTimeTextGroupRedrawer {
420
421 private TimeTextGroup parentTimeTextGroup = null;
422
423 /**
424 * AsyncTimeTextGroupRedrawer constructor.
425 *
426 * @param newParent Related time text group.
427 */
428 public AsyncTimeTextGroupRedrawer(TimeTextGroup newParent) {
429 parentTimeTextGroup = newParent;
430 }
431
432 /**
433 * Asynchronous SetValue for time text group.
434 *
435 * Basically, it just run "getParent().setValue(time)" in asyncExec.
436 *
437 * @param newTime The new time to set
438 */
439 public void asynchronousSetValue(final long newTime) {
440 // Ignore setting of value if widget is disposed
441 if (parentTimeTextGroup.getParent().isDisposed()) return;
442
443 Display display = parentTimeTextGroup.getParent().getDisplay();
444 display.asyncExec(new Runnable() {
445 public void run() {
446 if (!parentTimeTextGroup.getParent().isDisposed()) {
447 parentTimeTextGroup.setValue(newTime);
448 }
449 }
450 });
451 }
452
453 /**
454 * Asynchronous SetGroupName for time text group.
455 *
456 * Basically, it just run "getParent().setGroupName(name)" in asyncExec.
457 *
458 * @param newGroupName The new group name to set
459 */
460 public void asynchronousSetGroupName(String newGroupName) {
461 // Ignore setting of name if widget is disposed
462 if (parentTimeTextGroup.getParent().isDisposed()) return;
463
464 final String tmpName = newGroupName;
465 Display display = parentTimeTextGroup.getParent().getDisplay();
466 display.asyncExec(new Runnable() {
467 public void run() {
468 if (!parentTimeTextGroup.getParent().isDisposed()) {
469 parentTimeTextGroup.setGroupName(tmpName);
470 }
471 }
472 });
473 }
474
475 /**
476 * Function to redraw the related time text group asynchonously.<p>
477 *
478 * Basically, it just run "getParent().redraw()" in asyncExec.
479 *
480 */
481 public void asynchronousRedraw() {
482 // Ignore redraw if widget is disposed
483 if (parentTimeTextGroup.getParent().isDisposed()) return;
484
485 Display display = parentTimeTextGroup.getParent().getDisplay();
486 display.asyncExec(new Runnable() {
487 public void run() {
488 if (!parentTimeTextGroup.getParent().isDisposed()) {
489 parentTimeTextGroup.getParent().redraw();
490 }
491 }
492 });
493 }
494 }
This page took 0.042573 seconds and 5 git commands to generate.