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