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