KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > java > swing > plaf > nimbus > NimbusIcon


1 /*
2  * @(#)NimbusIcon.java 1.8 08/01/30
3  *
4  * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7 package com.sun.java.swing.plaf.nimbus;
8
9 import com.sun.java.swing.Painter;
10 import sun.swing.plaf.synth.SynthIcon;
11
12 import javax.swing.plaf.synth.SynthContext JavaDoc;
13 import javax.swing.*;
14 import java.awt.*;
15 import java.awt.geom.AffineTransform JavaDoc;
16 import java.awt.image.BufferedImage JavaDoc;
17
18 /**
19  * An icon that delegates to a painter.
20  * @author rbair
21  */

22 class NimbusIcon extends SynthIcon {
23     private int width;
24     private int height;
25     private String JavaDoc prefix;
26     private String JavaDoc key;
27
28     NimbusIcon(String JavaDoc prefix, String JavaDoc key, int w, int h) {
29         this.width = w;
30         this.height = h;
31         this.prefix = prefix;
32         this.key = key;
33     }
34
35     @Override JavaDoc
36     public void paintIcon(SynthContext JavaDoc context, Graphics g, int x, int y,
37                           int w, int h) {
38         Painter painter = null;
39         if (context != null) {
40             painter = (Painter)context.getStyle().get(context, key);
41         }
42         if (painter == null){
43             painter = (Painter) UIManager.get(prefix + "[Enabled]." + key);
44         }
45
46         if (painter != null && context != null) {
47             JComponent c = context.getComponent();
48             boolean rotate = false;
49             boolean flip = false;
50             if (c instanceof JToolBar) {
51                 rotate = ((JToolBar)c).getOrientation() == JToolBar.VERTICAL;
52                 flip = !((JToolBar)c).getComponentOrientation().isLeftToRight();
53             }
54             if (g instanceof Graphics2D){
55                 Graphics2D gfx = (Graphics2D)g;
56                 gfx.translate(x, y);
57                 if (rotate) {
58                     gfx.rotate(Math.toRadians(90));
59                     gfx.translate(0, -w);
60                     painter.paint(gfx, context.getComponent(), h, w);
61                     gfx.translate(0, w);
62                     gfx.rotate(Math.toRadians(-90));
63                 } else if (flip){
64                     gfx.scale(-1, 1);
65                     gfx.translate(-w,0);
66                     painter.paint(gfx, context.getComponent(), w, h);
67                     gfx.translate(w,0);
68                     gfx.scale(-1, 1);
69                 } else {
70                     painter.paint(gfx, context.getComponent(), w, h);
71                 }
72                 gfx.translate(-x, -y);
73             } else {
74                 // use image if we are printing to a Java 1.1 PrintGraphics as
75
// it is not a instance of Graphics2D
76
BufferedImage JavaDoc img = new BufferedImage JavaDoc(w,h,
77                         BufferedImage.TYPE_INT_ARGB);
78                 Graphics2D gfx = img.createGraphics();
79                 if (rotate) {
80                     gfx.rotate(Math.toRadians(90));
81                     gfx.translate(0, -w);
82                     painter.paint(gfx, context.getComponent(), h, w);
83                 } else if (flip){
84                     gfx.scale(-1, 1);
85                     gfx.translate(-w,0);
86                     painter.paint(gfx, context.getComponent(), w, h);
87                 } else {
88                     painter.paint(gfx, context.getComponent(), w, h);
89                 }
90                 gfx.dispose();
91                 g.drawImage(img,x,y,null);
92                 img = null;
93             }
94         }
95     }
96
97     /**
98      * Implements the standard Icon interface's paintIcon method as the standard
99      * synth stub passes null for the context and this will cause us to not
100      * paint any thing, so we override here so that we can paint the enabled
101      * state if no synth context is available
102      */

103     @Override JavaDoc
104     public void paintIcon(Component c, Graphics g, int x, int y) {
105         Painter painter = (Painter)UIManager.get(prefix + "[Enabled]." + key);
106         if (painter != null){
107             JComponent jc = (c instanceof JComponent) ? (JComponent)c : null;
108             Graphics2D gfx = (Graphics2D)g;
109             gfx.translate(x, y);
110             painter.paint(gfx, jc , width, height);
111             gfx.translate(-x, -y);
112         }
113     }
114
115     @Override JavaDoc
116     public int getIconWidth(SynthContext JavaDoc context) {
117         if (context == null) {
118             return width;
119         }
120         JComponent c = context.getComponent();
121         if (c instanceof JToolBar && ((JToolBar)c).getOrientation() == JToolBar.VERTICAL) {
122             Insets i = c.getInsets();
123             return c.getWidth() - i.left - i.right;
124         } else {
125             return scale(context, width);
126         }
127     }
128
129     @Override JavaDoc
130     public int getIconHeight(SynthContext JavaDoc context) {
131         if (context == null) {
132             return height;
133         }
134         JComponent c = context.getComponent();
135         if (c instanceof JToolBar){
136             if (((JToolBar)c).getOrientation() == JToolBar.HORIZONTAL) {
137                 Insets i = c.getInsets();
138                 return c.getHeight() - i.top - i.bottom;
139             } else {
140                 return scale(context, width);
141             }
142         } else {
143             return scale(context, height);
144         }
145     }
146
147     /**
148      * Scale a size based on the "JComponent.sizeVariant" client property of the
149      * component that is using this icon
150      *
151      * @param context The synthContext to get the component from
152      * @param size The size to scale
153      * @return The scaled size or original if "JComponent.sizeVariant" is not
154      * set
155      */

156     private int scale(SynthContext JavaDoc context, int size) {
157         if (context == null || context.getComponent() == null){
158             return size;
159         }
160         // The key "JComponent.sizeVariant" is used to match Apple's LAF
161
String JavaDoc scaleKey = (String JavaDoc) context.getComponent().getClientProperty(
162                 "JComponent.sizeVariant");
163         if (scaleKey != null) {
164             if (NimbusStyle.LARGE_KEY.equals(scaleKey)) {
165                 size *= NimbusStyle.LARGE_SCALE;
166             } else if (NimbusStyle.SMALL_KEY.equals(scaleKey)) {
167                 size *= NimbusStyle.SMALL_SCALE;
168             } else if (NimbusStyle.MINI_KEY.equals(scaleKey)) {
169                 // mini is not quite as small for icons as full mini is
170
// just too tiny
171
size *= NimbusStyle.MINI_SCALE + 0.07;
172             }
173         }
174         return size;
175     }
176 }
177
Popular Tags