|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2005 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
| 10 |
******************************************************************************/ |
| 11 |
package org.eclipse.jface.dialogs.taskassistance; |
| 12 |
|
| 13 |
import org.eclipse.core.runtime.Assert; |
| 14 |
import org.eclipse.jface.dialogs.Dialog; |
| 15 |
import org.eclipse.jface.dialogs.IDialogConstants; |
| 16 |
import org.eclipse.swt.SWT; |
| 17 |
import org.eclipse.swt.events.DisposeEvent; |
| 18 |
import org.eclipse.swt.events.DisposeListener; |
| 19 |
import org.eclipse.swt.events.FocusEvent; |
| 20 |
import org.eclipse.swt.events.FocusListener; |
| 21 |
import org.eclipse.swt.events.MouseAdapter; |
| 22 |
import org.eclipse.swt.events.MouseEvent; |
| 23 |
import org.eclipse.swt.events.MouseTrackListener; |
| 24 |
import org.eclipse.swt.events.PaintEvent; |
| 25 |
import org.eclipse.swt.events.PaintListener; |
| 26 |
import org.eclipse.swt.graphics.FontMetrics; |
| 27 |
import org.eclipse.swt.graphics.GC; |
| 28 |
import org.eclipse.swt.graphics.Point; |
| 29 |
import org.eclipse.swt.graphics.Region; |
| 30 |
import org.eclipse.swt.layout.FormAttachment; |
| 31 |
import org.eclipse.swt.layout.FormData; |
| 32 |
import org.eclipse.swt.layout.FormLayout; |
| 33 |
import org.eclipse.swt.layout.GridData; |
| 34 |
import org.eclipse.swt.widgets.Composite; |
| 35 |
import org.eclipse.swt.widgets.Control; |
| 36 |
import org.eclipse.swt.widgets.Display; |
| 37 |
import org.eclipse.swt.widgets.Label; |
| 38 |
import org.eclipse.swt.widgets.Shell; |
| 39 |
|
| 40 |
/** |
| 41 |
* A decorated field manages image decorations around a control. It allows |
| 42 |
* clients to specify an image decoration and a position for the decoration |
| 43 |
* relative to the field. Decorations may be assigned descriptions, which are |
| 44 |
* shown when the user hovers over the decoration. Clients can decorate any kind |
| 45 |
* of control by supplying an <code>IControlCreator</code> to create the |
| 46 |
* control that is decorated. |
| 47 |
* |
| 48 |
* <p> |
| 49 |
* This API is considered experimental. It is still evolving during 3.2 and is |
| 50 |
* subject to change. It is being released to obtain feedback from early |
| 51 |
* adopters. |
| 52 |
* |
| 53 |
* @since 3.2 |
| 54 |
*/ |
| 55 |
public class DecoratedField { |
| 56 |
|
| 57 |
/** |
| 58 |
* Number of pixels to reserve for decorations. |
| 59 |
*/ |
| 60 |
private static int RESERVED_WIDTH = 8; |
| 61 |
|
| 62 |
/** |
| 63 |
* Cached platform flags for dealing with platform-specific issues. |
| 64 |
*/ |
| 65 |
private static boolean CARBON = "carbon".equals(SWT.getPlatform()); //$NON-NLS-1$ |
| 66 |
|
| 67 |
/** |
| 68 |
* Constants describing the array indices used to hold the decorations in |
| 69 |
* array slots. |
| 70 |
*/ |
| 71 |
|
| 72 |
private static final int LEFT_TOP = 0; |
| 73 |
|
| 74 |
private static final int LEFT_BOTTOM = 1; |
| 75 |
|
| 76 |
private static final int RIGHT_TOP = 2; |
| 77 |
|
| 78 |
private static final int RIGHT_BOTTOM = 3; |
| 79 |
|
| 80 |
private static final int DECORATION_SLOTS = 4; |
| 81 |
|
| 82 |
/** |
| 83 |
* Set the width (in pixels) that should always be reserved for field |
| 84 |
* decorations, regardless of the actual width of any supplied decorations. |
| 85 |
* Field alignment within dialogs will look best when all decorations |
| 86 |
* supplied conform to the reserved width. However, the field decoration |
| 87 |
* area for a particular field will be expanded if decorations larger than |
| 88 |
* the reserved width are supplied. |
| 89 |
* |
| 90 |
* @param decorationWidth |
| 91 |
*/ |
| 92 |
public static void setReservedDecorationWidth(int decorationWidth) { |
| 93 |
RESERVED_WIDTH = decorationWidth; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Simple data structure class for specifying the internals for a field |
| 98 |
* decoration. This class contains data specific to the implementation of |
| 99 |
* field decorations as labels attached to the field. Clients should use |
| 100 |
* <code>FieldDecoration</code> for specifying a decoration. |
| 101 |
*/ |
| 102 |
private class FieldDecorationData { |
| 103 |
|
| 104 |
/* Package */FieldDecoration decoration; |
| 105 |
|
| 106 |
/* Package */Label label; |
| 107 |
|
| 108 |
/* Package */FormData data; |
| 109 |
|
| 110 |
/* Package */boolean showOnFocus; |
| 111 |
|
| 112 |
/* Package */boolean visible = true; |
| 113 |
|
| 114 |
/** |
| 115 |
* Create a decoration data representing the specified decoration, using |
| 116 |
* the specified label and form data for its representation. |
| 117 |
* |
| 118 |
* @param decoration |
| 119 |
* the decoration whose data is kept. |
| 120 |
* @param label |
| 121 |
* the label used to represent the decoration. |
| 122 |
* @param formData |
| 123 |
* the form data used to attach the decoration to its field. |
| 124 |
* @param showOnFocus |
| 125 |
* a boolean specifying whether the decoration should only be |
| 126 |
* shown when the field has focus. |
| 127 |
*/ |
| 128 |
FieldDecorationData(FieldDecoration decoration, Label label, |
| 129 |
FormData formData, boolean showOnFocus) { |
| 130 |
this.decoration = decoration; |
| 131 |
this.label = label; |
| 132 |
this.data = formData; |
| 133 |
this.showOnFocus = showOnFocus; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Decorations keyed by position. |
| 139 |
*/ |
| 140 |
private FieldDecorationData[] decDatas = new FieldDecorationData[DECORATION_SLOTS]; |
| 141 |
|
| 142 |
/** |
| 143 |
* The associated control |
| 144 |
*/ |
| 145 |
private Control control; |
| 146 |
|
| 147 |
/** |
| 148 |
* The composite with form layout used to manage decorations. |
| 149 |
*/ |
| 150 |
private Composite form; |
| 151 |
|
| 152 |
/** |
| 153 |
* The hover used for showing description text |
| 154 |
*/ |
| 155 |
private Hover hover; |
| 156 |
|
| 157 |
/** |
| 158 |
* The hover used to show a decoration image's description. |
| 159 |
*/ |
| 160 |
class Hover { |
| 161 |
/** |
| 162 |
* Offset of info hover arrow from the left or right side. |
| 163 |
*/ |
| 164 |
private int a_OFFSET = 10; |
| 165 |
|
| 166 |
/** |
| 167 |
* Width of info hover arrow. |
| 168 |
*/ |
| 169 |
private int a_WIDTH = 8; |
| 170 |
|
| 171 |
/** |
| 172 |
* Height of info hover arrow. |
| 173 |
*/ |
| 174 |
private int a_HEIGHT = 10; |
| 175 |
|
| 176 |
/** |
| 177 |
* Margin around info hover text. |
| 178 |
*/ |
| 179 |
private int h_MARGIN = 2; |
| 180 |
|
| 181 |
/** |
| 182 |
* This info hover's shell. |
| 183 |
*/ |
| 184 |
Shell hoverShell; |
| 185 |
|
| 186 |
/** |
| 187 |
* The info hover text. |
| 188 |
*/ |
| 189 |
String text = ""; //$NON-NLS-1$ |
| 190 |
|
| 191 |
Hover(Shell parent) { |
| 192 |
final Display display = parent.getDisplay(); |
| 193 |
hoverShell = new Shell(parent, SWT.NO_TRIM | SWT.ON_TOP |
| 194 |
| SWT.NO_FOCUS); |
| 195 |
hoverShell.setBackground(display |
| 196 |
.getSystemColor(SWT.COLOR_INFO_BACKGROUND)); |
| 197 |
hoverShell.setForeground(display |
| 198 |
.getSystemColor(SWT.COLOR_INFO_FOREGROUND)); |
| 199 |
hoverShell.addPaintListener(new PaintListener() { |
| 200 |
public void paintControl(PaintEvent pe) { |
| 201 |
pe.gc.drawString(text, h_MARGIN, h_MARGIN); |
| 202 |
if (!CARBON) |
| 203 |
pe.gc.drawPolygon(getPolygon(true)); |
| 204 |
} |
| 205 |
}); |
| 206 |
hoverShell.addMouseListener(new MouseAdapter() { |
| 207 |
public void mouseDown(MouseEvent e) { |
| 208 |
hideHover(); |
| 209 |
} |
| 210 |
}); |
| 211 |
} |
| 212 |
|
| 213 |
int[] getPolygon(boolean border) { |
| 214 |
Point e = getExtent(); |
| 215 |
if (border) |
| 216 |
return new int[] { 0, 0, e.x - 1, 0, e.x - 1, e.y - 1, |
| 217 |
a_OFFSET + a_WIDTH, e.y - 1, a_OFFSET + a_WIDTH / 2, |
| 218 |
e.y + a_HEIGHT - 1, a_OFFSET, e.y - 1, 0, e.y - 1, 0, 0 }; |
| 219 |
return new int[] { 0, 0, e.x, 0, e.x, e.y, a_OFFSET + a_WIDTH, e.y, |
| 220 |
a_OFFSET + a_WIDTH / 2, e.y + a_HEIGHT, a_OFFSET, e.y, 0, |
| 221 |
e.y, 0, 0 }; |
| 222 |
} |
| 223 |
|
| 224 |
void dispose() { |
| 225 |
if (!hoverShell.isDisposed()) |
| 226 |
hoverShell.dispose(); |
| 227 |
} |
| 228 |
|
| 229 |
void setVisible(boolean visible) { |
| 230 |
if (visible) { |
| 231 |
if (!hoverShell.isVisible()) |
| 232 |
hoverShell.setVisible(true); |
| 233 |
} else { |
| 234 |
if (hoverShell.isVisible()) |
| 235 |
hoverShell.setVisible(false); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
void setText(String t) { |
| 240 |
if (t == null) |
| 241 |
t = ""; //$NON-NLS-1$ |
| 242 |
if (!t.equals(text)) { |
| 243 |
Point oldSize = getExtent(); |
| 244 |
text = t; |
| 245 |
hoverShell.redraw(); |
| 246 |
Point newSize = getExtent(); |
| 247 |
if (!oldSize.equals(newSize)) { |
| 248 |
Region region = new Region(); |
| 249 |
region.add(getPolygon(false)); |
| 250 |
hoverShell.setRegion(region); |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
boolean isVisible() { |
| 256 |
return hoverShell.isVisible(); |
| 257 |
} |
| 258 |
|
| 259 |
void setLocation(Control control) { |
| 260 |
if (control != null) { |
| 261 |
int h = getExtent().y; |
| 262 |
hoverShell.setLocation(control.toDisplay(-a_OFFSET + a_WIDTH |
| 263 |
/ 2, -h - a_HEIGHT + 1)); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
Point getExtent() { |
| 268 |
GC gc = new GC(hoverShell); |
| 269 |
Point e = gc.textExtent(text); |
| 270 |
gc.dispose(); |
| 271 |
e.x += h_MARGIN * 2; |
| 272 |
e.y += h_MARGIN * 2; |
| 273 |
return e; |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Construct a decorated field which is parented by the specified composite |
| 279 |
* and has the given style bits. Use the controlCreator to create the |
| 280 |
* specific kind of control that is decorated inside the field. |
| 281 |
* |
| 282 |
* @param parent |
| 283 |
* the parent of the decorated field. |
| 284 |
* @param style |
| 285 |
* the desired style bits for the field. |
| 286 |
* @param controlCreator |
| 287 |
* the IControlCreator used to specify the specific kind of |
| 288 |
* control that is to be decorated. |
| 289 |
* |
| 290 |
* @see IControlCreator |
| 291 |
*/ |
| 292 |
public DecoratedField(Composite parent, int style, |
| 293 |
IControlCreator controlCreator) { |
| 294 |
this.form = createForm(parent); |
| 295 |
this.control = controlCreator.createControl(form, style); |
| 296 |
|
| 297 |
addControlListeners(); |
| 298 |
// Add a dummy decoration on each side to reserve the width needed. |
| 299 |
addFieldDecoration(new FieldDecoration(null, null), SWT.LEFT | SWT.TOP, |
| 300 |
true); |
| 301 |
addFieldDecoration(new FieldDecoration(null, null), |
| 302 |
SWT.RIGHT | SWT.TOP, true); |
| 303 |
form.setTabList(new Control[] { control }); |
| 304 |
|
| 305 |
// Set up the preferred width of the control and attachments to the |
| 306 |
// decorations. |
| 307 |
FormData data = new FormData(); |
| 308 |
GC gc = new GC(control); |
| 309 |
gc.setFont(control.getFont()); |
| 310 |
FontMetrics fontMetrics = gc.getFontMetrics(); |
| 311 |
gc.dispose(); |
| 312 |
data.width = Dialog.convertHorizontalDLUsToPixels(fontMetrics, |
| 313 |
IDialogConstants.ENTRY_FIELD_WIDTH) |
| 314 |
+ 2 * RESERVED_WIDTH; |
| 315 |
data.left = new FormAttachment(decDatas[LEFT_TOP].label); |
| 316 |
data.right = new FormAttachment(decDatas[RIGHT_TOP].label); |
| 317 |
data.top = new FormAttachment(0, 0); |
| 318 |
control.setLayoutData(data); |
| 319 |
|
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Adds an image decoration to the field. |
| 324 |
* |
| 325 |
* @param decoration |
| 326 |
* A FieldDecoration describing the image and description for the |
| 327 |
* decoration |
| 328 |
* |
| 329 |
* @param position |
| 330 |
* The SWT constant indicating the position of the decoration |
| 331 |
* relative to the field's control. The position should include |
| 332 |
* style bits describing both the vertical and horizontal |
| 333 |
* orientation. <code>SWT.LEFT</code> and |
| 334 |
* <code>SWT.RIGHT</code> describe the horizontal placement of |
| 335 |
* the decoration relative to the field, and the constants |
| 336 |
* <code>SWT.TOP</code> and <code>SWT.BOTTOM</code> describe |
| 337 |
* the vertical alignment of the decoration relative to the |
| 338 |
* field. Decorations always appear on either horizontal side of |
| 339 |
* the field, never above or below it. For example, a decoration |
| 340 |
* appearing on the left side of the field, at the top, is |
| 341 |
* specified as SWT.LEFT | SWT.TOP. If an image decoration |
| 342 |
* already exists in the specified position, it will be replaced |
| 343 |
* by the one specified. |
| 344 |
* @param showOnFocus |
| 345 |
* <code>true</code> if the decoration should only be shown |
| 346 |
* when the associated control has focus, <code>false</code> if |
| 347 |
* it should always be shown. |
| 348 |
* |
| 349 |
*/ |
| 350 |
public void addFieldDecoration(FieldDecoration decoration, int position, |
| 351 |
boolean showOnFocus) { |
| 352 |
final Label label; |
| 353 |
FormData data; |
| 354 |
int i = indexForPosition(position); |
| 355 |
if (decDatas[i] == null) { |
| 356 |
data = createFormDataForIndex(i); |
| 357 |
label = new Label(form, SWT.HORIZONTAL | SWT.VERTICAL | SWT.CENTER); |
| 358 |
label.addMouseTrackListener(new MouseTrackListener() { |
| 359 |
public void mouseHover(MouseEvent event) { |
| 360 |
FieldDecorationData decData = (FieldDecorationData) event.widget |
| 361 |
.getData(); |
| 362 |
String desc = decData.decoration.getDescription(); |
| 363 |
if (desc != null) { |
| 364 |
showHoverText(desc, label); |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
public void mouseEnter(MouseEvent event) { |
| 369 |
} |
| 370 |
|
| 371 |
public void mouseExit(MouseEvent event) { |
| 372 |
hideHover(); |
| 373 |
} |
| 374 |
}); |
| 375 |
decDatas[i] = new FieldDecorationData(decoration, label, data, |
| 376 |
showOnFocus); |
| 377 |
} else { |
| 378 |
label = decDatas[i].label; |
| 379 |
data = decDatas[i].data; |
| 380 |
decDatas[i].decoration = decoration; |
| 381 |
decDatas[i].showOnFocus = showOnFocus; |
| 382 |
} |
| 383 |
label.setImage(decDatas[i].decoration.getImage()); |
| 384 |
label.setData(decDatas[i]); |
| 385 |
label.setVisible(!showOnFocus); |
| 386 |
label.setLayoutData(data); |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* Get the control that is decorated by the receiver. |
| 391 |
* |
| 392 |
* @return the Control decorated by the receiver, or <code>null</code> if |
| 393 |
* none has been created yet. |
| 394 |
*/ |
| 395 |
public Control getControl() { |
| 396 |
return control; |
| 397 |
} |
| 398 |
|
| 399 |
/** |
| 400 |
* Get the control that represents the decorated field. This composite |
| 401 |
* should be used to lay out the field within its parent. |
| 402 |
* |
| 403 |
* @return the Control that should be layed out in the field's parent's |
| 404 |
* layout. This is typically not the control itself, since |
| 405 |
* additional controls are used to represent the decorations. |
| 406 |
*/ |
| 407 |
public Control getLayoutControl() { |
| 408 |
return form; |
| 409 |
} |
| 410 |
|
| 411 |
/** |
| 412 |
* Create the parent composite and a form layout that will be used to manage |
| 413 |
* decorations. |
| 414 |
*/ |
| 415 |
private Composite createForm(Composite parent) { |
| 416 |
Composite composite = new Composite(parent, SWT.NO_FOCUS); |
| 417 |
composite.setLayout(new FormLayout()); |
| 418 |
composite.setLayoutData(new GridData(SWT.BEGINNING, SWT.BEGINNING, |
| 419 |
true, true)); |
| 420 |
return composite; |
| 421 |
} |
| 422 |
|
| 423 |
/** |
| 424 |
* Add any listeners needed on the control. |
| 425 |
*/ |
| 426 |
private void addControlListeners() { |
| 427 |
control.addDisposeListener(new DisposeListener() { |
| 428 |
public void widgetDisposed(DisposeEvent event) { |
| 429 |
if (hover != null) |
| 430 |
hover.dispose(); |
| 431 |
} |
| 432 |
}); |
| 433 |
control.addFocusListener(new FocusListener() { |
| 434 |
public void focusGained(FocusEvent event) { |
| 435 |
controlFocusGained(); |
| 436 |
} |
| 437 |
|
| 438 |
public void focusLost(FocusEvent event) { |
| 439 |
controlFocusLost(); |
| 440 |
} |
| 441 |
|
| 442 |
}); |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* Return the index in the array of decoration datas that represents the |
| 447 |
* specified SWT position. |
| 448 |
* |
| 449 |
* @param position |
| 450 |
* The SWT constant indicating the position of the decoration |
| 451 |
* relative to the field's control. The position should include |
| 452 |
* style bits describing both the vertical and horizontal |
| 453 |
* orientation. <code>SWT.LEFT</code> and |
| 454 |
* <code>SWT.RIGHT</code> describe the horizontal placement of |
| 455 |
* the decoration relative to the field, and the constants |
| 456 |
* <code>SWT.TOP</code> and <code>SWT.BOTTOM</code> describe |
| 457 |
* the vertical alignment of the decoration relative to the |
| 458 |
* field. Decorations always appear on either horizontal side of |
| 459 |
* the field, never above or below it. For example, a decoration |
| 460 |
* appearing on the left side of the field, at the top, is |
| 461 |
* specified as SWT.LEFT | SWT.TOP. If an image decoration |
| 462 |
* already exists in the specified position, it will be replaced |
| 463 |
* by the one specified. |
| 464 |
* |
| 465 |
* @return index the index in the array of decorations that represents the |
| 466 |
* specified SWT position. If the position is not an expected |
| 467 |
* position, the index representing the top left position will be |
| 468 |
* returned. |
| 469 |
* |
| 470 |
*/ |
| 471 |
int indexForPosition(int position) { |
| 472 |
switch (position) { |
| 473 |
case SWT.LEFT | SWT.BOTTOM: |
| 474 |
return LEFT_BOTTOM; |
| 475 |
case SWT.RIGHT | SWT.TOP: |
| 476 |
return RIGHT_TOP; |
| 477 |
case SWT.RIGHT | SWT.BOTTOM: |
| 478 |
return RIGHT_BOTTOM; |
| 479 |
default: |
| 480 |
return LEFT_TOP; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
/** |
| 485 |
* Create a form data that will place the decoration at the specified |
| 486 |
* position. |
| 487 |
* |
| 488 |
* @param position |
| 489 |
* The SWT constant indicating the position of the decoration |
| 490 |
* relative to the field's control. The position should include |
| 491 |
* style bits describing both the vertical and horizontal |
| 492 |
* orientation. <code>SWT.LEFT</code> and |
| 493 |
* <code>SWT.RIGHT</code> describe the horizontal placement of |
| 494 |
* the decoration relative to the field, and the constants |
| 495 |
* <code>SWT.TOP</code> and <code>SWT.BOTTOM</code> describe |
| 496 |
* the vertical alignment of the decoration relative to the |
| 497 |
* field. Decorations always appear on either horizontal side of |
| 498 |
* the field, never above or below it. For example, a decoration |
| 499 |
* appearing on the left side of the field, at the top, is |
| 500 |
* specified as SWT.LEFT | SWT.TOP. If an image decoration |
| 501 |
* already exists in the specified position, it will be replaced |
| 502 |
* by the one specified. |
| 503 |
* |
| 504 |
*/ |
| 505 |
private FormData createFormDataForIndex(int index) { |
| 506 |
Assert.isTrue(index >= 0 && index < DECORATION_SLOTS, |
| 507 |
"Index out of range"); //$NON-NLS-1$ |
| 508 |
|
| 509 |
FormData data = new FormData(); |
| 510 |
data.width = RESERVED_WIDTH; |
| 511 |
switch (index) { |
| 512 |
case LEFT_TOP: |
| 513 |
data.left = new FormAttachment(0, 0); |
| 514 |
data.top = new FormAttachment(0, 0); |
| 515 |
return data; |
| 516 |
case LEFT_BOTTOM: |
| 517 |
data.left = new FormAttachment(0, 0); |
| 518 |
data.bottom = new FormAttachment(100, 0); |
| 519 |
return data; |
| 520 |
case RIGHT_TOP: |
| 521 |
data.right = new FormAttachment(100, 0); |
| 522 |
data.top = new FormAttachment(0, 0); |
| 523 |
return data; |
| 524 |
case RIGHT_BOTTOM: |
| 525 |
data.right = new FormAttachment(100, 0); |
| 526 |
data.bottom = new FormAttachment(100, 0); |
| 527 |
return data; |
| 528 |
} |
| 529 |
// should never get here, making compiler happy |
| 530 |
return data; |
| 531 |
} |
| 532 |
|
| 533 |
/** |
| 534 |
* Show the specified text using the same hover dialog as is used to show |
| 535 |
* decorator descriptions. Normally, a decoration's description text will be |
| 536 |
* shown in an info hover over the field's control whenever the mouse hovers |
| 537 |
* over the decoration. This method can be used to show a decoration's |
| 538 |
* description text at other times (such as when the control receives |
| 539 |
* focus), or to show other text associated with the field. |
| 540 |
* |
| 541 |
* <p> |
| 542 |
* If there is currently a hover visible, the hover's text will be replaced |
| 543 |
* with the specified text. |
| 544 |
* |
| 545 |
* @param text |
| 546 |
* the text to be shown in the info hover, or <code>null</code> |
| 547 |
* if no text should be shown. |
| 548 |
*/ |
| 549 |
public void showHoverText(String text) { |
| 550 |
showHoverText(text, control); |
| 551 |
} |
| 552 |
|
| 553 |
/** |
| 554 |
* Hide any hover popups that are currently showing on the control. |
| 555 |
* Normally, a decoration's description text will be shown in an info hover |
| 556 |
* over the field's control as long as the mouse hovers over the decoration, |
| 557 |
* and will be hidden when the mouse exits the control. This method can be |
| 558 |
* used to hide a hover that was shown using <code>showHoverText</code>, |
| 559 |
* or to programatically hide the current decoration hover. |
| 560 |
* |
| 561 |
* <p> |
| 562 |
* This message has no effect if there is no current hover. |
| 563 |
* |
| 564 |
*/ |
| 565 |
public void hideHover() { |
| 566 |
if (hover != null) { |
| 567 |
hover.setVisible(false); |
| 568 |
} |
| 569 |
} |
| 570 |
|
| 571 |
private void controlFocusGained() { |
| 572 |
for (int i = 0; i < DECORATION_SLOTS; i++) { |
| 573 |
if (decDatas[i] != null && decDatas[i].showOnFocus) |
| 574 |
setVisible(decDatas[i], true); |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
private void controlFocusLost() { |
| 579 |
for (int i = 0; i < DECORATION_SLOTS; i++) { |
| 580 |
if (decDatas[i] != null && decDatas[i].showOnFocus) |
| 581 |
setVisible(decDatas[i], false); |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
/** |
| 586 |
* Show the specified decoration. This message has no effect if the |
| 587 |
* decoration is already showing, or was not already added to the field |
| 588 |
* using <code>addFieldDecoration</code>. |
| 589 |
* |
| 590 |
* @param decoration |
| 591 |
* the decoration to be shown. |
| 592 |
*/ |
| 593 |
public void showDecoration(FieldDecoration decoration) { |
| 594 |
FieldDecorationData data = getDecorationData(decoration); |
| 595 |
if (data == null) |
| 596 |
return; |
| 597 |
// record the fact that client would like it to be visible |
| 598 |
data.visible = true; |
| 599 |
// even if it is supposed to be shown, if the field does not have focus, |
| 600 |
// do not show it (yet) |
| 601 |
if (!data.showOnFocus || control.isFocusControl()) |
| 602 |
setVisible(data, true); |
| 603 |
} |
| 604 |
|
| 605 |
/** |
| 606 |
* Hide the specified decoration. This message has no effect if the |
| 607 |
* decoration is already hidden, or was not already added to the field using |
| 608 |
* <code>addFieldDecoration</code>. |
| 609 |
* |
| 610 |
* @param decoration |
| 611 |
* the decoration to be hidden. |
| 612 |
*/ |
| 613 |
public void hideDecoration(FieldDecoration decoration) { |
| 614 |
FieldDecorationData data = getDecorationData(decoration); |
| 615 |
if (data == null) |
| 616 |
return; |
| 617 |
// store the visibility in the decoration data so internal changes in |
| 618 |
// visibility don't |
| 619 |
// accidentally show it. |
| 620 |
data.visible = false; |
| 621 |
setVisible(data, false); |
| 622 |
} |
| 623 |
|
| 624 |
private void setVisible(FieldDecorationData decData, boolean visible) { |
| 625 |
// Don't set it visible if the client has instructed us to hide it. |
| 626 |
if (visible && decData.visible) |
| 627 |
decData.label.setVisible(true); |
| 628 |
else |
| 629 |
decData.label.setVisible(false); |
| 630 |
} |
| 631 |
|
| 632 |
private FieldDecorationData getDecorationData(FieldDecoration dec) { |
| 633 |
for (int i = 0; i < DECORATION_SLOTS; i++) { |
| 634 |
if (decDatas[i] != null && dec == decDatas[i].decoration |
| 635 |
&& decDatas[i].label != null |
| 636 |
&& !decDatas[i].label.isDisposed()) |
| 637 |
return decDatas[i]; |
| 638 |
} |
| 639 |
return null; |
| 640 |
} |
| 641 |
|
| 642 |
private void showHoverText(String text, Control hoverNear) { |
| 643 |
if (text == null) { |
| 644 |
hideHover(); |
| 645 |
return; |
| 646 |
} |
| 647 |
|
| 648 |
if (hover == null) { |
| 649 |
hover = new Hover(hoverNear.getShell()); |
| 650 |
} |
| 651 |
hover.setText(text); |
| 652 |
hover.setLocation(hoverNear); |
| 653 |
hover.setVisible(true); |
| 654 |
} |
| 655 |
} |