001: package net.xoetrope.swing;
002:
003: import java.awt.Graphics;
004: import javax.swing.JComponent;
005:
006: import net.xoetrope.xui.XAttributedComponent;
007: import net.xoetrope.xui.XTextHolder;
008: import net.xoetrope.xui.XTextRenderer;
009: import java.awt.Rectangle;
010: import java.awt.Dimension;
011:
012: /**
013: * Draws text. The text may be wrapped over multiple lines.
014: * <p>Copyright (c) Xoetrope Ltd., 1998-2004<br>
015: * License: see license.txt
016: * @version 1.0
017: */
018: public class XLabel extends JComponent implements XTextHolder,
019: XAttributedComponent {
020: protected String text;
021: protected XTextRenderer renderer = new XTextRenderer();
022:
023: public void XLabel() {
024: setOpaque(true);
025: }
026:
027: /**
028: * Reset the text
029: * @param str the new text
030: */
031: public void setText(String str) {
032: text = str;
033: repaint();
034: }
035:
036: /**
037: * Update the component
038: * @param g
039: */
040: public void update(Graphics g) {
041: paint(g);
042: }
043:
044: /**
045: * Repaint the component once it has been created
046: */
047: public void addNotify() {
048: super .addNotify();
049: repaint(0);
050: }
051:
052: /**
053: * Render the text
054: * @param g
055: */
056: public void paintComponent(Graphics g) {
057: renderer.setTransparent(!isOpaque());
058: renderer.paintText(this , g, text);
059: }
060:
061: /**
062: * Gets the text.
063: * @return the label text
064: */
065: public String getText() {
066: return text;
067: }
068:
069: public int getAlignment() {
070: return renderer.getAlignment();
071: }
072:
073: /**
074: * Set one or more attributes of the component. Attributes include <br>
075: * align (left|right|center ) or<br>
076: * alignment (left|right|center )<br>
077: * buffered (true|false) double buffering<br>
078: * @param b 1 to right align the text, 0 for left alignment and 2 for centered text
079: */
080: public void setAlignment(int align) {
081: renderer.setAlignment(align);
082: }
083:
084: /**
085: * Sets the transparency of the text.
086: * @param b true to make text transparent
087: */
088: public void setTransparent(boolean b) {
089: renderer.setTransparent(b);
090: }
091:
092: /**
093: * Set one or more attributes of the component.
094: * <OL>
095: * <LI>align (left|right|center ) or</LI>
096: * <LI>alignment (left|right|center )</LI>
097: * <LI>buffered (true|false) double buffering</LI>
098: * </OL>
099: * @param attribName the attribute name
100: * @param attribValue the attribute value
101: */
102: public void setAttribute(String attribName, String attribValue) {
103: String attribNameLwr = attribName.toLowerCase();
104: String attribValueLwr = attribValue.toLowerCase();
105: if ((attribNameLwr.compareTo("align") == 0)
106: || (attribNameLwr.compareTo("alignment") == 0)) {
107: if (attribValueLwr.compareTo("right") == 0)
108: setAlignment(XTextRenderer.RIGHT);
109: else if (attribValueLwr.compareTo("center") == 0)
110: setAlignment(XTextRenderer.CENTER);
111: else
112: setAlignment(XTextRenderer.LEFT);
113: } else if (attribNameLwr.compareTo("buffered") == 0)
114: setDoubleBuffered(attribValueLwr.equals("true"));
115: else if (attribNameLwr.compareTo("opaque") == 0)
116: setOpaque(attribValue.compareTo("true") == 0 ? true : false);
117: }
118:
119: /**
120: * Gets the preferred size of this component.
121: * @return a dimension object indicating this component's preferred size
122: * @see #getMinimumSize
123: * @see LayoutManager
124: */
125: public Dimension getPreferredSize() {
126: return renderer.getPreferredSize(this, text);
127: }
128: }
|