1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.lttng
.ui
.views
.histogram
;
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
;
31 * <b><u>TimeTextGroup</u></b>
33 * Special control for HistogramView
35 * This control will give you a group, a text box and a label at once.
37 public class TimeTextGroup
implements FocusListener
, KeyListener
{
40 // 2010-06-10 Yuriy: Has been moved to header into HistogramView.java
41 protected static final String NANOSEC_LABEL = "sec";
43 private static final String LONGEST_STRING_VALUE
= "." + Long
.MAX_VALUE
;
44 private static final int MAX_CHAR_IN_TEXTBOX
= LONGEST_STRING_VALUE
.length();
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;
51 private static final int DEFAULT_INDENT_SIZE
= 10;
53 private HistogramView parentView
= null;
54 private AsyncTimeTextGroupRedrawer asyncRedrawer
= null;
56 private Group grpName
= null;
57 private Text txtNanosec
= null;
58 private Label lblNanosec
= null;
60 private long timeValue
= 0L;
63 * Default Constructor.<p>
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
70 public TimeTextGroup(HistogramView newParentView
, Composite parent
, int textStyle
, int groupStyle
) {
71 this(newParentView
, parent
, textStyle
, groupStyle
, "", HistogramConstant
.formatNanoSecondsTime(0L), false);
75 * Default Constructor with adjustement for small screen.<p>
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.
83 public TimeTextGroup(HistogramView newParentView
, Composite parent
, int textStyle
, int groupStyle
, boolean isSpaceSaverNeeded
) {
84 this(newParentView
, parent
, textStyle
, groupStyle
, "", HistogramConstant
.formatNanoSecondsTime(0L), isSpaceSaverNeeded
);
88 * Default Constructor, allow you to give the groupname and the textbox value.<p>
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.
97 public TimeTextGroup(HistogramView newParentView
, Composite parent
, int textStyle
, int groupStyle
, String groupValue
, String textValue
) {
98 this(newParentView
, parent
, textStyle
, groupStyle
, groupValue
, textValue
, false);
102 * Default Constructor with adjustement for small screen, allow you to give the groupname and the textbox value.<p>
104 * @param newParentView Parent HistogramView
105 * @param parent Parent Composite, used to position the inner controls.
106 * @param textStyle Style of the textbox. 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 textbox.
110 * @param isSpaceSaverNeeded Value that tell if we try to save some space in the control.
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];
116 Font smallFont
= null;
117 int textBoxSize
= -1;
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());
124 // No minimum textBoxSize and no indent size
129 // We use only a slightly smaller font
130 smallFont
= new Font(font
.getDevice(), tmpFontData
.getName(), tmpFontData
.getHeight()-SMALL_FONT_MODIFIER
, tmpFontData
.getStyle());
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
);
138 indentSize
= DEFAULT_INDENT_SIZE
;
141 parentView
= newParentView
;
143 // ** Creation of the group
144 GridLayout gridLayoutgroup
= new GridLayout(2, false);
145 gridLayoutgroup
.horizontalSpacing
= 0;
146 gridLayoutgroup
.verticalSpacing
= 0;
147 grpName
= new Group(parent
, groupStyle
);
148 grpName
.setText(groupValue
);
149 grpName
.setFont(smallFont
);
150 grpName
.setLayout(gridLayoutgroup
);
152 txtNanosec
= new Text(grpName
, textStyle
);
153 txtNanosec
.setTextLimit( MAX_CHAR_IN_TEXTBOX
);
154 txtNanosec
.setText( textValue
);
155 txtNanosec
.setFont(smallFont
);
156 GridData gridDataTextBox
= new GridData(SWT
.LEFT
, SWT
.CENTER
, true, false);
157 gridDataTextBox
.horizontalIndent
= indentSize
;
158 gridDataTextBox
.verticalIndent
= 0;
159 gridDataTextBox
.minimumWidth
= textBoxSize
;
160 txtNanosec
.setLayoutData(gridDataTextBox
);
162 // ** Creation of the label
164 lblNanosec = new Label(grpName, SWT.LEFT);
165 lblNanosec.setText(NANOSEC_LABEL);
166 lblNanosec.setFont(smallFont);
167 GridData gridDataLabel = new GridData(SWT.LEFT, SWT.CENTER, false, false);
168 gridDataLabel.horizontalIndent = indentSize;
169 gridDataLabel.verticalIndent = 0;
170 lblNanosec.setLayoutData(gridDataLabel);
174 addNeededListeners();
178 * Create and add all listeners needed by our control.<p>
180 protected void addNeededListeners() {
182 // AsyncCanvasRedrawer is an internal class
183 // This is used to redraw the canvas without danger from a different thread
184 asyncRedrawer
= new AsyncTimeTextGroupRedrawer(this);
186 txtNanosec
.addFocusListener(this);
187 txtNanosec
.addKeyListener(this);
191 * Getter for the layout data currently in use.<p>
195 public Object
getLayoutData() {
196 return grpName
.getLayoutData();
200 * Set a new layoutData for our control.<p>
202 * @param layoutData the new layout data
204 public void setLayoutData(Object layoutData
) {
205 grpName
.setLayoutData(layoutData
);
209 * Get the control's parent.<p>
211 * @return Currently used parent
213 public Composite
getParent() {
214 return grpName
.getParent();
218 * Set a new parent for the control.<p>
220 * @return Currently used parent
222 public void setParent(Composite newParent
) {
223 grpName
.setParent(newParent
);
224 txtNanosec
.setParent(newParent
);
225 lblNanosec
.setParent(newParent
);
229 * Getter for the time value of the control.<p>
231 * @return The nanoseconds time value
233 public long getValue() {
238 * Set a new String value to the control.<p>
239 * Note : The String value will be converted in long before being applied;
240 * if any conversion error occur, 0 will be used. <p>
242 * @param newTimeAsString The value to convert and set.
244 public void setValue(String newTimeAsString
) {
245 long timeAsLong
= HistogramConstant
.convertStringToNanoseconds(newTimeAsString
);
246 setValue( timeAsLong
);
250 * Set a new value to the control.<p>
251 * Note : The value will be formatted as nanosecond value,
252 * missing zero will be added if needed.<p>
254 * @param newTime The value to set.
256 public void setValue(long newTime
) {
258 txtNanosec
.setText( HistogramConstant
.formatNanoSecondsTime(newTime
) );
262 * Set a new String value, asynchronously.<p>
263 * This will call setValue(String) in async.Exec to avoid Thread Access problem to UI.<p>
265 * @param newTimeAsString The value to convert and set.
267 public void setValueAsynchronously(String newTimeAsString
) {
268 long timeAsLong
= HistogramConstant
.convertStringToNanoseconds(newTimeAsString
);
269 setValueAsynchronously( timeAsLong
);
273 * Set a new String value, asynchronously.<p>
274 * This will call setValue(long) in async.Exec to avoid Thread Access problem to UI.<p>
276 * @param newTimeAsString The value to set.
278 public void setValueAsynchronously(long newTime
) {
279 // Set the correct value ASAP
282 // Create a new redrawer in case it doesn't exist yet (we never know with thread!)
283 if ( asyncRedrawer
== null ) {
284 asyncRedrawer
= new AsyncTimeTextGroupRedrawer(this);
287 asyncRedrawer
.asynchronousSetValue(newTime
);
291 * Set a new group name (label) for this control.<p>
293 * @param newName The new name to set.
295 public void setGroupName(String newName
) {
296 grpName
.setText(newName
);
300 * Set a new group name (label) for this control, asynchronously.<p>
301 * This will call setValue(long) in async.Exec to avoid Thread Access problem to UI.<p>
303 * @param newName The new name to set.
305 public void setGroupNameAsynchronously(String newGroupName
) {
306 // Create a new redrawer in case it doesn't exist yet (we never know with thread!)
307 if ( asyncRedrawer
== null ) {
308 asyncRedrawer
= new AsyncTimeTextGroupRedrawer(this);
311 asyncRedrawer
.asynchronousSetGroupName(newGroupName
);
316 * Method to call the "Asynchronous redrawer" for this time text group<p>
317 * This allow safe redraw from different threads.
319 public void redrawAsynchronously() {
320 // Create a new redrawer in case it doesn't exist yet (we never know with thread!)
321 if ( asyncRedrawer
== null ) {
322 asyncRedrawer
= new AsyncTimeTextGroupRedrawer(this);
325 asyncRedrawer
.asynchronousRedraw();
331 public void redraw () {
338 * This function is called when an user enter a new string in the control by hand.<p>
339 * It will ensure the format of the String is valid.
341 protected void handleNewStringValue() {
342 String valueInText
= txtNanosec
.getText();
343 long valueAsLong
= HistogramConstant
.convertStringToNanoseconds(valueInText
);
345 if ( getValue() != valueAsLong
) {
346 setValue(valueAsLong
);
347 // Notify our parent that the control was updated
348 notifyParentUpdatedTextGroupValue();
353 * This function notify our parent HistogramView that our value changed.
355 public void notifyParentUpdatedTextGroupValue() {
356 parentView
.timeTextGroupChangeNotification();
360 * Function that is called when the canvas get focus.<p>
362 * Doesn't do anything yet...
364 * @param event The focus event generated.
366 public void focusGained(FocusEvent event
) {
371 * Function that is called when the canvas loose focus.<p>
372 * It will validate that the String entered by the user (if any) is valid.<p>
374 * @param event The focus event generated.
376 public void focusLost(FocusEvent event
) {
377 handleNewStringValue();
381 * Function that is called when a key is pressed.<p>
383 * - Enter (CR) : Validate the entered String.<p>
385 * @param event The KeyEvent generated when the key was pressed.
387 public void keyPressed(KeyEvent event
) {
388 switch (event
.keyCode
) {
389 // SWT.CR is "ENTER" Key
391 handleNewStringValue();
399 * Function that is called when a key is released.<p>
403 * @param event The KeyEvent generated when the key was pressed.
405 public void keyReleased(KeyEvent e
) {
411 * <b><u>AsyncTimeTextGroupRedrawer Inner Class</u></b>
413 * Asynchronous redrawer for the TimeTextGroup
415 * This class role is to call method that update the UI on asynchronously.
416 * This should prevent any "invalid thread access" exception when trying to update UI from a different thread.
418 class AsyncTimeTextGroupRedrawer
{
420 private TimeTextGroup parentTimeTextGroup
= null;
423 * AsyncTimeTextGroupRedrawer constructor.
425 * @param newParent Related time text group.
427 public AsyncTimeTextGroupRedrawer(TimeTextGroup newParent
) {
428 parentTimeTextGroup
= newParent
;
432 * Asynchronous SetValue for time text group.
434 * Basically, it just run "getParent().setValue(time)" in asyncExec.
436 * @param newTime The new time to set
438 public void asynchronousSetValue(long newTime
) {
439 // Ignore setting of value if widget is disposed
440 if (parentTimeTextGroup
.getParent().isDisposed()) return;
442 final long tmpTime
= newTime
;
444 Display display
= parentTimeTextGroup
.getParent().getDisplay();
445 display
.asyncExec(new Runnable() {
447 if (!parentTimeTextGroup
.getParent().isDisposed()) {
448 parentTimeTextGroup
.setValue(tmpTime
);
455 * Asynchronous SetGroupName for time text group.
457 * Basically, it just run "getParent().setGroupName(name)" in asyncExec.
459 * @param newGroupName The new group name to set
461 public void asynchronousSetGroupName(String newGroupName
) {
462 // Ignore setting of name if widget is disposed
463 if (parentTimeTextGroup
.getParent().isDisposed()) return;
465 final String tmpName
= newGroupName
;
466 Display display
= parentTimeTextGroup
.getParent().getDisplay();
467 display
.asyncExec(new Runnable() {
469 if (!parentTimeTextGroup
.getParent().isDisposed()) {
470 parentTimeTextGroup
.setGroupName(tmpName
);
477 * Function to redraw the related time text group asynchonously.<p>
479 * Basically, it just run "getParent().redraw()" in asyncExec.
482 public void asynchronousRedraw() {
483 // Ignore redraw if widget is disposed
484 if (parentTimeTextGroup
.getParent().isDisposed()) return;
486 Display display
= parentTimeTextGroup
.getParent().getDisplay();
487 display
.asyncExec(new Runnable() {
489 if (!parentTimeTextGroup
.getParent().isDisposed()) {
490 parentTimeTextGroup
.getParent().redraw();