001: /*
002: * Copyright 1995-1996 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: package sun.awt.motif;
026:
027: import java.awt.*;
028: import java.awt.peer.*;
029:
030: class MLabelPeer extends MComponentPeer implements LabelPeer {
031: native void create(MComponentPeer parent);
032:
033: public void initialize() {
034: Label l = (Label) target;
035: String txt;
036: int align;
037:
038: if ((txt = l.getText()) != null) {
039: setText(l.getText());
040: }
041: if ((align = l.getAlignment()) != Label.LEFT) {
042: setAlignment(align);
043: }
044: super .initialize();
045: }
046:
047: MLabelPeer(Label target) {
048: super (target);
049: }
050:
051: public Dimension getMinimumSize() {
052: FontMetrics fm = getFontMetrics(target.getFont());
053: String label = ((Label) target).getText();
054: if (label == null)
055: label = "";
056: return new Dimension(fm.stringWidth(label) + 14,
057: fm.getHeight() + 8);
058: }
059:
060: public native void setText(String label);
061:
062: public native void setAlignment(int alignment);
063:
064: /*
065: * Print the native component by rendering the Motif look ourselves.
066: */
067: public void print(Graphics g) {
068: Label l = (Label) target;
069: Dimension d = l.size();
070: Color bg = l.getBackground();
071: Color fg = l.getForeground();
072:
073: g.setColor(bg);
074: g.fillRect(1, 1, d.width - 2, d.height - 2);
075:
076: g.setColor(fg);
077: g.setFont(l.getFont());
078: FontMetrics fm = g.getFontMetrics();
079: String lbl = l.getText();
080:
081: switch (l.getAlignment()) {
082: case Label.LEFT:
083: g.drawString(lbl, 2, (d.height + fm.getMaxAscent() - fm
084: .getMaxDescent()) / 2);
085: break;
086: case Label.RIGHT:
087: g
088: .drawString(lbl, d.width
089: - (fm.stringWidth(lbl) + 2),
090: (d.height + fm.getMaxAscent() - fm
091: .getMaxDescent()) / 2);
092: break;
093: case Label.CENTER:
094: g
095: .drawString(lbl,
096: (d.width - fm.stringWidth(lbl)) / 2,
097: (d.height + fm.getMaxAscent() - fm
098: .getMaxDescent()) / 2);
099: break;
100: }
101:
102: target.print(g);
103: }
104:
105: /**
106: * DEPRECATED
107: */
108: public Dimension minimumSize() {
109: return getMinimumSize();
110: }
111:
112: }
|