2802 lines · 115.2 KB · Java
RawDownload
/* * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */package javax.swing.plaf.nimbus; import java.awt.*;import java.awt.geom.AffineTransform;import java.awt.geom.NoninvertibleTransformException;import java.awt.image.BufferedImage;import java.util.*;import javax.swing.*;import javax.swing.plaf.synth.SynthContext;import javax.swing.plaf.synth.SynthPainter;import javax.swing.plaf.synth.SynthConstants; import javax.swing.Painter;  class SynthPainterImpl extends SynthPainter {    private NimbusStyle style;     SynthPainterImpl(NimbusStyle style) {        this.style = style;    }     /**     * Paint the provided painter using the provided transform at the specified     * position and size. Handles if g is a non 2D Graphics by painting via a     * BufferedImage.     */    private void paint(Painter p, SynthContext ctx, Graphics g, int x, int y,                       int w, int h, AffineTransform transform) {        if (p != null) {            if (g instanceof Graphics2D){                Graphics2D gfx = (Graphics2D)g;                if (transform!=null){                    gfx.transform(transform);                }                gfx.translate(x, y);                p.paint(gfx, ctx.getComponent(), w, h);                gfx.translate(-x, -y);                if (transform!=null){                    try {                        gfx.transform(transform.createInverse());                    } catch (NoninvertibleTransformException e) {                        // this should never happen as we are in control of all                        // calls into this method and only ever pass in simple                        // transforms of rotate, flip and translates                        e.printStackTrace();                    }                }            } else {                // use image if we are printing to a Java 1.1 PrintGraphics as                // it is not a instance of Graphics2D                BufferedImage img = new BufferedImage(w,h,                        BufferedImage.TYPE_INT_ARGB);                Graphics2D gfx = img.createGraphics();                if (transform!=null){                    gfx.transform(transform);                }                p.paint(gfx, ctx.getComponent(), w, h);                gfx.dispose();                g.drawImage(img,x,y,null);                img = null;            }        }    }     private void paintBackground(SynthContext ctx, Graphics g, int x, int y,                                 int w, int h, AffineTransform transform) {        // if the background color of the component is 100% transparent        // then we should not paint any background graphics. This is a solution        // for there being no way of turning off Nimbus background painting as        // basic components are all non-opaque by default.        Component c = ctx.getComponent();        Color bg = (c != null) ? c.getBackground() : null;        if (bg == null || bg.getAlpha() > 0){            Painter backgroundPainter = style.getBackgroundPainter(ctx);            if (backgroundPainter != null) {                paint(backgroundPainter, ctx, g, x, y, w, h,transform);            }        }    }     private void paintForeground(SynthContext ctx, Graphics g, int x, int y,                                 int w, int h, AffineTransform transform) {        Painter foregroundPainter = style.getForegroundPainter(ctx);        if (foregroundPainter != null) {            paint(foregroundPainter, ctx, g, x, y, w, h,transform);        }    }     private void paintBorder(SynthContext ctx, Graphics g, int x, int y, int w,                             int h, AffineTransform transform) {        Painter borderPainter = style.getBorderPainter(ctx);        if (borderPainter != null) {            paint(borderPainter, ctx, g, x, y, w, h,transform);        }    }     private void paintBackground(SynthContext ctx, Graphics g, int x, int y, int w, int h, int orientation) {        Component c = ctx.getComponent();        boolean ltr = c.getComponentOrientation().isLeftToRight();        // Don't RTL flip JSpliders as they handle it internaly        if (ctx.getComponent() instanceof JSlider) ltr = true;         if (orientation == SwingConstants.VERTICAL && ltr) {            AffineTransform transform = new AffineTransform();            transform.scale(-1, 1);            transform.rotate(Math.toRadians(90));            paintBackground(ctx, g, y, x, h, w, transform);        } else if (orientation == SwingConstants.VERTICAL) {            AffineTransform transform = new AffineTransform();            transform.rotate(Math.toRadians(90));            transform.translate(0,-(x+w));            paintBackground(ctx, g, y, x, h, w, transform);        } else if (orientation == SwingConstants.HORIZONTAL && ltr) {            paintBackground(ctx, g, x, y, w, h, null);        } else {            //horizontal and right-to-left orientation            AffineTransform transform = new AffineTransform();            transform.translate(x,y);            transform.scale(-1, 1);            transform.translate(-w,0);            paintBackground(ctx, g, 0, 0, w, h, transform);        }    }     private void paintBorder(SynthContext ctx, Graphics g, int x, int y, int w, int h, int orientation) {        Component c = ctx.getComponent();        boolean ltr = c.getComponentOrientation().isLeftToRight();        if (orientation == SwingConstants.VERTICAL && ltr) {            AffineTransform transform = new AffineTransform();            transform.scale(-1, 1);            transform.rotate(Math.toRadians(90));            paintBorder(ctx, g, y, x, h, w, transform);        } else if (orientation == SwingConstants.VERTICAL) {            AffineTransform transform = new AffineTransform();            transform.rotate(Math.toRadians(90));            transform.translate(0, -(x + w));            paintBorder(ctx, g, y, 0, h, w, transform);        } else if (orientation == SwingConstants.HORIZONTAL && ltr) {            paintBorder(ctx, g, x, y, w, h, null);        } else {            //horizontal and right-to-left orientation            paintBorder(ctx, g, x, y, w, h, null);        }    }     private void paintForeground(SynthContext ctx, Graphics g, int x, int y, int w, int h, int orientation) {        Component c = ctx.getComponent();        boolean ltr = c.getComponentOrientation().isLeftToRight();        if (orientation == SwingConstants.VERTICAL && ltr) {            AffineTransform transform = new AffineTransform();            transform.scale(-1, 1);            transform.rotate(Math.toRadians(90));            paintForeground(ctx, g, y, x, h, w, transform);        } else if (orientation == SwingConstants.VERTICAL) {            AffineTransform transform = new AffineTransform();            transform.rotate(Math.toRadians(90));            transform.translate(0, -(x + w));            paintForeground(ctx, g, y, 0, h, w, transform);        } else if (orientation == SwingConstants.HORIZONTAL && ltr) {            paintForeground(ctx, g, x, y, w, h, null);        } else {            //horizontal and right-to-left orientation            paintForeground(ctx, g, x, y, w, h, null);        }    }     /**     * Paints the background of an arrow button. Arrow buttons are created by     * some components, such as <code>JScrollBar</code>.     *     * @param context SynthContext identifying the <code>JComponent</code> and     *        <code>Region</code> to paint to     * @param g <code>Graphics</code> to paint to     * @param x X coordinate of the area to paint to     * @param y Y coordinate of the area to paint to     * @param w Width of the area to paint to     * @param h Height of the area to paint to     */    public void paintArrowButtonBackground(SynthContext context,                                           Graphics g, int x, int y,                                           int w, int h) {        if (context.getComponent().getComponentOrientation().isLeftToRight()){            paintBackground(context, g, x, y, w, h, null);        } else {            AffineTransform transform = new AffineTransform();            transform.translate(x,y);            transform.scale(-1, 1);            transform.translate(-w,0);            paintBackground(context, g, 0, 0, w, h, transform);        }    }     /**     * Paints the border of an arrow button. Arrow buttons are created by     * some components, such as <code>JScrollBar</code>.     *     * @param context SynthContext identifying the <code>JComponent</code> and     *        <code>Region</code> to paint to     * @param g <code>Graphics</code> to paint to     * @param x X coordinate of the area to paint to     * @param y Y coordinate of the area to paint to     * @param w Width of the area to paint to     * @param h Height of the area to paint to     */    public void paintArrowButtonBorder(SynthContext context,                                       Graphics g, int x, int y,                                       int w, int h) {        paintBorder(context, g, x, y, w, h, null);    }     /**     * Paints the foreground of an arrow button. This method is responsible     * for drawing a graphical representation of a direction, typically     * an arrow. Arrow buttons are created by     * some components, such as <code>JScrollBar</code>     *     * @param context SynthContext identifying the <code>JComponent</code> and     *        <code>Region</code> to paint to     * @param g <code>Graphics</code> to paint to     * @param x X coordinate of the area to paint to     * @param y Y coordinate of the area to paint to     * @param w Width of the area to paint to     * @param h Height of the area to paint to     * @param direction One of SwingConstants.NORTH, SwingConstants.SOUTH     *                  SwingConstants.EAST or SwingConstants.WEST     */    public void paintArrowButtonForeground(SynthContext context,                                           Graphics g, int x, int y,                                           int w, int h,                                           int direction) {        //assume that the painter is arranged with the arrow pointing... LEFT?        String compName = context.getComponent().getName();        boolean ltr = context.getComponent().                getComponentOrientation().isLeftToRight();        // The hard coding for spinners here needs to be replaced by a more        // general method for disabling rotation        if ("Spinner.nextButton".equals(compName) ||                "Spinner.previousButton".equals(compName)) {            if (ltr){                paintForeground(context, g, x, y, w, h, null);            } else {                AffineTransform transform = new AffineTransform();                transform.translate(w, 0);                transform.scale(-1, 1);                paintForeground(context, g, x, y, w, h, transform);            }        } else if (direction == SwingConstants.WEST) {            paintForeground(context, g, x, y, w, h, null);        } else if (direction == SwingConstants.NORTH) {            if (ltr){                AffineTransform transform = new AffineTransform();                transform.scale(-1, 1);                transform.rotate(Math.toRadians(90));                paintForeground(context, g, y, 0, h, w, transform);            } else {                AffineTransform transform = new AffineTransform();                transform.rotate(Math.toRadians(90));                transform.translate(0, -(x + w));                paintForeground(context, g, y, 0, h, w, transform);            }        } else if (direction == SwingConstants.EAST) {            AffineTransform transform = new AffineTransform();            transform.translate(w, 0);            transform.scale(-1, 1);            paintForeground(context, g, x, y, w, h, transform);        } else if (direction == SwingConstants.SOUTH) {            if (ltr){                AffineTransform transform = new AffineTransform();                transform.rotate(Math.toRadians(-90));                transform.translate(-h, 0);                paintForeground(context, g, y, x, h, w, transform);            } else {                AffineTransform transform = new AffineTransform();                transform.scale(-1, 1);                transform.rotate(Math.toRadians(-90));                transform.translate(-(h+y), -(w+x));                paintForeground(context, g, y, x, h, w, transform);            }        }    }     /**     * Paints the background of a button.     *     * @param context SynthContext identifying the <code>JComponent</code> and     *        <code>Region</code> to paint to     * @param g <code>Graphics</code> to paint to     * @param x X coordinate of the area to paint to     * @param y Y coordinate of the area to paint to     * @param w Width of the area to paint to     * @param h Height of the area to paint to     */    public void paintButtonBackground(SynthContext context,                                      Graphics g, int x, int y,                                      int w, int h) {        paintBackground(context, g, x, y, w, h, null);    }     /**     * Paints the border of a button.     *     * @param context SynthContext identifying the <code>JComponent</code> and     *        <code>Region</code> to paint to     * @param g <code>Graphics</code> to paint to     * @param x X coordinate of the area to paint to     * @param y Y coordinate of the area to paint to     * @param w Width of the area to paint to     * @param h Height of the area to paint to     */    public void paintButtonBorder(SynthContext context,                                  Graphics g, int x, int y,                                  int w, int h) {        paintBorder(context, g, x, y, w, h, null);    }     /**     * Paints the background of a check box menu item.     *     * @param context SynthContext identifying the <code>JComponent</code> and     *        <code>Region</code> to paint to     * @param g <code>Graphics</code> to paint to     * @param x X coordinate of the area to paint to     * @param y Y coordinate of the area to paint to     * @param w Width of the area to paint to     * @param h Height of the area to paint to     */    public void paintCheckBoxMenuItemBackground(SynthContext context,                                                Graphics g, int x, int y,                                                int w, int h) {        paintBackground(context, g, x, y, w, h, null);    }     /**     * Paints the border of a check box menu item.     *     * @param context SynthContext identifying the <code>JComponent</code> and     *        <code>Region</code> to paint to     * @param g <code>Graphics</code> to paint to     * @param x X coordinate of the area to paint to     * @param y Y coordinate of the area to paint to     * @param w Width of the area to paint to     * @param h Height of the area to paint to     */    public void paintCheckBoxMenuItemBorder(SynthContext context,                                            Graphics g, int x, int y,                                            int w, int h) {        paintBorder(context, g, x, y, w, h, null);    }     /**     * Paints the background of a check box.     *     * @param context SynthContext identifying the <code>JComponent</code> and     *        <code>Region</code> to paint to     * @param g <code>Graphics</code> to paint to     * @param x X coordinate of the area to paint to     * @param y Y coordinate of the area to paint to     * @param w Width of the area to paint to     * @param h Height of the area to paint to     */    public void paintCheckBoxBackground(SynthContext context,                                        Graphics g, int x, int y,                                        int w, int h) {        paintBackground(context, g, x, y, w, h, null);    }     /**     * Paints the border of a check box.     *     * @param context SynthContext identifying the <code>JComponent</code> and     *        <code>Region</code> to paint to     * @param g <code>Graphics</code> to paint to     * @param x X coordinate of the area to paint to     * @param y Y coordinate of the area to paint to     * @param w Width of the area to paint to     * @param h Height of the area to paint to     */    public void paintCheckBoxBorder(SynthContext context,                                    Graphics g, int x, int y,                                    int w, int h) {        paintBorder(context, g, x, y, w, h, null);    }     /**     * Paints the background of a color chooser.     *     * @param context SynthContext identifying the <code>JComponent</code> and     *        <code>Region</code> to paint to     * @param g <code>Graphics</code> to paint to     * @param x X coordinate of the area to paint to     * @param y Y coordinate of the area to paint to     * @param w Width of the area to paint to     * @param h Height of the area to paint to     */    public void paintColorChooserBackground(SynthContext context,                                            Graphics g, int x, int y,                                            int w, int h) {        paintBackground(context, g, x, y, w, h, null);    }     /**     * Paints the border of a color chooser.     *     * @param context SynthContext identifying the <code>JComponent</code> and     *        <code>Region</code> to paint to     * @param g <code>Graphics</code> to paint to     * @param x X coordinate of the area to paint to     * @param y Y coordinate of the area to paint to     * @param w Width of the area to paint to     * @param h Height of the area to paint to     */    public void paintColorChooserBorder(SynthContext context,                                        Graphics g, int x, int y,                                        int w, int h) {        paintBorder(context, g, x, y, w, h, null);    }     /**     * Paints the background of a combo box.     *     * @param context SynthContext identifying the <code>JComponent</code> and     *        <code>Region</code> to paint to     * @param g <code>Graphics</code> to paint to     * @param x X coordinate of the area to paint to     * @param y Y coordinate of the area to paint to     * @param w Width of the area to paint to     * @param h Height of the area to paint to     */    public void paintComboBoxBackground(SynthContext context,                                        Graphics g, int x, int y,                                        int w, int h) {        if (context.getComponent().getComponentOrientation().isLeftToRight()){            paintBackground(context, g, x, y, w, h, null);        } else {            AffineTransform transform = new AffineTransform();            transform.translate(x,y);            transform.scale(-1, 1);            transform.translate(-w,0);            paintBackground(context, g, 0, 0, w, h, transform);        }    }     /**     * Paints the border of a combo box.     *     * @param context SynthContext identifying the <code>JComponent</code> and     *        <code>Region</code> to paint to     * @param g <code>Graphics</code> to paint to     * @param x X coordinate of the area to paint to     * @param y Y coordinate of the area to paint to     * @param w Width of the area to paint to     * @param h Height of the area to paint to     */    public void paintComboBoxBorder(SynthContext context,                                        Graphics g, int x, int y,                                        int w, int h) {        paintBorder(context, g, x, y, w, h, null);    }     /**     * Paints the background of a desktop icon.     *     * @param context SynthContext identifying the <code>JComponent</code> and     *        <code>Region</code> to paint to     * @param g <code>Graphics</code> to paint to     * @param x X coordinate of the area to paint to     * @param y Y coordinate of the area to paint to     * @param w Width of the area to paint to     * @param h Height of the area to paint to     */    public void paintDesktopIconBackground(SynthContext context,                                        Graphics g, int x, int y,                                        int w, int h) {        paintBackground(context, g, x, y, w, h, null);    }

Lines 1–500 of 2802.