0N/A/*
2362N/A * Copyright (c) 2002, 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.awt.X11;
0N/A
0N/Aimport java.awt.*;
0N/Aimport java.awt.peer.*;
0N/A
0N/Aclass XLabelPeer extends XComponentPeer implements LabelPeer {
0N/A /**
0N/A * Create the label
0N/A */
0N/A
0N/A static final int TEXT_XPAD = 8;
0N/A static final int TEXT_YPAD = 6;
0N/A String label;
0N/A int alignment;
0N/A
0N/A FontMetrics cachedFontMetrics;
0N/A Font oldfont;
0N/A
0N/A FontMetrics getFontMetrics()
0N/A {
0N/A if (cachedFontMetrics != null)
0N/A return cachedFontMetrics;
0N/A else return getFontMetrics(getPeerFont());
0N/A
0N/A }
0N/A
0N/A void preInit(XCreateWindowParams params) {
0N/A super.preInit(params);
0N/A Label target = (Label) this.target;
0N/A label = target.getText();
0N/A if (label == null) {
0N/A label = "";
0N/A }
0N/A alignment = target.getAlignment();
0N/A }
0N/A
0N/A XLabelPeer(Label target) {
0N/A super(target);
0N/A }
0N/A
0N/A /**
0N/A * Minimum size.
0N/A */
0N/A public Dimension getMinimumSize() {
0N/A FontMetrics fm = getFontMetrics();
0N/A int w;
0N/A try {
0N/A w = fm.stringWidth(label);
0N/A }
0N/A catch (NullPointerException e) {
0N/A w = 0;
0N/A }
0N/A return new Dimension(w + TEXT_XPAD,
0N/A fm.getAscent() + fm.getMaxDescent() + TEXT_YPAD);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Paint the label
0N/A */
0N/A // NOTE: This method is called by privileged threads.
0N/A // DO NOT INVOKE CLIENT CODE ON THIS THREAD!
0N/A public void paint(Graphics g) {
0N/A int textX = 0;
0N/A int textY = 0;
0N/A g.setColor(getPeerBackground());
0N/A g.fillRect(0, 0, width, height);
0N/A
0N/A Font f = getPeerFont();
0N/A g.setFont(f);
0N/A FontMetrics fm = g.getFontMetrics();
0N/A
0N/A if (cachedFontMetrics == null)
0N/A {
0N/A cachedFontMetrics = fm;
0N/A }
0N/A else
0N/A {
0N/A if (oldfont != f)
0N/A cachedFontMetrics = fm;
0N/A }
0N/A
0N/A switch (alignment) {
0N/A case Label.LEFT:
0N/A textX = 2;
0N/A textY = (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2;
0N/A break;
0N/A case Label.RIGHT:
0N/A textX = width - (fm.stringWidth(label) + 2);
0N/A textY = (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2;
0N/A break;
0N/A case Label.CENTER:
0N/A textX = (width - fm.stringWidth(label)) / 2;
0N/A textY = (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2;
0N/A break;
0N/A }
0N/A if (isEnabled()) {
0N/A g.setColor(getPeerForeground());
0N/A g.drawString(label, textX, textY);
0N/A }
0N/A else {
0N/A g.setColor(getPeerBackground().brighter());
0N/A g.drawString(label, textX, textY);
0N/A g.setColor(getPeerBackground().darker());
0N/A g.drawString(label, textX - 1, textY - 1);
0N/A }
0N/A }
0N/A
0N/A public void setText(String text) {
0N/A label = text;
0N/A if (label == null) {
0N/A label = "";
0N/A }
0N/A repaint();
0N/A }
0N/A public void setFont(Font f) {
0N/A super.setFont(f);
5302N/A repaint();
0N/A }
0N/A
0N/A public void setAlignment(int align) {
0N/A alignment = align;
0N/A repaint();
0N/A }
0N/A}