001: /*
002: * SmoothGradientScrollBarUI.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.swing.plaf.smoothgradient;
023:
024: import java.awt.Adjustable;
025: import java.awt.Color;
026: import java.awt.Graphics;
027: import java.awt.Rectangle;
028:
029: import javax.swing.JButton;
030: import javax.swing.JComponent;
031: import javax.swing.UIManager;
032: import javax.swing.plaf.ComponentUI;
033: import javax.swing.plaf.metal.MetalScrollBarUI;
034:
035: /* ----------------------------------------------------------
036: * CVS NOTE: Changes to the CVS repository prior to the
037: * release of version 3.0.0beta1 has meant a
038: * resetting of CVS revision numbers.
039: * ----------------------------------------------------------
040: */
041:
042: /**
043: *
044: * @author Takis Diakoumis
045: * @version $Revision: 1.4 $
046: * @date $Date: 2006/05/14 06:56:07 $
047: */
048: public final class SmoothGradientScrollBarUI extends MetalScrollBarUI {
049:
050: private static final String PROPERTY_PREFIX = "ScrollBar.";
051: public static final String MAX_BUMPS_WIDTH_KEY = PROPERTY_PREFIX
052: + "maxBumpsWidth";
053:
054: private static Color shadowColor;
055: private static Color highlightColor;
056: private static Color darkShadowColor;
057: private static Color thumbColor;
058: private static Color thumbShadow;
059: private static Color thumbHighlightColor;
060:
061: private static Color centerLineHighlight;
062: private static Color centerLineShadow;
063:
064: // private SmoothGradientBumps bumps;
065:
066: public static ComponentUI createUI(JComponent b) {
067: return new SmoothGradientScrollBarUI();
068: }
069:
070: protected void installDefaults() {
071: super .installDefaults();
072: //bumps = new SmoothGradientBumps(10, 10, thumbHighlightColor, thumbShadow, thumbColor);
073: }
074:
075: protected JButton createDecreaseButton(int orientation) {
076: decreaseButton = new SmoothGradientArrowButton(orientation,
077: scrollBarWidth, isFreeStanding);
078: return decreaseButton;
079: }
080:
081: protected JButton createIncreaseButton(int orientation) {
082: increaseButton = new SmoothGradientArrowButton(orientation,
083: scrollBarWidth, isFreeStanding);
084: return increaseButton;
085: }
086:
087: protected void configureScrollBarColors() {
088: super .configureScrollBarColors();
089: shadowColor = UIManager.getColor(PROPERTY_PREFIX + "shadow");
090: highlightColor = UIManager.getColor(PROPERTY_PREFIX
091: + "highlight");
092: darkShadowColor = UIManager.getColor(PROPERTY_PREFIX
093: + "darkShadow");
094: thumbColor = UIManager.getColor(PROPERTY_PREFIX + "thumb");
095: thumbShadow = UIManager.getColor(PROPERTY_PREFIX
096: + "thumbShadow");
097: thumbHighlightColor = UIManager.getColor(PROPERTY_PREFIX
098: + "thumbHighlight");
099:
100: centerLineHighlight = new Color(198, 198, 229);
101: centerLineShadow = new Color(109, 109, 177);
102:
103: }
104:
105: protected void paintTrack(Graphics g, JComponent c,
106: Rectangle trackBounds) {
107: g.translate(trackBounds.x, trackBounds.y);
108:
109: boolean leftToRight = SmoothGradientUtils.isLeftToRight(c);
110:
111: if (scrollbar.getOrientation() == Adjustable.VERTICAL) {
112: if (!isFreeStanding) {
113: if (!leftToRight) {
114: trackBounds.width += 1;
115: g.translate(-1, 0);
116: } else {
117: trackBounds.width += 2;
118: }
119: }
120:
121: if (c.isEnabled()) {
122: g.setColor(darkShadowColor);
123: g.drawLine(0, 0, 0, trackBounds.height - 1);
124: g.drawLine(trackBounds.width - 2, 0,
125: trackBounds.width - 2, trackBounds.height - 1);
126: g.drawLine(1, trackBounds.height - 1,
127: trackBounds.width - 1, trackBounds.height - 1);
128: g.drawLine(1, 0, trackBounds.width - 2, 0);
129:
130: g.setColor(shadowColor);
131: // g.setColor( Color.red);
132: g.drawLine(1, 1, 1, trackBounds.height - 2);
133: g.drawLine(1, 1, trackBounds.width - 3, 1);
134: if (scrollbar.getValue() != scrollbar.getMaximum()) { // thumb shadow
135: int y = thumbRect.y + thumbRect.height
136: - trackBounds.y;
137: g.drawLine(1, y, trackBounds.width - 1, y);
138: }
139: g.setColor(highlightColor);
140: g.drawLine(trackBounds.width - 1, 0,
141: trackBounds.width - 1, trackBounds.height - 1);
142: } else {
143: SmoothGradientUtils.drawDisabledBorder(g, 0, 0,
144: trackBounds.width, trackBounds.height);
145: }
146:
147: if (!isFreeStanding) {
148: if (!leftToRight) {
149: trackBounds.width -= 1;
150: g.translate(1, 0);
151: } else {
152: trackBounds.width -= 2;
153: }
154: }
155: } else { // HORIZONTAL
156: if (!isFreeStanding) {
157: trackBounds.height += 2;
158: }
159:
160: if (c.isEnabled()) {
161: g.setColor(darkShadowColor);
162: g.drawLine(0, 0, trackBounds.width - 1, 0); // top
163: g.drawLine(0, 1, 0, trackBounds.height - 2); // left
164: g.drawLine(0, trackBounds.height - 2,
165: trackBounds.width - 1, trackBounds.height - 2);
166: // bottom
167: g.drawLine(trackBounds.width - 1, 1,
168: trackBounds.width - 1, trackBounds.height - 1);
169:
170: // right
171: g.setColor(shadowColor);
172: // g.setColor( Color.red);
173: g.drawLine(1, 1, trackBounds.width - 2, 1); // top
174: g.drawLine(1, 1, 1, trackBounds.height - 3); // left
175: g.drawLine(0, trackBounds.height - 1,
176: trackBounds.width - 1, trackBounds.height - 1);
177: // bottom
178: if (scrollbar.getValue() != scrollbar.getMaximum()) { // thumb shadow
179: int x = thumbRect.x + thumbRect.width
180: - trackBounds.x;
181: g.drawLine(x, 1, x, trackBounds.height - 1);
182: }
183: } else {
184: SmoothGradientUtils.drawDisabledBorder(g, 0, 0,
185: trackBounds.width, trackBounds.height);
186: }
187:
188: if (!isFreeStanding) {
189: trackBounds.height -= 2;
190: }
191: }
192: g.translate(-trackBounds.x, -trackBounds.y);
193: }
194:
195: protected void paintThumb(Graphics g, JComponent c,
196: Rectangle thumbBounds) {
197:
198: if (!c.isEnabled()) {
199: return;
200: }
201:
202: boolean leftToRight = SmoothGradientUtils.isLeftToRight(c);
203:
204: g.translate(thumbBounds.x, thumbBounds.y);
205:
206: if (scrollbar.getOrientation() == Adjustable.VERTICAL) {
207:
208: if (!isFreeStanding) {
209: if (!leftToRight) {
210: thumbBounds.width += 1;
211: g.translate(-1, 0);
212: } else {
213: thumbBounds.width += 2;
214: }
215:
216: }
217:
218: g.setColor(thumbColor);
219: g.fillRect(0, 0, thumbBounds.width - 2,
220: thumbBounds.height - 1);
221:
222: g.setColor(thumbShadow);
223: g.drawRect(0, 0, thumbBounds.width - 2,
224: thumbBounds.height - 1);
225:
226: g.setColor(thumbHighlightColor);
227: g.drawLine(1, 1, thumbBounds.width - 3, 1);
228: g.drawLine(1, 1, 1, thumbBounds.height - 2);
229:
230: // paintBumps(g, c, 3, 4, thumbBounds.width - 6, thumbBounds.height - 7);
231:
232: // draw the center lines
233: int lineY = 4 + (thumbBounds.height - 13) / 2;
234: g.setColor(centerLineHighlight);
235: g.drawLine(4, lineY, 11, lineY);
236: g.drawLine(4, lineY + 3, 11, lineY + 3);
237: g.drawLine(4, lineY + 6, 11, lineY + 6);
238: g.setColor(centerLineShadow);
239: g.drawLine(5, lineY + 1, 12, lineY + 1);
240: g.drawLine(5, lineY + 4, 12, lineY + 4);
241: g.drawLine(5, lineY + 7, 12, lineY + 7);
242:
243: if (!isFreeStanding) {
244: if (!leftToRight) {
245: thumbBounds.width -= 1;
246: g.translate(1, 0);
247: } else {
248: thumbBounds.width -= 2;
249: }
250: }
251: } else { // HORIZONTAL
252:
253: if (!isFreeStanding) {
254: thumbBounds.height += 2;
255: }
256:
257: g.setColor(thumbColor);
258: g.fillRect(0, 0, thumbBounds.width - 1,
259: thumbBounds.height - 2);
260:
261: g.setColor(thumbShadow);
262: g.drawRect(0, 0, thumbBounds.width - 1,
263: thumbBounds.height - 2);
264:
265: g.setColor(thumbHighlightColor);
266: g.drawLine(1, 1, thumbBounds.width - 2, 1);
267: g.drawLine(1, 1, 1, thumbBounds.height - 3);
268:
269: // paintBumps(g, c, 4, 3, thumbBounds.width - 7, thumbBounds.height - 6);
270:
271: // draw the center lines
272: int lineX = 4 + (thumbBounds.width - 7) / 2;
273: g.setColor(centerLineShadow);
274: g.drawLine(lineX, 4, lineX, 11);
275: g.drawLine(lineX + 3, 4, lineX + 3, 11);
276: g.drawLine(lineX + 6, 4, lineX + 6, 11);
277: g.setColor(centerLineHighlight);
278: g.drawLine(lineX + 1, 5, lineX + 1, 12);
279: g.drawLine(lineX + 4, 5, lineX + 4, 12);
280: g.drawLine(lineX + 7, 5, lineX + 7, 12);
281:
282: if (!isFreeStanding) {
283: thumbBounds.height -= 2;
284: }
285:
286: }
287:
288: g.translate(-thumbBounds.x, -thumbBounds.y);
289:
290: if (SmoothGradientUtils.is3D(PROPERTY_PREFIX))
291: paintThumb3D(g, thumbBounds);
292:
293: }
294:
295: /*
296: private void paintBumps(Graphics g, JComponent c, int x, int y, int width, int height) {
297: // if (!useNarrowBumps()) {
298: // bumps.setBumpArea(width, height);
299: // bumps.paintIcon(c, g, x, y);
300: // } else {
301: int MAX_WIDTH= UIManager.getInt(MAX_BUMPS_WIDTH_KEY);
302: int myWidth = Math.min(MAX_WIDTH, width);
303: int myHeight = Math.min(MAX_WIDTH, height);
304: int myX = x + (width - myWidth) / 2;
305: int myY = y + (height - myHeight) / 2;
306: bumps.setBumpArea(myWidth, myHeight);
307: bumps.paintIcon(c, g, myX, myY);
308: // }
309: }
310:
311: */
312: private void paintThumb3D(Graphics g, Rectangle thumbBounds) {
313: boolean isHorizontal = scrollbar.getOrientation() == Adjustable.HORIZONTAL;
314: int width = thumbBounds.width - (isHorizontal ? 3 : 1);
315: int height = thumbBounds.height - (isHorizontal ? 1 : 3);
316: Rectangle r = new Rectangle(thumbBounds.x + 2,
317: thumbBounds.y + 2, width, height);
318: SmoothGradientUtils.addLight3DEffekt(g, r, isHorizontal);
319: }
320:
321: // Accessing Special Client Properties **********************************************
322:
323: private boolean useNarrowBumps() {
324: Object value = UIManager.get(MAX_BUMPS_WIDTH_KEY);
325: return value != null && value instanceof Integer;
326: }
327: }
|