KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > plastic > PlasticXPSpinnerUI


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.plastic;
32
33 import java.awt.Color JavaDoc;
34 import java.awt.Component JavaDoc;
35 import java.awt.Graphics JavaDoc;
36
37 import javax.swing.JComponent JavaDoc;
38 import javax.swing.SwingConstants JavaDoc;
39 import javax.swing.UIManager JavaDoc;
40 import javax.swing.plaf.ComponentUI JavaDoc;
41
42 import com.jgoodies.looks.common.ExtBasicArrowButtonHandler;
43
44
45 /**
46  * The JGoodies PlasticXP Look&amp;Feel implementation of <code>SpinnerUI</code>.
47  * Configures the default editor to adjust font baselines and component
48  * bounds. Also, changes the border of the buttons and the size of the arrows.
49  *
50  * @author Karsten Lentzsch
51  * @version $Revision: 1.2 $
52  */

53 public final class PlasticXPSpinnerUI extends PlasticSpinnerUI {
54     
55     public static ComponentUI JavaDoc createUI(JComponent JavaDoc b) {
56         return new PlasticXPSpinnerUI();
57     }
58
59
60     /**
61      * The mouse/action listeners that are added to the spinner's
62      * arrow buttons. These listeners are shared by all
63      * spinner arrow buttons.
64      *
65      * @see #createNextButton
66      * @see #createPreviousButton
67      */

68     private static final ExtBasicArrowButtonHandler nextButtonHandler
69                                 = new ExtBasicArrowButtonHandler("increment", true);
70     private static final ExtBasicArrowButtonHandler previousButtonHandler
71                                 = new ExtBasicArrowButtonHandler("decrement", false);
72
73
74     /**
75      * Create a component that will replace the spinner models value
76      * with the object returned by <code>spinner.getPreviousValue</code>.
77      * By default the <code>previousButton</code> is a JButton
78      * who's <code>ActionListener</code> updates it's <code>JSpinner</code>
79      * ancestors model. If a previousButton isn't needed (in a subclass)
80      * then override this method to return null.
81      *
82      * @return a component that will replace the spinners model with the
83      * next value in the sequence, or null
84      * @see #installUI
85      * @see #createNextButton
86      */

87     protected Component JavaDoc createPreviousButton() {
88         return new SpinnerXPArrowButton(SwingConstants.SOUTH, previousButtonHandler);
89     }
90
91
92     /**
93      * Create a component that will replace the spinner models value
94      * with the object returned by <code>spinner.getNextValue</code>.
95      * By default the <code>nextButton</code> is a JButton
96      * who's <code>ActionListener</code> updates it's <code>JSpinner</code>
97      * ancestors model. If a nextButton isn't needed (in a subclass)
98      * then override this method to return null.
99      *
100      * @return a component that will replace the spinners model with the
101      * next value in the sequence, or null
102      * @see #installUI
103      * @see #createPreviousButton
104      */

105     protected Component JavaDoc createNextButton() {
106         return new SpinnerXPArrowButton(SwingConstants.NORTH, nextButtonHandler);
107     }
108
109
110     /**
111      * It differs from its superclass in that it uses the same formula as JDK
112      * to calculate the arrow height.
113      */

114     private static class SpinnerXPArrowButton extends PlasticArrowButton {
115
116         private SpinnerXPArrowButton(int direction,
117                 ExtBasicArrowButtonHandler handler) {
118             super(direction, UIManager.getInt("ScrollBar.width") - 2, false);
119             addActionListener(handler);
120             addMouseListener(handler);
121         }
122
123         protected int calculateArrowHeight(int height, int width) {
124             int arrowHeight = Math.min((height - 4) / 3, (width - 4) / 3);
125             return Math.max(arrowHeight, 3);
126         }
127                 
128         protected boolean isPaintingNorthBottom() {
129             return true;
130         }
131         
132         protected void paintNorth(Graphics JavaDoc g, boolean leftToRight, boolean isEnabled,
133                 Color JavaDoc arrowColor, boolean isPressed,
134                 int width, int height, int w, int h, int arrowHeight, int arrowOffset,
135                 boolean paintBottom) {
136                 if (!isFreeStanding) {
137                     height += 1;
138                     g.translate(0, -1);
139                     if (!leftToRight) {
140                         width += 1;
141                         g.translate(-1, 0);
142                     } else {
143                         width += 2;
144                     }
145                 }
146                 
147                 // Draw the arrow
148
g.setColor(arrowColor);
149                 int startY = ((h + 1) - arrowHeight) / 2; // KL was (h + 1)
150
int startX = w / 2;
151                 // System.out.println( "startX :" + startX + " startY :"+startY);
152
for (int line = 0; line < arrowHeight; line++) {
153                     g.fillRect(startX - line - arrowOffset, startY + line, 2*(line + 1), 1);
154                 }
155                 
156                 if (isEnabled) {
157                     Color JavaDoc shadowColor = UIManager.getColor("ScrollBar.darkShadow");
158
159                     g.setColor(shadowColor);
160                     g.drawLine(0, 0, width - 2, 0);
161                     g.drawLine(0, 0, 0, height - 1);
162                     g.drawLine(width - 2, 1, width - 2, height - 1);
163                     if (paintBottom) {
164                         g.fillRect(0, height - 1, width - 1, 1);
165                     }
166                 } else {
167                     PlasticUtils.drawDisabledBorder(g, 0, 0, width, height + 1);
168                     if (paintBottom) {
169                         g.setColor(PlasticLookAndFeel.getControlShadow());
170                         g.fillRect(0, height - 1, width - 1, 1);
171                     }
172                 }
173                 if (!isFreeStanding) {
174                     height -= 1;
175                     g.translate(0, 1);
176                     if (!leftToRight) {
177                         width -= 1;
178                         g.translate(1, 0);
179                     } else {
180                         width -= 2;
181                     }
182                 }
183             }
184
185         protected void paintSouth(Graphics JavaDoc g, boolean leftToRight, boolean isEnabled,
186                 Color JavaDoc arrowColor, boolean isPressed,
187                 int width, int height, int w, int h, int arrowHeight, int arrowOffset) {
188                     
189                 if (!isFreeStanding) {
190                     height += 1;
191                     if (!leftToRight) {
192                         width += 1;
193                         g.translate(-1, 0);
194                     } else {
195                         width += 2;
196                     }
197                 }
198                 
199                 // Draw the arrow
200
g.setColor(arrowColor);
201                 
202                 int startY = (((h + 0) - arrowHeight) / 2) + arrowHeight - 1; // KL was h + 1
203
int startX = w / 2;
204                 
205                 // System.out.println( "startX2 :" + startX + " startY2 :"+startY);
206

207                 for (int line = 0; line < arrowHeight; line++) {
208                     g.fillRect(startX - line - arrowOffset, startY - line, 2*(line + 1), 1);
209                 }
210                 
211                 if (isEnabled) {
212                     Color JavaDoc shadowColor = UIManager.getColor("ScrollBar.darkShadow");
213                     g.setColor(shadowColor);
214                     g.drawLine(0, 0, 0, height - 2);
215                     g.drawLine(width - 2, 0, width - 2, height - 2);
216                     //g.drawLine(1, height - 2, width - 2, height - 2);
217
} else {
218                     PlasticUtils.drawDisabledBorder(g, 0, -1, width, height + 1);
219                 }
220                 
221                 if (!isFreeStanding) {
222                     height -= 1;
223                     if (!leftToRight) {
224                         width -= 1;
225                         g.translate(1, 0);
226                     } else {
227                         width -= 2;
228                     }
229                 }
230             }
231     
232     }
233
234 }
Popular Tags