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