1 /*******************************************************************************
2 * Copyright (c) 2009, 2011 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 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.lttng
.ui
.views
.histogram
;
16 import org
.eclipse
.swt
.SWT
;
17 import org
.eclipse
.swt
.events
.FocusEvent
;
18 import org
.eclipse
.swt
.events
.FocusListener
;
19 import org
.eclipse
.swt
.events
.KeyEvent
;
20 import org
.eclipse
.swt
.events
.KeyListener
;
21 import org
.eclipse
.swt
.graphics
.Font
;
22 import org
.eclipse
.swt
.graphics
.FontData
;
23 import org
.eclipse
.swt
.layout
.GridData
;
24 import org
.eclipse
.swt
.layout
.GridLayout
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
26 import org
.eclipse
.swt
.widgets
.Display
;
27 import org
.eclipse
.swt
.widgets
.Group
;
28 import org
.eclipse
.swt
.widgets
.Text
;
31 * <b><u>HistogramTextControl</u></b>
33 * This control provides a group containing a text control.
35 public abstract class HistogramTextControl
implements FocusListener
, KeyListener
{
37 // ------------------------------------------------------------------------
39 // ------------------------------------------------------------------------
41 protected final HistogramView fParentView
;
42 private final Composite fParent
;
45 private final Group fGroup
;
46 protected final Text fTextValue
;
49 // ------------------------------------------------------------------------
51 // ------------------------------------------------------------------------
53 public HistogramTextControl(HistogramView parentView
, Composite parent
, int textStyle
, int groupStyle
) {
54 this(parentView
, parent
, textStyle
, groupStyle
, "", HistogramUtils
.nanosecondsToString(0L)); //$NON-NLS-1$
57 public HistogramTextControl(HistogramView parentView
, Composite parent
, int textStyle
, int groupStyle
, String groupValue
, String textValue
) {
59 fParentView
= parentView
;
62 // --------------------------------------------------------------------
63 // Reduce font size for a more pleasing rendering
64 // --------------------------------------------------------------------
66 final int fontSizeAdjustment
= -1;
67 final Font font
= parent
.getFont();
68 final FontData fontData
= font
.getFontData()[0];
69 final Font adjustedFont
= new Font(font
.getDevice(), fontData
.getName(), fontData
.getHeight() + fontSizeAdjustment
, fontData
.getStyle());
71 // --------------------------------------------------------------------
72 // Pre-compute the size of the control
73 // --------------------------------------------------------------------
75 final String longestStringValue
= "." + Long
.MAX_VALUE
; //$NON-NLS-1$
76 final int maxChars
= longestStringValue
.length();
77 final int textBoxSize
= HistogramUtils
.getTextSizeInControl(parent
, longestStringValue
);
79 // --------------------------------------------------------------------
81 // --------------------------------------------------------------------
83 // Re-used layout variables
84 GridLayout gridLayout
;
88 gridLayout
= new GridLayout(1, false);
89 gridLayout
.horizontalSpacing
= 0;
90 gridLayout
.verticalSpacing
= 0;
91 fGroup
= new Group(fParent
, groupStyle
);
92 fGroup
.setText(groupValue
);
93 fGroup
.setFont(adjustedFont
);
94 fGroup
.setLayout(gridLayout
);
97 gridData
= new GridData(SWT
.LEFT
, SWT
.CENTER
, true, false);
98 gridData
.horizontalIndent
= 0;
99 gridData
.verticalIndent
= 0;
100 gridData
.minimumWidth
= textBoxSize
;
101 fTextValue
= new Text(fGroup
, textStyle
);
102 fTextValue
.setTextLimit(maxChars
);
103 fTextValue
.setText(textValue
);
104 fTextValue
.setFont(adjustedFont
);
105 fTextValue
.setLayoutData(gridData
);
107 // --------------------------------------------------------------------
109 // --------------------------------------------------------------------
111 fTextValue
.addFocusListener(this);
112 fTextValue
.addKeyListener(this);
115 // ------------------------------------------------------------------------
117 // ------------------------------------------------------------------------
120 public boolean isDisposed() {
121 return fGroup
.isDisposed();
124 // ------------------------------------------------------------------------
126 // ------------------------------------------------------------------------
128 protected abstract void updateValue();
131 public void setLayoutData(GridData layoutData
) {
132 fGroup
.setLayoutData(layoutData
);
136 public void setValue(String timeString
) {
137 long timeValue
= HistogramUtils
.stringToNanoseconds(timeString
);
141 public void setValue(final long time
) {
142 // If this is the UI thread, process now
143 Display display
= Display
.getCurrent();
144 if (display
!= null) {
146 fTextValue
.setText(HistogramUtils
.nanosecondsToString(time
));
150 // Call "recursively" from the UI thread
152 Display
.getDefault().asyncExec(new Runnable() {
163 public long getValue() {
167 // ------------------------------------------------------------------------
169 // ------------------------------------------------------------------------
172 public void focusGained(FocusEvent event
) {
176 public void focusLost(FocusEvent event
) {
180 // ------------------------------------------------------------------------
182 // ------------------------------------------------------------------------
185 public void keyPressed(KeyEvent event
) {
186 switch (event
.keyCode
) {
196 public void keyReleased(KeyEvent e
) {