org.eclipse.swt.custom
Class CLabel

java.lang.Object
  |
  +--org.eclipse.swt.widgets.Widget
        |
        +--org.eclipse.swt.widgets.Control
              |
              +--org.eclipse.swt.widgets.Scrollable
                    |
                    +--org.eclipse.swt.widgets.Composite
                          |
                          +--org.eclipse.swt.widgets.Canvas
                                |
                                +--org.eclipse.swt.custom.CLabel
All Implemented Interfaces:
Drawable

public class CLabel
extends Canvas

Safe: A Label which supports aligned text and/or an image and different border styles.

If there is not enough space a CLabel uses the following strategy to fit the information into the available space:

 		ignores the indent in left align mode
 		ignores the image and the gap
 		shortens the text by replacing the center portion of the label with an ellipsis
 		shortens the text by removing the center portion of the label
 

Styles:
LEFT, RIGHT, CENTER, SHADOW_IN, SHADOW_OUT, SHADOW_NONE
Events:


Field Summary
private  int align
          the alignment.
private  String appToolTipText
           
private  Image backgroundImage
           
private static String ellipsis
          a string inserted in the middle of text that has been shortened
private static int GAP
          Gap between icon and text
private  Color[] gradientColors
           
private  int[] gradientPercents
           
private  int hIndent
           
private  Image image
          the current icon
private static int INDENT
          Left and right margins
private  String text
          the current text
private  int vIndent
           
 
Fields inherited from class org.eclipse.swt.widgets.Canvas
 
Fields inherited from class org.eclipse.swt.widgets.Composite
 
Fields inherited from class org.eclipse.swt.widgets.Scrollable
 
Fields inherited from class org.eclipse.swt.widgets.Control
handle
 
Fields inherited from class org.eclipse.swt.widgets.Widget
 
Constructor Summary
CLabel(Composite parent, int style)
          Enabled: Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.
 
Method Summary
private static int checkStyle(int style)
          Check the style bits to ensure that no invalid styles are applied.
 Point computeSize(int wHint, int hHint, boolean changed)
          Enabled:
private  void drawBevelRect(GC gc, int x, int y, int w, int h, Color topleft, Color bottomright)
          Draw a rectangle in the given colors.
 int getAlignment()
          Enabled: Returns the alignment.
 Image getImage()
          Enabled: Return the CLabel's image or null.
 String getText()
          Enabled: Return the Label's text.
 String getToolTipText()
          Enabled:
private  Point getTotalSize(Image image, String text)
          Compute the minimum size.
private  void initAccessible()
           
private  void onDispose(DisposeEvent event)
           
private  void onPaint(PaintEvent event)
           
private  void paintBorder(GC gc, Rectangle r)
          Paint the Label's border.
 void setAlignment(int align)
          Enabled: Set the alignment of the CLabel.
 void setBackground(Color color)
          Enabled:
 void setBackground(Color[] colors, int[] percents)
          Enabled: Specify a gradient of colours to be drawn in the background of the CLabel.
 void setBackground(Image image)
          Enabled: Set the image to be drawn in the background of the label.
 void setFont(Font font)
          Enabled:
 void setImage(Image image)
          Enabled: Set the label's Image.
 void setText(String text)
          Enabled: Set the label's text.
 void setToolTipText(String string)
          Enabled:
protected  String shortenText(GC gc, String t, int width)
          Shorten the given text t so that its length doesn't exceed the given width.
 
Methods inherited from class org.eclipse.swt.widgets.Canvas
getCaret, scroll, setCaret
 
Methods inherited from class org.eclipse.swt.widgets.Composite
checkSubclass, getChildren, getLayout, getTabList, layout, layout, moveAbove, moveBelow, setFocus, setLayout, setTabList
 
Methods inherited from class org.eclipse.swt.widgets.Scrollable
computeTrim, getClientArea, getHorizontalBar, getVerticalBar
 
Methods inherited from class org.eclipse.swt.widgets.Control
addControlListener, addFocusListener, addHelpListener, addKeyListener, addMouseListener, addMouseMoveListener, addMouseTrackListener, addPaintListener, addTraverseListener, computeSize, forceFocus, getAccessible, getBackground, getBorderWidth, getBounds, getDisplay, getEnabled, getFont, getForeground, getLayoutData, getLocation, getMenu, getParent, getShell, getSize, getVisible, internal_dispose_GC, internal_new_GC, isDisposed, isEnabled, isFocusControl, isReparentable, isVisible, moveAbove, moveBelow, pack, pack, redraw, redraw, removeControlListener, removeFocusListener, removeHelpListener, removeKeyListener, removeMouseListener, removeMouseMoveListener, removeMouseTrackListener, removePaintListener, removeTraverseListener, setBounds, setBounds, setCapture, setCursor, setEnabled, setForeground, setLayoutData, setLocation, setLocation, setMenu, setParent, setRedraw, setSize, setSize, setVisible, toControl, toControl, toDisplay, toDisplay, transferActive, transferFocus, traverse, update
 
Methods inherited from class org.eclipse.swt.widgets.Widget
addDisposeListener, addListener, checkWidget, dispose, getData, getData, getStyle, isListening, notifyListeners, removeDisposeListener, removeListener, removeListener, setData, setData, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

GAP

private static final int GAP
Gap between icon and text


INDENT

private static final int INDENT
Left and right margins


ellipsis

private static final String ellipsis
a string inserted in the middle of text that has been shortened


align

private int align
the alignment. Either CENTER, RIGHT, LEFT. Default is LEFT


hIndent

private int hIndent

vIndent

private int vIndent

text

private String text
the current text


image

private Image image
the current icon


appToolTipText

private String appToolTipText

backgroundImage

private Image backgroundImage

gradientColors

private Color[] gradientColors

gradientPercents

private int[] gradientPercents
Constructor Detail

CLabel

public CLabel(Composite parent,
              int style)
Enabled: Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.

The style value is either one of the style constants defined in class SWT which is applicable to instances of this class, or must be built by bitwise OR'ing together (that is, using the int "|" operator) two or more of those SWT style constants. The class description lists the style constants that are applicable to the class. Style bits are also inherited from superclasses.

Parameters:
parent - a widget which will be the parent of the new instance (cannot be null)
style - the style of widget to construct
See Also:
SWT.LEFT, SWT.RIGHT, SWT.CENTER, SWT.SHADOW_IN, SWT.SHADOW_OUT, SWT.SHADOW_NONE, Widget.getStyle()
Method Detail

checkStyle

private static int checkStyle(int style)
Check the style bits to ensure that no invalid styles are applied.


computeSize

public Point computeSize(int wHint,
                         int hHint,
                         boolean changed)
Enabled:

Overrides:
computeSize in class Composite
Parameters:
wHint - the width hint (can be SWT.DEFAULT)
hHint - the height hint (can be SWT.DEFAULT)
changed - true if the control's contents have changed, and false otherwise
Returns:
the preferred size of the control.
See Also:
Layout, Control.getBorderWidth(), Control.getBounds(), Control.getSize(), Control.pack(), "computeTrim, getClientArea for controls that implement them"

drawBevelRect

private void drawBevelRect(GC gc,
                           int x,
                           int y,
                           int w,
                           int h,
                           Color topleft,
                           Color bottomright)
Draw a rectangle in the given colors.


getAlignment

public int getAlignment()
Enabled: Returns the alignment. The alignment style (LEFT, CENTER or RIGHT) is returned.

Returns:
SWT.LEFT, SWT.RIGHT or SWT.CENTER

getImage

public Image getImage()
Enabled: Return the CLabel's image or null.

Returns:
the image of the label or null

getTotalSize

private Point getTotalSize(Image image,
                           String text)
Compute the minimum size.


setToolTipText

public void setToolTipText(String string)
Enabled:

Overrides:
setToolTipText in class Control
Parameters:
string - the new tool tip text (or null)

getText

public String getText()
Enabled: Return the Label's text.

Returns:
the text of the label or null

getToolTipText

public String getToolTipText()
Enabled:

Overrides:
getToolTipText in class Control
Returns:
the receiver's tool tip text

paintBorder

private void paintBorder(GC gc,
                         Rectangle r)
Paint the Label's border.


initAccessible

private void initAccessible()

onDispose

private void onDispose(DisposeEvent event)

onPaint

private void onPaint(PaintEvent event)

setAlignment

public void setAlignment(int align)
Enabled: Set the alignment of the CLabel. Use the values LEFT, CENTER and RIGHT to align image and text within the available space.

Parameters:
align - the alignment style of LEFT, RIGHT or CENTER

setBackground

public void setBackground(Color color)
Enabled:

Overrides:
setBackground in class Control
Parameters:
color - the new color (or null)

setBackground

public void setBackground(Color[] colors,
                          int[] percents)
Enabled: Specify a gradient of colours to be drawn in the background of the CLabel.

For example, to draw a gradient that varies from dark blue to blue and then to white and stays white for the right hald of the label, use the following call to setBackground:

	clabel.setBackground(new Color[]{display.getSystemColor(SWT.COLOR_DARK_BLUE), 
		                           display.getSystemColor(SWT.COLOR_BLUE),
		                           display.getSystemColor(SWT.COLOR_WHITE), 
		                           display.getSystemColor(SWT.COLOR_WHITE)},
		               new int[] {25, 50, 100});
 

Parameters:
colors - an array of Color that specifies the colors to appear in the gradient in order of appearance from left to right; The value null clears the background gradient; the value null can be used inside the array of Color to specify the background color.
percents - an array of integers between 0 and 100 specifying the percent of the width of the widget at which the color should change; the size of the percents array must be one less than the size of the colors array.

setBackground

public void setBackground(Image image)
Enabled: Set the image to be drawn in the background of the label.

Parameters:
image - the image to be drawn in the background

setFont

public void setFont(Font font)
Enabled:

Overrides:
setFont in class Canvas
Parameters:
font - the new font (or null)

setImage

public void setImage(Image image)
Enabled: Set the label's Image. The value null clears it.

Parameters:
image - the image to be displayed in the label or null

setText

public void setText(String text)
Enabled: Set the label's text. The value null clears it.

Parameters:
text - the text to be displayed in the label or null

shortenText

protected String shortenText(GC gc,
                             String t,
                             int width)
Shorten the given text t so that its length doesn't exceed the given width. The default implementation replaces characters in the center of the original string with an ellipsis ("..."). Override if you need a different strategy.



comments?