001: /*
002: * Copyright 2002-2003 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.awt.X11;
027:
028: import java.awt.*;
029: import java.awt.peer.*;
030:
031: class XLabelPeer extends XComponentPeer implements LabelPeer {
032: /**
033: * Create the label
034: */
035:
036: static final int TEXT_XPAD = 8;
037: static final int TEXT_YPAD = 6;
038: String label;
039: int alignment;
040:
041: FontMetrics cachedFontMetrics;
042: Font oldfont;
043:
044: FontMetrics getFontMetrics() {
045: if (cachedFontMetrics != null)
046: return cachedFontMetrics;
047: else
048: return getFontMetrics(getPeerFont());
049:
050: }
051:
052: void preInit(XCreateWindowParams params) {
053: super .preInit(params);
054: Label target = (Label) this .target;
055: label = target.getText();
056: if (label == null) {
057: label = "";
058: }
059: alignment = target.getAlignment();
060: }
061:
062: XLabelPeer(Label target) {
063: super (target);
064: }
065:
066: /**
067: * Minimum size.
068: */
069: public Dimension getMinimumSize() {
070: FontMetrics fm = getFontMetrics();
071: int w;
072: try {
073: w = fm.stringWidth(label);
074: } catch (NullPointerException e) {
075: w = 0;
076: }
077: return new Dimension(w + TEXT_XPAD, fm.getAscent()
078: + fm.getMaxDescent() + TEXT_YPAD);
079: }
080:
081: /**
082: * Paint the label
083: */
084: // NOTE: This method is called by privileged threads.
085: // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
086: public void paint(Graphics g) {
087: int textX = 0;
088: int textY = 0;
089: g.setColor(getPeerBackground());
090: g.fillRect(0, 0, width, height);
091:
092: Font f = getPeerFont();
093: g.setFont(f);
094: FontMetrics fm = g.getFontMetrics();
095:
096: if (cachedFontMetrics == null) {
097: cachedFontMetrics = fm;
098: } else {
099: if (oldfont != f)
100: cachedFontMetrics = fm;
101: }
102:
103: switch (alignment) {
104: case Label.LEFT:
105: textX = 2;
106: textY = (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2;
107: break;
108: case Label.RIGHT:
109: textX = width - (fm.stringWidth(label) + 2);
110: textY = (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2;
111: break;
112: case Label.CENTER:
113: textX = (width - fm.stringWidth(label)) / 2;
114: textY = (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2;
115: break;
116: }
117: if (isEnabled()) {
118: g.setColor(getPeerForeground());
119: g.drawString(label, textX, textY);
120: } else {
121: g.setColor(getPeerBackground().brighter());
122: g.drawString(label, textX, textY);
123: g.setColor(getPeerBackground().darker());
124: g.drawString(label, textX - 1, textY - 1);
125: }
126: }
127:
128: public void setText(String text) {
129: label = text;
130: if (label == null) {
131: label = "";
132: }
133: repaint();
134: }
135:
136: public void setFont(Font f) {
137: super .setFont(f);
138: target.repaint();
139: }
140:
141: public void setAlignment(int align) {
142: alignment = align;
143: repaint();
144: }
145: }
|