Runtime

org.eclipse.gmf.runtime.draw2d.ui.figures
Class WrapLabel

java.lang.Object
  extended by org.eclipse.draw2d.Figure
      extended by org.eclipse.gmf.runtime.draw2d.ui.figures.WrapLabel
All Implemented Interfaces:
IFigure, PositionConstants

public class WrapLabel
extends Figure
implements PositionConstants

An extended label that has the following extra features: 1- It is capable of showing selection and focus feedback (primary or secondary) 2- It is capable of optionally underlining the label's text 3- It is capable of wrapping the label's text at a given width with a given alignment 4- It is capable of supporting multiple label icons (temporary feature) This class was originally deriving off Draw2d's Label class but with the introduction of the auto-wrapping feature, a copy had to be made overriding was not straightforward. Hopefully, this extended version can be pushed to opensource

Code taken from Eclipse reference bugzilla #98820


Nested Class Summary
 
Nested classes/interfaces inherited from class org.eclipse.draw2d.Figure
Figure.FigureIterator, Figure.IdentitySearch
 
Nested classes/interfaces inherited from interface org.eclipse.draw2d.IFigure
IFigure.NoInsets
 
Field Summary
 
Fields inherited from class org.eclipse.draw2d.Figure
bgColor, border, bounds, fgColor, flags, font, MAX_FLAG, maxSize, minSize, NO_MANAGER, prefSize, toolTip
 
Fields inherited from interface org.eclipse.draw2d.PositionConstants
ALWAYS_LEFT, ALWAYS_RIGHT, BOTTOM, CENTER, EAST, EAST_WEST, HORIZONTAL, LEFT, LEFT_CENTER_RIGHT, MIDDLE, NONE, NORTH, NORTH_EAST, NORTH_SOUTH, NORTH_WEST, NSEW, RIGHT, SOUTH, SOUTH_EAST, SOUTH_WEST, TOP, TOP_MIDDLE_BOTTOM, VERTICAL, WEST
 
Fields inherited from interface org.eclipse.draw2d.IFigure
MAX_DIMENSION, MIN_DIMENSION, NO_INSETS
 
Constructor Summary
WrapLabel()
          Construct an empty Label.
WrapLabel(Image i)
          Construct a Label with passed Image as its icon.
WrapLabel(java.lang.String s)
          Construct a Label with passed String as its text.
WrapLabel(java.lang.String s, Image i)
          Construct a Label with passed String as text and passed Image as its icon.
 
Method Summary
protected  Dimension calculateLabelSize(Dimension txtSize)
          Calculates the size of the Label using the passed Dimension as the size of the Label's text.
protected  Dimension calculateSubStringTextSize()
          Calculates the size of the Label's text size.
protected  Dimension calculateTextSize(int wHint, int hHint)
          Calculates and returns the size of the Label's text.
protected  java.lang.String getEllipse()
          Return the ellipse string.
 Image getIcon()
          Returns the Label's icon.
 Image getIcon(int index)
          Gets the label's icon at the given index
 int getIconAlignment()
          Returns the current alignment of the Label's icon.
 Rectangle getIconBounds()
          Returns the bounds of the Label's icon.
protected  Point getIconLocation()
          Returns the location of the Label's icon relative to the Label.
protected  Dimension getIconSize(int index)
          getIconSize
 int getIconTextGap()
          Returns the gap in pixels between the Label's icon and its text.
 Dimension getMaximumSize()
           
 Dimension getMinimumSize(int w, int h)
           
protected  int getNumberofIcons()
          getIconNumber
 Dimension getPreferredSize(int wHint, int hHint)
           
 java.lang.String getSubStringText()
          Calculates the amount of the Label's current text will fit in the Label, including an elipsis "..." if truncation is required.
protected  Dimension getSubStringTextSize()
          Returns the size of the Label's current text.
 java.lang.String getText()
          Returns the text of the label.
 int getTextAlignment()
          Returns the current alignment of the Label's text.
 Rectangle getTextBounds()
          Returns the bounds of the label's text.
protected  Point getTextLocation()
          Returns the location of the label's text relative to the label.
 int getTextPlacement()
          Returns the current placement of the label's text relative to its icon.
protected  Dimension getTextSize(int wHint, int hHint)
          Returns the size of the label's complete text.
 int getTextWrapAlignment()
           
protected  Dimension getTotalIconSize()
          getTotalIconSize Calculates the total union of icon sizes
 boolean hasFocus()
           
protected  boolean hasIcons()
          Determines if there is any icons by checking if icon size is zeros.
 void invalidate()
           
 boolean isSelected()
           
 boolean isTextStrikedThrough()
           
 boolean isTextTruncated()
          Returns true if the label's text is currently truncated and is displaying an ellipsis, false otherwise.
 boolean isTextUnderlined()
           
 boolean isTextWrapped()
           
 void paintFigure(Graphics graphics)
           
 void setFocus(boolean b)
          Sets the focus state of this label
 void setIcon(Image image)
          Sets the label's icon to the passed image.
 void setIcon(Image image, int index)
          Sets the label's icon at given index
 void setIconAlignment(int align)
          Sets the icon alignment relative to the .abel's alignment to the passed value.
 void setLabelAlignment(int align)
          Sets the Label's alignment to the passed value.
 void setSelected(boolean b)
          Sets the selection state of this label
 void setText(java.lang.String s)
          Sets the label's text.
 void setTextAlignment(int align)
          Sets the text alignment of the Label relative to the label alignment.
 void setTextPlacement(int where)
          Sets the text placement of the label relative to its icon.
 void setTextStrikeThrough(boolean b)
          Sets whether the label text should be striked-through
 void setTextUnderline(boolean b)
          Sets whether the label text should be underlined
 void setTextWrap(boolean b)
          Sets whether the label text should wrap
 void setTextWrapAlignment(int i)
          Sets the wrapping width of the label text.
 void setTextWrapWidth(int i)
          Sets the wrapping width of the label text.
 
Methods inherited from class org.eclipse.draw2d.Figure
add, add, add, add, addAncestorListener, addCoordinateListener, addFigureListener, addFocusListener, addKeyListener, addLayoutListener, addListener, addMouseListener, addMouseMotionListener, addNotify, addPropertyChangeListener, addPropertyChangeListener, containsPoint, containsPoint, erase, findDescendantAtExcluding, findFigureAt, findFigureAt, findFigureAt, findFigureAtExcluding, findMouseEventTargetAt, findMouseEventTargetInDescendantsAt, fireCoordinateSystemChanged, fireFigureMoved, fireMoved, firePropertyChange, firePropertyChange, firePropertyChange, getBackgroundColor, getBorder, getBounds, getChildren, getClientArea, getClientArea, getCursor, getFlag, getFont, getForegroundColor, getInsets, getLayoutManager, getListeners, getLocalBackgroundColor, getLocalFont, getLocalForegroundColor, getLocation, getMinimumSize, getParent, getPreferredSize, getSize, getToolTip, getUpdateManager, handleFocusGained, handleFocusLost, handleKeyPressed, handleKeyReleased, handleMouseDoubleClicked, handleMouseDragged, handleMouseEntered, handleMouseExited, handleMouseHover, handleMouseMoved, handleMousePressed, handleMouseReleased, internalGetEventDispatcher, intersects, invalidateTree, isCoordinateSystem, isEnabled, isFocusTraversable, isMirrored, isMouseEventTarget, isOpaque, isRequestFocusEnabled, isShowing, isValid, isValidationRoot, isVisible, layout, paint, paintBorder, paintChildren, paintClientArea, primTranslate, remove, removeAll, removeAncestorListener, removeCoordinateListener, removeFigureListener, removeFocusListener, removeKeyListener, removeLayoutListener, removeListener, removeMouseListener, removeMouseMotionListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, revalidate, setBackgroundColor, setBorder, setBounds, setChildrenDirection, setChildrenEnabled, setChildrenOrientation, setConstraint, setCursor, setEnabled, setFlag, setFocusTraversable, setFont, setForegroundColor, setLayoutManager, setLocation, setMaximumSize, setMinimumSize, setOpaque, setParent, setPreferredSize, setPreferredSize, setRequestFocusEnabled, setSize, setSize, setToolTip, setValid, setVisible, translate, translateFromParent, translateToAbsolute, translateToParent, translateToRelative, useLocalCoordinates, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WrapLabel

public WrapLabel()
Construct an empty Label.

Since:
2.0

WrapLabel

public WrapLabel(java.lang.String s)
Construct a Label with passed String as its text.

Parameters:
s - the label text
Since:
2.0

WrapLabel

public WrapLabel(Image i)
Construct a Label with passed Image as its icon.

Parameters:
i - the label image
Since:
2.0

WrapLabel

public WrapLabel(java.lang.String s,
                 Image i)
Construct a Label with passed String as text and passed Image as its icon.

Parameters:
s - the label text
i - the label image
Since:
2.0
Method Detail

calculateLabelSize

protected Dimension calculateLabelSize(Dimension txtSize)
Calculates the size of the Label using the passed Dimension as the size of the Label's text.

Parameters:
txtSize - the precalculated size of the label's text
Returns:
the label's size
Since:
2.0

calculateSubStringTextSize

protected Dimension calculateSubStringTextSize()
Calculates the size of the Label's text size. The text size calculated takes into consideration if the Label's text is currently truncated. If text size without considering current truncation is desired, use calculateTextSize(int, int).

Returns:
the size of the label's text, taking into account truncation
Since:
2.0

calculateTextSize

protected Dimension calculateTextSize(int wHint,
                                      int hHint)
Calculates and returns the size of the Label's text. Note that this Dimension is calculated using the Label's full text, regardless of whether or not its text is currently truncated. If text size considering current truncation is desired, use calculateSubStringTextSize().

Parameters:
wHint - a width hint
hHint - a height hint
Returns:
the size of the label's text, ignoring truncation
Since:
2.0

getIcon

public Image getIcon()
Returns the Label's icon.

Returns:
the label icon
Since:
2.0

getIcon

public Image getIcon(int index)
Gets the label's icon at the given index

Parameters:
index - The icon index
Returns:
the Image that is the icon for the given index.

hasIcons

protected boolean hasIcons()
Determines if there is any icons by checking if icon size is zeros.

Returns:
true if icons are present, false otherwise

getIconAlignment

public int getIconAlignment()
Returns the current alignment of the Label's icon. The default is PositionConstants.CENTER.

Returns:
the icon alignment
Since:
2.0

getIconBounds

public Rectangle getIconBounds()
Returns the bounds of the Label's icon.

Returns:
the icon's bounds
Since:
2.0

getIconLocation

protected Point getIconLocation()
Returns the location of the Label's icon relative to the Label.

Returns:
the icon's location
Since:
2.0

getIconTextGap

public int getIconTextGap()
Returns the gap in pixels between the Label's icon and its text.

Returns:
the gap
Since:
2.0

getMinimumSize

public Dimension getMinimumSize(int w,
                                int h)
Specified by:
getMinimumSize in interface IFigure
Overrides:
getMinimumSize in class Figure
See Also:
IFigure#getMinimumSize(int, int)

getPreferredSize

public Dimension getPreferredSize(int wHint,
                                  int hHint)
Specified by:
getPreferredSize in interface IFigure
Overrides:
getPreferredSize in class Figure

getMaximumSize

public Dimension getMaximumSize()
Specified by:
getMaximumSize in interface IFigure
Overrides:
getMaximumSize in class Figure

getSubStringText

public java.lang.String getSubStringText()
Calculates the amount of the Label's current text will fit in the Label, including an elipsis "..." if truncation is required.

Returns:
the substring
Since:
2.0

getSubStringTextSize

protected Dimension getSubStringTextSize()
Returns the size of the Label's current text. If the text is currently truncated, the truncated text with its ellipsis is used to calculate the size.

Returns:
the size of this label's text, taking into account truncation
Since:
2.0

getText

public java.lang.String getText()
Returns the text of the label. Note that this is the complete text of the label, regardless of whether it is currently being truncated. Call getSubStringText()to return the label's current text contents with truncation considered.

Returns:
the complete text of this label
Since:
2.0

getTextAlignment

public int getTextAlignment()
Returns the current alignment of the Label's text. The default text alignment is PositionConstants.CENTER.

Returns:
the text alignment

getTextBounds

public Rectangle getTextBounds()
Returns the bounds of the label's text. Note that the bounds are calculated using the label's complete text regardless of whether the label's text is currently truncated.

Returns:
the bounds of this label's complete text
Since:
2.0

getTextLocation

protected Point getTextLocation()
Returns the location of the label's text relative to the label.

Returns:
the text location
Since:
2.0

getTextPlacement

public int getTextPlacement()
Returns the current placement of the label's text relative to its icon. The default text placement is PositionConstants.EAST.

Returns:
the text placement
Since:
2.0

getTextSize

protected Dimension getTextSize(int wHint,
                                int hHint)
Returns the size of the label's complete text. Note that the text used to make this calculation is the label's full text, regardless of whether the label's text is currently being truncated and is displaying an ellipsis. If the size considering current truncation is desired, call getSubStringTextSize().

Parameters:
wHint - a width hint
hHint - a height hint
Returns:
the size of this label's complete text
Since:
2.0

invalidate

public void invalidate()
Specified by:
invalidate in interface IFigure
Overrides:
invalidate in class Figure
See Also:
IFigure#invalidate()

isTextTruncated

public boolean isTextTruncated()
Returns true if the label's text is currently truncated and is displaying an ellipsis, false otherwise.

Returns:
true if the label's text is truncated
Since:
2.0

paintFigure

public void paintFigure(Graphics graphics)
Overrides:
paintFigure in class Figure
See Also:
Figure.paintFigure(org.eclipse.draw2d.Graphics)

setIcon

public void setIcon(Image image)
Sets the label's icon to the passed image.

Parameters:
image - the new label image
Since:
2.0

setIcon

public void setIcon(Image image,
                    int index)
Sets the label's icon at given index

Parameters:
image - The icon image or null to remove the icon
index - The icon index

setIconAlignment

public void setIconAlignment(int align)
Sets the icon alignment relative to the .abel's alignment to the passed value. The default is PositionConstants.CENTER. Other possible values are PositionConstants.TOP, PositionConstants.BOTTOM,PositionConstants.LEFTand PositionConstants.RIGHT.

Parameters:
align - the icon alignment
Since:
2.0

getIconSize

protected Dimension getIconSize(int index)
getIconSize

Parameters:
index - of icon to retrieve size of.
Returns:
Dimension representing the icon size.

getNumberofIcons

protected int getNumberofIcons()
getIconNumber

Returns:
int number of icons in the wrap label

getTotalIconSize

protected Dimension getTotalIconSize()
getTotalIconSize Calculates the total union of icon sizes

Returns:
Dimension that is the union of icon sizes

setLabelAlignment

public void setLabelAlignment(int align)
Sets the Label's alignment to the passed value. The default is PositionConstants.CENTER. Other possible values are PositionConstants.TOP,PositionConstants.BOTTOM, PositionConstants.LEFTand PositionConstants.RIGHT.

Parameters:
align - label alignment

getEllipse

protected java.lang.String getEllipse()
Return the ellipse string.

Returns:
the String that represents the fact that the text has been truncated and that more text is available but hidden. Usually this is represented by "...".

setText

public void setText(java.lang.String s)
Sets the label's text.

Parameters:
s - the new label text
Since:
2.0

setTextAlignment

public void setTextAlignment(int align)
Sets the text alignment of the Label relative to the label alignment. The default is PositionConstants.CENTER. Other possible values are PositionConstants.TOP,PositionConstants.BOTTOM, PositionConstants.LEFTand PositionConstants.RIGHT.

Parameters:
align - the text alignment
Since:
2.0

setTextPlacement

public void setTextPlacement(int where)
Sets the text placement of the label relative to its icon. The default is PositionConstants.EAST. Other possible values are PositionConstants.NORTH,PositionConstants.SOUTHand PositionConstants.WEST.

Parameters:
where - the text placement
Since:
2.0

setTextUnderline

public void setTextUnderline(boolean b)
Sets whether the label text should be underlined

Parameters:
b - Wether the label text should be underlined

isTextUnderlined

public boolean isTextUnderlined()
Returns:
whether the label text is underlined

setTextStrikeThrough

public void setTextStrikeThrough(boolean b)
Sets whether the label text should be striked-through

Parameters:
b - Wether the label text should be stricked-through

isTextStrikedThrough

public boolean isTextStrikedThrough()
Returns:
wether the label text is stricked-through

setTextWrap

public void setTextWrap(boolean b)
Sets whether the label text should wrap

Parameters:
b - whether the label text should wrap

isTextWrapped

public boolean isTextWrapped()
Returns:
wether the label text wrap is on

setTextWrapWidth

public void setTextWrapWidth(int i)
Sets the wrapping width of the label text. This is only valid if text wrapping is turned on

Parameters:
i - The label text wrapping width

setTextWrapAlignment

public void setTextWrapAlignment(int i)
Sets the wrapping width of the label text. This is only valid if text wrapping is turned on

Parameters:
i - The label text wrapping width

getTextWrapAlignment

public int getTextWrapAlignment()
Returns:
the label text wrapping width

setSelected

public void setSelected(boolean b)
Sets the selection state of this label

Parameters:
b - true will cause the label to appear selected

isSelected

public boolean isSelected()
Returns:
the selection state of this label

setFocus

public void setFocus(boolean b)
Sets the focus state of this label

Parameters:
b - true will cause a focus rectangle to be drawn around the text of the Label

hasFocus

public boolean hasFocus()
Specified by:
hasFocus in interface IFigure
Overrides:
hasFocus in class Figure
Returns:
the focus state of this label

Runtime

Guidelines for using Eclipse APIs.

Copyright (c) IBM Corp., Borland Software Corp., and others 2005,2006. All rights reserved.