KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > windows > WindowsArrowButton


1 /*
2  * Copyright (c) 2001-2005 JGoodies Karsten Lentzsch. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * o Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * o Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * o Neither the name of JGoodies Karsten Lentzsch nor the names of
15  * its contributors may be used to endorse or promote products derived
16  * from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.looks.windows;
32
33 import java.awt.Color JavaDoc;
34 import java.awt.Dimension JavaDoc;
35 import java.awt.Graphics JavaDoc;
36
37 import javax.swing.UIManager JavaDoc;
38 import javax.swing.plaf.basic.BasicArrowButton JavaDoc;
39
40 /**
41  * The JGoodies Windows Look&Feel implementation for
42  * arrow buttons used in scrollbars and comboboxes.
43  * <p>
44  * It differs from <code>BasicArrowButton</code> in that the preferred size
45  * is always a square.
46  * It differs from <code>WindowsScrollBarUI.WindowsArrowButton</code>
47  * in that the triangle is black and positioned correctly.
48  *
49  * @author Karsten Lentzsch
50  * @version $Revision: 1.2 $
51  */

52 final class WindowsArrowButton extends BasicArrowButton JavaDoc {
53
54     public WindowsArrowButton(int direction) {
55         super(direction);
56     }
57
58     public Dimension JavaDoc getPreferredSize() {
59         int width = Math.max(5, UIManager.getInt("ScrollBar.width"));
60         return new Dimension JavaDoc(width, width);
61     }
62
63     public void paintTriangle(
64         Graphics JavaDoc g,
65         int x,
66         int y,
67         int size,
68         int triangleDirection,
69         boolean isEnabled) {
70         Color JavaDoc oldColor = g.getColor();
71         int mid, i, j;
72
73         j = 0;
74         size = Math.max(size, 2);
75         mid = (size - 1) / 2; // Modified by JGoodies
76

77         g.translate(x, y);
78         if (isEnabled)
79             g.setColor(Color.black);
80         else
81             g.setColor(UIManager.getColor("controlShadow"));
82
83         switch (triangleDirection) {
84             case NORTH :
85                 for (i = 0; i < size; i++) {
86                     g.drawLine(mid - i, i, mid + i, i);
87                 }
88                 if (!isEnabled) {
89                     g.setColor(UIManager.getColor("controlLtHighlight"));
90                     g.drawLine(mid - i + 2, i, mid + i, i);
91                 }
92                 break;
93             case SOUTH :
94                 if (!isEnabled) {
95                     g.translate(1, 1);
96                     g.setColor(UIManager.getColor("controlLtHighlight"));
97                     for (i = size - 1; i >= 0; i--) {
98                         g.drawLine(mid - i, j, mid + i, j);
99                         j++;
100                     }
101                     g.translate(-1, -1);
102                     g.setColor(UIManager.getColor("controlShadow"));
103                 }
104
105                 j = 0;
106                 for (i = size - 1; i >= 0; i--) {
107                     g.drawLine(mid - i, j, mid + i, j);
108                     j++;
109                 }
110                 break;
111             case WEST :
112                 for (i = 0; i < size; i++) {
113                     g.drawLine(i, mid - i, i, mid + i);
114                 }
115                 if (!isEnabled) {
116                     g.setColor(UIManager.getColor("controlLtHighlight"));
117                     g.drawLine(i, mid - i + 2, i, mid + i);
118                 }
119                 break;
120             case EAST :
121                 if (!isEnabled) {
122                     g.translate(1, 1);
123                     g.setColor(UIManager.getColor("controlLtHighlight"));
124                     for (i = size - 1; i >= 0; i--) {
125                         g.drawLine(j, mid - i, j, mid + i);
126                         j++;
127                     }
128                     g.translate(-1, -1);
129                     g.setColor(UIManager.getColor("controlShadow"));
130                 }
131
132                 j = 0;
133                 for (i = size - 1; i >= 0; i--) {
134                     g.drawLine(j, mid - i, j, mid + i);
135                     j++;
136                 }
137                 break;
138         }
139         g.translate(-x, -y);
140         g.setColor(oldColor);
141     }
142 }
143
Popular Tags