001: package org.swingml.component;
002:
003: import java.awt.*;
004: import java.awt.font.*;
005: import java.text.*;
006:
007: import javax.swing.*;
008:
009: /**
010: * @author CrossLogic
011: */
012: public class JLabelComponent extends JLabel {
013:
014: private final FontRenderContext frc = new FontRenderContext(null,
015: false, false);
016: private boolean justify;
017: private Insets margin = new Insets(5, 5, 5, 5);
018: private int maxWidth = Integer.MAX_VALUE;
019: private String text;
020:
021: public int getMaxWidth() {
022: return maxWidth;
023: }
024:
025: public Dimension getMinimumSize() {
026: return getPreferredSize();
027: }
028:
029: public Dimension getPreferredSize() {
030: return paintOrGetSize(null, getMaxWidth());
031: }
032:
033: public String getText() {
034: return text;
035: }
036:
037: public boolean isJustified() {
038: return justify;
039: }
040:
041: private void morph() {
042: revalidate();
043: repaint();
044: }
045:
046: protected void paintComponent(Graphics g) {
047: super .paintComponent(g);
048: paintOrGetSize((Graphics2D) g, getWidth());
049: }
050:
051: private Dimension paintOrGetSize(Graphics2D g, int width) {
052: Insets insets = getInsets();
053: width -= insets.left + insets.right + margin.left
054: + margin.right;
055: float w = insets.left + insets.right + margin.left
056: + margin.right;
057: float x = insets.left + margin.left, y = insets.top
058: + margin.top;
059: if (width > 0 && text != null && text.length() > 0) {
060: AttributedString as = new AttributedString(getText());
061: as.addAttribute(TextAttribute.FONT, getFont());
062: AttributedCharacterIterator aci = as.getIterator();
063: LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc);
064: float max = 0;
065: while (lbm.getPosition() < aci.getEndIndex()) {
066: TextLayout textLayout = lbm.nextLayout(width);
067: if (g != null
068: && isJustified()
069: && textLayout.getVisibleAdvance() > 0.80 * width)
070: textLayout = textLayout.getJustifiedLayout(width);
071: if (g != null)
072: textLayout.draw(g, x, y + textLayout.getAscent());
073: y += textLayout.getDescent() + textLayout.getLeading()
074: + textLayout.getAscent();
075: max = Math.max(max, textLayout.getVisibleAdvance());
076: }
077: w += max;
078: }
079: return new Dimension((int) Math.ceil(w), (int) Math.ceil(y)
080: + insets.bottom + margin.bottom);
081: }
082:
083: public void setJustified(boolean justify) {
084: boolean old = this .justify;
085: this .justify = justify;
086: firePropertyChange("justified", old, this .justify);
087: if (old != this .justify)
088: repaint();
089: }
090:
091: public void setMaxWidth(int maxWidth) {
092: if (maxWidth <= 0)
093: throw new IllegalArgumentException();
094: int old = this .maxWidth;
095: this .maxWidth = maxWidth;
096: firePropertyChange("maxWidth", old, this .maxWidth);
097: if (old != this .maxWidth)
098: morph();
099: }
100:
101: public void setText(String text) {
102: String old = this .text;
103: this .text = text;
104: firePropertyChange("text", old, this.text);
105: if ((old == null) ? text != null : !old.equals(text))
106: morph();
107: }
108: }
|