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