1
|
/*******************************************************************************
|
2
|
* Copyright (c) 2000, 2010 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.draw2d;
|
12
|
|
13
|
import org.eclipse.swt.graphics.Font;
|
14
|
import org.eclipse.swt.graphics.Image;
|
15
|
|
16
|
import org.eclipse.draw2d.geometry.Dimension;
|
17
|
import org.eclipse.draw2d.geometry.Insets;
|
18
|
import org.eclipse.draw2d.geometry.Point;
|
19
|
import org.eclipse.draw2d.geometry.Rectangle;
|
20
|
|
21
|
/**
|
22
|
* A figure that can display text and/or an image.
|
23
|
*/
|
24
|
public class Label extends Figure implements PositionConstants {
|
25
|
|
26
|
private static String ELLIPSIS = "..."; //$NON-NLS-1$
|
27
|
|
28
|
private Image icon;
|
29
|
private String text = "";//$NON-NLS-1$
|
30
|
private String subStringText;
|
31
|
private Dimension textSize;
|
32
|
private Dimension subStringTextSize;
|
33
|
private Dimension iconSize = new Dimension(0, 0);
|
34
|
private Point iconLocation;
|
35
|
private Point textLocation;
|
36
|
private int textAlignment = CENTER;
|
37
|
private int iconAlignment = CENTER;
|
38
|
private int labelAlignment = CENTER;
|
39
|
private int textPlacement = EAST;
|
40
|
private int iconTextGap = 3;
|
41
|
|
42
|
/**
|
43
|
* Construct an empty Label.
|
44
|
*
|
45
|
* @since 2.0
|
46
|
*/
|
47
|
public Label() {
|
48
|
}
|
49
|
|
50
|
/**
|
51
|
* Construct a Label with passed String as its text.
|
52
|
*
|
53
|
* @param s
|
54
|
* the label text
|
55
|
* @since 2.0
|
56
|
*/
|
57
|
public Label(String s) {
|
58
|
setText(s);
|
59
|
}
|
60
|
|
61
|
/**
|
62
|
* Construct a Label with passed Image as its icon.
|
63
|
*
|
64
|
* @param i
|
65
|
* the label image
|
66
|
* @since 2.0
|
67
|
*/
|
68
|
public Label(Image i) {
|
69
|
setIcon(i);
|
70
|
}
|
71
|
|
72
|
/**
|
73
|
* Construct a Label with passed String as text and passed Image as its
|
74
|
* icon.
|
75
|
*
|
76
|
* @param s
|
77
|
* the label text
|
78
|
* @param i
|
79
|
* the label image
|
80
|
* @since 2.0
|
81
|
*/
|
82
|
public Label(String s, Image i) {
|
83
|
setText(s);
|
84
|
setIcon(i);
|
85
|
}
|
86
|
|
87
|
private void alignOnHeight(Point loc, Dimension size, int alignment) {
|
88
|
Insets insets = getInsets();
|
89
|
switch (alignment) {
|
90
|
case TOP:
|
91
|
loc.y = insets.top;
|
92
|
break;
|
93
|
case BOTTOM:
|
94
|
loc.y = bounds.height - size.height - insets.bottom;
|
95
|
break;
|
96
|
default:
|
97
|
loc.y = (bounds.height - size.height) / 2;
|
98
|
}
|
99
|
}
|
100
|
|
101
|
private void alignOnWidth(Point loc, Dimension size, int alignment) {
|
102
|
Insets insets = getInsets();
|
103
|
switch (alignment) {
|
104
|
case LEFT:
|
105
|
loc.x = insets.left;
|
106
|
break;
|
107
|
case RIGHT:
|
108
|
loc.x = bounds.width - size.width - insets.right;
|
109
|
break;
|
110
|
default:
|
111
|
loc.x = (bounds.width - size.width) / 2;
|
112
|
}
|
113
|
}
|
114
|
|
115
|
private void calculateAlignment() {
|
116
|
switch (textPlacement) {
|
117
|
case EAST:
|
118
|
case WEST:
|
119
|
alignOnHeight(textLocation, getTextSize(), textAlignment);
|
120
|
alignOnHeight(iconLocation, getIconSize(), iconAlignment);
|
121
|
break;
|
122
|
case NORTH:
|
123
|
case SOUTH:
|
124
|
alignOnWidth(textLocation, getSubStringTextSize(), textAlignment);
|
125
|
alignOnWidth(iconLocation, getIconSize(), iconAlignment);
|
126
|
break;
|
127
|
}
|
128
|
}
|
129
|
|
130
|
/**
|
131
|
* Calculates the size of the Label using the passed Dimension as the size
|
132
|
* of the Label's text.
|
133
|
*
|
134
|
* @param txtSize
|
135
|
* the precalculated size of the label's text
|
136
|
* @return the label's size
|
137
|
* @since 2.0
|
138
|
*/
|
139
|
protected Dimension calculateLabelSize(Dimension txtSize) {
|
140
|
int gap = getIconTextGap();
|
141
|
if (getIcon() == null || getText().equals("")) //$NON-NLS-1$
|
142
|
gap = 0;
|
143
|
Dimension d = new Dimension(0, 0);
|
144
|
if (textPlacement == WEST || textPlacement == EAST) {
|
145
|
d.width = getIconSize().width + gap + txtSize.width;
|
146
|
d.height = Math.max(getIconSize().height, txtSize.height);
|
147
|
} else {
|
148
|
d.width = Math.max(getIconSize().width, txtSize.width);
|
149
|
d.height = getIconSize().height + gap + txtSize.height;
|
150
|
}
|
151
|
return d;
|
152
|
}
|
153
|
|
154
|
private void calculateLocations() {
|
155
|
textLocation = new Point();
|
156
|
iconLocation = new Point();
|
157
|
|
158
|
calculatePlacement();
|
159
|
calculateAlignment();
|
160
|
Dimension offset = getSize().getDifference(getPreferredSize());
|
161
|
offset.width += getTextSize().width - getSubStringTextSize().width;
|
162
|
switch (labelAlignment) {
|
163
|
case CENTER:
|
164
|
offset.scale(0.5f);
|
165
|
break;
|
166
|
case LEFT:
|
167
|
offset.scale(0.0f);
|
168
|
break;
|
169
|
case RIGHT:
|
170
|
offset.scale(1.0f);
|
171
|
break;
|
172
|
case TOP:
|
173
|
offset.height = 0;
|
174
|
offset.scale(0.5f);
|
175
|
break;
|
176
|
case BOTTOM:
|
177
|
offset.height = offset.height * 2;
|
178
|
offset.scale(0.5f);
|
179
|
break;
|
180
|
default:
|
181
|
offset.scale(0.5f);
|
182
|
break;
|
183
|
}
|
184
|
|
185
|
switch (textPlacement) {
|
186
|
case EAST:
|
187
|
case WEST:
|
188
|
offset.height = 0;
|
189
|
break;
|
190
|
case NORTH:
|
191
|
case SOUTH:
|
192
|
offset.width = 0;
|
193
|
break;
|
194
|
}
|
195
|
|
196
|
textLocation.translate(offset);
|
197
|
iconLocation.translate(offset);
|
198
|
}
|
199
|
|
200
|
private void calculatePlacement() {
|
201
|
int gap = getIconTextGap();
|
202
|
if (icon == null || text.equals("")) //$NON-NLS-1$
|
203
|
gap = 0;
|
204
|
Insets insets = getInsets();
|
205
|
|
206
|
switch (textPlacement) {
|
207
|
case EAST:
|
208
|
iconLocation.x = insets.left;
|
209
|
textLocation.x = getIconSize().width + gap + insets.left;
|
210
|
break;
|
211
|
case WEST:
|
212
|
textLocation.x = insets.left;
|
213
|
iconLocation.x = getSubStringTextSize().width + gap + insets.left;
|
214
|
break;
|
215
|
case NORTH:
|
216
|
textLocation.y = insets.top;
|
217
|
iconLocation.y = getTextSize().height + gap + insets.top;
|
218
|
break;
|
219
|
case SOUTH:
|
220
|
textLocation.y = getIconSize().height + gap + insets.top;
|
221
|
iconLocation.y = insets.top;
|
222
|
}
|
223
|
}
|
224
|
|
225
|
/**
|
226
|
* Calculates the size of the Label's text size. The text size calculated
|
227
|
* takes into consideration if the Label's text is currently truncated. If
|
228
|
* text size without considering current truncation is desired, use
|
229
|
* {@link #calculateTextSize()}.
|
230
|
*
|
231
|
* @return the size of the label's text, taking into account truncation
|
232
|
* @since 2.0
|
233
|
*/
|
234
|
protected Dimension calculateSubStringTextSize() {
|
235
|
return getTextUtilities().getTextExtents(getSubStringText(), getFont());
|
236
|
}
|
237
|
|
238
|
/**
|
239
|
* Calculates and returns the size of the Label's text. Note that this
|
240
|
* Dimension is calculated using the Label's full text, regardless of
|
241
|
* whether or not its text is currently truncated. If text size considering
|
242
|
* current truncation is desired, use {@link #calculateSubStringTextSize()}.
|
243
|
*
|
244
|
* @return the size of the label's text, ignoring truncation
|
245
|
* @since 2.0
|
246
|
*/
|
247
|
protected Dimension calculateTextSize() {
|
248
|
return getTextUtilities().getTextExtents(getText(), getFont());
|
249
|
}
|
250
|
|
251
|
private void clearLocations() {
|
252
|
iconLocation = textLocation = null;
|
253
|
}
|
254
|
|
255
|
/**
|
256
|
* Returns the Label's icon.
|
257
|
*
|
258
|
* @return the label icon
|
259
|
* @since 2.0
|
260
|
*/
|
261
|
public Image getIcon() {
|
262
|
return icon;
|
263
|
}
|
264
|
|
265
|
/**
|
266
|
* Returns the current alignment of the Label's icon. The default is
|
267
|
* {@link PositionConstants#CENTER}.
|
268
|
*
|
269
|
* @return the icon alignment
|
270
|
* @since 2.0
|
271
|
*/
|
272
|
public int getIconAlignment() {
|
273
|
return iconAlignment;
|
274
|
}
|
275
|
|
276
|
/**
|
277
|
* Returns the bounds of the Label's icon.
|
278
|
*
|
279
|
* @return the icon's bounds
|
280
|
* @since 2.0
|
281
|
*/
|
282
|
public Rectangle getIconBounds() {
|
283
|
Rectangle bounds = getBounds();
|
284
|
return new Rectangle(bounds.getLocation().translate(getIconLocation()),
|
285
|
getIconSize());
|
286
|
}
|
287
|
|
288
|
/**
|
289
|
* Returns the location of the Label's icon relative to the Label.
|
290
|
*
|
291
|
* @return the icon's location
|
292
|
* @since 2.0
|
293
|
*/
|
294
|
protected Point getIconLocation() {
|
295
|
if (iconLocation == null)
|
296
|
calculateLocations();
|
297
|
return iconLocation;
|
298
|
}
|
299
|
|
300
|
/**
|
301
|
* Returns the gap in pixels between the Label's icon and its text.
|
302
|
*
|
303
|
* @return the gap
|
304
|
* @since 2.0
|
305
|
*/
|
306
|
public int getIconTextGap() {
|
307
|
return iconTextGap;
|
308
|
}
|
309
|
|
310
|
/**
|
311
|
* @see IFigure#getMinimumSize(int, int)
|
312
|
*/
|
313
|
public Dimension getMinimumSize(int w, int h) {
|
314
|
if (minSize != null)
|
315
|
return minSize;
|
316
|
minSize = new Dimension();
|
317
|
if (getLayoutManager() != null)
|
318
|
minSize.setSize(getLayoutManager().getMinimumSize(this, w, h));
|
319
|
|
320
|
Dimension labelSize = calculateLabelSize(getTextUtilities()
|
321
|
.getTextExtents(getTruncationString(), getFont())
|
322
|
.intersect(
|
323
|
getTextUtilities().getTextExtents(getText(), getFont())));
|
324
|
Insets insets = getInsets();
|
325
|
labelSize.expand(insets.getWidth(), insets.getHeight());
|
326
|
minSize.union(labelSize);
|
327
|
return minSize;
|
328
|
}
|
329
|
|
330
|
/**
|
331
|
* @see IFigure#getPreferredSize(int, int)
|
332
|
*/
|
333
|
public Dimension getPreferredSize(int wHint, int hHint) {
|
334
|
if (prefSize == null) {
|
335
|
prefSize = calculateLabelSize(getTextSize());
|
336
|
Insets insets = getInsets();
|
337
|
prefSize.expand(insets.getWidth(), insets.getHeight());
|
338
|
if (getLayoutManager() != null)
|
339
|
prefSize.union(getLayoutManager().getPreferredSize(this, wHint,
|
340
|
hHint));
|
341
|
}
|
342
|
if (wHint >= 0 && wHint < prefSize.width) {
|
343
|
Dimension minSize = getMinimumSize(wHint, hHint);
|
344
|
Dimension result = prefSize.getCopy();
|
345
|
result.width = Math.min(result.width, wHint);
|
346
|
result.width = Math.max(minSize.width, result.width);
|
347
|
return result;
|
348
|
}
|
349
|
return prefSize;
|
350
|
}
|
351
|
|
352
|
/**
|
353
|
* Calculates the amount of the Label's current text will fit in the Label,
|
354
|
* including an elipsis "..." if truncation is required.
|
355
|
*
|
356
|
* @return the substring
|
357
|
* @since 2.0
|
358
|
*/
|
359
|
public String getSubStringText() {
|
360
|
if (subStringText != null)
|
361
|
return subStringText;
|
362
|
|
363
|
subStringText = text;
|
364
|
int widthShrink = getPreferredSize().width - getSize().width;
|
365
|
if (widthShrink <= 0)
|
366
|
return subStringText;
|
367
|
|
368
|
Dimension effectiveSize = getTextSize().getExpanded(-widthShrink, 0);
|
369
|
Font currentFont = getFont();
|
370
|
int dotsWidth = getTextUtilities().getTextExtents(
|
371
|
getTruncationString(), currentFont).width;
|
372
|
|
373
|
if (effectiveSize.width < dotsWidth)
|
374
|
effectiveSize.width = dotsWidth;
|
375
|
|
376
|
int subStringLength = getTextUtilities().getLargestSubstringConfinedTo(
|
377
|
text, currentFont, effectiveSize.width - dotsWidth);
|
378
|
subStringText = new String(text.substring(0, subStringLength)
|
379
|
+ getTruncationString());
|
380
|
return subStringText;
|
381
|
}
|
382
|
|
383
|
/**
|
384
|
* Returns the size of the Label's current text. If the text is currently
|
385
|
* truncated, the truncated text with its ellipsis is used to calculate the
|
386
|
* size.
|
387
|
*
|
388
|
* @return the size of this label's text, taking into account truncation
|
389
|
* @since 2.0
|
390
|
*/
|
391
|
protected Dimension getSubStringTextSize() {
|
392
|
if (subStringTextSize == null)
|
393
|
subStringTextSize = calculateSubStringTextSize();
|
394
|
return subStringTextSize;
|
395
|
}
|
396
|
|
397
|
/**
|
398
|
* Returns the text of the label. Note that this is the complete text of the
|
399
|
* label, regardless of whether it is currently being truncated. Call
|
400
|
* {@link #getSubStringText()} to return the label's current text contents
|
401
|
* with truncation considered.
|
402
|
*
|
403
|
* @return the complete text of this label
|
404
|
* @since 2.0
|
405
|
*/
|
406
|
public String getText() {
|
407
|
return text;
|
408
|
}
|
409
|
|
410
|
/**
|
411
|
* Returns the current alignment of the Label's text. The default text
|
412
|
* alignment is {@link PositionConstants#CENTER}.
|
413
|
*
|
414
|
* @return the text alignment
|
415
|
*/
|
416
|
public int getTextAlignment() {
|
417
|
return textAlignment;
|
418
|
}
|
419
|
|
420
|
/**
|
421
|
* Returns the bounds of the label's text. Note that the bounds are
|
422
|
* calculated using the label's complete text regardless of whether the
|
423
|
* label's text is currently truncated.
|
424
|
*
|
425
|
* @return the bounds of this label's complete text
|
426
|
* @since 2.0
|
427
|
*/
|
428
|
public Rectangle getTextBounds() {
|
429
|
Rectangle bounds = getBounds();
|
430
|
return new Rectangle(bounds.getLocation().translate(getTextLocation()),
|
431
|
textSize);
|
432
|
}
|
433
|
|
434
|
/**
|
435
|
* Returns the location of the label's text relative to the label.
|
436
|
*
|
437
|
* @return the text location
|
438
|
* @since 2.0
|
439
|
*/
|
440
|
protected Point getTextLocation() {
|
441
|
if (textLocation != null)
|
442
|
return textLocation;
|
443
|
calculateLocations();
|
444
|
return textLocation;
|
445
|
}
|
446
|
|
447
|
/**
|
448
|
* Returns the current placement of the label's text relative to its icon.
|
449
|
* The default text placement is {@link PositionConstants#EAST}.
|
450
|
*
|
451
|
* @return the text placement
|
452
|
* @since 2.0
|
453
|
*/
|
454
|
public int getTextPlacement() {
|
455
|
return textPlacement;
|
456
|
}
|
457
|
|
458
|
/**
|
459
|
* Returns the size of the label's complete text. Note that the text used to
|
460
|
* make this calculation is the label's full text, regardless of whether the
|
461
|
* label's text is currently being truncated and is displaying an ellipsis.
|
462
|
* If the size considering current truncation is desired, call
|
463
|
* {@link #getSubStringTextSize()}.
|
464
|
*
|
465
|
* @return the size of this label's complete text
|
466
|
* @since 2.0
|
467
|
*/
|
468
|
protected Dimension getTextSize() {
|
469
|
if (textSize == null)
|
470
|
textSize = calculateTextSize();
|
471
|
return textSize;
|
472
|
}
|
473
|
|
474
|
/**
|
475
|
* @see IFigure#invalidate()
|
476
|
*/
|
477
|
public void invalidate() {
|
478
|
prefSize = null;
|
479
|
minSize = null;
|
480
|
clearLocations();
|
481
|
textSize = null;
|
482
|
subStringTextSize = null;
|
483
|
subStringText = null;
|
484
|
super.invalidate();
|
485
|
}
|
486
|
|
487
|
/**
|
488
|
* Returns <code>true</code> if the label's text is currently truncated and
|
489
|
* is displaying an ellipsis, <code>false</code> otherwise.
|
490
|
*
|
491
|
* @return <code>true</code> if the label's text is truncated
|
492
|
* @since 2.0
|
493
|
*/
|
494
|
public boolean isTextTruncated() {
|
495
|
return !getSubStringText().equals(getText());
|
496
|
}
|
497
|
|
498
|
/**
|
499
|
* @see Figure#paintFigure(Graphics)
|
500
|
*/
|
501
|
protected void paintFigure(Graphics graphics) {
|
502
|
if (isOpaque())
|
503
|
super.paintFigure(graphics);
|
504
|
Rectangle bounds = getBounds();
|
505
|
graphics.translate(bounds.x, bounds.y);
|
506
|
if (icon != null)
|
507
|
graphics.drawImage(icon, getIconLocation());
|
508
|
if (!isEnabled()) {
|
509
|
graphics.translate(1, 1);
|
510
|
graphics.setForegroundColor(ColorConstants.buttonLightest);
|
511
|
graphics.drawText(getSubStringText(), getTextLocation());
|
512
|
graphics.translate(-1, -1);
|
513
|
graphics.setForegroundColor(ColorConstants.buttonDarker);
|
514
|
}
|
515
|
graphics.drawText(getSubStringText(), getTextLocation());
|
516
|
graphics.translate(-bounds.x, -bounds.y);
|
517
|
}
|
518
|
|
519
|
/**
|
520
|
* Sets the label's icon to the passed image.
|
521
|
*
|
522
|
* @param image
|
523
|
* the new label image
|
524
|
* @since 2.0
|
525
|
*/
|
526
|
public void setIcon(Image image) {
|
527
|
if (icon == image)
|
528
|
return;
|
529
|
icon = image;
|
530
|
// Call repaint, in case the image dimensions are the same.
|
531
|
repaint();
|
532
|
if (icon == null)
|
533
|
setIconDimension(new Dimension());
|
534
|
else
|
535
|
setIconDimension(new Dimension(image));
|
536
|
}
|
537
|
|
538
|
/**
|
539
|
* This method sets the alignment of the icon within the bounds of the
|
540
|
* label. If the label is larger than the icon, then the icon will be
|
541
|
* aligned according to this alignment. Valid values are:
|
542
|
* <UL>
|
543
|
* <LI><EM>{@link PositionConstants#CENTER}</EM>
|
544
|
* <LI>{@link PositionConstants#TOP}
|
545
|
* <LI>{@link PositionConstants#BOTTOM}
|
546
|
* <LI>{@link PositionConstants#LEFT}
|
547
|
* <LI>{@link PositionConstants#RIGHT}
|
548
|
* </UL>
|
549
|
*
|
550
|
* @param align
|
551
|
* the icon alignment
|
552
|
* @since 2.0
|
553
|
*/
|
554
|
public void setIconAlignment(int align) {
|
555
|
if (iconAlignment == align)
|
556
|
return;
|
557
|
iconAlignment = align;
|
558
|
clearLocations();
|
559
|
repaint();
|
560
|
}
|
561
|
|
562
|
/**
|
563
|
* Sets the label's icon size to the passed Dimension.
|
564
|
*
|
565
|
* @param d
|
566
|
* the new icon size
|
567
|
* @deprecated the icon is automatically displayed at 1:1
|
568
|
* @since 2.0
|
569
|
*/
|
570
|
public void setIconDimension(Dimension d) {
|
571
|
if (d.equals(getIconSize()))
|
572
|
return;
|
573
|
iconSize = d;
|
574
|
revalidate();
|
575
|
}
|
576
|
|
577
|
/**
|
578
|
* Sets the gap in pixels between the label's icon and text to the passed
|
579
|
* value. The default is 4.
|
580
|
*
|
581
|
* @param gap
|
582
|
* the gap
|
583
|
* @since 2.0
|
584
|
*/
|
585
|
public void setIconTextGap(int gap) {
|
586
|
if (iconTextGap == gap)
|
587
|
return;
|
588
|
iconTextGap = gap;
|
589
|
repaint();
|
590
|
revalidate();
|
591
|
}
|
592
|
|
593
|
/**
|
594
|
* Sets the alignment of the label (icon and text) within the figure. If
|
595
|
* this figure's bounds are larger than the size needed to display the
|
596
|
* label, the label will be aligned accordingly. Valid values are:
|
597
|
* <UL>
|
598
|
* <LI><EM>{@link PositionConstants#CENTER}</EM>
|
599
|
* <LI>{@link PositionConstants#TOP}
|
600
|
* <LI>{@link PositionConstants#BOTTOM}
|
601
|
* <LI>{@link PositionConstants#LEFT}
|
602
|
* <LI>{@link PositionConstants#RIGHT}
|
603
|
* </UL>
|
604
|
*
|
605
|
* @param align
|
606
|
* label alignment
|
607
|
*/
|
608
|
public void setLabelAlignment(int align) {
|
609
|
if (labelAlignment == align)
|
610
|
return;
|
611
|
labelAlignment = align;
|
612
|
clearLocations();
|
613
|
repaint();
|
614
|
}
|
615
|
|
616
|
/**
|
617
|
* Sets the label's text.
|
618
|
*
|
619
|
* @param s
|
620
|
* the new label text
|
621
|
* @since 2.0
|
622
|
*/
|
623
|
public void setText(String s) {
|
624
|
// "text" will never be null.
|
625
|
if (s == null)
|
626
|
s = "";//$NON-NLS-1$
|
627
|
if (text.equals(s))
|
628
|
return;
|
629
|
text = s;
|
630
|
revalidate();
|
631
|
repaint();
|
632
|
}
|
633
|
|
634
|
/**
|
635
|
* Sets the alignment of the text relative to the icon within the label. The
|
636
|
* text alignment must be orthogonal to the text placement. For example, if
|
637
|
* the placement is EAST, then the text can be aligned using TOP, CENTER, or
|
638
|
* BOTTOM. Valid values are:
|
639
|
* <UL>
|
640
|
* <LI><EM>{@link PositionConstants#CENTER}</EM>
|
641
|
* <LI>{@link PositionConstants#TOP}
|
642
|
* <LI>{@link PositionConstants#BOTTOM}
|
643
|
* <LI>{@link PositionConstants#LEFT}
|
644
|
* <LI>{@link PositionConstants#RIGHT}
|
645
|
* </UL>
|
646
|
*
|
647
|
* @see #setLabelAlignment(int)
|
648
|
* @param align
|
649
|
* the text alignment
|
650
|
* @since 2.0
|
651
|
*/
|
652
|
public void setTextAlignment(int align) {
|
653
|
if (textAlignment == align)
|
654
|
return;
|
655
|
textAlignment = align;
|
656
|
clearLocations();
|
657
|
repaint();
|
658
|
}
|
659
|
|
660
|
/**
|
661
|
* Sets the placement of the text relative to the icon within the label.
|
662
|
* Valid values are:
|
663
|
* <UL>
|
664
|
* <LI><EM>{@link PositionConstants#EAST}</EM>
|
665
|
* <LI>{@link PositionConstants#NORTH}
|
666
|
* <LI>{@link PositionConstants#SOUTH}
|
667
|
* <LI>{@link PositionConstants#WEST}
|
668
|
* </UL>
|
669
|
*
|
670
|
* @param where
|
671
|
* the text placement
|
672
|
* @since 2.0
|
673
|
*/
|
674
|
public void setTextPlacement(int where) {
|
675
|
if (textPlacement == where)
|
676
|
return;
|
677
|
textPlacement = where;
|
678
|
revalidate();
|
679
|
repaint();
|
680
|
}
|
681
|
|
682
|
/**
|
683
|
* Gets the <code>TextUtilities</code> instance to be used in measurement
|
684
|
* calculations.
|
685
|
*
|
686
|
* @return a <code>TextUtilities</code> instance
|
687
|
* @since 3.4
|
688
|
*/
|
689
|
public TextUtilities getTextUtilities() {
|
690
|
return TextUtilities.INSTANCE;
|
691
|
}
|
692
|
|
693
|
/**
|
694
|
* Gets the string that will be appended to the text when the label is
|
695
|
* truncated. By default, this returns an ellipsis.
|
696
|
*
|
697
|
* @return the string to append to the text when truncated
|
698
|
* @since 3.4
|
699
|
*/
|
700
|
protected String getTruncationString() {
|
701
|
return ELLIPSIS;
|
702
|
}
|
703
|
|
704
|
/**
|
705
|
* Gets the icon size
|
706
|
*
|
707
|
* @return the icon size
|
708
|
* @since 3.4
|
709
|
*/
|
710
|
protected Dimension getIconSize() {
|
711
|
return iconSize;
|
712
|
}
|
713
|
|
714
|
/**
|
715
|
* Returns the alignment of the entire label (icon and text). The default
|
716
|
* label alignment is {@link PositionConstants#CENTER}.
|
717
|
*
|
718
|
* @return the label alignment
|
719
|
* @since 3.5
|
720
|
*/
|
721
|
public int getLabelAlignment() {
|
722
|
return labelAlignment;
|
723
|
}
|
724
|
}
|