KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > ScrollBarUI


1 /*
2  * ScrollBarUI.java
3  *
4  * Copyright (C) 2003 Peter Graves
5  * $Id: ScrollBarUI.java,v 1.2 2003/07/26 23:58:51 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.awt.Color JavaDoc;
25 import java.awt.Dimension JavaDoc;
26 import java.awt.Graphics JavaDoc;
27 import java.awt.Rectangle JavaDoc;
28 import javax.swing.JButton JavaDoc;
29 import javax.swing.JComponent JavaDoc;
30 import javax.swing.UIManager JavaDoc;
31 import javax.swing.plaf.ComponentUI JavaDoc;
32 import javax.swing.plaf.basic.BasicScrollBarUI JavaDoc;
33
34 public final class ScrollBarUI extends BasicScrollBarUI JavaDoc
35 {
36     public static ComponentUI JavaDoc createUI(JComponent JavaDoc c)
37     {
38         return new ScrollBarUI();
39     }
40
41     protected void paintThumb(Graphics JavaDoc g, JComponent JavaDoc c, Rectangle JavaDoc thumbBounds)
42     {
43     if (thumbBounds.isEmpty() || !scrollbar.isEnabled())
44         return;
45         int w = thumbBounds.width;
46         int h = thumbBounds.height;
47     g.translate(thumbBounds.x, thumbBounds.y);
48         g.setColor(thumbHighlightColor);
49         g.drawLine(0, 0, 0, h - 2); // Left side.
50
g.drawLine(0, 0, w - 2, 0); // Top.
51
g.setColor(thumbDarkShadowColor);
52         g.drawLine(w - 1, 0, w - 1, h - 1); // Right side.
53
g.drawLine(0, h - 1, w - 1, h - 1); // Bottom.
54
g.setColor(thumbLightShadowColor);
55         g.drawLine(w - 2, 1, w - 2, h - 2); // Right side.
56
g.drawLine(1, h - 2, w - 2, h - 2); // Bottom.
57
// Fill in the middle.
58
g.setColor(thumbColor);
59         g.fillRect(1, 1, w - 3, h - 3);
60         // Done.
61
g.translate(-thumbBounds.x, -thumbBounds.y);
62     }
63
64     protected JButton JavaDoc createDecreaseButton(int orientation)
65     {
66         return new ArrowButton(orientation);
67     }
68
69     protected JButton JavaDoc createIncreaseButton(int orientation)
70     {
71         return new ArrowButton(orientation);
72     }
73
74     private static class ArrowButton extends JButton JavaDoc
75     {
76         private final int direction;
77         private final int h;
78         private final int w;
79         private final Color JavaDoc thumb;
80         private final Color JavaDoc shadow;
81         private final Color JavaDoc darkShadow;
82         private final Color JavaDoc highlight;
83
84         public ArrowButton(int direction)
85         {
86             this.direction = direction;
87             h = w = UIManager.getInt("ScrollBar.width");
88             thumb = UIManager.getColor("ScrollBar.thumb");
89         shadow = UIManager.getColor("ScrollBar.thumbShadow");
90         darkShadow = UIManager.getColor("ScrollBar.thumbDarkShadow");
91         highlight = UIManager.getColor("ScrollBar.thumbHighlight");
92         setRequestFocusEnabled(false);
93     }
94
95     public void paint(Graphics JavaDoc g)
96         {
97         final Color JavaDoc origColor = g.getColor();
98         final boolean isPressed = getModel().isPressed();
99             if (isPressed) {
100                 g.setColor(shadow);
101                 g.drawRect(0, 0, w - 1, h - 1);
102             } else {
103                 g.setColor(highlight);
104                 g.drawLine(0, 0, 0, h - 1);
105                 g.drawLine(1, 0, w - 2, 0);
106                 g.setColor(shadow);
107                 g.drawLine(1, h - 2, w - 2, h - 2);
108                 g.drawLine(w - 2, 1, w - 2, h - 3);
109                 g.setColor(darkShadow);
110                 g.drawLine(0, h - 1, w - 1, h - 1);
111                 g.drawLine(w - 1, 0, w - 1, h - 1);
112             }
113             g.setColor(thumb);
114             g.fillRect(1, 1, w - 3, h - 3);
115             if (isPressed)
116                 g.translate(1, 1);
117         paintTriangle(g);
118             if (isPressed)
119                 g.translate(-1, -1);
120         g.setColor(origColor);
121         }
122
123         public Dimension JavaDoc getPreferredSize()
124         {
125             return new Dimension JavaDoc(w, h);
126         }
127
128         public Dimension JavaDoc getMinimumSize()
129         {
130             return new Dimension JavaDoc(w, h);
131         }
132
133         public Dimension JavaDoc getMaximumSize()
134         {
135             return new Dimension JavaDoc(w, h);
136         }
137
138         public boolean isFocusTraversable()
139         {
140             return false;
141     }
142
143     private void paintTriangle(Graphics JavaDoc g)
144         {
145             final int size = 4;
146             int x = (w - size) / 2;
147             int y = (h - size) / 2;
148             if (direction == NORTH)
149                 --y;
150             else if (direction == WEST)
151                 --x;
152         g.translate(x, y);
153             g.setColor(isEnabled() ? darkShadow : shadow);
154         final int mid = (size / 2) - 1;
155             switch (direction) {
156                 case NORTH:
157                     for (int i = 0; i < size; i++)
158                         g.drawLine(mid - i, i, mid + i, i);
159                     break;
160                 case SOUTH:
161                     for (int i = size, j = 0; i-- > 0;) {
162                         g.drawLine(mid - i, j, mid + i, j);
163                         j++;
164                     }
165                     break;
166                 case WEST:
167                     for (int i = 0; i < size; i++)
168                         g.drawLine(i, mid - i, i, mid + i);
169                     break;
170                 case EAST:
171                     for (int i = size, j = 0; i-- > 0;) {
172                         g.drawLine(j, mid - i, j, mid + i);
173                         j++;
174                     }
175                     break;
176             }
177         g.translate(-x, -y);
178     }
179     }
180 }
181
Popular Tags