Commit | Line | Data |
---|---|---|
c392540b FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2011 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 | * Wiliam Bourque - Adapted from SpinnerGroup (in TimeFrameView) | |
11 | * Francois Chouinard - Cleanup and refactoring | |
12 | *******************************************************************************/ | |
13 | ||
14 | package org.eclipse.linuxtools.lttng.ui.views.histogram; | |
15 | ||
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; | |
29 | ||
30 | /** | |
31 | * <b><u>HistogramTextControl</u></b> | |
32 | * <p> | |
33 | * This control provides a group containing a text control. | |
34 | */ | |
35 | public abstract class HistogramTextControl implements FocusListener, KeyListener { | |
36 | ||
37 | // ------------------------------------------------------------------------ | |
38 | // Attributes | |
39 | // ------------------------------------------------------------------------ | |
40 | ||
41 | protected final HistogramView fParentView; | |
42 | private final Composite fParent; | |
43 | ||
44 | // Controls | |
45 | private final Group fGroup; | |
46 | protected final Text fTextValue; | |
47 | private long fValue; | |
48 | ||
49 | // ------------------------------------------------------------------------ | |
50 | // Constructors | |
51 | // ------------------------------------------------------------------------ | |
52 | ||
53 | public HistogramTextControl(HistogramView parentView, Composite parent, int textStyle, int groupStyle) { | |
54 | this(parentView, parent, textStyle, groupStyle, "", HistogramUtils.nanosecondsToString(0L)); //$NON-NLS-1$ | |
55 | } | |
56 | ||
57 | public HistogramTextControl(HistogramView parentView, Composite parent, int textStyle, int groupStyle, String groupValue, String textValue) { | |
58 | ||
59 | fParentView = parentView; | |
60 | fParent = parent; | |
61 | ||
62 | // -------------------------------------------------------------------- | |
63 | // Reduce font size for a more pleasing rendering | |
64 | // -------------------------------------------------------------------- | |
65 | ||
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()); | |
70 | ||
71 | // -------------------------------------------------------------------- | |
72 | // Pre-compute the size of the control | |
73 | // -------------------------------------------------------------------- | |
74 | ||
75 | final String longestStringValue = "." + Long.MAX_VALUE; //$NON-NLS-1$ | |
76 | final int maxChars = longestStringValue.length(); | |
77 | final int textBoxSize = HistogramUtils.getTextSizeInControl(parent, longestStringValue); | |
78 | ||
79 | // -------------------------------------------------------------------- | |
80 | // Create the group | |
81 | // -------------------------------------------------------------------- | |
82 | ||
83 | // Re-used layout variables | |
84 | GridLayout gridLayout; | |
85 | GridData gridData; | |
86 | ||
87 | // Group control | |
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); | |
95 | ||
96 | // Group control | |
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); | |
106 | ||
107 | // -------------------------------------------------------------------- | |
108 | // Add listeners | |
109 | // -------------------------------------------------------------------- | |
110 | ||
111 | fTextValue.addFocusListener(this); | |
112 | fTextValue.addKeyListener(this); | |
113 | } | |
114 | ||
115 | // ------------------------------------------------------------------------ | |
116 | // Accessors | |
117 | // ------------------------------------------------------------------------ | |
118 | ||
119 | // State | |
120 | public boolean isDisposed() { | |
121 | return fGroup.isDisposed(); | |
122 | } | |
123 | ||
124 | // ------------------------------------------------------------------------ | |
125 | // Operations | |
126 | // ------------------------------------------------------------------------ | |
127 | ||
128 | protected abstract void updateValue(); | |
129 | ||
130 | // LayoutData | |
131 | public void setLayoutData(GridData layoutData) { | |
132 | fGroup.setLayoutData(layoutData); | |
133 | } | |
134 | ||
135 | // Time value | |
136 | public void setValue(String timeString) { | |
137 | long timeValue = HistogramUtils.stringToNanoseconds(timeString); | |
138 | setValue(timeValue); | |
139 | } | |
140 | ||
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) { | |
145 | fValue = time; | |
146 | fTextValue.setText(HistogramUtils.nanosecondsToString(time)); | |
147 | return; | |
148 | } | |
149 | ||
150 | // Call "recursively" from the UI thread | |
151 | if (!isDisposed()) { | |
152 | Display.getDefault().asyncExec(new Runnable() { | |
153 | @Override | |
154 | public void run() { | |
155 | if (!isDisposed()) { | |
156 | setValue(time); | |
157 | } | |
158 | } | |
159 | }); | |
160 | } | |
161 | } | |
162 | ||
163 | public long getValue() { | |
164 | return fValue; | |
165 | } | |
166 | ||
167 | // ------------------------------------------------------------------------ | |
168 | // FocusListener | |
169 | // ------------------------------------------------------------------------ | |
170 | ||
171 | @Override | |
172 | public void focusGained(FocusEvent event) { | |
173 | } | |
174 | ||
175 | @Override | |
176 | public void focusLost(FocusEvent event) { | |
177 | updateValue(); | |
178 | } | |
179 | ||
180 | // ------------------------------------------------------------------------ | |
181 | // KeyListener | |
182 | // ------------------------------------------------------------------------ | |
183 | ||
184 | @Override | |
185 | public void keyPressed(KeyEvent event) { | |
186 | switch (event.keyCode) { | |
187 | case SWT.CR: | |
188 | updateValue(); | |
189 | break; | |
190 | default: | |
191 | break; | |
192 | } | |
193 | } | |
194 | ||
195 | @Override | |
196 | public void keyReleased(KeyEvent e) { | |
197 | } | |
198 | ||
199 | } |