com.izforge.izpack.util
Class MultiLineLabel

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by javax.swing.JComponent
              extended by com.izforge.izpack.util.MultiLineLabel
All Implemented Interfaces:
java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable

public class MultiLineLabel
extends javax.swing.JComponent


MultiLineLabel may be used in place of javax.swing.JLabel.

This class implements a component that is capable of displaying multiple lines of text. Line breaks are inserted automatically whenever a line of text extends beyond the predefined maximum line length. Line breaks will only be inserted between words, except where a single word is longer than the maximum line length. Line breaks may be forced at any location in the text by inserting a newline (\n). White space that is not valuable (i.e. is placed at the beginning of a new line or at the very beginning or end of the text) is removed.

Note: you can set the maximum width of the label either through one of the constructors or you can call setMaxWidth() explicitly. If this is not set, MultiLineLabel will derive its width from the parent component.

Version:
1.0 / 04-13-02
Author:
Elmar Grom
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JComponent
javax.swing.JComponent.AccessibleJComponent
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
protected  int alignment
           
static int CENTER
           
static int DEFAULT_ALIGN
           
static int DEFAULT_MARGIN
           
private static int DONE
           
private static int FOUND
           
protected  java.lang.String labelText
           
static int LEAST_ALLOWED
           
static int LEFT
           
protected  java.util.Vector<java.lang.String> line
           
protected  int lineAscent
           
protected  int lineDescent
           
protected  int lineHeight
           
protected  int[] lineWidth
           
protected  int marginHeight
           
protected  int marginWidth
           
private  int maxAllowed
           
private  boolean maxAllowedSet
           
protected  int maxWidth
           
private static char NEW_LINE
           
private static int NOT_DONE
           
private static int NOT_FOUND
           
protected  int numLines
           
static int RIGHT
           
private static long serialVersionUID
           
private static char[] SPACES
           
private static char[] WHITE_SPACE
           
 
Fields inherited from class javax.swing.JComponent
accessibleContext, listenerList, TOOL_TIP_TEXT_KEY, ui, UNDEFINED_CONDITION, WHEN_ANCESTOR_OF_FOCUSED_COMPONENT, WHEN_FOCUSED, WHEN_IN_FOCUSED_WINDOW
 
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
MultiLineLabel(java.lang.String label)
          Constructor using default max-width, alignment, and margin.
MultiLineLabel(java.lang.String label, int alignment)
          Constructor using default max-width, and margin.
MultiLineLabel(java.lang.String label, int marginWidth, int marginHeight)
          Constructor using default max-width and alignment.
MultiLineLabel(java.lang.String text, int horMargin, int vertMargin, int maxWidth, int justify)
          Constructor
 
Method Summary
 void addNotify()
          This method is called by the system after this object is first created.
(package private)  int breakWord(java.lang.String word, java.awt.FontMetrics fm)
          This method scans the input string until the max allowed width is reached.
private  void divideLabel()
          This method breaks the label text up into multiple lines of text.
 int getAlignment()
          This method may be used to retrieve the text alignment for the label
 int getMarginHeight()
          This method may be used to retrieve the vertical margin for the label
 int getMarginWidth()
          This method may be used to retrieve the horizontal margin for the label
 java.awt.Dimension getMinimumSize()
          This method is typically used by the layout manager, it reports the absolute minimum space required to display the entire label.
(package private)  int getPosition(java.lang.String target, int start, char[] source, int mode)
          This method searches the target string for occurences of any of the characters in the source string.
 java.awt.Dimension getPreferredSize()
          This method is typically used by the layout manager, it reports the necessary space to display the label comfortably.
protected  void measure()
          This method finds the font size, each line width and the widest line.
 void paint(java.awt.Graphics graphics)
          This method draws the label.
 void setBounds(int x, int y, int width, int height)
          Moves and resizes this component.
 void setColor(java.awt.Color color)
          This method may be used to set the color in which the text should be drawn
 void setFont(java.awt.Font font)
          This method may be used to set the font that should be used to draw the label
 void setJustify(int alignment)
          This method may be used to set the text alignment for the label
 void setMarginHeight(int margin)
          This method may be used to set the vertical margin for the label
 void setMarginWidth(int margin)
          This method may be used to set the horizontal margin
 void setMaxWidth(int width)
          This method may be used to set the max allowed line width
 void setText(java.lang.String labelText)
          This method may be used to set the label text
 
Methods inherited from class javax.swing.JComponent
addAncestorListener, addVetoableChangeListener, computeVisibleRect, contains, createToolTip, disable, enable, firePropertyChange, firePropertyChange, firePropertyChange, fireVetoableChange, getAccessibleContext, getActionForKeyStroke, getActionMap, getAlignmentX, getAlignmentY, getAncestorListeners, getAutoscrolls, getBorder, getBounds, getClientProperty, getComponentGraphics, getComponentPopupMenu, getConditionForKeyStroke, getDebugGraphicsOptions, getDefaultLocale, getFontMetrics, getGraphics, getHeight, getInheritsPopupMenu, getInputMap, getInputMap, getInputVerifier, getInsets, getInsets, getListeners, getLocation, getMaximumSize, getNextFocusableComponent, getPopupLocation, getRegisteredKeyStrokes, getRootPane, getSize, getToolTipLocation, getToolTipText, getToolTipText, getTopLevelAncestor, getTransferHandler, getUIClassID, getVerifyInputWhenFocusTarget, getVetoableChangeListeners, getVisibleRect, getWidth, getX, getY, grabFocus, isDoubleBuffered, isLightweightComponent, isManagingFocus, isOpaque, isOptimizedDrawingEnabled, isPaintingTile, isRequestFocusEnabled, isValidateRoot, paintBorder, paintChildren, paintComponent, paintImmediately, paintImmediately, paramString, print, printAll, printBorder, printChildren, printComponent, processComponentKeyEvent, processKeyBinding, processKeyEvent, processMouseEvent, processMouseMotionEvent, putClientProperty, registerKeyboardAction, registerKeyboardAction, removeAncestorListener, removeNotify, removeVetoableChangeListener, repaint, repaint, requestDefaultFocus, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resetKeyboardActions, reshape, revalidate, scrollRectToVisible, setActionMap, setAlignmentX, setAlignmentY, setAutoscrolls, setBackground, setBorder, setComponentPopupMenu, setDebugGraphicsOptions, setDefaultLocale, setDoubleBuffered, setEnabled, setFocusTraversalKeys, setForeground, setInheritsPopupMenu, setInputMap, setInputVerifier, setMaximumSize, setMinimumSize, setNextFocusableComponent, setOpaque, setPreferredSize, setRequestFocusEnabled, setToolTipText, setTransferHandler, setUI, setVerifyInputWhenFocusTarget, setVisible, unregisterKeyboardAction, update, updateUI
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, addImpl, addPropertyChangeListener, addPropertyChangeListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalKeys, getFocusTraversalPolicy, getLayout, getMousePosition, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, printComponents, processContainerEvent, processEvent, remove, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusCycleRoot, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setLayout, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, createImage, createImage, createVolatileImage, createVolatileImage, disableEvents, dispatchEvent, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusCycleRootAncestor, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getForeground, getGraphicsConfiguration, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputContext, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocale, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getToolkit, getTreeLock, gotFocus, handleEvent, hasFocus, hide, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isPreferredSizeSet, isShowing, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, postEvent, prepareImage, prepareImage, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processMouseWheelEvent, remove, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, resize, resize, setBounds, setComponentOrientation, setCursor, setDropTarget, setFocusable, setFocusTraversalKeysEnabled, setIgnoreRepaint, setLocale, setLocation, setLocation, setName, setSize, setSize, show, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

LEFT

public static final int LEFT
See Also:
Constant Field Values

CENTER

public static final int CENTER
See Also:
Constant Field Values

RIGHT

public static final int RIGHT
See Also:
Constant Field Values

DEFAULT_MARGIN

public static final int DEFAULT_MARGIN
See Also:
Constant Field Values

DEFAULT_ALIGN

public static final int DEFAULT_ALIGN
See Also:
Constant Field Values

LEAST_ALLOWED

public static final int LEAST_ALLOWED
See Also:
Constant Field Values

FOUND

private static final int FOUND
See Also:
Constant Field Values

NOT_FOUND

private static final int NOT_FOUND
See Also:
Constant Field Values

NOT_DONE

private static final int NOT_DONE
See Also:
Constant Field Values

DONE

private static final int DONE
See Also:
Constant Field Values

WHITE_SPACE

private static final char[] WHITE_SPACE

SPACES

private static final char[] SPACES

NEW_LINE

private static final char NEW_LINE
See Also:
Constant Field Values

line

protected java.util.Vector<java.lang.String> line

labelText

protected java.lang.String labelText

numLines

protected int numLines

marginHeight

protected int marginHeight

marginWidth

protected int marginWidth

lineHeight

protected int lineHeight

lineAscent

protected int lineAscent

lineDescent

protected int lineDescent

lineWidth

protected int[] lineWidth

maxWidth

protected int maxWidth

maxAllowed

private int maxAllowed

maxAllowedSet

private boolean maxAllowedSet

alignment

protected int alignment
Constructor Detail

MultiLineLabel

public MultiLineLabel(java.lang.String text,
                      int horMargin,
                      int vertMargin,
                      int maxWidth,
                      int justify)
Constructor

Parameters:
text - the text to be displayed
horMargin - the horizontal margin for the label
vertMargin - the vertical margin for the label
maxWidth - the maximum allowed width of the text
justify - the text alignment for the label

MultiLineLabel

public MultiLineLabel(java.lang.String label,
                      int marginWidth,
                      int marginHeight)
Constructor using default max-width and alignment.

Parameters:
label - the text to be displayed
marginWidth - the horizontal margin for the label
marginHeight - the vertical margin for the label

MultiLineLabel

public MultiLineLabel(java.lang.String label,
                      int alignment)
Constructor using default max-width, and margin.

Parameters:
label - the text to be displayed
alignment - the text alignment for the label

MultiLineLabel

public MultiLineLabel(java.lang.String label)
Constructor using default max-width, alignment, and margin.

Parameters:
label - the text to be displayed
Method Detail

getPosition

int getPosition(java.lang.String target,
                int start,
                char[] source,
                int mode)
This method searches the target string for occurences of any of the characters in the source string. The return value is the position of the first hit. Based on the mode parameter the hit position is either the position where any of the source characters first was found or the first position where none of the source characters where found.

Parameters:
target - the text to be searched
start - the start position for the search
source - the list of characters to be searched for
mode - the search mode FOUND = reports first found NOT_FOUND = reports first not found
Returns:
position of the first occurence

breakWord

int breakWord(java.lang.String word,
              java.awt.FontMetrics fm)
This method scans the input string until the max allowed width is reached. The return value indicates the position just before this happens.

Parameters:
word - word to break
Returns:
position character position just before the string is too long

divideLabel

private void divideLabel()
This method breaks the label text up into multiple lines of text. Line breaks are established based on the maximum available space. A new line is started whenever a line break is encountered, even if the permissible length is not yet reached. Words are broken only if a single word happens to be longer than one line.


measure

protected void measure()
This method finds the font size, each line width and the widest line.


paint

public void paint(java.awt.Graphics graphics)
This method draws the label.

Overrides:
paint in class javax.swing.JComponent
Parameters:
graphics - the device context

setText

public void setText(java.lang.String labelText)
This method may be used to set the label text

Parameters:
labelText - the text to be displayed

setFont

public void setFont(java.awt.Font font)
This method may be used to set the font that should be used to draw the label

Overrides:
setFont in class javax.swing.JComponent
Parameters:
font - font to be used within the label

setColor

public void setColor(java.awt.Color color)
This method may be used to set the color in which the text should be drawn

Parameters:
color - the text color

setJustify

public void setJustify(int alignment)
This method may be used to set the text alignment for the label

Parameters:
alignment - the alignment, possible values are LEFT, CENTER, RIGHT

setMaxWidth

public void setMaxWidth(int width)
This method may be used to set the max allowed line width

Parameters:
width - the max allowed line width in pixels

setMarginWidth

public void setMarginWidth(int margin)
This method may be used to set the horizontal margin

Parameters:
margin - the margin to the left and to the right of the label

setMarginHeight

public void setMarginHeight(int margin)
This method may be used to set the vertical margin for the label

Parameters:
margin - the margin on the top and bottom of the label

setBounds

public void setBounds(int x,
                      int y,
                      int width,
                      int height)
Moves and resizes this component. The new location of the top-left corner is specified by x and y, and the new size is specified by width and height.

Overrides:
setBounds in class java.awt.Component
Parameters:
x - The new x-coordinate of this component.
y - The new y-coordinate of this component.
width - The new width of this component.
height - The new height of this component.

getAlignment

public int getAlignment()
This method may be used to retrieve the text alignment for the label

Returns:
alignment the text alignment currently in use for the label

getMarginWidth

public int getMarginWidth()
This method may be used to retrieve the horizontal margin for the label

Returns:
marginWidth the margin currently in use to the left and right of the label

getMarginHeight

public int getMarginHeight()
This method may be used to retrieve the vertical margin for the label

Returns:
marginHeight the margin currently in use on the top and bottom of the label

getPreferredSize

public java.awt.Dimension getPreferredSize()
This method is typically used by the layout manager, it reports the necessary space to display the label comfortably.

Overrides:
getPreferredSize in class javax.swing.JComponent

getMinimumSize

public java.awt.Dimension getMinimumSize()
This method is typically used by the layout manager, it reports the absolute minimum space required to display the entire label.

Overrides:
getMinimumSize in class javax.swing.JComponent

addNotify

public void addNotify()
This method is called by the system after this object is first created.

Overrides:
addNotify in class javax.swing.JComponent