001: /*
002: * Copyright 2005 Patrick Gotthardt
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package com.pagosoft.plaf;
017:
018: import javax.swing.*;
019: import javax.swing.plaf.metal.*;
020: import java.awt.*;
021: import java.awt.event.*;
022:
023: public class PgsScrollBarButton extends MetalScrollButton {
024: private static Color shadowColor;
025: private static Color highlightColor;
026: private boolean isFreeStanding = false;
027:
028: public PgsScrollBarButton(int direction, int width,
029: boolean freeStanding) {
030: super (direction, width, freeStanding);
031:
032: shadowColor = UIManager.getColor("ScrollBar.darkShadow");
033: highlightColor = UIManager.getColor("ScrollBar.highlight");
034: isFreeStanding = freeStanding;
035:
036: putClientProperty("rolloverBackground", UIManager
037: .getColor("Button.rolloverBackground"));
038: putClientProperty("pgs.isFlat", UIManager.get("Button.isFlat"));
039: putClientProperty("gradientStart", UIManager
040: .get("Button.gradientStart"));
041: putClientProperty("gradientEnd", UIManager
042: .get("Button.gradientEnd"));
043: putClientProperty("rollover.gradientStart", UIManager
044: .get("Button.rolloverGradientStart"));
045: putClientProperty("rollover.gradientEnd", UIManager
046: .get("Button.rolloverGradientEnd"));
047:
048: //setRolloverEnabled(true);
049: //addMouseListener(new RolloverHandler());
050: }
051:
052: private class RolloverHandler extends MouseAdapter {
053: public void mouseEntered(MouseEvent e) {
054: AbstractButton b = (AbstractButton) e.getSource();
055: ButtonModel model = b.getModel();
056: if (b.isRolloverEnabled()
057: && !SwingUtilities.isLeftMouseButton(e)) {
058: model.setRollover(true);
059: }
060: if (model.isPressed()) {
061: model.setArmed(true);
062: }
063: b.repaint();
064: }
065:
066: public void mouseExited(MouseEvent e) {
067: AbstractButton b = (AbstractButton) e.getSource();
068: ButtonModel model = b.getModel();
069: if (b.isRolloverEnabled()) {
070: model.setRollover(false);
071: }
072: if (model.isPressed()) {
073: model.setArmed(false);
074: }
075: b.repaint();
076: }
077: }
078:
079: public void setFreeStanding(boolean freeStanding) {
080: super .setFreeStanding(freeStanding);
081: isFreeStanding = freeStanding;
082: }
083:
084: public void paint(Graphics g) {
085: boolean leftToRight = PgsUtils.isLeftToRight(this );
086: boolean isEnabled = getParent().isEnabled();
087: boolean isPressed = getModel().isPressed();
088:
089: Color arrowColor = isEnabled ? PgsLookAndFeel.getControlInfo()
090: : PgsLookAndFeel.getControlDisabled();
091: int width = getWidth();
092: int height = getHeight();
093: int arrowHeight = (height + 1) / 4;
094:
095: // @todo The background is not completly painted
096: if (isPressed) {
097: PgsUtils.drawGradient(g, -1, 0, getWidth(), getHeight(),
098: UIManager.getColor("Button.select"), UIManager
099: .getColor("Button.select").brighter());
100: } else {
101: PgsUtils.drawGradient(g, this );
102: }
103:
104: if (getDirection() == NORTH) {
105: paintNorth(g, leftToRight, isEnabled, arrowColor,
106: isPressed, width, height, width, height,
107: arrowHeight);
108: } else if (getDirection() == SOUTH) {
109: paintSouth(g, leftToRight, isEnabled, arrowColor,
110: isPressed, width, height, width, height,
111: arrowHeight);
112: } else if (getDirection() == EAST) {
113: paintEast(g, isEnabled, arrowColor, isPressed, width,
114: height, width, height, arrowHeight);
115: } else if (getDirection() == WEST) {
116: paintWest(g, isEnabled, arrowColor, isPressed, width,
117: height, width, height, arrowHeight);
118: }
119:
120: if (getModel().isEnabled() && getModel().isRollover()) {
121: PgsUtils.drawButtonBorder(g, 1, 1, width - 3, height - 3,
122: PgsUtils.rolloverBorderStroke, PgsLookAndFeel
123: .getGlow());
124: }
125: g.setColor(PgsLookAndFeel.getControlDarkShadow());
126: g.drawRect(0, 0, width - 1, height - 1);
127: }
128:
129: /* The following code is taken from JGoodies and is copyrighted by Karsten Lentzsch */
130: /*
131: * Copyright (c) 2003 JGoodies Karsten Lentzsch. All Rights Reserved.
132: *
133: * Redistribution and use in source and binary forms, with or without
134: * modification, are permitted provided that the following conditions are met:
135: *
136: * o Redistributions of source code must retain the above copyright notice,
137: * this list of conditions and the following disclaimer.
138: *
139: * o Redistributions in binary form must reproduce the above copyright notice,
140: * this list of conditions and the following disclaimer in the documentation
141: * and/or other materials provided with the distribution.
142: *
143: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
144: * its contributors may be used to endorse or promote products derived
145: * from this software without specific prior written permission.
146: *
147: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
148: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
149: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
150: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
151: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
152: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
153: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
154: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
155: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
156: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
157: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
158: */
159: private void paintWest(Graphics g, boolean isEnabled,
160: Color arrowColor, boolean isPressed, int width, int height,
161: int w, int h, int arrowHeight) {
162:
163: if (!isFreeStanding) {
164: height += 2;
165: width += 1;
166: g.translate(-1, 0);
167: }
168:
169: // Draw the arrow
170: g.setColor(arrowColor);
171:
172: int startX = (((w + 1) - arrowHeight) / 2);
173: int startY = (h / 2);
174:
175: for (int line = 0; line < arrowHeight; line++) {
176: g.drawLine(startX + line, startY - line, startX + line,
177: startY + line);
178: }
179:
180: if (isEnabled) {
181: g.setColor(highlightColor);
182:
183: if (!isPressed) {
184: g.drawLine(1, 1, width - 1, 1);
185: g.drawLine(1, 1, 1, height - 3);
186: }
187: g.drawLine(1, height - 1, width - 1, height - 1);
188:
189: g.setColor(shadowColor);
190: g.drawLine(0, 0, width - 1, 0);
191: g.drawLine(0, 0, 0, height - 2);
192: g.drawLine(2, height - 2, width - 1, height - 2);
193: } else {
194: PgsUtils.drawDisabledBorder(g, 0, 0, width + 1, height);
195: }
196:
197: if (!isFreeStanding) {
198: height -= 2;
199: width -= 1;
200: g.translate(1, 0);
201: }
202: }
203:
204: private void paintEast(Graphics g, boolean isEnabled,
205: Color arrowColor, boolean isPressed, int width, int height,
206: int w, int h, int arrowHeight) {
207: if (!isFreeStanding) {
208: height += 2;
209: width += 1;
210: }
211:
212: // Draw the arrow
213: g.setColor(arrowColor);
214:
215: int startX = (((w + 1) - arrowHeight) / 2) + arrowHeight - 1;
216: int startY = (h / 2);
217: for (int line = 0; line < arrowHeight; line++) {
218: g.drawLine(startX - line, startY - line, startX - line,
219: startY + line);
220: }
221:
222: if (isEnabled) {
223: g.setColor(highlightColor);
224: if (!isPressed) {
225: g.drawLine(0, 1, width - 3, 1);
226: g.drawLine(0, 1, 0, height - 3);
227: }
228: g.drawLine(width - 1, 1, width - 1, height - 1);
229: g.drawLine(0, height - 1, width - 1, height - 1);
230:
231: g.setColor(shadowColor);
232: g.drawLine(0, 0, width - 2, 0);
233: g.drawLine(width - 2, 1, width - 2, height - 2);
234: g.drawLine(0, height - 2, width - 2, height - 2);
235: } else {
236: PgsUtils.drawDisabledBorder(g, -1, 0, width + 1, height);
237: }
238: if (!isFreeStanding) {
239: height -= 2;
240: width -= 1;
241: }
242: }
243:
244: private void paintSouth(Graphics g, boolean leftToRight,
245: boolean isEnabled, Color arrowColor, boolean isPressed,
246: int width, int height, int w, int h, int arrowHeight) {
247:
248: if (!isFreeStanding) {
249: height += 1;
250: if (!leftToRight) {
251: width += 1;
252: g.translate(-1, 0);
253: } else {
254: width += 2;
255: }
256: }
257:
258: // Draw the arrow
259: g.setColor(arrowColor);
260:
261: int startY = (((h + 1) - arrowHeight) / 2) + arrowHeight - 1;
262: int startX = (w / 2);
263:
264: // System.out.println( "startX2 :" + startX + " startY2 :"+startY);
265:
266: for (int line = 0; line < arrowHeight; line++) {
267: g.drawLine(startX - line, startY - line, startX + line,
268: startY - line);
269: }
270:
271: if (isEnabled) {
272: g.setColor(highlightColor);
273: if (!isPressed) {
274: g.drawLine(1, 0, width - 3, 0);
275: g.drawLine(1, 0, 1, height - 3);
276: }
277: g.drawLine(1, height - 1, width - 1, height - 1);
278: g.drawLine(width - 1, 0, width - 1, height - 1);
279:
280: g.setColor(shadowColor);
281: g.drawLine(0, 0, 0, height - 2);
282: g.drawLine(width - 2, 0, width - 2, height - 2);
283: g.drawLine(1, height - 2, width - 2, height - 2);
284: } else {
285: PgsUtils.drawDisabledBorder(g, 0, -1, width, height + 1);
286: }
287:
288: if (!isFreeStanding) {
289: height -= 1;
290: if (!leftToRight) {
291: width -= 1;
292: g.translate(1, 0);
293: } else {
294: width -= 2;
295: }
296: }
297: }
298:
299: private void paintNorth(Graphics g, boolean leftToRight,
300: boolean isEnabled, Color arrowColor, boolean isPressed,
301: int width, int height, int w, int h, int arrowHeight) {
302: if (!isFreeStanding) {
303: height += 1;
304: g.translate(0, -1);
305: if (!leftToRight) {
306: width += 1;
307: g.translate(-1, 0);
308: } else {
309: width += 2;
310: }
311: }
312:
313: // Draw the arrow
314: g.setColor(arrowColor);
315: int startY = ((h + 1) - arrowHeight) / 2;
316: int startX = (w / 2);
317: // System.out.println( "startX :" + startX + " startY :"+startY);
318: for (int line = 0; line < arrowHeight; line++) {
319: g.drawLine(startX - line, startY + line, startX + line,
320: startY + line);
321: }
322:
323: if (isEnabled) {
324: g.setColor(highlightColor);
325:
326: if (!isPressed) {
327: g.drawLine(1, 1, width - 3, 1);
328: g.drawLine(1, 1, 1, height - 1);
329: }
330:
331: g.drawLine(width - 1, 1, width - 1, height - 1);
332:
333: g.setColor(shadowColor);
334: g.drawLine(0, 0, width - 2, 0);
335: g.drawLine(0, 0, 0, height - 1);
336: g.drawLine(width - 2, 2, width - 2, height - 1);
337: } else {
338: PgsUtils.drawDisabledBorder(g, 0, 0, width, height + 1);
339: }
340: if (!isFreeStanding) {
341: height -= 1;
342: g.translate(0, 1);
343: if (!leftToRight) {
344: width -= 1;
345: g.translate(1, 0);
346: } else {
347: width -= 2;
348: }
349: }
350: }
351: }
|