1/*
2 * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.awt.X11;
27
28import java.awt.*;
29import java.awt.peer.*;
30
31class XLabelPeer extends XComponentPeer implements LabelPeer {
32    /**
33     * Create the label
34     */
35
36    static final int            TEXT_XPAD = 8;
37    static final int            TEXT_YPAD = 6;
38    String label;
39    int alignment;
40
41    FontMetrics cachedFontMetrics;
42    Font oldfont;
43
44    FontMetrics getFontMetrics()
45    {
46        if (cachedFontMetrics != null)
47            return cachedFontMetrics;
48        else return getFontMetrics(getPeerFont());
49
50    }
51
52    void preInit(XCreateWindowParams params) {
53        super.preInit(params);
54        Label target = (Label) this.target;
55        label = target.getText();
56        if (label == null) {
57            label = "";
58        }
59        alignment = target.getAlignment();
60    }
61
62    XLabelPeer(Label target) {
63        super(target);
64    }
65
66    /**
67     * Minimum size.
68     */
69    public Dimension getMinimumSize() {
70        FontMetrics fm = getFontMetrics();
71        int w;
72        try {
73            w = fm.stringWidth(label);
74        }
75        catch (NullPointerException e) {
76            w = 0;
77        }
78        return new Dimension(w + TEXT_XPAD,
79                             fm.getAscent() + fm.getMaxDescent() + TEXT_YPAD);
80    }
81
82
83    /**
84     * Paint the label
85     */
86    // NOTE: This method is called by privileged threads.
87    //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
88    @Override
89    void paintPeer(final Graphics g) {
90        int textX = 0;
91        int textY = 0;
92        g.setColor(getPeerBackground());
93        g.fillRect(0, 0, width, height);
94
95        Font f = getPeerFont();
96        g.setFont(f);
97        FontMetrics fm = g.getFontMetrics();
98
99        if (cachedFontMetrics == null)
100        {
101            cachedFontMetrics = fm;
102        }
103        else
104        {
105            if (oldfont != f)
106                cachedFontMetrics = fm;
107        }
108
109        switch (alignment) {
110          case Label.LEFT:
111              textX = 2;
112              textY = (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2;
113              break;
114          case Label.RIGHT:
115              textX = width - (fm.stringWidth(label) + 2);
116              textY = (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2;
117              break;
118          case Label.CENTER:
119              textX = (width - fm.stringWidth(label)) / 2;
120              textY = (height + fm.getMaxAscent() - fm.getMaxDescent()) / 2;
121              break;
122        }
123        if (isEnabled()) {
124            g.setColor(getPeerForeground());
125            g.drawString(label, textX, textY);
126        }
127        else {
128            g.setColor(getPeerBackground().brighter());
129            g.drawString(label, textX, textY);
130            g.setColor(getPeerBackground().darker());
131            g.drawString(label, textX - 1, textY - 1);
132        }
133    }
134
135    @Override
136    public void setText(String label) {
137        if (label == null) {
138            label = "";
139        }
140        if (!label.equals(this.label)) {
141            this.label = label;
142            repaint();
143        }
144    }
145
146    @Override
147    public void setAlignment(final int alignment) {
148        if (this.alignment != alignment) {
149            this.alignment = alignment;
150            repaint();
151        }
152    }
153}
154