NimbusIcon.java revision 2362
0N/A/*
3261N/A * Copyright (c) 2005, 2006, 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.nimbus;
0N/A
0N/Aimport javax.swing.Painter;
0N/Aimport sun.swing.plaf.synth.SynthIcon;
0N/A
0N/Aimport javax.swing.plaf.synth.SynthContext;
0N/Aimport javax.swing.*;
0N/Aimport java.awt.*;
0N/Aimport java.awt.image.BufferedImage;
0N/Aimport javax.swing.plaf.UIResource;
0N/A
0N/A/**
0N/A * An icon that delegates to a painter.
0N/A * @author rbair
0N/A */
0N/Aclass NimbusIcon extends SynthIcon {
0N/A private int width;
0N/A private int height;
0N/A private String prefix;
0N/A private String key;
0N/A
0N/A NimbusIcon(String prefix, String key, int w, int h) {
0N/A this.width = w;
0N/A this.height = h;
0N/A this.prefix = prefix;
0N/A this.key = key;
0N/A }
0N/A
0N/A @Override
0N/A public void paintIcon(SynthContext context, Graphics g, int x, int y,
0N/A int w, int h) {
0N/A Painter painter = null;
0N/A if (context != null) {
0N/A painter = (Painter)context.getStyle().get(context, key);
0N/A }
0N/A if (painter == null){
0N/A painter = (Painter) UIManager.get(prefix + "[Enabled]." + key);
0N/A }
0N/A
0N/A if (painter != null && context != null) {
0N/A JComponent c = context.getComponent();
0N/A boolean rotate = false;
0N/A boolean flip = false;
0N/A //translatex and translatey are additional translations that
0N/A //must occur on the graphics context when rendering a toolbar
0N/A //icon
0N/A int translatex = 0;
0N/A int translatey = 0;
0N/A if (c instanceof JToolBar) {
0N/A JToolBar toolbar = (JToolBar)c;
0N/A rotate = toolbar.getOrientation() == JToolBar.VERTICAL;
0N/A flip = !toolbar.getComponentOrientation().isLeftToRight();
0N/A Object o = NimbusLookAndFeel.resolveToolbarConstraint(toolbar);
524N/A //we only do the +1 hack for UIResource borders, assuming
0N/A //that the border is probably going to be our border
0N/A if (toolbar.getBorder() instanceof UIResource) {
0N/A if (o == BorderLayout.SOUTH) {
0N/A translatey = 1;
0N/A } else if (o == BorderLayout.EAST) {
0N/A translatex = 1;
0N/A }
0N/A }
0N/A } else if (c instanceof JMenu) {
0N/A flip = ! c.getComponentOrientation().isLeftToRight();
0N/A }
0N/A if (g instanceof Graphics2D){
0N/A Graphics2D gfx = (Graphics2D)g;
0N/A gfx.translate(x, y);
0N/A gfx.translate(translatex, translatey);
0N/A if (rotate) {
0N/A gfx.rotate(Math.toRadians(90));
0N/A gfx.translate(0, -w);
0N/A painter.paint(gfx, context.getComponent(), h, w);
0N/A gfx.translate(0, w);
0N/A gfx.rotate(Math.toRadians(-90));
0N/A } else if (flip){
0N/A gfx.scale(-1, 1);
0N/A gfx.translate(-w,0);
0N/A painter.paint(gfx, context.getComponent(), w, h);
0N/A gfx.translate(w,0);
0N/A gfx.scale(-1, 1);
0N/A } else {
0N/A painter.paint(gfx, context.getComponent(), w, h);
0N/A }
0N/A gfx.translate(-translatex, -translatey);
0N/A gfx.translate(-x, -y);
0N/A } else {
0N/A // use image if we are printing to a Java 1.1 PrintGraphics as
0N/A // it is not a instance of Graphics2D
0N/A BufferedImage img = new BufferedImage(w,h,
0N/A BufferedImage.TYPE_INT_ARGB);
0N/A Graphics2D gfx = img.createGraphics();
0N/A if (rotate) {
0N/A gfx.rotate(Math.toRadians(90));
0N/A gfx.translate(0, -w);
524N/A painter.paint(gfx, context.getComponent(), h, w);
0N/A } else if (flip){
0N/A gfx.scale(-1, 1);
0N/A gfx.translate(-w,0);
524N/A painter.paint(gfx, context.getComponent(), w, h);
0N/A } else {
0N/A painter.paint(gfx, context.getComponent(), w, h);
0N/A }
0N/A gfx.dispose();
0N/A g.drawImage(img,x,y,null);
0N/A img = null;
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Implements the standard Icon interface's paintIcon method as the standard
0N/A * synth stub passes null for the context and this will cause us to not
0N/A * paint any thing, so we override here so that we can paint the enabled
0N/A * state if no synth context is available
0N/A */
0N/A @Override
0N/A public void paintIcon(Component c, Graphics g, int x, int y) {
0N/A Painter painter = (Painter)UIManager.get(prefix + "[Enabled]." + key);
0N/A if (painter != null){
0N/A JComponent jc = (c instanceof JComponent) ? (JComponent)c : null;
0N/A Graphics2D gfx = (Graphics2D)g;
0N/A gfx.translate(x, y);
0N/A painter.paint(gfx, jc , width, height);
0N/A gfx.translate(-x, -y);
0N/A }
0N/A }
0N/A
524N/A @Override
0N/A public int getIconWidth(SynthContext context) {
0N/A if (context == null) {
0N/A return width;
0N/A }
0N/A JComponent c = context.getComponent();
0N/A if (c instanceof JToolBar && ((JToolBar)c).getOrientation() == JToolBar.VERTICAL) {
0N/A //we only do the -1 hack for UIResource borders, assuming
0N/A //that the border is probably going to be our border
0N/A if (c.getBorder() instanceof UIResource) {
0N/A return c.getWidth() - 1;
0N/A } else {
0N/A return c.getWidth();
0N/A }
0N/A } else {
0N/A return scale(context, width);
0N/A }
0N/A }
0N/A
0N/A @Override
0N/A public int getIconHeight(SynthContext context) {
2494N/A if (context == null) {
0N/A return height;
2494N/A }
0N/A JComponent c = context.getComponent();
0N/A if (c instanceof JToolBar){
0N/A JToolBar toolbar = (JToolBar)c;
0N/A if (toolbar.getOrientation() == JToolBar.HORIZONTAL) {
0N/A //we only do the -1 hack for UIResource borders, assuming
0N/A //that the border is probably going to be our border
0N/A if (toolbar.getBorder() instanceof UIResource) {
0N/A return c.getHeight() - 1;
2494N/A } else {
2494N/A return c.getHeight();
2494N/A }
0N/A } else {
0N/A return scale(context, width);
0N/A }
0N/A } else {
0N/A return scale(context, height);
0N/A }
0N/A }
0N/A
0N/A /**
2494N/A * Scale a size based on the "JComponent.sizeVariant" client property of the
2494N/A * component that is using this icon
0N/A *
0N/A * @param context The synthContext to get the component from
0N/A * @param size The size to scale
0N/A * @return The scaled size or original if "JComponent.sizeVariant" is not
0N/A * set
0N/A */
0N/A private int scale(SynthContext context, int size) {
0N/A if (context == null || context.getComponent() == null){
0N/A return size;
0N/A }
0N/A // The key "JComponent.sizeVariant" is used to match Apple's LAF
0N/A String scaleKey = (String) context.getComponent().getClientProperty(
0N/A "JComponent.sizeVariant");
0N/A if (scaleKey != null) {
0N/A if (NimbusStyle.LARGE_KEY.equals(scaleKey)) {
0N/A size *= NimbusStyle.LARGE_SCALE;
0N/A } else if (NimbusStyle.SMALL_KEY.equals(scaleKey)) {
0N/A size *= NimbusStyle.SMALL_SCALE;
0N/A } else if (NimbusStyle.MINI_KEY.equals(scaleKey)) {
0N/A // mini is not quite as small for icons as full mini is
0N/A // just too tiny
0N/A size *= NimbusStyle.MINI_SCALE + 0.07;
0N/A }
0N/A }
0N/A return size;
0N/A }
2494N/A}
2494N/A