001: package net.xoetrope.awt;
002:
003: import java.awt.Canvas;
004: import java.awt.Graphics;
005:
006: import net.xoetrope.xui.XAttributedComponent;
007: import net.xoetrope.xui.XTextHolder;
008: import net.xoetrope.xui.XTextRenderer;
009: import java.awt.Dimension;
010: import java.awt.Image;
011:
012: /**
013: * Draws text. The text may be wrapped over multiple lines. Double buffering is
014: * switched off by default.
015: * <p>Copyright (c) Xoetrope Ltd., 1998-2003<br>
016: * License: see license.txt
017: * @version $Revision: 1.16 $
018: */
019: public class XLabel extends Canvas implements XTextHolder,
020: XAttributedComponent {
021: protected String text;
022: protected XTextRenderer renderer = new XTextRenderer();
023: protected boolean doubleBuffered = false;
024: protected Image bufferImage = null;
025: protected int bufferWidth;
026: protected int bufferHeight;
027:
028: /**
029: * Reset the text
030: * @param str the new text
031: */
032: public void setText(String str) {
033: text = str;
034: repaint();
035: }
036:
037: /**
038: * Update the component
039: * @param g
040: */
041: public void update(Graphics g) {
042: paint(g);
043: }
044:
045: /**
046: * Repaint the component once it has been created
047: */
048: public void addNotify() {
049: super .addNotify();
050: repaint(0);
051: }
052:
053: /**
054: * Render the text
055: * @param g
056: */
057: public void paint(Graphics g) {
058: if (doubleBuffered) {
059: // Check the buffersize with the current panelsize
060: // or initialises the image with the first paint
061: if (bufferWidth != getSize().width
062: || bufferHeight != getSize().height
063: || bufferImage == null)
064: resetBuffer();
065:
066: Graphics bufferGraphics = bufferImage.getGraphics();
067: // Clear the offscreen image
068: bufferGraphics.clearRect(0, 0, bufferWidth, bufferHeight);
069:
070: if (text != null)
071: renderer.paintText(this , bufferGraphics, text);
072:
073: // Copy offscreen image onto the onscreen image
074: g.drawImage(bufferImage, 0, 0, this );
075:
076: // Cleanup
077: bufferGraphics.dispose();
078: bufferGraphics = null;
079: } else if (text != null)
080: renderer.paintText(this , g, text);
081: }
082:
083: /**
084: * Gets the text.
085: * @return the label text
086: */
087: public String getText() {
088: return text;
089: }
090:
091: public int getAlignment() {
092: return renderer.getAlignment();
093: }
094:
095: /**
096: * Sets the alignment of the text.
097: * @param b 1 to right align the text, 0 for left alignment and 2 for centered text
098: */
099: public void setAlignment(int align) {
100: renderer.setAlignment(align);
101: }
102:
103: /**
104: * Sets the transparency of the text.
105: * @param b true to make text transparent
106: */
107: public void setTransparent(boolean b) {
108: renderer.setTransparent(b);
109: }
110:
111: /**
112: * Set one or more attributes of the component. Attributes include <br>
113: * <OL>
114: * <LI>align (left|right|center ) or</LI>
115: * <LI>alignment (left|right|center )</LI>
116: * <LI>buffered (true|false) double buffering</LI>
117: * </OL>
118: * @param attribName the attribute name
119: * @param attribValue the attribute value
120: */
121: public void setAttribute(String attribName, String attribValue) {
122: String attribNameLwr = attribName.toLowerCase();
123: String attribValueLwr = attribValue.toLowerCase();
124: if ((attribNameLwr.compareTo("align") == 0)
125: || (attribNameLwr.compareTo("alignment") == 0)) {
126: if (attribValueLwr.compareTo("right") == 0)
127: setAlignment(XTextRenderer.RIGHT);
128: else if (attribValueLwr.compareTo("center") == 0)
129: setAlignment(XTextRenderer.CENTER);
130: else
131: setAlignment(XTextRenderer.LEFT);
132: } else if (attribNameLwr.compareTo("buffered") == 0)
133: setDoubleBuffered(attribValueLwr.equals("true"));
134: // else if ( attribNameLwr.compareTo( "opaque" ) == 0 )
135: // this.setop
136: }
137:
138: /**
139: * Gets the preferred size of this component.
140: * @return a dimension object indicating this component's preferred size
141: * @see #getMinimumSize
142: * @see LayoutManager
143: */
144: public Dimension getPreferredSize() {
145: return renderer.getPreferredSize(this , text);
146: }
147:
148: /**
149: * Toggle use of double buffering when painting this component
150: * @param buffer true to double buffer
151: */
152: public void setDoubleBuffered(boolean buffer) {
153: doubleBuffered = buffer;
154: }
155:
156: private void resetBuffer() {
157: // always keep track of the image size
158: Dimension size = getSize();
159: bufferWidth = size.width;
160: bufferHeight = size.height;
161:
162: if (bufferImage != null) {
163: bufferImage.flush();
164: bufferImage = null;
165: }
166: System.gc();
167:
168: // create the new image with the size of the panel
169: bufferImage = createImage(bufferWidth, bufferHeight);
170: }
171: }
|