001: /*
002: * ScrollBarUI.java
003: *
004: * Copyright (C) 2003 Peter Graves
005: * $Id: ScrollBarUI.java,v 1.2 2003/07/26 23:58:51 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.awt.Color;
025: import java.awt.Dimension;
026: import java.awt.Graphics;
027: import java.awt.Rectangle;
028: import javax.swing.JButton;
029: import javax.swing.JComponent;
030: import javax.swing.UIManager;
031: import javax.swing.plaf.ComponentUI;
032: import javax.swing.plaf.basic.BasicScrollBarUI;
033:
034: public final class ScrollBarUI extends BasicScrollBarUI {
035: public static ComponentUI createUI(JComponent c) {
036: return new ScrollBarUI();
037: }
038:
039: protected void paintThumb(Graphics g, JComponent c,
040: Rectangle thumbBounds) {
041: if (thumbBounds.isEmpty() || !scrollbar.isEnabled())
042: return;
043: int w = thumbBounds.width;
044: int h = thumbBounds.height;
045: g.translate(thumbBounds.x, thumbBounds.y);
046: g.setColor(thumbHighlightColor);
047: g.drawLine(0, 0, 0, h - 2); // Left side.
048: g.drawLine(0, 0, w - 2, 0); // Top.
049: g.setColor(thumbDarkShadowColor);
050: g.drawLine(w - 1, 0, w - 1, h - 1); // Right side.
051: g.drawLine(0, h - 1, w - 1, h - 1); // Bottom.
052: g.setColor(thumbLightShadowColor);
053: g.drawLine(w - 2, 1, w - 2, h - 2); // Right side.
054: g.drawLine(1, h - 2, w - 2, h - 2); // Bottom.
055: // Fill in the middle.
056: g.setColor(thumbColor);
057: g.fillRect(1, 1, w - 3, h - 3);
058: // Done.
059: g.translate(-thumbBounds.x, -thumbBounds.y);
060: }
061:
062: protected JButton createDecreaseButton(int orientation) {
063: return new ArrowButton(orientation);
064: }
065:
066: protected JButton createIncreaseButton(int orientation) {
067: return new ArrowButton(orientation);
068: }
069:
070: private static class ArrowButton extends JButton {
071: private final int direction;
072: private final int h;
073: private final int w;
074: private final Color thumb;
075: private final Color shadow;
076: private final Color darkShadow;
077: private final Color highlight;
078:
079: public ArrowButton(int direction) {
080: this .direction = direction;
081: h = w = UIManager.getInt("ScrollBar.width");
082: thumb = UIManager.getColor("ScrollBar.thumb");
083: shadow = UIManager.getColor("ScrollBar.thumbShadow");
084: darkShadow = UIManager
085: .getColor("ScrollBar.thumbDarkShadow");
086: highlight = UIManager.getColor("ScrollBar.thumbHighlight");
087: setRequestFocusEnabled(false);
088: }
089:
090: public void paint(Graphics g) {
091: final Color origColor = g.getColor();
092: final boolean isPressed = getModel().isPressed();
093: if (isPressed) {
094: g.setColor(shadow);
095: g.drawRect(0, 0, w - 1, h - 1);
096: } else {
097: g.setColor(highlight);
098: g.drawLine(0, 0, 0, h - 1);
099: g.drawLine(1, 0, w - 2, 0);
100: g.setColor(shadow);
101: g.drawLine(1, h - 2, w - 2, h - 2);
102: g.drawLine(w - 2, 1, w - 2, h - 3);
103: g.setColor(darkShadow);
104: g.drawLine(0, h - 1, w - 1, h - 1);
105: g.drawLine(w - 1, 0, w - 1, h - 1);
106: }
107: g.setColor(thumb);
108: g.fillRect(1, 1, w - 3, h - 3);
109: if (isPressed)
110: g.translate(1, 1);
111: paintTriangle(g);
112: if (isPressed)
113: g.translate(-1, -1);
114: g.setColor(origColor);
115: }
116:
117: public Dimension getPreferredSize() {
118: return new Dimension(w, h);
119: }
120:
121: public Dimension getMinimumSize() {
122: return new Dimension(w, h);
123: }
124:
125: public Dimension getMaximumSize() {
126: return new Dimension(w, h);
127: }
128:
129: public boolean isFocusTraversable() {
130: return false;
131: }
132:
133: private void paintTriangle(Graphics g) {
134: final int size = 4;
135: int x = (w - size) / 2;
136: int y = (h - size) / 2;
137: if (direction == NORTH)
138: --y;
139: else if (direction == WEST)
140: --x;
141: g.translate(x, y);
142: g.setColor(isEnabled() ? darkShadow : shadow);
143: final int mid = (size / 2) - 1;
144: switch (direction) {
145: case NORTH:
146: for (int i = 0; i < size; i++)
147: g.drawLine(mid - i, i, mid + i, i);
148: break;
149: case SOUTH:
150: for (int i = size, j = 0; i-- > 0;) {
151: g.drawLine(mid - i, j, mid + i, j);
152: j++;
153: }
154: break;
155: case WEST:
156: for (int i = 0; i < size; i++)
157: g.drawLine(i, mid - i, i, mid + i);
158: break;
159: case EAST:
160: for (int i = size, j = 0; i-- > 0;) {
161: g.drawLine(j, mid - i, j, mid + i);
162: j++;
163: }
164: break;
165: }
166: g.translate(-x, -y);
167: }
168: }
169: }
|