001: /*
002: * @(#)GLabelPeer.java 1.15 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package sun.awt.gtk;
029:
030: import java.awt.*;
031: import sun.awt.peer.*;
032:
033: /**
034: * The peer for label. We don't use GtkLabel widgets from this peer
035: * as they don't support the alignment of text. Instead we simply
036: * override the paint method and draw the label in Java.
037: *
038: * @author Nicholas Allen
039: */
040:
041: public class GLabelPeer extends GComponentPeer implements LabelPeer {
042: /** Creates a new GLabelPeer. */
043:
044: public GLabelPeer(GToolkit toolkit, Label target) {
045: super (toolkit, target);
046: }
047:
048: protected native void create();
049:
050: public Dimension getPreferredSize() {
051: FontMetrics fm = getFontMetrics(target.getFont());
052: String label = ((Label) target).getText();
053: if (label == null)
054: label = "";
055: return new Dimension(fm.stringWidth(label) + 14,
056: fm.getHeight() + 8);
057: }
058:
059: public Dimension getMinimumSize() {
060: return getPreferredSize();
061: }
062:
063: private void paintLabel(Graphics g) {
064: Label label = (Label) target;
065: Dimension size = target.getSize();
066: int alignment = label.getAlignment();
067: Font font = label.getFont();
068: FontMetrics fm = toolkit.getFontMetrics(font);
069: String text = label.getText();
070: int x;
071: Color background = target.getBackground();
072: if (background != null) {
073: g.setColor(background);
074: g.fillRect(0, 0, size.width, size.height);
075: }
076: /* 4723775 - ensure string is not null */
077: if (text == null) {
078: return;
079: }
080: g.setColor(target.getForeground());
081: g.setFont(font);
082: if (alignment == Label.LEFT)
083: x = 0;
084: else {
085: int textWidth = fm.stringWidth(text);
086: x = size.width - textWidth;
087: if (alignment == Label.CENTER)
088: x /= 2;
089: }
090: g.drawString(text, x, (size.height - fm.getHeight()) / 2
091: + fm.getAscent());
092: }
093:
094: public void paint(Graphics g) {
095: paintLabel(g);
096: target.paint(g);
097: }
098:
099: public void update(Graphics g) {
100: paintLabel(g);
101: target.update(g);
102: }
103:
104: public void setBackground(Color c) {
105: target.repaint();
106: }
107:
108: public void setForeground(Color c) {
109: target.repaint();
110: }
111:
112: public void setFont(Font f) {
113: target.repaint();
114: }
115:
116: public void setText(String text) {
117: target.repaint();
118: }
119:
120: public void setAlignment(int alignment) {
121: target.repaint();
122: }
123: }
|