001: /*
002: * Copyright (c) 2001-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.looks.plastic;
032:
033: import java.awt.Adjustable;
034: import java.awt.Color;
035: import java.awt.Graphics;
036: import java.awt.Rectangle;
037:
038: import javax.swing.JButton;
039: import javax.swing.JComponent;
040: import javax.swing.UIManager;
041: import javax.swing.plaf.ComponentUI;
042: import javax.swing.plaf.metal.MetalScrollBarUI;
043:
044: /**
045: * The JGoodies Plastic L&F implementation of <code>ScrollBarUI</code>.
046: * Can add a pseudo 3D effect and honors the Plastic Option
047: * <tt>ScrollBar.maxBumpsWidth</tt> to limit the with of the scroll bar bumps.
048: *
049: * @author Karsten Lentzsch
050: * @version $Revision: 1.4 $
051: */
052:
053: public final class PlasticScrollBarUI extends MetalScrollBarUI {
054:
055: private static final String PROPERTY_PREFIX = "ScrollBar.";
056: public static final String MAX_BUMPS_WIDTH_KEY = PROPERTY_PREFIX
057: + "maxBumpsWidth";
058:
059: private Color shadowColor;
060: private Color highlightColor;
061: private Color darkShadowColor;
062: private Color thumbColor;
063: private Color thumbShadow;
064: private Color thumbHighlightColor;
065:
066: private PlasticBumps bumps;
067:
068: public static ComponentUI createUI(JComponent b) {
069: return new PlasticScrollBarUI();
070: }
071:
072: protected void installDefaults() {
073: super .installDefaults();
074: bumps = new PlasticBumps(10, 10, thumbHighlightColor,
075: thumbShadow, thumbColor);
076: }
077:
078: protected JButton createDecreaseButton(int orientation) {
079: decreaseButton = new PlasticArrowButton(orientation,
080: scrollBarWidth, isFreeStanding);
081: return decreaseButton;
082: }
083:
084: protected JButton createIncreaseButton(int orientation) {
085: increaseButton = new PlasticArrowButton(orientation,
086: scrollBarWidth, isFreeStanding);
087: return increaseButton;
088: }
089:
090: protected void configureScrollBarColors() {
091: super .configureScrollBarColors();
092: shadowColor = UIManager.getColor(PROPERTY_PREFIX + "shadow");
093: highlightColor = UIManager.getColor(PROPERTY_PREFIX
094: + "highlight");
095: darkShadowColor = UIManager.getColor(PROPERTY_PREFIX
096: + "darkShadow");
097: thumbColor = UIManager.getColor(PROPERTY_PREFIX + "thumb");
098: thumbShadow = UIManager.getColor(PROPERTY_PREFIX
099: + "thumbShadow");
100: thumbHighlightColor = UIManager.getColor(PROPERTY_PREFIX
101: + "thumbHighlight");
102: }
103:
104: protected void paintTrack(Graphics g, JComponent c,
105: Rectangle trackBounds) {
106: g.translate(trackBounds.x, trackBounds.y);
107:
108: boolean leftToRight = PlasticUtils.isLeftToRight(c);
109:
110: if (scrollbar.getOrientation() == Adjustable.VERTICAL) {
111: if (!isFreeStanding) {
112: if (!leftToRight) {
113: trackBounds.width += 1;
114: g.translate(-1, 0);
115: } else {
116: trackBounds.width += 2;
117: }
118: }
119:
120: if (c.isEnabled()) {
121: g.setColor(darkShadowColor);
122: g.drawLine(0, 0, 0, trackBounds.height - 1);
123: g.drawLine(trackBounds.width - 2, 0,
124: trackBounds.width - 2, trackBounds.height - 1);
125: g.drawLine(1, trackBounds.height - 1,
126: trackBounds.width - 1, trackBounds.height - 1);
127: g.drawLine(1, 0, trackBounds.width - 2, 0);
128:
129: g.setColor(shadowColor);
130: // g.setColor( Color.red);
131: g.drawLine(1, 1, 1, trackBounds.height - 2);
132: g.drawLine(1, 1, trackBounds.width - 3, 1);
133: if (scrollbar.getValue() != scrollbar.getMaximum()) { // thumb shadow
134: int y = thumbRect.y + thumbRect.height
135: - trackBounds.y;
136: g.drawLine(1, y, trackBounds.width - 1, y);
137: }
138: g.setColor(highlightColor);
139: g.drawLine(trackBounds.width - 1, 0,
140: trackBounds.width - 1, trackBounds.height - 1);
141: } else {
142: PlasticUtils.drawDisabledBorder(g, 0, 0,
143: trackBounds.width, trackBounds.height);
144: }
145:
146: if (!isFreeStanding) {
147: if (!leftToRight) {
148: trackBounds.width -= 1;
149: g.translate(1, 0);
150: } else {
151: trackBounds.width -= 2;
152: }
153: }
154: } else { // HORIZONTAL
155: if (!isFreeStanding) {
156: trackBounds.height += 2;
157: }
158:
159: if (c.isEnabled()) {
160: g.setColor(darkShadowColor);
161: g.drawLine(0, 0, trackBounds.width - 1, 0); // top
162: g.drawLine(0, 1, 0, trackBounds.height - 2); // left
163: g.drawLine(0, trackBounds.height - 2,
164: trackBounds.width - 1, trackBounds.height - 2);
165: // bottom
166: g.drawLine(trackBounds.width - 1, 1,
167: trackBounds.width - 1, trackBounds.height - 1);
168:
169: // right
170: g.setColor(shadowColor);
171: // g.setColor( Color.red);
172: g.drawLine(1, 1, trackBounds.width - 2, 1); // top
173: g.drawLine(1, 1, 1, trackBounds.height - 3); // left
174: g.drawLine(0, trackBounds.height - 1,
175: trackBounds.width - 1, trackBounds.height - 1);
176: // bottom
177: if (scrollbar.getValue() != scrollbar.getMaximum()) { // thumb shadow
178: int x = thumbRect.x + thumbRect.width
179: - trackBounds.x;
180: g.drawLine(x, 1, x, trackBounds.height - 1);
181: }
182: } else {
183: PlasticUtils.drawDisabledBorder(g, 0, 0,
184: trackBounds.width, trackBounds.height);
185: }
186:
187: if (!isFreeStanding) {
188: trackBounds.height -= 2;
189: }
190: }
191: g.translate(-trackBounds.x, -trackBounds.y);
192: }
193:
194: protected void paintThumb(Graphics g, JComponent c,
195: Rectangle thumbBounds) {
196: if (!c.isEnabled()) {
197: return;
198: }
199:
200: boolean leftToRight = PlasticUtils.isLeftToRight(c);
201:
202: g.translate(thumbBounds.x, thumbBounds.y);
203:
204: if (scrollbar.getOrientation() == Adjustable.VERTICAL) {
205: if (!isFreeStanding) {
206: if (!leftToRight) {
207: thumbBounds.width += 1;
208: g.translate(-1, 0);
209: } else {
210: thumbBounds.width += 2;
211: }
212:
213: }
214:
215: g.setColor(thumbColor);
216: g.fillRect(0, 0, thumbBounds.width - 2,
217: thumbBounds.height - 1);
218:
219: g.setColor(thumbShadow);
220: g.drawRect(0, 0, thumbBounds.width - 2,
221: thumbBounds.height - 1);
222:
223: g.setColor(thumbHighlightColor);
224: g.drawLine(1, 1, thumbBounds.width - 3, 1);
225: g.drawLine(1, 1, 1, thumbBounds.height - 2);
226:
227: paintBumps(g, c, 3, 4, thumbBounds.width - 6,
228: thumbBounds.height - 7);
229:
230: if (!isFreeStanding) {
231: if (!leftToRight) {
232: thumbBounds.width -= 1;
233: g.translate(1, 0);
234: } else {
235: thumbBounds.width -= 2;
236: }
237: }
238: } else { // HORIZONTAL
239: if (!isFreeStanding) {
240: thumbBounds.height += 2;
241: }
242:
243: g.setColor(thumbColor);
244: g.fillRect(0, 0, thumbBounds.width - 1,
245: thumbBounds.height - 2);
246:
247: g.setColor(thumbShadow);
248: g.drawRect(0, 0, thumbBounds.width - 1,
249: thumbBounds.height - 2);
250:
251: g.setColor(thumbHighlightColor);
252: g.drawLine(1, 1, thumbBounds.width - 2, 1);
253: g.drawLine(1, 1, 1, thumbBounds.height - 3);
254:
255: paintBumps(g, c, 4, 3, thumbBounds.width - 7,
256: thumbBounds.height - 6);
257:
258: if (!isFreeStanding) {
259: thumbBounds.height -= 2;
260: }
261: }
262: g.translate(-thumbBounds.x, -thumbBounds.y);
263:
264: if (PlasticUtils.is3D(PROPERTY_PREFIX))
265: paintThumb3D(g, thumbBounds);
266:
267: }
268:
269: private void paintBumps(Graphics g, JComponent c, int x, int y,
270: int width, int height) {
271: if (!useNarrowBumps()) {
272: bumps.setBumpArea(width, height);
273: bumps.paintIcon(c, g, x, y);
274: } else {
275: int maxWidth = UIManager.getInt(MAX_BUMPS_WIDTH_KEY);
276: int myWidth = Math.min(maxWidth, width);
277: int myHeight = Math.min(maxWidth, height);
278: int myX = x + (width - myWidth) / 2;
279: int myY = y + (height - myHeight) / 2;
280: bumps.setBumpArea(myWidth, myHeight);
281: bumps.paintIcon(c, g, myX, myY);
282: }
283: }
284:
285: private void paintThumb3D(Graphics g, Rectangle thumbBounds) {
286: boolean isHorizontal = scrollbar.getOrientation() == Adjustable.HORIZONTAL;
287: int width = thumbBounds.width - (isHorizontal ? 3 : 1);
288: int height = thumbBounds.height - (isHorizontal ? 1 : 3);
289: Rectangle r = new Rectangle(thumbBounds.x + 2,
290: thumbBounds.y + 2, width, height);
291: PlasticUtils.addLight3DEffekt(g, r, isHorizontal);
292: }
293:
294: // Accessing Special Client Properties **********************************************
295:
296: private boolean useNarrowBumps() {
297: Object value = UIManager.get(MAX_BUMPS_WIDTH_KEY);
298: return value != null && value instanceof Integer;
299: }
300: }
|