1 /*******************************************************************************
2 * Copyright (c) 2009, 2011, 2012 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 * Wiliam Bourque - Adapted from SpinnerGroup (in TimeFrameView)
11 * Francois Chouinard - Cleanup and refactoring
12 * Francois Chouinard - Moved from LTTng to TMF
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.tmf
.ui
.views
.histogram
;
17 import org
.eclipse
.swt
.SWT
;
18 import org
.eclipse
.swt
.events
.FocusEvent
;
19 import org
.eclipse
.swt
.events
.FocusListener
;
20 import org
.eclipse
.swt
.events
.KeyEvent
;
21 import org
.eclipse
.swt
.events
.KeyListener
;
22 import org
.eclipse
.swt
.graphics
.Font
;
23 import org
.eclipse
.swt
.graphics
.FontData
;
24 import org
.eclipse
.swt
.layout
.GridData
;
25 import org
.eclipse
.swt
.layout
.GridLayout
;
26 import org
.eclipse
.swt
.widgets
.Composite
;
27 import org
.eclipse
.swt
.widgets
.Display
;
28 import org
.eclipse
.swt
.widgets
.Group
;
29 import org
.eclipse
.swt
.widgets
.Text
;
32 * <b><u>HistogramTextControl</u></b>
34 * This control provides a group containing a text control.
36 public abstract class HistogramTextControl
implements FocusListener
, KeyListener
{
38 // ------------------------------------------------------------------------
40 // ------------------------------------------------------------------------
42 protected final HistogramView fParentView
;
43 private final Composite fParent
;
46 private final Group fGroup
;
47 protected final Text fTextValue
;
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
54 public HistogramTextControl(HistogramView parentView
, Composite parent
, int textStyle
, int groupStyle
) {
55 this(parentView
, parent
, textStyle
, groupStyle
, "", HistogramUtils
.nanosecondsToString(0L)); //$NON-NLS-1$
58 public HistogramTextControl(HistogramView parentView
, Composite parent
, int textStyle
, int groupStyle
, String groupValue
, String textValue
) {
60 fParentView
= parentView
;
63 // --------------------------------------------------------------------
64 // Reduce font size for a more pleasing rendering
65 // --------------------------------------------------------------------
67 final int fontSizeAdjustment
= -1;
68 final Font font
= parent
.getFont();
69 final FontData fontData
= font
.getFontData()[0];
70 final Font adjustedFont
= new Font(font
.getDevice(), fontData
.getName(), fontData
.getHeight() + fontSizeAdjustment
, fontData
.getStyle());
72 // --------------------------------------------------------------------
73 // Pre-compute the size of the control
74 // --------------------------------------------------------------------
76 final String longestStringValue
= "." + Long
.MAX_VALUE
; //$NON-NLS-1$
77 final int maxChars
= longestStringValue
.length();
78 final int textBoxSize
= HistogramUtils
.getTextSizeInControl(parent
, longestStringValue
);
80 // --------------------------------------------------------------------
82 // --------------------------------------------------------------------
84 // Re-used layout variables
85 GridLayout gridLayout
;
89 gridLayout
= new GridLayout(1, false);
90 gridLayout
.horizontalSpacing
= 0;
91 gridLayout
.verticalSpacing
= 0;
92 fGroup
= new Group(fParent
, groupStyle
);
93 fGroup
.setText(groupValue
);
94 fGroup
.setFont(adjustedFont
);
95 fGroup
.setLayout(gridLayout
);
98 gridData
= new GridData(SWT
.LEFT
, SWT
.CENTER
, true, false);
99 gridData
.horizontalIndent
= 0;
100 gridData
.verticalIndent
= 0;
101 gridData
.minimumWidth
= textBoxSize
;
102 fTextValue
= new Text(fGroup
, textStyle
);
103 fTextValue
.setTextLimit(maxChars
);
104 fTextValue
.setText(textValue
);
105 fTextValue
.setFont(adjustedFont
);
106 fTextValue
.setLayoutData(gridData
);
108 // --------------------------------------------------------------------
110 // --------------------------------------------------------------------
112 fTextValue
.addFocusListener(this);
113 fTextValue
.addKeyListener(this);
116 // ------------------------------------------------------------------------
118 // ------------------------------------------------------------------------
121 public boolean isDisposed() {
122 return fGroup
.isDisposed();
125 // ------------------------------------------------------------------------
127 // ------------------------------------------------------------------------
129 protected abstract void updateValue();
132 public void setLayoutData(GridData layoutData
) {
133 fGroup
.setLayoutData(layoutData
);
137 public void setValue(String timeString
) {
138 long timeValue
= HistogramUtils
.stringToNanoseconds(timeString
);
142 public void setValue(final long time
) {
143 // If this is the UI thread, process now
144 Display display
= Display
.getCurrent();
145 if (display
!= null) {
147 fTextValue
.setText(HistogramUtils
.nanosecondsToString(time
));
151 // Call "recursively" from the UI thread
153 Display
.getDefault().asyncExec(new Runnable() {
164 public long getValue() {
168 // ------------------------------------------------------------------------
170 // ------------------------------------------------------------------------
173 public void focusGained(FocusEvent event
) {
177 public void focusLost(FocusEvent event
) {
181 // ------------------------------------------------------------------------
183 // ------------------------------------------------------------------------
186 public void keyPressed(KeyEvent event
) {
187 switch (event
.keyCode
) {
197 public void keyReleased(KeyEvent e
) {