KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > plastic > PlasticScrollBarUI


1 /*
2  * Copyright (c) 2001-2005 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.looks.plastic;
32
33 import java.awt.Adjustable JavaDoc;
34 import java.awt.Color JavaDoc;
35 import java.awt.Graphics JavaDoc;
36 import java.awt.Rectangle JavaDoc;
37
38 import javax.swing.JButton JavaDoc;
39 import javax.swing.JComponent JavaDoc;
40 import javax.swing.UIManager JavaDoc;
41 import javax.swing.plaf.ComponentUI JavaDoc;
42 import javax.swing.plaf.metal.MetalScrollBarUI JavaDoc;
43
44
45 /**
46  * The JGoodies Plastic L&amp;F implementation of <code>ScrollBarUI</code>.
47  * Can add a pseudo 3D effect and honors the Plastic Option
48  * <tt>ScrollBar.maxBumpsWidth</tt> to limit the with of the scroll bar bumps.
49  *
50  * @author Karsten Lentzsch
51  * @version $Revision: 1.2 $
52  */

53
54 public final class PlasticScrollBarUI extends MetalScrollBarUI JavaDoc {
55     
56     private static final String JavaDoc PROPERTY_PREFIX = "ScrollBar.";
57     public static final String JavaDoc MAX_BUMPS_WIDTH_KEY = PROPERTY_PREFIX + "maxBumpsWidth";
58     
59     private static Color JavaDoc shadowColor;
60     private static Color JavaDoc highlightColor;
61     private static Color JavaDoc darkShadowColor;
62     private static Color JavaDoc thumbColor;
63     private static Color JavaDoc thumbShadow;
64     private static Color JavaDoc thumbHighlightColor;
65
66     private PlasticBumps bumps;
67
68
69     public static ComponentUI JavaDoc createUI(JComponent JavaDoc b) {
70         return new PlasticScrollBarUI();
71     }
72     
73     
74     protected void installDefaults() {
75         super.installDefaults();
76         bumps = new PlasticBumps(10, 10, thumbHighlightColor, thumbShadow, thumbColor);
77     }
78     
79     
80     protected JButton JavaDoc createDecreaseButton(int orientation) {
81         decreaseButton = new PlasticArrowButton(orientation, scrollBarWidth, isFreeStanding);
82         return decreaseButton;
83     }
84     
85     
86     protected JButton JavaDoc createIncreaseButton(int orientation) {
87         increaseButton = new PlasticArrowButton(orientation, scrollBarWidth, isFreeStanding);
88         return increaseButton;
89     }
90     
91     
92     protected void configureScrollBarColors() {
93         super.configureScrollBarColors();
94         shadowColor = UIManager.getColor(PROPERTY_PREFIX + "shadow");
95         highlightColor = UIManager.getColor(PROPERTY_PREFIX + "highlight");
96         darkShadowColor = UIManager.getColor(PROPERTY_PREFIX + "darkShadow");
97         thumbColor = UIManager.getColor(PROPERTY_PREFIX + "thumb");
98         thumbShadow = UIManager.getColor(PROPERTY_PREFIX + "thumbShadow");
99         thumbHighlightColor = UIManager.getColor(PROPERTY_PREFIX + "thumbHighlight");
100     }
101     
102     
103     protected void paintTrack(Graphics JavaDoc g, JComponent JavaDoc c, Rectangle JavaDoc trackBounds) {
104         g.translate(trackBounds.x, trackBounds.y);
105
106         boolean leftToRight = PlasticUtils.isLeftToRight(c);
107
108         if (scrollbar.getOrientation() == Adjustable.VERTICAL) {
109             if (!isFreeStanding) {
110                 if (!leftToRight) {
111                     trackBounds.width += 1;
112                     g.translate(-1, 0);
113                 } else {
114                     trackBounds.width += 2;
115                 }
116             }
117
118             if (c.isEnabled()) {
119                 g.setColor(darkShadowColor);
120                 g.drawLine(0, 0, 0, trackBounds.height - 1);
121                 g.drawLine(trackBounds.width - 2, 0, trackBounds.width - 2, trackBounds.height - 1);
122                 g.drawLine(1, trackBounds.height - 1, trackBounds.width - 1, trackBounds.height - 1);
123                 g.drawLine(1, 0, trackBounds.width - 2, 0);
124
125                 g.setColor(shadowColor);
126                 // g.setColor( Color.red);
127
g.drawLine(1, 1, 1, trackBounds.height - 2);
128                 g.drawLine(1, 1, trackBounds.width - 3, 1);
129                 if (scrollbar.getValue() != scrollbar.getMaximum()) { // thumb shadow
130
int y = thumbRect.y + thumbRect.height - trackBounds.y;
131                     g.drawLine(1, y, trackBounds.width - 1, y);
132                 }
133                 g.setColor(highlightColor);
134                 g.drawLine(trackBounds.width - 1, 0, trackBounds.width - 1, trackBounds.height - 1);
135             } else {
136                 PlasticUtils.drawDisabledBorder(g, 0, 0, trackBounds.width, trackBounds.height);
137             }
138
139             if (!isFreeStanding) {
140                 if (!leftToRight) {
141                     trackBounds.width -= 1;
142                     g.translate(1, 0);
143                 } else {
144                     trackBounds.width -= 2;
145                 }
146             }
147         } else { // HORIZONTAL
148
if (!isFreeStanding) {
149                 trackBounds.height += 2;
150             }
151
152             if (c.isEnabled()) {
153                 g.setColor(darkShadowColor);
154                 g.drawLine(0, 0, trackBounds.width - 1, 0); // top
155
g.drawLine(0, 1, 0, trackBounds.height - 2); // left
156
g.drawLine(0, trackBounds.height - 2, trackBounds.width - 1, trackBounds.height - 2);
157                 // bottom
158
g.drawLine(trackBounds.width - 1, 1, trackBounds.width - 1, trackBounds.height - 1);
159
160                 // right
161
g.setColor(shadowColor);
162                 // g.setColor( Color.red);
163
g.drawLine(1, 1, trackBounds.width - 2, 1); // top
164
g.drawLine(1, 1, 1, trackBounds.height - 3); // left
165
g.drawLine(0, trackBounds.height - 1, trackBounds.width - 1, trackBounds.height - 1);
166                 // bottom
167
if (scrollbar.getValue() != scrollbar.getMaximum()) { // thumb shadow
168
int x = thumbRect.x + thumbRect.width - trackBounds.x;
169                     g.drawLine(x, 1, x, trackBounds.height - 1);
170                 }
171             } else {
172                 PlasticUtils.drawDisabledBorder(g, 0, 0, trackBounds.width, trackBounds.height);
173             }
174
175             if (!isFreeStanding) {
176                 trackBounds.height -= 2;
177             }
178         }
179         g.translate(-trackBounds.x, -trackBounds.y);
180     }
181     
182     
183     protected void paintThumb(Graphics JavaDoc g, JComponent JavaDoc c, Rectangle JavaDoc thumbBounds) {
184         if (!c.isEnabled()) {
185             return;
186         }
187
188         boolean leftToRight = PlasticUtils.isLeftToRight(c);
189
190         g.translate(thumbBounds.x, thumbBounds.y);
191
192         if (scrollbar.getOrientation() == Adjustable.VERTICAL) {
193             if (!isFreeStanding) {
194                 if (!leftToRight) {
195                     thumbBounds.width += 1;
196                     g.translate(-1, 0);
197                 } else {
198                     thumbBounds.width += 2;
199                 }
200
201             }
202
203             g.setColor(thumbColor);
204             g.fillRect(0, 0, thumbBounds.width - 2, thumbBounds.height - 1);
205
206             g.setColor(thumbShadow);
207             g.drawRect(0, 0, thumbBounds.width - 2, thumbBounds.height - 1);
208
209             g.setColor(thumbHighlightColor);
210             g.drawLine(1, 1, thumbBounds.width - 3, 1);
211             g.drawLine(1, 1, 1, thumbBounds.height - 2);
212
213             paintBumps(g, c, 3, 4, thumbBounds.width - 6, thumbBounds.height - 7);
214
215             if (!isFreeStanding) {
216                 if (!leftToRight) {
217                     thumbBounds.width -= 1;
218                     g.translate(1, 0);
219                 } else {
220                     thumbBounds.width -= 2;
221                 }
222             }
223         } else { // HORIZONTAL
224
if (!isFreeStanding) {
225                 thumbBounds.height += 2;
226             }
227
228             g.setColor(thumbColor);
229             g.fillRect(0, 0, thumbBounds.width - 1, thumbBounds.height - 2);
230
231             g.setColor(thumbShadow);
232             g.drawRect(0, 0, thumbBounds.width - 1, thumbBounds.height - 2);
233
234             g.setColor(thumbHighlightColor);
235             g.drawLine(1, 1, thumbBounds.width - 2, 1);
236             g.drawLine(1, 1, 1, thumbBounds.height - 3);
237
238             paintBumps(g, c, 4, 3, thumbBounds.width - 7, thumbBounds.height - 6);
239
240             if (!isFreeStanding) {
241                 thumbBounds.height -= 2;
242             }
243         }
244         g.translate(-thumbBounds.x, -thumbBounds.y);
245
246         if (PlasticUtils.is3D(PROPERTY_PREFIX))
247             paintThumb3D(g, thumbBounds);
248         
249     }
250     
251     
252     private void paintBumps(Graphics JavaDoc g, JComponent JavaDoc c, int x, int y, int width, int height) {
253         if (!useNarrowBumps()) {
254             bumps.setBumpArea(width, height);
255             bumps.paintIcon(c, g, x, y);
256         } else {
257             int MAX_WIDTH= UIManager.getInt(MAX_BUMPS_WIDTH_KEY);
258             int myWidth = Math.min(MAX_WIDTH, width);
259             int myHeight = Math.min(MAX_WIDTH, height);
260             int myX = x + (width - myWidth) / 2;
261             int myY = y + (height - myHeight) / 2;
262             bumps.setBumpArea(myWidth, myHeight);
263             bumps.paintIcon(c, g, myX, myY);
264         }
265     }
266     
267     
268     private void paintThumb3D(Graphics JavaDoc g, Rectangle JavaDoc thumbBounds) {
269         boolean isHorizontal = scrollbar.getOrientation() == Adjustable.HORIZONTAL;
270         int width = thumbBounds.width - (isHorizontal ? 3 : 1);
271         int height = thumbBounds.height - (isHorizontal ? 1 : 3);
272         Rectangle JavaDoc r = new Rectangle JavaDoc(thumbBounds.x + 2, thumbBounds.y + 2, width, height);
273         PlasticUtils.addLight3DEffekt(g, r, isHorizontal);
274     }
275     
276     
277     // Accessing Special Client Properties **********************************************
278

279     private boolean useNarrowBumps() {
280         Object JavaDoc value = UIManager.get(MAX_BUMPS_WIDTH_KEY);
281         return value != null && value instanceof Integer JavaDoc;
282     }
283 }
Popular Tags