com.alee.extended.label
Class WebMultiLineLabelUI

java.lang.Object
  extended by javax.swing.plaf.ComponentUI
      extended by javax.swing.plaf.LabelUI
          extended by javax.swing.plaf.basic.BasicLabelUI
              extended by com.alee.extended.label.WebMultiLineLabelUI
All Implemented Interfaces:
java.awt.event.ComponentListener, java.beans.PropertyChangeListener, java.util.EventListener

public class WebMultiLineLabelUI
extends javax.swing.plaf.basic.BasicLabelUI
implements java.awt.event.ComponentListener

Label UI delegate that supports multiple lines and line wrapping. Hard line breaks (\n) are preserved. If the dimensions of the label is too small to fit all content, the string will be clipped and "..." appended to the end of the visible text (similar to the default behavior of JLabel).

Author:
Samuel Sjoberg, http://samuelsjoberg.com

Nested Class Summary
protected static class WebMultiLineLabelUI.SegmentCache
          Static singleton Segment cache.
 
Field Summary
protected static java.awt.Rectangle paintIconR
           
protected static java.awt.Rectangle paintTextR
           
protected static java.awt.Insets paintViewInsets
           
protected static java.awt.Rectangle paintViewR
           
static java.lang.String PROPERTY_KEY
          Client property key used to store the calculated wrapped lines on the JLabel.
 
Fields inherited from class javax.swing.plaf.basic.BasicLabelUI
labelUI
 
Constructor Summary
WebMultiLineLabelUI()
           
 
Method Summary
protected  int alignmentX(javax.swing.JLabel label, java.awt.FontMetrics fm, java.lang.String s)
          Establish the horizontal text alignment.
protected  int alignmentY(javax.swing.JLabel label, java.awt.FontMetrics fm, java.awt.Rectangle bounds)
          Establish the vertical text alignment.
protected  int calculateBreakPosition(javax.swing.text.Document doc, int p0, int p1)
          Calculate the position on which to break (wrap) the line.
protected  void clearCache(javax.swing.JLabel l)
          Clear the wrapped line cache.
protected  java.lang.String clip(java.lang.String text)
          Add a clip indication to the string.
 void componentHidden(java.awt.event.ComponentEvent e)
          
 void componentMoved(java.awt.event.ComponentEvent e)
          
 void componentResized(java.awt.event.ComponentEvent e)
          
 void componentShown(java.awt.event.ComponentEvent e)
          
static javax.swing.plaf.ComponentUI createUI(javax.swing.JComponent c)
          UI instance creation.
protected  int getAvailableHeight(javax.swing.JLabel l)
          Returns the available height to paint text on.
protected  int getPreferredHeight(javax.swing.JLabel label)
          The preferred height of the label is the height of the lines with added top and bottom insets.
 java.awt.Dimension getPreferredSize(javax.swing.JComponent c)
          
 java.awt.Color getShadeColor()
           
protected  java.util.List<java.lang.String> getTextLines(javax.swing.JLabel l)
          Get the lines of text contained in the text label.
protected  void installListeners(javax.swing.JLabel c)
          
 void installUI(javax.swing.JComponent c)
           
 boolean isDrawShade()
          View settings
 void paint(java.awt.Graphics g, javax.swing.JComponent c)
          
protected  void paintDisabledText(javax.swing.JLabel l, java.awt.Graphics g, java.lang.String s, int textX, int textY)
          
protected  void paintEnabledText(javax.swing.JLabel l, java.awt.Graphics g, java.lang.String s, int textX, int textY)
          
protected  void paintTextLines(java.awt.Graphics g, javax.swing.JLabel label, java.awt.FontMetrics fm)
          Paint the wrapped text lines.
protected  java.util.List<java.lang.String> prepareLines(javax.swing.JLabel l)
          Prepare the text lines for rendering.
 void propertyChange(java.beans.PropertyChangeEvent e)
          
 void setDrawShade(boolean drawShade)
           
 void setShadeColor(java.awt.Color shadeColor)
           
protected  void uninstallDefaults(javax.swing.JLabel c)
          
protected  void uninstallListeners(javax.swing.JLabel c)
          
protected  void updateLayout(javax.swing.JLabel l, java.awt.FontMetrics fm, int width, int height)
          Calculate the paint rectangles for the icon and text for the passed label.
protected  void wrap(java.util.List<java.lang.String> lines, javax.swing.text.Element elem)
          If necessary, wrap the text into multiple lines.
 
Methods inherited from class javax.swing.plaf.basic.BasicLabelUI
getBaseline, getBaselineResizeBehavior, getMaximumSize, getMinimumSize, installComponents, installDefaults, installKeyboardActions, layoutCL, uninstallComponents, uninstallKeyboardActions, uninstallUI
 
Methods inherited from class javax.swing.plaf.ComponentUI
contains, getAccessibleChild, getAccessibleChildrenCount, update
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PROPERTY_KEY

public static final java.lang.String PROPERTY_KEY
Client property key used to store the calculated wrapped lines on the JLabel.

See Also:
Constant Field Values

paintIconR

protected static java.awt.Rectangle paintIconR

paintTextR

protected static java.awt.Rectangle paintTextR

paintViewR

protected static java.awt.Rectangle paintViewR

paintViewInsets

protected static java.awt.Insets paintViewInsets
Constructor Detail

WebMultiLineLabelUI

public WebMultiLineLabelUI()
Method Detail

createUI

public static javax.swing.plaf.ComponentUI createUI(javax.swing.JComponent c)
UI instance creation.

Parameters:
c - the component about to be installed
Returns:
the shared UI delegate instance

installUI

public void installUI(javax.swing.JComponent c)
Overrides:
installUI in class javax.swing.plaf.basic.BasicLabelUI

uninstallDefaults

protected void uninstallDefaults(javax.swing.JLabel c)

Overrides:
uninstallDefaults in class javax.swing.plaf.basic.BasicLabelUI

installListeners

protected void installListeners(javax.swing.JLabel c)

Overrides:
installListeners in class javax.swing.plaf.basic.BasicLabelUI

uninstallListeners

protected void uninstallListeners(javax.swing.JLabel c)

Overrides:
uninstallListeners in class javax.swing.plaf.basic.BasicLabelUI

isDrawShade

public boolean isDrawShade()
View settings


setDrawShade

public void setDrawShade(boolean drawShade)

getShadeColor

public java.awt.Color getShadeColor()

setShadeColor

public void setShadeColor(java.awt.Color shadeColor)

clearCache

protected void clearCache(javax.swing.JLabel l)
Clear the wrapped line cache.

Parameters:
l - the label containing a cached value

propertyChange

public void propertyChange(java.beans.PropertyChangeEvent e)

Specified by:
propertyChange in interface java.beans.PropertyChangeListener
Overrides:
propertyChange in class javax.swing.plaf.basic.BasicLabelUI

updateLayout

protected void updateLayout(javax.swing.JLabel l,
                            java.awt.FontMetrics fm,
                            int width,
                            int height)
Calculate the paint rectangles for the icon and text for the passed label.

Parameters:
l - a label
fm - the font metrics to use, or null to get the font metrics from the label
width - label width
height - label height

paint

public void paint(java.awt.Graphics g,
                  javax.swing.JComponent c)

Overrides:
paint in class javax.swing.plaf.basic.BasicLabelUI

paintTextLines

protected void paintTextLines(java.awt.Graphics g,
                              javax.swing.JLabel label,
                              java.awt.FontMetrics fm)
Paint the wrapped text lines.

Parameters:
g - graphics component to paint on
label - the label being painted
fm - font metrics for current font

paintEnabledText

protected void paintEnabledText(javax.swing.JLabel l,
                                java.awt.Graphics g,
                                java.lang.String s,
                                int textX,
                                int textY)

Overrides:
paintEnabledText in class javax.swing.plaf.basic.BasicLabelUI

paintDisabledText

protected void paintDisabledText(javax.swing.JLabel l,
                                 java.awt.Graphics g,
                                 java.lang.String s,
                                 int textX,
                                 int textY)

Overrides:
paintDisabledText in class javax.swing.plaf.basic.BasicLabelUI

getAvailableHeight

protected int getAvailableHeight(javax.swing.JLabel l)
Returns the available height to paint text on. This is the height of the passed component with insets subtracted.

Parameters:
l - a component
Returns:
the available height

clip

protected java.lang.String clip(java.lang.String text)
Add a clip indication to the string. It is important that the string length does not exceed the length or the original string.

Parameters:
text - the to be painted
Returns:
the clipped string

alignmentY

protected int alignmentY(javax.swing.JLabel label,
                         java.awt.FontMetrics fm,
                         java.awt.Rectangle bounds)
Establish the vertical text alignment. The default alignment is to center the text in the label.

Parameters:
label - the label to paint
fm - font metrics
bounds - the text bounds rectangle
Returns:
the vertical text alignment, defaults to CENTER.

alignmentX

protected int alignmentX(javax.swing.JLabel label,
                         java.awt.FontMetrics fm,
                         java.lang.String s)
Establish the horizontal text alignment. The default alignment is left aligned text.

Parameters:
label - the label to paint
fm - font metrics
s - the string to paint
Returns:
the x-coordinate to use when painting for proper alignment

getPreferredSize

public java.awt.Dimension getPreferredSize(javax.swing.JComponent c)

Overrides:
getPreferredSize in class javax.swing.plaf.basic.BasicLabelUI

getPreferredHeight

protected int getPreferredHeight(javax.swing.JLabel label)
The preferred height of the label is the height of the lines with added top and bottom insets.

Parameters:
label - the label
Returns:
the preferred height of the wrapped lines.

getTextLines

protected java.util.List<java.lang.String> getTextLines(javax.swing.JLabel l)
Get the lines of text contained in the text label. The prepared lines is cached as a client property, accessible via PROPERTY_KEY.

Parameters:
l - the label
Returns:
the text lines of the label.

componentHidden

public void componentHidden(java.awt.event.ComponentEvent e)

Specified by:
componentHidden in interface java.awt.event.ComponentListener

componentMoved

public void componentMoved(java.awt.event.ComponentEvent e)

Specified by:
componentMoved in interface java.awt.event.ComponentListener

componentResized

public void componentResized(java.awt.event.ComponentEvent e)

Specified by:
componentResized in interface java.awt.event.ComponentListener

componentShown

public void componentShown(java.awt.event.ComponentEvent e)

Specified by:
componentShown in interface java.awt.event.ComponentListener

prepareLines

protected java.util.List<java.lang.String> prepareLines(javax.swing.JLabel l)
Prepare the text lines for rendering. The lines are wrapped to fit in the current available space for text. Explicit line breaks are preserved.

Parameters:
l - the label to render
Returns:
a list of text lines to render

wrap

protected void wrap(java.util.List<java.lang.String> lines,
                    javax.swing.text.Element elem)
If necessary, wrap the text into multiple lines.

Parameters:
lines - line array in which to store the wrapped lines
elem - the document element containing the text content

calculateBreakPosition

protected int calculateBreakPosition(javax.swing.text.Document doc,
                                     int p0,
                                     int p1)
Calculate the position on which to break (wrap) the line.

Parameters:
doc - the document
p0 - start position
p1 - end position
Returns:
the actual end position, will be p1 if content does not need to wrap, otherwise it will be less than p1.