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.Color;
034: import java.awt.Graphics;
035: import java.awt.Rectangle;
036:
037: import javax.swing.ButtonModel;
038: import javax.swing.UIManager;
039: import javax.swing.plaf.metal.MetalScrollButton;
040:
041: /**
042: * Renders the arrow buttons in scroll bars and spinners.
043: *
044: * @author Karsten Lentzsch
045: * @version $Revision: 1.4 $
046: */
047: class PlasticArrowButton extends MetalScrollButton {
048:
049: private Color shadowColor;
050: private Color highlightColor;
051:
052: protected boolean isFreeStanding;
053:
054: public PlasticArrowButton(int direction, int width,
055: boolean freeStanding) {
056: super (direction, width, freeStanding);
057: shadowColor = UIManager.getColor("ScrollBar.darkShadow");
058: highlightColor = UIManager.getColor("ScrollBar.highlight");
059: isFreeStanding = freeStanding;
060: }
061:
062: public void setFreeStanding(boolean freeStanding) {
063: super .setFreeStanding(freeStanding);
064: isFreeStanding = freeStanding;
065: }
066:
067: public void paint(Graphics g) {
068: boolean leftToRight = PlasticUtils.isLeftToRight(this );
069: boolean isEnabled = getParent().isEnabled();
070: boolean isPressed = getModel().isPressed();
071:
072: Color arrowColor = isEnabled ? PlasticLookAndFeel
073: .getControlInfo() : PlasticLookAndFeel
074: .getControlDisabled();
075: int width = getWidth();
076: int height = getHeight();
077: int w = width;
078: int h = height;
079: int arrowHeight = calculateArrowHeight(height, width);
080: int arrowOffset = calculateArrowOffset();
081: boolean paintNorthBottom = isPaintingNorthBottom();
082:
083: g.setColor(isPressed ? PlasticLookAndFeel.getControlShadow()
084: : getBackground());
085: g.fillRect(0, 0, width, height);
086:
087: if (getDirection() == NORTH) {
088: paintNorth(g, leftToRight, isEnabled, arrowColor,
089: isPressed, width, height, w, h, arrowHeight,
090: arrowOffset, paintNorthBottom);
091: } else if (getDirection() == SOUTH) {
092: paintSouth(g, leftToRight, isEnabled, arrowColor,
093: isPressed, width, height, w, h, arrowHeight,
094: arrowOffset);
095: } else if (getDirection() == EAST) {
096: paintEast(g, isEnabled, arrowColor, isPressed, width,
097: height, w, h, arrowHeight);
098: } else if (getDirection() == WEST) {
099: paintWest(g, isEnabled, arrowColor, isPressed, width,
100: height, w, h, arrowHeight);
101: }
102: if (PlasticUtils.is3D("ScrollBar."))
103: paint3D(g);
104: }
105:
106: /**
107: * Computes and returns the arrow height based on the specified
108: * buttons height and width.
109: *
110: * @param height the height of the button to be used for calculation.
111: * @param width the width of the button to be used for calculation.
112: * @return the height of the arrow
113: */
114: protected int calculateArrowHeight(int height, int width) {
115: return (height + 1) / 4;
116: }
117:
118: protected int calculateArrowOffset() {
119: return 0;
120: }
121:
122: protected boolean isPaintingNorthBottom() {
123: return false;
124: }
125:
126: private void paintWest(Graphics g, boolean isEnabled,
127: Color arrowColor, boolean isPressed, int width, int height,
128: int w, int h, int arrowHeight) {
129:
130: if (!isFreeStanding) {
131: height += 2;
132: width += 1;
133: g.translate(-1, 0);
134: }
135:
136: // Draw the arrow
137: g.setColor(arrowColor);
138:
139: int startX = (((w + 1) - arrowHeight) / 2);
140: int startY = (h / 2);
141:
142: for (int line = 0; line < arrowHeight; line++) {
143: g.drawLine(startX + line, startY - line, startX + line,
144: startY + line + 1);
145: }
146:
147: if (isEnabled) {
148: g.setColor(highlightColor);
149:
150: if (!isPressed) {
151: g.drawLine(1, 1, width - 1, 1);
152: g.drawLine(1, 1, 1, height - 3);
153: }
154: g.drawLine(1, height - 1, width - 1, height - 1);
155:
156: g.setColor(shadowColor);
157: g.drawLine(0, 0, width - 1, 0);
158: g.drawLine(0, 0, 0, height - 2);
159: g.drawLine(1, height - 2, width - 1, height - 2);
160: } else {
161: PlasticUtils.drawDisabledBorder(g, 0, 0, width + 1, height);
162: }
163:
164: if (!isFreeStanding) {
165: height -= 2;
166: width -= 1;
167: g.translate(1, 0);
168: }
169: }
170:
171: private void paintEast(Graphics g, boolean isEnabled,
172: Color arrowColor, boolean isPressed, int width, int height,
173: int w, int h, int arrowHeight) {
174: if (!isFreeStanding) {
175: height += 2;
176: width += 1;
177: }
178:
179: // Draw the arrow
180: g.setColor(arrowColor);
181:
182: int startX = (((w + 1) - arrowHeight) / 2) + arrowHeight - 1;
183: int startY = (h / 2);
184: for (int line = 0; line < arrowHeight; line++) {
185: g.drawLine(startX - line, startY - line, startX - line,
186: startY + line + 1);
187: }
188:
189: if (isEnabled) {
190: g.setColor(highlightColor);
191: if (!isPressed) {
192: g.drawLine(0, 1, width - 3, 1);
193: g.drawLine(0, 1, 0, height - 3);
194: }
195: g.drawLine(width - 1, 1, width - 1, height - 1);
196: g.drawLine(0, height - 1, width - 1, height - 1);
197:
198: g.setColor(shadowColor);
199: g.drawLine(0, 0, width - 2, 0);
200: g.drawLine(width - 2, 1, width - 2, height - 2);
201: g.drawLine(0, height - 2, width - 2, height - 2);
202: } else {
203: PlasticUtils
204: .drawDisabledBorder(g, -1, 0, width + 1, height);
205: }
206: if (!isFreeStanding) {
207: height -= 2;
208: width -= 1;
209: }
210: }
211:
212: protected void paintSouth(Graphics g, boolean leftToRight,
213: boolean isEnabled, Color arrowColor, boolean isPressed,
214: int width, int height, int w, int h, int arrowHeight,
215: int arrowOffset) {
216:
217: if (!isFreeStanding) {
218: height += 1;
219: if (!leftToRight) {
220: width += 1;
221: g.translate(-1, 0);
222: } else {
223: width += 2;
224: }
225: }
226:
227: // Draw the arrow
228: g.setColor(arrowColor);
229:
230: int startY = (((h + 0) - arrowHeight) / 2) + arrowHeight - 1; // KL was h + 1
231: int startX = w / 2;
232:
233: // System.out.println( "startX2 :" + startX + " startY2 :"+startY);
234:
235: for (int line = 0; line < arrowHeight; line++) {
236: g.fillRect(startX - line - arrowOffset, startY - line,
237: 2 * (line + 1), 1);
238: }
239:
240: if (isEnabled) {
241: g.setColor(highlightColor);
242: if (!isPressed) {
243: g.drawLine(1, 0, width - 3, 0);
244: g.drawLine(1, 0, 1, height - 3);
245: }
246: g.drawLine(0, height - 1, width - 1, height - 1);
247: g.drawLine(width - 1, 0, width - 1, height - 1);
248:
249: g.setColor(shadowColor);
250: g.drawLine(0, 0, 0, height - 2);
251: g.drawLine(width - 2, 0, width - 2, height - 2);
252: g.drawLine(1, height - 2, width - 2, height - 2);
253: } else {
254: PlasticUtils
255: .drawDisabledBorder(g, 0, -1, width, height + 1);
256: }
257:
258: if (!isFreeStanding) {
259: height -= 1;
260: if (!leftToRight) {
261: width -= 1;
262: g.translate(1, 0);
263: } else {
264: width -= 2;
265: }
266: }
267: }
268:
269: protected void paintNorth(Graphics g, boolean leftToRight,
270: boolean isEnabled, Color arrowColor, boolean isPressed,
271: int width, int height, int w, int h, int arrowHeight,
272: int arrowOffset, boolean paintBottom) {
273: if (!isFreeStanding) {
274: height += 1;
275: g.translate(0, -1);
276: if (!leftToRight) {
277: width += 1;
278: g.translate(-1, 0);
279: } else {
280: width += 2;
281: }
282: }
283:
284: // Draw the arrow
285: g.setColor(arrowColor);
286: int startY = ((h + 1) - arrowHeight) / 2; // KL was (h + 1)
287: int startX = w / 2;
288: // System.out.println( "startX :" + startX + " startY :"+startY);
289: for (int line = 0; line < arrowHeight; line++) {
290: g.fillRect(startX - line - arrowOffset, startY + line,
291: 2 * (line + 1), 1);
292: }
293:
294: if (isEnabled) {
295: g.setColor(highlightColor);
296:
297: if (!isPressed) {
298: g.drawLine(1, 1, width - 3, 1);
299: g.drawLine(1, 1, 1, height - 1);
300: }
301:
302: g.drawLine(width - 1, 1, width - 1, height - 1);
303:
304: g.setColor(shadowColor);
305: g.drawLine(0, 0, width - 2, 0);
306: g.drawLine(0, 0, 0, height - 1);
307: g.drawLine(width - 2, 1, width - 2, height - 1);
308: if (paintBottom) {
309: g.fillRect(0, height - 1, width - 1, 1);
310: }
311: } else {
312: PlasticUtils.drawDisabledBorder(g, 0, 0, width, height + 1);
313: if (paintBottom) {
314: g.setColor(PlasticLookAndFeel.getControlShadow());
315: g.fillRect(0, height - 1, width - 1, 1);
316: }
317: }
318: if (!isFreeStanding) {
319: height -= 1;
320: g.translate(0, 1);
321: if (!leftToRight) {
322: width -= 1;
323: g.translate(1, 0);
324: } else {
325: width -= 2;
326: }
327: }
328: }
329:
330: private void paint3D(Graphics g) {
331: ButtonModel buttonModel = getModel();
332: if (buttonModel.isArmed() && buttonModel.isPressed()
333: || buttonModel.isSelected())
334: return;
335:
336: int width = getWidth();
337: int height = getHeight();
338: if (getDirection() == EAST)
339: width -= 2;
340: else if (getDirection() == SOUTH)
341: height -= 2;
342:
343: Rectangle r = new Rectangle(1, 1, width, height);
344: boolean isHorizontal = (getDirection() == EAST || getDirection() == WEST);
345: PlasticUtils.addLight3DEffekt(g, r, isHorizontal);
346: }
347: }
|