0N/A/*
2362N/A * Copyright (c) 1997, 2008, 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/Apackage javax.swing.plaf.basic;
0N/A
0N/Aimport javax.swing.*;
0N/Aimport java.awt.Component;
0N/Aimport java.awt.Color;
0N/Aimport java.awt.Dimension;
0N/Aimport java.awt.Font;
0N/Aimport java.awt.FontMetrics;
0N/Aimport java.awt.Graphics;
0N/Aimport java.awt.Insets;
0N/Aimport java.awt.Rectangle;
1735N/Aimport java.awt.Toolkit;
0N/Aimport java.awt.event.KeyEvent;
1735N/Aimport java.awt.event.InputEvent;
1735N/A
0N/Aimport sun.swing.SwingUtilities2;
0N/A
0N/A
0N/A/*
0N/A * @author Hans Muller
0N/A */
0N/A
0N/Apublic class BasicGraphicsUtils
0N/A{
0N/A
0N/A private static final Insets GROOVE_INSETS = new Insets(2, 2, 2, 2);
0N/A private static final Insets ETCHED_INSETS = new Insets(2, 2, 2, 2);
0N/A
0N/A public static void drawEtchedRect(Graphics g, int x, int y, int w, int h,
0N/A Color shadow, Color darkShadow,
0N/A Color highlight, Color lightHighlight)
0N/A {
0N/A Color oldColor = g.getColor(); // Make no net change to g
0N/A g.translate(x, y);
0N/A
0N/A g.setColor(shadow);
0N/A g.drawLine(0, 0, w-1, 0); // outer border, top
0N/A g.drawLine(0, 1, 0, h-2); // outer border, left
0N/A
0N/A g.setColor(darkShadow);
0N/A g.drawLine(1, 1, w-3, 1); // inner border, top
0N/A g.drawLine(1, 2, 1, h-3); // inner border, left
0N/A
0N/A g.setColor(lightHighlight);
0N/A g.drawLine(w-1, 0, w-1, h-1); // outer border, bottom
0N/A g.drawLine(0, h-1, w-1, h-1); // outer border, right
0N/A
0N/A g.setColor(highlight);
0N/A g.drawLine(w-2, 1, w-2, h-3); // inner border, right
0N/A g.drawLine(1, h-2, w-2, h-2); // inner border, bottom
0N/A
0N/A g.translate(-x, -y);
0N/A g.setColor(oldColor);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns the amount of space taken up by a border drawn by
0N/A * <code>drawEtchedRect()</code>
0N/A *
0N/A * @return the inset of an etched rect
0N/A */
0N/A public static Insets getEtchedInsets() {
0N/A return ETCHED_INSETS;
0N/A }
0N/A
0N/A
0N/A public static void drawGroove(Graphics g, int x, int y, int w, int h,
0N/A Color shadow, Color highlight)
0N/A {
0N/A Color oldColor = g.getColor(); // Make no net change to g
0N/A g.translate(x, y);
0N/A
0N/A g.setColor(shadow);
0N/A g.drawRect(0, 0, w-2, h-2);
0N/A
0N/A g.setColor(highlight);
0N/A g.drawLine(1, h-3, 1, 1);
0N/A g.drawLine(1, 1, w-3, 1);
0N/A
0N/A g.drawLine(0, h-1, w-1, h-1);
0N/A g.drawLine(w-1, h-1, w-1, 0);
0N/A
0N/A g.translate(-x, -y);
0N/A g.setColor(oldColor);
0N/A }
0N/A
0N/A /**
0N/A * Returns the amount of space taken up by a border drawn by
0N/A * <code>drawGroove()</code>
0N/A *
0N/A * @return the inset of a groove border
0N/A */
0N/A public static Insets getGrooveInsets() {
0N/A return GROOVE_INSETS;
0N/A }
0N/A
0N/A
0N/A public static void drawBezel(Graphics g, int x, int y, int w, int h,
0N/A boolean isPressed, boolean isDefault,
0N/A Color shadow, Color darkShadow,
0N/A Color highlight, Color lightHighlight)
0N/A {
0N/A Color oldColor = g.getColor(); // Make no net change to g
0N/A g.translate(x, y);
0N/A
0N/A if (isPressed && isDefault) {
0N/A g.setColor(darkShadow);
0N/A g.drawRect(0, 0, w - 1, h - 1);
0N/A g.setColor(shadow);
0N/A g.drawRect(1, 1, w - 3, h - 3);
0N/A } else if (isPressed) {
0N/A drawLoweredBezel(g, x, y, w, h,
0N/A shadow, darkShadow, highlight, lightHighlight);
0N/A } else if (isDefault) {
0N/A g.setColor(darkShadow);
0N/A g.drawRect(0, 0, w-1, h-1);
0N/A
0N/A g.setColor(lightHighlight);
0N/A g.drawLine(1, 1, 1, h-3);
0N/A g.drawLine(2, 1, w-3, 1);
0N/A
0N/A g.setColor(highlight);
0N/A g.drawLine(2, 2, 2, h-4);
0N/A g.drawLine(3, 2, w-4, 2);
0N/A
0N/A g.setColor(shadow);
0N/A g.drawLine(2, h-3, w-3, h-3);
0N/A g.drawLine(w-3, 2, w-3, h-4);
0N/A
0N/A g.setColor(darkShadow);
0N/A g.drawLine(1, h-2, w-2, h-2);
0N/A g.drawLine(w-2, h-2, w-2, 1);
0N/A } else {
0N/A g.setColor(lightHighlight);
0N/A g.drawLine(0, 0, 0, h-1);
0N/A g.drawLine(1, 0, w-2, 0);
0N/A
0N/A g.setColor(highlight);
0N/A g.drawLine(1, 1, 1, h-3);
0N/A g.drawLine(2, 1, w-3, 1);
0N/A
0N/A g.setColor(shadow);
0N/A g.drawLine(1, h-2, w-2, h-2);
0N/A g.drawLine(w-2, 1, w-2, h-3);
0N/A
0N/A g.setColor(darkShadow);
0N/A g.drawLine(0, h-1, w-1, h-1);
0N/A g.drawLine(w-1, h-1, w-1, 0);
0N/A }
0N/A g.translate(-x, -y);
0N/A g.setColor(oldColor);
0N/A }
0N/A
0N/A public static void drawLoweredBezel(Graphics g, int x, int y, int w, int h,
0N/A Color shadow, Color darkShadow,
0N/A Color highlight, Color lightHighlight) {
0N/A g.setColor(darkShadow);
0N/A g.drawLine(0, 0, 0, h-1);
0N/A g.drawLine(1, 0, w-2, 0);
0N/A
0N/A g.setColor(shadow);
0N/A g.drawLine(1, 1, 1, h-2);
0N/A g.drawLine(1, 1, w-3, 1);
0N/A
0N/A g.setColor(lightHighlight);
0N/A g.drawLine(0, h-1, w-1, h-1);
0N/A g.drawLine(w-1, h-1, w-1, 0);
0N/A
0N/A g.setColor(highlight);
0N/A g.drawLine(1, h-2, w-2, h-2);
0N/A g.drawLine(w-2, h-2, w-2, 1);
0N/A }
0N/A
0N/A
0N/A /** Draw a string with the graphics <code>g</code> at location (x,y)
0N/A * just like <code>g.drawString</code> would.
0N/A * The first occurrence of <code>underlineChar</code>
0N/A * in text will be underlined. The matching algorithm is
0N/A * not case sensitive.
0N/A */
0N/A public static void drawString(Graphics g,String text,int underlinedChar,int x,int y) {
0N/A int index=-1;
0N/A
0N/A if (underlinedChar != '\0') {
0N/A char uc = Character.toUpperCase((char)underlinedChar);
0N/A char lc = Character.toLowerCase((char)underlinedChar);
0N/A int uci = text.indexOf(uc);
0N/A int lci = text.indexOf(lc);
0N/A
0N/A if(uci == -1) {
0N/A index = lci;
0N/A }
0N/A else if(lci == -1) {
0N/A index = uci;
0N/A }
0N/A else {
0N/A index = (lci < uci) ? lci : uci;
0N/A }
0N/A }
0N/A drawStringUnderlineCharAt(g, text, index, x, y);
0N/A }
0N/A
0N/A /**
0N/A * Draw a string with the graphics <code>g</code> at location
0N/A * (<code>x</code>, <code>y</code>)
0N/A * just like <code>g.drawString</code> would.
0N/A * The character at index <code>underlinedIndex</code>
0N/A * in text will be underlined. If <code>index</code> is beyond the
0N/A * bounds of <code>text</code> (including < 0), nothing will be
0N/A * underlined.
0N/A *
0N/A * @param g Graphics to draw with
0N/A * @param text String to draw
0N/A * @param underlinedIndex Index of character in text to underline
0N/A * @param x x coordinate to draw at
0N/A * @param y y coordinate to draw at
0N/A * @since 1.4
0N/A */
0N/A public static void drawStringUnderlineCharAt(Graphics g, String text,
0N/A int underlinedIndex, int x,int y) {
0N/A SwingUtilities2.drawStringUnderlineCharAt(null, g, text,
0N/A underlinedIndex, x, y);
0N/A }
0N/A
0N/A public static void drawDashedRect(Graphics g,int x,int y,int width,int height) {
0N/A int vx,vy;
0N/A
0N/A // draw upper and lower horizontal dashes
0N/A for (vx = x; vx < (x + width); vx+=2) {
0N/A g.fillRect(vx, y, 1, 1);
0N/A g.fillRect(vx, y + height-1, 1, 1);
0N/A }
0N/A
0N/A // draw left and right vertical dashes
0N/A for (vy = y; vy < (y + height); vy+=2) {
0N/A g.fillRect(x, vy, 1, 1);
0N/A g.fillRect(x+width-1, vy, 1, 1);
0N/A }
0N/A }
0N/A
0N/A public static Dimension getPreferredButtonSize(AbstractButton b, int textIconGap)
0N/A {
0N/A if(b.getComponentCount() > 0) {
0N/A return null;
0N/A }
0N/A
614N/A Icon icon = b.getIcon();
0N/A String text = b.getText();
0N/A
0N/A Font font = b.getFont();
0N/A FontMetrics fm = b.getFontMetrics(font);
0N/A
0N/A Rectangle iconR = new Rectangle();
0N/A Rectangle textR = new Rectangle();
0N/A Rectangle viewR = new Rectangle(Short.MAX_VALUE, Short.MAX_VALUE);
0N/A
0N/A SwingUtilities.layoutCompoundLabel(
614N/A b, fm, text, icon,
0N/A b.getVerticalAlignment(), b.getHorizontalAlignment(),
0N/A b.getVerticalTextPosition(), b.getHorizontalTextPosition(),
0N/A viewR, iconR, textR, (text == null ? 0 : textIconGap)
0N/A );
0N/A
0N/A /* The preferred size of the button is the size of
0N/A * the text and icon rectangles plus the buttons insets.
0N/A */
0N/A
0N/A Rectangle r = iconR.union(textR);
0N/A
0N/A Insets insets = b.getInsets();
0N/A r.width += insets.left + insets.right;
0N/A r.height += insets.top + insets.bottom;
0N/A
0N/A return r.getSize();
0N/A }
0N/A
0N/A /*
0N/A * Convenience function for determining ComponentOrientation. Helps us
0N/A * avoid having Munge directives throughout the code.
0N/A */
0N/A static boolean isLeftToRight( Component c ) {
0N/A return c.getComponentOrientation().isLeftToRight();
0N/A }
1735N/A
1735N/A static boolean isMenuShortcutKeyDown(InputEvent event) {
1735N/A return (event.getModifiers() &
1735N/A Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()) != 0;
1735N/A }
0N/A}