1173N/A/*
2362N/A * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
1173N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1173N/A *
1173N/A * This code is free software; you can redistribute it and/or modify it
1173N/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
1173N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1173N/A *
1173N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1173N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1173N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1173N/A * version 2 for more details (a copy is included in the LICENSE file that
1173N/A * accompanied this code).
1173N/A *
1173N/A * You should have received a copy of the GNU General Public License version
1173N/A * 2 along with this work; if not, write to the Free Software Foundation,
1173N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1173N/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.
1173N/A */
1173N/Apackage javax.swing.plaf.nimbus;
1173N/A
1173N/Aimport javax.swing.Painter;
1173N/Aimport sun.swing.plaf.synth.SynthIcon;
1173N/A
1173N/Aimport javax.swing.plaf.synth.SynthContext;
1173N/Aimport javax.swing.*;
1173N/Aimport java.awt.*;
1173N/Aimport java.awt.image.BufferedImage;
1173N/Aimport javax.swing.plaf.UIResource;
1173N/A
1173N/A/**
1173N/A * An icon that delegates to a painter.
1173N/A * @author rbair
1173N/A */
1173N/Aclass NimbusIcon extends SynthIcon {
1173N/A private int width;
1173N/A private int height;
1173N/A private String prefix;
1173N/A private String key;
1173N/A
1173N/A NimbusIcon(String prefix, String key, int w, int h) {
1173N/A this.width = w;
1173N/A this.height = h;
1173N/A this.prefix = prefix;
1173N/A this.key = key;
1173N/A }
1173N/A
1173N/A @Override
1173N/A public void paintIcon(SynthContext context, Graphics g, int x, int y,
1173N/A int w, int h) {
1173N/A Painter painter = null;
1173N/A if (context != null) {
1173N/A painter = (Painter)context.getStyle().get(context, key);
1173N/A }
1173N/A if (painter == null){
1173N/A painter = (Painter) UIManager.get(prefix + "[Enabled]." + key);
1173N/A }
1173N/A
1173N/A if (painter != null && context != null) {
1173N/A JComponent c = context.getComponent();
1173N/A boolean rotate = false;
1173N/A boolean flip = false;
1173N/A //translatex and translatey are additional translations that
1173N/A //must occur on the graphics context when rendering a toolbar
1173N/A //icon
1173N/A int translatex = 0;
1173N/A int translatey = 0;
1173N/A if (c instanceof JToolBar) {
1173N/A JToolBar toolbar = (JToolBar)c;
1173N/A rotate = toolbar.getOrientation() == JToolBar.VERTICAL;
1173N/A flip = !toolbar.getComponentOrientation().isLeftToRight();
1173N/A Object o = NimbusLookAndFeel.resolveToolbarConstraint(toolbar);
1173N/A //we only do the +1 hack for UIResource borders, assuming
1173N/A //that the border is probably going to be our border
1173N/A if (toolbar.getBorder() instanceof UIResource) {
1173N/A if (o == BorderLayout.SOUTH) {
1173N/A translatey = 1;
1173N/A } else if (o == BorderLayout.EAST) {
1173N/A translatex = 1;
1173N/A }
1173N/A }
1450N/A } else if (c instanceof JMenu) {
1450N/A flip = ! c.getComponentOrientation().isLeftToRight();
1173N/A }
1173N/A if (g instanceof Graphics2D){
1173N/A Graphics2D gfx = (Graphics2D)g;
1173N/A gfx.translate(x, y);
1173N/A gfx.translate(translatex, translatey);
1173N/A if (rotate) {
1173N/A gfx.rotate(Math.toRadians(90));
1173N/A gfx.translate(0, -w);
1173N/A painter.paint(gfx, context.getComponent(), h, w);
1173N/A gfx.translate(0, w);
1173N/A gfx.rotate(Math.toRadians(-90));
1173N/A } else if (flip){
1173N/A gfx.scale(-1, 1);
1173N/A gfx.translate(-w,0);
1173N/A painter.paint(gfx, context.getComponent(), w, h);
1173N/A gfx.translate(w,0);
1173N/A gfx.scale(-1, 1);
1173N/A } else {
1173N/A painter.paint(gfx, context.getComponent(), w, h);
1173N/A }
1173N/A gfx.translate(-translatex, -translatey);
1173N/A gfx.translate(-x, -y);
1173N/A } else {
1173N/A // use image if we are printing to a Java 1.1 PrintGraphics as
1173N/A // it is not a instance of Graphics2D
1173N/A BufferedImage img = new BufferedImage(w,h,
1173N/A BufferedImage.TYPE_INT_ARGB);
1173N/A Graphics2D gfx = img.createGraphics();
1173N/A if (rotate) {
1173N/A gfx.rotate(Math.toRadians(90));
1173N/A gfx.translate(0, -w);
1173N/A painter.paint(gfx, context.getComponent(), h, w);
1173N/A } else if (flip){
1173N/A gfx.scale(-1, 1);
1173N/A gfx.translate(-w,0);
1173N/A painter.paint(gfx, context.getComponent(), w, h);
1173N/A } else {
1173N/A painter.paint(gfx, context.getComponent(), w, h);
1173N/A }
1173N/A gfx.dispose();
1173N/A g.drawImage(img,x,y,null);
1173N/A img = null;
1173N/A }
1173N/A }
1173N/A }
1173N/A
1173N/A /**
1173N/A * Implements the standard Icon interface's paintIcon method as the standard
1173N/A * synth stub passes null for the context and this will cause us to not
1173N/A * paint any thing, so we override here so that we can paint the enabled
1173N/A * state if no synth context is available
1173N/A */
1173N/A @Override
1173N/A public void paintIcon(Component c, Graphics g, int x, int y) {
1173N/A Painter painter = (Painter)UIManager.get(prefix + "[Enabled]." + key);
1173N/A if (painter != null){
1173N/A JComponent jc = (c instanceof JComponent) ? (JComponent)c : null;
1173N/A Graphics2D gfx = (Graphics2D)g;
1173N/A gfx.translate(x, y);
1173N/A painter.paint(gfx, jc , width, height);
1173N/A gfx.translate(-x, -y);
1173N/A }
1173N/A }
1173N/A
1173N/A @Override
1173N/A public int getIconWidth(SynthContext context) {
1173N/A if (context == null) {
1173N/A return width;
1173N/A }
1173N/A JComponent c = context.getComponent();
1173N/A if (c instanceof JToolBar && ((JToolBar)c).getOrientation() == JToolBar.VERTICAL) {
1173N/A //we only do the -1 hack for UIResource borders, assuming
1173N/A //that the border is probably going to be our border
1173N/A if (c.getBorder() instanceof UIResource) {
1173N/A return c.getWidth() - 1;
1173N/A } else {
1173N/A return c.getWidth();
1173N/A }
1173N/A } else {
1173N/A return scale(context, width);
1173N/A }
1173N/A }
1173N/A
1173N/A @Override
1173N/A public int getIconHeight(SynthContext context) {
1173N/A if (context == null) {
1173N/A return height;
1173N/A }
1173N/A JComponent c = context.getComponent();
1173N/A if (c instanceof JToolBar){
1173N/A JToolBar toolbar = (JToolBar)c;
1173N/A if (toolbar.getOrientation() == JToolBar.HORIZONTAL) {
1173N/A //we only do the -1 hack for UIResource borders, assuming
1173N/A //that the border is probably going to be our border
1173N/A if (toolbar.getBorder() instanceof UIResource) {
1173N/A return c.getHeight() - 1;
1173N/A } else {
1173N/A return c.getHeight();
1173N/A }
1173N/A } else {
1173N/A return scale(context, width);
1173N/A }
1173N/A } else {
1173N/A return scale(context, height);
1173N/A }
1173N/A }
1173N/A
1173N/A /**
1173N/A * Scale a size based on the "JComponent.sizeVariant" client property of the
1173N/A * component that is using this icon
1173N/A *
1173N/A * @param context The synthContext to get the component from
1173N/A * @param size The size to scale
1173N/A * @return The scaled size or original if "JComponent.sizeVariant" is not
1173N/A * set
1173N/A */
1173N/A private int scale(SynthContext context, int size) {
1173N/A if (context == null || context.getComponent() == null){
1173N/A return size;
1173N/A }
1173N/A // The key "JComponent.sizeVariant" is used to match Apple's LAF
1173N/A String scaleKey = (String) context.getComponent().getClientProperty(
1173N/A "JComponent.sizeVariant");
1173N/A if (scaleKey != null) {
1173N/A if (NimbusStyle.LARGE_KEY.equals(scaleKey)) {
1173N/A size *= NimbusStyle.LARGE_SCALE;
1173N/A } else if (NimbusStyle.SMALL_KEY.equals(scaleKey)) {
1173N/A size *= NimbusStyle.SMALL_SCALE;
1173N/A } else if (NimbusStyle.MINI_KEY.equals(scaleKey)) {
1173N/A // mini is not quite as small for icons as full mini is
1173N/A // just too tiny
1173N/A size *= NimbusStyle.MINI_SCALE + 0.07;
1173N/A }
1173N/A }
1173N/A return size;
1173N/A }
1173N/A}