0N/A/*
2362N/A * Copyright (c) 1998, 2005, 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/A
0N/Apackage javax.swing.plaf.metal;
0N/A
0N/Aimport javax.swing.plaf.*;
0N/Aimport javax.swing.*;
0N/Aimport java.awt.*;
0N/Aimport java.awt.image.*;
0N/Aimport java.lang.ref.*;
0N/Aimport java.util.*;
0N/Aimport sun.swing.CachedPainter;
0N/Aimport sun.swing.ImageIconUIResource;
0N/A
0N/A/**
0N/A * This is a dumping ground for random stuff we want to use in several places.
0N/A *
0N/A * @author Steve Wilson
0N/A */
0N/A
0N/Aclass MetalUtils {
0N/A
0N/A static void drawFlush3DBorder(Graphics g, Rectangle r) {
0N/A drawFlush3DBorder(g, r.x, r.y, r.width, r.height);
0N/A }
0N/A
0N/A /**
0N/A * This draws the "Flush 3D Border" which is used throughout the Metal L&F
0N/A */
0N/A static void drawFlush3DBorder(Graphics g, int x, int y, int w, int h) {
0N/A g.translate( x, y);
0N/A g.setColor( MetalLookAndFeel.getControlDarkShadow() );
0N/A g.drawRect( 0, 0, w-2, h-2 );
0N/A g.setColor( MetalLookAndFeel.getControlHighlight() );
0N/A g.drawRect( 1, 1, w-2, h-2 );
0N/A g.setColor( MetalLookAndFeel.getControl() );
0N/A g.drawLine( 0, h-1, 1, h-2 );
0N/A g.drawLine( w-1, 0, w-2, 1 );
0N/A g.translate( -x, -y);
0N/A }
0N/A
0N/A /**
0N/A * This draws a variant "Flush 3D Border"
0N/A * It is used for things like pressed buttons.
0N/A */
0N/A static void drawPressed3DBorder(Graphics g, Rectangle r) {
0N/A drawPressed3DBorder( g, r.x, r.y, r.width, r.height );
0N/A }
0N/A
0N/A static void drawDisabledBorder(Graphics g, int x, int y, int w, int h) {
0N/A g.translate( x, y);
0N/A g.setColor( MetalLookAndFeel.getControlShadow() );
0N/A g.drawRect( 0, 0, w-1, h-1 );
0N/A g.translate(-x, -y);
0N/A }
0N/A
0N/A /**
0N/A * This draws a variant "Flush 3D Border"
0N/A * It is used for things like pressed buttons.
0N/A */
0N/A static void drawPressed3DBorder(Graphics g, int x, int y, int w, int h) {
0N/A g.translate( x, y);
0N/A
0N/A drawFlush3DBorder(g, 0, 0, w, h);
0N/A
0N/A g.setColor( MetalLookAndFeel.getControlShadow() );
0N/A g.drawLine( 1, 1, 1, h-2 );
0N/A g.drawLine( 1, 1, w-2, 1 );
0N/A g.translate( -x, -y);
0N/A }
0N/A
0N/A /**
0N/A * This draws a variant "Flush 3D Border"
0N/A * It is used for things like active toggle buttons.
0N/A * This is used rarely.
0N/A */
0N/A static void drawDark3DBorder(Graphics g, Rectangle r) {
0N/A drawDark3DBorder(g, r.x, r.y, r.width, r.height);
0N/A }
0N/A
0N/A /**
0N/A * This draws a variant "Flush 3D Border"
0N/A * It is used for things like active toggle buttons.
0N/A * This is used rarely.
0N/A */
0N/A static void drawDark3DBorder(Graphics g, int x, int y, int w, int h) {
0N/A g.translate( x, y);
0N/A
0N/A drawFlush3DBorder(g, 0, 0, w, h);
0N/A
0N/A g.setColor( MetalLookAndFeel.getControl() );
0N/A g.drawLine( 1, 1, 1, h-2 );
0N/A g.drawLine( 1, 1, w-2, 1 );
0N/A g.setColor( MetalLookAndFeel.getControlShadow() );
0N/A g.drawLine( 1, h-2, 1, h-2 );
0N/A g.drawLine( w-2, 1, w-2, 1 );
0N/A g.translate( -x, -y);
0N/A }
0N/A
0N/A static void drawButtonBorder(Graphics g, int x, int y, int w, int h, boolean active) {
0N/A if (active) {
0N/A drawActiveButtonBorder(g, x, y, w, h);
0N/A } else {
0N/A drawFlush3DBorder(g, x, y, w, h);
0N/A }
0N/A }
0N/A
0N/A static void drawActiveButtonBorder(Graphics g, int x, int y, int w, int h) {
0N/A drawFlush3DBorder(g, x, y, w, h);
0N/A g.setColor( MetalLookAndFeel.getPrimaryControl() );
0N/A g.drawLine( x+1, y+1, x+1, h-3 );
0N/A g.drawLine( x+1, y+1, w-3, x+1 );
0N/A g.setColor( MetalLookAndFeel.getPrimaryControlDarkShadow() );
0N/A g.drawLine( x+2, h-2, w-2, h-2 );
0N/A g.drawLine( w-2, y+2, w-2, h-2 );
0N/A }
0N/A
0N/A static void drawDefaultButtonBorder(Graphics g, int x, int y, int w, int h, boolean active) {
0N/A drawButtonBorder(g, x+1, y+1, w-1, h-1, active);
0N/A g.translate(x, y);
0N/A g.setColor( MetalLookAndFeel.getControlDarkShadow() );
0N/A g.drawRect( 0, 0, w-3, h-3 );
0N/A g.drawLine( w-2, 0, w-2, 0);
0N/A g.drawLine( 0, h-2, 0, h-2);
0N/A g.translate(-x, -y);
0N/A }
0N/A
0N/A static void drawDefaultButtonPressedBorder(Graphics g, int x, int y, int w, int h) {
0N/A drawPressed3DBorder(g, x + 1, y + 1, w - 1, h - 1);
0N/A g.translate(x, y);
0N/A g.setColor(MetalLookAndFeel.getControlDarkShadow());
0N/A g.drawRect(0, 0, w - 3, h - 3);
0N/A g.drawLine(w - 2, 0, w - 2, 0);
0N/A g.drawLine(0, h - 2, 0, h - 2);
0N/A g.setColor(MetalLookAndFeel.getControl());
0N/A g.drawLine(w - 1, 0, w - 1, 0);
0N/A g.drawLine(0, h - 1, 0, h - 1);
0N/A g.translate(-x, -y);
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 }
0N/A
0N/A static int getInt(Object key, int defaultValue) {
0N/A Object value = UIManager.get(key);
0N/A
0N/A if (value instanceof Integer) {
0N/A return ((Integer)value).intValue();
0N/A }
0N/A if (value instanceof String) {
0N/A try {
0N/A return Integer.parseInt((String)value);
0N/A } catch (NumberFormatException nfe) {}
0N/A }
0N/A return defaultValue;
0N/A }
0N/A
0N/A //
0N/A // Ocean specific stuff.
0N/A //
0N/A /**
0N/A * Draws a radial type gradient. The gradient will be drawn vertically if
0N/A * <code>vertical</code> is true, otherwise horizontally.
0N/A * The UIManager key consists of five values:
0N/A * r1 r2 c1 c2 c3. The gradient is broken down into four chunks drawn
0N/A * in order from the origin.
0N/A * <ol>
0N/A * <li>Gradient r1 % of the size from c1 to c2
0N/A * <li>Rectangle r2 % of the size in c2.
0N/A * <li>Gradient r1 % of the size from c2 to c1
0N/A * <li>The remaining size will be filled with a gradient from c1 to c3.
0N/A * </ol>
0N/A *
0N/A * @param c Component rendering to
0N/A * @param g Graphics to draw to.
0N/A * @param key UIManager key used to look up gradient values.
0N/A * @param x X coordinate to draw from
0N/A * @param y Y coordinate to draw from
0N/A * @param w Width to draw to
0N/A * @param h Height to draw to
0N/A * @param vertical Direction of the gradient
0N/A * @return true if <code>key</code> exists, otherwise false.
0N/A */
0N/A static boolean drawGradient(Component c, Graphics g, String key,
0N/A int x, int y, int w, int h, boolean vertical) {
0N/A java.util.List gradient = (java.util.List)UIManager.get(key);
0N/A if (gradient == null || !(g instanceof Graphics2D)) {
0N/A return false;
0N/A }
0N/A
0N/A if (w <= 0 || h <= 0) {
0N/A return true;
0N/A }
0N/A
0N/A GradientPainter.INSTANCE.paint(
0N/A c, (Graphics2D)g, gradient, x, y, w, h, vertical);
0N/A return true;
0N/A }
0N/A
0N/A
0N/A private static class GradientPainter extends CachedPainter {
0N/A /**
0N/A * Instance used for painting. This is the only instance that is
0N/A * ever created.
0N/A */
0N/A public static final GradientPainter INSTANCE = new GradientPainter(8);
0N/A
0N/A // Size of images to create. For vertical gradients this is the width,
0N/A // otherwise it's the height.
0N/A private static final int IMAGE_SIZE = 64;
0N/A
0N/A /**
0N/A * This is the actual width we're painting in, or last painted to.
0N/A */
0N/A private int w;
0N/A /**
0N/A * This is the actual height we're painting in, or last painted to
0N/A */
0N/A private int h;
0N/A
0N/A
0N/A GradientPainter(int count) {
0N/A super(count);
0N/A }
0N/A
0N/A public void paint(Component c, Graphics2D g,
0N/A java.util.List gradient, int x, int y, int w,
0N/A int h, boolean isVertical) {
0N/A int imageWidth;
0N/A int imageHeight;
0N/A if (isVertical) {
0N/A imageWidth = IMAGE_SIZE;
0N/A imageHeight = h;
0N/A }
0N/A else {
0N/A imageWidth = w;
0N/A imageHeight = IMAGE_SIZE;
0N/A }
0N/A synchronized(c.getTreeLock()) {
0N/A this.w = w;
0N/A this.h = h;
0N/A paint(c, g, x, y, imageWidth, imageHeight,
0N/A gradient, isVertical);
0N/A }
0N/A }
0N/A
0N/A protected void paintToImage(Component c, Image image, Graphics g,
0N/A int w, int h, Object[] args) {
0N/A Graphics2D g2 = (Graphics2D)g;
0N/A java.util.List gradient = (java.util.List)args[0];
0N/A boolean isVertical = ((Boolean)args[1]).booleanValue();
0N/A // Render to the VolatileImage
0N/A if (isVertical) {
0N/A drawVerticalGradient(g2,
0N/A ((Number)gradient.get(0)).floatValue(),
0N/A ((Number)gradient.get(1)).floatValue(),
0N/A (Color)gradient.get(2),
0N/A (Color)gradient.get(3),
0N/A (Color)gradient.get(4), w, h);
0N/A }
0N/A else {
0N/A drawHorizontalGradient(g2,
0N/A ((Number)gradient.get(0)).floatValue(),
0N/A ((Number)gradient.get(1)).floatValue(),
0N/A (Color)gradient.get(2),
0N/A (Color)gradient.get(3),
0N/A (Color)gradient.get(4), w, h);
0N/A }
0N/A }
0N/A
0N/A protected void paintImage(Component c, Graphics g,
0N/A int x, int y, int imageW, int imageH,
0N/A Image image, Object[] args) {
0N/A boolean isVertical = ((Boolean)args[1]).booleanValue();
0N/A // Render to the screen
0N/A g.translate(x, y);
0N/A if (isVertical) {
0N/A for (int counter = 0; counter < w; counter += IMAGE_SIZE) {
0N/A int tileSize = Math.min(IMAGE_SIZE, w - counter);
0N/A g.drawImage(image, counter, 0, counter + tileSize, h,
0N/A 0, 0, tileSize, h, null);
0N/A }
0N/A }
0N/A else {
0N/A for (int counter = 0; counter < h; counter += IMAGE_SIZE) {
0N/A int tileSize = Math.min(IMAGE_SIZE, h - counter);
0N/A g.drawImage(image, 0, counter, w, counter + tileSize,
0N/A 0, 0, w, tileSize, null);
0N/A }
0N/A }
0N/A g.translate(-x, -y);
0N/A }
0N/A
0N/A private void drawVerticalGradient(Graphics2D g, float ratio1,
0N/A float ratio2, Color c1,Color c2,
0N/A Color c3, int w, int h) {
0N/A int mid = (int)(ratio1 * h);
0N/A int mid2 = (int)(ratio2 * h);
0N/A if (mid > 0) {
0N/A g.setPaint(getGradient((float)0, (float)0, c1, (float)0,
0N/A (float)mid, c2));
0N/A g.fillRect(0, 0, w, mid);
0N/A }
0N/A if (mid2 > 0) {
0N/A g.setColor(c2);
0N/A g.fillRect(0, mid, w, mid2);
0N/A }
0N/A if (mid > 0) {
0N/A g.setPaint(getGradient((float)0, (float)mid + mid2, c2,
0N/A (float)0, (float)mid * 2 + mid2, c1));
0N/A g.fillRect(0, mid + mid2, w, mid);
0N/A }
0N/A if (h - mid * 2 - mid2 > 0) {
0N/A g.setPaint(getGradient((float)0, (float)mid * 2 + mid2, c1,
0N/A (float)0, (float)h, c3));
0N/A g.fillRect(0, mid * 2 + mid2, w, h - mid * 2 - mid2);
0N/A }
0N/A }
0N/A
0N/A private void drawHorizontalGradient(Graphics2D g, float ratio1,
0N/A float ratio2, Color c1,Color c2,
0N/A Color c3, int w, int h) {
0N/A int mid = (int)(ratio1 * w);
0N/A int mid2 = (int)(ratio2 * w);
0N/A if (mid > 0) {
0N/A g.setPaint(getGradient((float)0, (float)0, c1,
0N/A (float)mid, (float)0, c2));
0N/A g.fillRect(0, 0, mid, h);
0N/A }
0N/A if (mid2 > 0) {
0N/A g.setColor(c2);
0N/A g.fillRect(mid, 0, mid2, h);
0N/A }
0N/A if (mid > 0) {
0N/A g.setPaint(getGradient((float)mid + mid2, (float)0, c2,
0N/A (float)mid * 2 + mid2, (float)0, c1));
0N/A g.fillRect(mid + mid2, 0, mid, h);
0N/A }
0N/A if (w - mid * 2 - mid2 > 0) {
0N/A g.setPaint(getGradient((float)mid * 2 + mid2, (float)0, c1,
0N/A w, (float)0, c3));
0N/A g.fillRect(mid * 2 + mid2, 0, w - mid * 2 - mid2, h);
0N/A }
0N/A }
0N/A
0N/A private GradientPaint getGradient(float x1, float y1,
0N/A Color c1, float x2, float y2,
0N/A Color c2) {
0N/A return new GradientPaint(x1, y1, c1, x2, y2, c2, true);
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Returns true if the specified widget is in a toolbar.
0N/A */
0N/A static boolean isToolBarButton(JComponent c) {
0N/A return (c.getParent() instanceof JToolBar);
0N/A }
0N/A
0N/A static Icon getOceanToolBarIcon(Image i) {
0N/A ImageProducer prod = new FilteredImageSource(i.getSource(),
0N/A new OceanToolBarImageFilter());
0N/A return new ImageIconUIResource(Toolkit.getDefaultToolkit().createImage(prod));
0N/A }
0N/A
0N/A static Icon getOceanDisabledButtonIcon(Image image) {
0N/A Object[] range = (Object[])UIManager.get("Button.disabledGrayRange");
0N/A int min = 180;
0N/A int max = 215;
0N/A if (range != null) {
0N/A min = ((Integer)range[0]).intValue();
0N/A max = ((Integer)range[1]).intValue();
0N/A }
0N/A ImageProducer prod = new FilteredImageSource(image.getSource(),
0N/A new OceanDisabledButtonImageFilter(min , max));
0N/A return new ImageIconUIResource(Toolkit.getDefaultToolkit().createImage(prod));
0N/A }
0N/A
0N/A
0N/A
0N/A
0N/A /**
0N/A * Used to create a disabled Icon with the ocean look.
0N/A */
0N/A private static class OceanDisabledButtonImageFilter extends RGBImageFilter{
0N/A private float min;
0N/A private float factor;
0N/A
0N/A OceanDisabledButtonImageFilter(int min, int max) {
0N/A canFilterIndexColorModel = true;
0N/A this.min = (float)min;
0N/A this.factor = (max - min) / 255f;
0N/A }
0N/A
0N/A public int filterRGB(int x, int y, int rgb) {
0N/A // Coefficients are from the sRGB color space:
0N/A int gray = Math.min(255, (int)(((0.2125f * ((rgb >> 16) & 0xFF)) +
0N/A (0.7154f * ((rgb >> 8) & 0xFF)) +
0N/A (0.0721f * (rgb & 0xFF)) + .5f) * factor + min));
0N/A
0N/A return (rgb & 0xff000000) | (gray << 16) | (gray << 8) |
0N/A (gray << 0);
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Used to create the rollover icons with the ocean look.
0N/A */
0N/A private static class OceanToolBarImageFilter extends RGBImageFilter {
0N/A OceanToolBarImageFilter() {
0N/A canFilterIndexColorModel = true;
0N/A }
0N/A
0N/A public int filterRGB(int x, int y, int rgb) {
0N/A int r = ((rgb >> 16) & 0xff);
0N/A int g = ((rgb >> 8) & 0xff);
0N/A int b = (rgb & 0xff);
0N/A int gray = Math.max(Math.max(r, g), b);
0N/A return (rgb & 0xff000000) | (gray << 16) | (gray << 8) |
0N/A (gray << 0);
0N/A }
0N/A }
0N/A}