jdk/src/share/classes/javax/swing/plaf/basic/BasicGraphicsUtils.java
author darcy
Wed, 02 Jul 2014 23:03:27 -0700
changeset 25565 ce603b34c98d
parent 23010 6dadb192ad81
child 25761 c408b10ef757
permissions -rw-r--r--
8043548: Fix raw and unchecked lint warnings in javax.swing.plaf.* 8042849: Fix raw and unchecked warnings in com.sun.java.swing Reviewed-by: pchelko, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 20169
diff changeset
     2
 * Copyright (c) 1997, 2013, 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: 3977
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: 3977
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: 3977
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3977
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3977
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.plaf.basic;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import javax.swing.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Dimension;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.awt.FontMetrics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.awt.Graphics;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.awt.Insets;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.awt.Rectangle;
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 1639
diff changeset
    36
import java.awt.Toolkit;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.awt.event.KeyEvent;
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 1639
diff changeset
    38
import java.awt.event.InputEvent;
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 1639
diff changeset
    39
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import sun.swing.SwingUtilities2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class BasicGraphicsUtils
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final Insets GROOVE_INSETS = new Insets(2, 2, 2, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final Insets ETCHED_INSETS = new Insets(2, 2, 2, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static void drawEtchedRect(Graphics g, int x, int y, int w, int h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                                      Color shadow, Color darkShadow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                                      Color highlight, Color lightHighlight)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        Color oldColor = g.getColor();  // Make no net change to g
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        g.translate(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        g.setColor(shadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        g.drawLine(0, 0, w-1, 0);      // outer border, top
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        g.drawLine(0, 1, 0, h-2);      // outer border, left
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        g.setColor(darkShadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        g.drawLine(1, 1, w-3, 1);      // inner border, top
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        g.drawLine(1, 2, 1, h-3);      // inner border, left
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        g.setColor(lightHighlight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        g.drawLine(w-1, 0, w-1, h-1);  // outer border, bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        g.drawLine(0, h-1, w-1, h-1);  // outer border, right
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        g.setColor(highlight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        g.drawLine(w-2, 1, w-2, h-3);  // inner border, right
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        g.drawLine(1, h-2, w-2, h-2);  // inner border, bottom
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        g.translate(-x, -y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        g.setColor(oldColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Returns the amount of space taken up by a border drawn by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * <code>drawEtchedRect()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @return  the inset of an etched rect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public static Insets getEtchedInsets() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return ETCHED_INSETS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public static void drawGroove(Graphics g, int x, int y, int w, int h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                  Color shadow, Color highlight)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        Color oldColor = g.getColor();  // Make no net change to g
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        g.translate(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        g.setColor(shadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        g.drawRect(0, 0, w-2, h-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        g.setColor(highlight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        g.drawLine(1, h-3, 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        g.drawLine(1, 1, w-3, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        g.drawLine(0, h-1, w-1, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        g.drawLine(w-1, h-1, w-1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        g.translate(-x, -y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        g.setColor(oldColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Returns the amount of space taken up by a border drawn by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <code>drawGroove()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @return  the inset of a groove border
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    public static Insets getGrooveInsets() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        return GROOVE_INSETS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public static void drawBezel(Graphics g, int x, int y, int w, int h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                 boolean isPressed, boolean isDefault,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                                 Color shadow, Color darkShadow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                 Color highlight, Color lightHighlight)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        Color oldColor = g.getColor();  // Make no net change to g
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        g.translate(x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (isPressed && isDefault) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            g.setColor(darkShadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            g.drawRect(0, 0, w - 1, h - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            g.setColor(shadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            g.drawRect(1, 1, w - 3, h - 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } else if (isPressed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            drawLoweredBezel(g, x, y, w, h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                             shadow, darkShadow, highlight, lightHighlight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        } else if (isDefault) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            g.setColor(darkShadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            g.drawRect(0, 0, w-1, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            g.setColor(lightHighlight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            g.drawLine(1, 1, 1, h-3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            g.drawLine(2, 1, w-3, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            g.setColor(highlight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            g.drawLine(2, 2, 2, h-4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            g.drawLine(3, 2, w-4, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            g.setColor(shadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            g.drawLine(2, h-3, w-3, h-3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            g.drawLine(w-3, 2, w-3, h-4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            g.setColor(darkShadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            g.drawLine(1, h-2, w-2, h-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            g.drawLine(w-2, h-2, w-2, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            g.setColor(lightHighlight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            g.drawLine(0, 0, 0, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            g.drawLine(1, 0, w-2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            g.setColor(highlight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            g.drawLine(1, 1, 1, h-3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            g.drawLine(2, 1, w-3, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            g.setColor(shadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            g.drawLine(1, h-2, w-2, h-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            g.drawLine(w-2, 1, w-2, h-3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            g.setColor(darkShadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            g.drawLine(0, h-1, w-1, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            g.drawLine(w-1, h-1, w-1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        g.translate(-x, -y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        g.setColor(oldColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public static void drawLoweredBezel(Graphics g, int x, int y, int w, int h,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                                        Color shadow, Color darkShadow,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                                        Color highlight, Color lightHighlight)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        g.setColor(darkShadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        g.drawLine(0, 0, 0, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        g.drawLine(1, 0, w-2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        g.setColor(shadow);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        g.drawLine(1, 1, 1, h-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        g.drawLine(1, 1, w-3, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        g.setColor(lightHighlight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        g.drawLine(0, h-1, w-1, h-1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        g.drawLine(w-1, h-1, w-1, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        g.setColor(highlight);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        g.drawLine(1, h-2, w-2, h-2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        g.drawLine(w-2, h-2, w-2, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    /** Draw a string with the graphics <code>g</code> at location (x,y)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *  just like <code>g.drawString</code> would.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *  The first occurrence of <code>underlineChar</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *  in text will be underlined. The matching algorithm is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *  not case sensitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public static void drawString(Graphics g,String text,int underlinedChar,int x,int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        int index=-1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (underlinedChar != '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            char uc = Character.toUpperCase((char)underlinedChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            char lc = Character.toLowerCase((char)underlinedChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            int uci = text.indexOf(uc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            int lci = text.indexOf(lc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            if(uci == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                index = lci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            else if(lci == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                index = uci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                index = (lci < uci) ? lci : uci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        drawStringUnderlineCharAt(g, text, index, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Draw a string with the graphics <code>g</code> at location
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * (<code>x</code>, <code>y</code>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * just like <code>g.drawString</code> would.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * The character at index <code>underlinedIndex</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * in text will be underlined. If <code>index</code> is beyond the
20169
d7fa6d7586c9 8025085: [javadoc] some errors in javax/swing
yan
parents: 5506
diff changeset
   234
     * bounds of <code>text</code> (including &lt; 0), nothing will be
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * underlined.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param g Graphics to draw with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param text String to draw
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * @param underlinedIndex Index of character in text to underline
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @param x x coordinate to draw at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * @param y y coordinate to draw at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public static void drawStringUnderlineCharAt(Graphics g, String text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                           int underlinedIndex, int x,int y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        SwingUtilities2.drawStringUnderlineCharAt(null, g, text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                                                  underlinedIndex, x, y);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public static void drawDashedRect(Graphics g,int x,int y,int width,int height) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        int vx,vy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        // draw upper and lower horizontal dashes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        for (vx = x; vx < (x + width); vx+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            g.fillRect(vx, y, 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            g.fillRect(vx, y + height-1, 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        // draw left and right vertical dashes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        for (vy = y; vy < (y + height); vy+=2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            g.fillRect(x, vy, 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            g.fillRect(x+width-1, vy, 1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        if(b.getComponentCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   272
        Icon icon = b.getIcon();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        String text = b.getText();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        Font font = b.getFont();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        FontMetrics fm = b.getFontMetrics(font);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        Rectangle iconR = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        Rectangle textR = new Rectangle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        Rectangle viewR = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        SwingUtilities.layoutCompoundLabel(
1290
da8902cd496c 6727661: Code improvement and warnings removing from the swing/plaf packages
rupashka
parents: 2
diff changeset
   283
            b, fm, text, icon,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            b.getVerticalAlignment(), b.getHorizontalAlignment(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            viewR, iconR, textR, (text == null ? 0 : textIconGap)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        /* The preferred size of the button is the size of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
         * the text and icon rectangles plus the buttons insets.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        Rectangle r = iconR.union(textR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        Insets insets = b.getInsets();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        r.width += insets.left + insets.right;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        r.height += insets.top + insets.bottom;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return r.getSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Convenience function for determining ComponentOrientation.  Helps us
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * avoid having Munge directives throughout the code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    static boolean isLeftToRight( Component c ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        return c.getComponentOrientation().isLeftToRight();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
3977
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 1639
diff changeset
   309
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 1639
diff changeset
   310
    static boolean isMenuShortcutKeyDown(InputEvent event) {
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 1639
diff changeset
   311
        return (event.getModifiers() &
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 1639
diff changeset
   312
                Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) != 0;
0da8e3baf0b5 4833524: BasicTreeUI.isToggleSelectionEvent() does not properly handle popup triggers
alexp
parents: 1639
diff changeset
   313
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
}