fi.hut.tml.xsmiles.gui.components.awt
Class XWrappingLabel

java.lang.Object
  extended byjava.awt.Component
      extended byjava.awt.Canvas
          extended byfi.hut.tml.xsmiles.gui.components.awt.XWrappingLabel
All Implemented Interfaces:
javax.accessibility.Accessible, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable

public class XWrappingLabel
extends java.awt.Canvas

This class is similar to Symantec WrappingLabel class (a lot of copy-paste here). The class displays a long text string in several lines.

Version:
$Revision: 4936 $
Author:
Aki Teppo
See Also:
Serialized Form

Nested Class Summary
 
Nested classes inherited from class java.awt.Canvas
java.awt.Canvas.AccessibleAWTCanvas
 
Nested classes inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
protected  int align
           
protected  int baseline
           
protected  java.awt.FontMetrics fm
           
protected  java.lang.String text
           
protected  java.util.Enumeration textEnumeration
           
protected  java.util.Vector textVector
           
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
XWrappingLabel()
          Constructs a default wrapping label.
XWrappingLabel(java.util.Enumeration e)
           
XWrappingLabel(java.lang.String s)
          Constructs a wrapping label that displays the specified string.
XWrappingLabel(java.lang.String s, int a)
          Constructs wrapping label with the specified text and alignment.
XWrappingLabel(java.util.Vector v)
           
 
Method Summary
 int calculateHeight(int width)
          Calculates the needed height for current text.
 void createSize()
          This is a method that calculates and sets the size of the label.
protected  void drawAlignedString(java.awt.Graphics g, java.lang.String s, int x, int y, int width)
          This helper method draws a string aligned the requested way.
 void paint(java.awt.Graphics g)
          Paints this component using the given graphics context.
 void setAlignStyle(int newAlignStyle)
          Sets the text alignment.
 void setBounds(int x, int y, int width, int height)
          Moves and/or resizes this component.
 void setSize(int width, int height)
          Resizes this component.
 void setText(java.lang.String newText)
          Sets the label text.
 
Methods inherited from class java.awt.Canvas
addNotify, createBufferStrategy, createBufferStrategy, getAccessibleContext, getBufferStrategy, update
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, deliverEvent, disable, disableEvents, dispatchEvent, doLayout, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, getAlignmentX, getAlignmentY, getBackground, getBounds, getBounds, getColorModel, getComponentAt, getComponentAt, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeys, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getGraphics, getGraphicsConfiguration, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getListeners, getLocale, getLocation, getLocation, getLocationOnScreen, getMaximumSize, getMinimumSize, getMouseListeners, getMouseMotionListeners, getMouseWheelListeners, getName, getParent, getPeer, getPreferredSize, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getToolkit, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, invalidate, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusCycleRoot, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isOpaque, isShowing, isValid, isVisible, keyDown, keyUp, layout, list, list, list, list, list, locate, location, lostFocus, minimumSize, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, paramString, postEvent, preferredSize, prepareImage, prepareImage, print, printAll, processComponentEvent, processEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removeNotify, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, reshape, resize, resize, setBackground, setBounds, setComponentOrientation, setCursor, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeys, setFocusTraversalKeysEnabled, setFont, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setVisible, show, show, size, toString, transferFocus, transferFocusBackward, transferFocusUpCycle, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

textEnumeration

protected java.util.Enumeration textEnumeration

textVector

protected java.util.Vector textVector

text

protected java.lang.String text

fm

protected transient java.awt.FontMetrics fm

align

protected int align

baseline

protected int baseline
Constructor Detail

XWrappingLabel

public XWrappingLabel()
Constructs a default wrapping label. Default value is an empty string.


XWrappingLabel

public XWrappingLabel(java.util.Enumeration e)

XWrappingLabel

public XWrappingLabel(java.util.Vector v)

XWrappingLabel

public XWrappingLabel(java.lang.String s)
Constructs a wrapping label that displays the specified string.

Parameters:
s - string to be displayed in label

XWrappingLabel

public XWrappingLabel(java.lang.String s,
                      int a)
Constructs wrapping label with the specified text and alignment.

Parameters:
s - the string to be displayed in label
a - the alignment, one of ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT
Method Detail

setText

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

Parameters:
newText - the new label text

setAlignStyle

public void setAlignStyle(int newAlignStyle)
Sets the text alignment.

Parameters:
newAlignStyle - the new alignment style, one of ALIGN_LEFT, ALIGN_CENTERED, or ALIGN_RIGHT

setBounds

public void setBounds(int x,
                      int y,
                      int width,
                      int height)
Moves and/or resizes this component. This is a standard Java AWT method which gets called to move and/or resize this component. Components that are in containers with layout managers should not call this method, but rely on the layout manager instead. It is overridden here to re-wrap the text as necessary.

Parameters:
x - horizontal position in the parent's coordinate space
y - vertical position in the parent's coordinate space
width - the new width
height - the new height

setSize

public void setSize(int width,
                    int height)
Resizes this component. This is a standard Java AWT method which gets called to resize this component. Components that are in containers with layout managers should not call this method, but rely on the layout manager instead. It is overridden here to re-wrap the text as necessary.

Parameters:
width - the new width, in pixels
height - the new height, in pixels

createSize

public void createSize()
This is a method that calculates and sets the size of the label. It uses the fontmetrics to find the lenght of the longest string and the height of the entire text.


paint

public void paint(java.awt.Graphics g)
Paints this component using the given graphics context. This is a standard Java AWT method which typically gets called by the AWT to handle painting this component. It paints this component using the given graphics context. The graphics context clipping region is set to the bounding rectangle of this component and its [0,0] coordinate is this component's top-left corner.

Parameters:
g - the graphics context used for painting
See Also:
Component.repaint(), Component.update(java.awt.Graphics)

calculateHeight

public int calculateHeight(int width)
Calculates the needed height for current text.

Parameters:
width - the width against which the heigh must be calculated
Returns:
needed height for current text

drawAlignedString

protected void drawAlignedString(java.awt.Graphics g,
                                 java.lang.String s,
                                 int x,
                                 int y,
                                 int width)
This helper method draws a string aligned the requested way.

Parameters:
g - the graphics context used for painting
s - the string to draw
x - the point to start drawing from, x coordinate
y - the point to start drawing from, y coordinate
width - the width of the area to draw in, in pixels


X-Smiles 1.2