KickJava   Java API By Example, From Geeks To Geeks.

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


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 its
15  * contributors may be used to endorse or promote products derived from this
16  * 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, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */

30
31 package com.jgoodies.looks.windows;
32
33 import java.awt.Component JavaDoc;
34 import java.awt.LayoutManager JavaDoc;
35
36 import javax.swing.JButton JavaDoc;
37 import javax.swing.JComponent JavaDoc;
38 import javax.swing.JSpinner JavaDoc;
39 import javax.swing.SwingConstants JavaDoc;
40 import javax.swing.border.Border JavaDoc;
41 import javax.swing.border.EmptyBorder JavaDoc;
42 import javax.swing.plaf.BorderUIResource JavaDoc;
43 import javax.swing.plaf.ComponentUI JavaDoc;
44
45 import com.jgoodies.looks.LookUtils;
46 import com.jgoodies.looks.common.ExtBasicArrowButtonHandler;
47 import com.jgoodies.looks.common.ExtBasicSpinnerLayout;
48
49 /**
50  * The JGoodies Windows L&amp;F implementation of <code>SpinnerUI</code>.
51  *
52  * @author Karsten Lentzsch
53  * @version $Revision: 1.2 $
54  */

55 public final class WindowsSpinnerUI extends com.sun.java.swing.plaf.windows.WindowsSpinnerUI {
56
57     private static final Border JavaDoc EMPTY_BORDER = new BorderUIResource JavaDoc(
58                                                      new EmptyBorder JavaDoc(2, 2, 2, 2));
59
60
61     public static ComponentUI JavaDoc createUI(JComponent JavaDoc b) {
62         return new WindowsSpinnerUI();
63     }
64
65     /**
66      * The mouse/action listeners that are added to the spinner's arrow
67      * buttons. These listeners are shared by all spinner arrow buttons.
68      *
69      * @see #createNextButton
70      * @see #createPreviousButton
71      */

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

93     protected Component JavaDoc createPreviousButton() {
94         if (LookUtils.IS_LAF_WINDOWS_XP_ENABLED)
95             return super.createPreviousButton();
96         
97         JButton JavaDoc b = new WindowsArrowButton(SwingConstants.SOUTH);
98         b.addActionListener(previousButtonHandler);
99         b.addMouseListener(previousButtonHandler);
100         return b;
101     }
102
103
104     /**
105      * Create a component that will replace the spinner models value with the
106      * object returned by <code>spinner.getNextValue</code>. By default the
107      * <code>nextButton</code> is a JButton who's <code>ActionListener</code>
108      * updates it's <code>JSpinner</code> ancestors model. If a nextButton
109      * isn't needed (in a subclass) then override this method to return null.
110      *
111      * @return a component that will replace the spinners model with the next
112      * value in the sequence, or null
113      * @see #installUI
114      * @see #createPreviousButton
115      */

116     protected Component JavaDoc createNextButton() {
117         if (LookUtils.IS_LAF_WINDOWS_XP_ENABLED)
118             return super.createNextButton();
119         
120         JButton JavaDoc b = new WindowsArrowButton(SwingConstants.NORTH);
121         b.addActionListener(nextButtonHandler);
122         b.addMouseListener(nextButtonHandler);
123         return b;
124     }
125
126
127     /**
128      * This method is called by installUI to get the editor component of the
129      * <code>JSpinner</code>. By default it just returns <code>JSpinner.getEditor()</code>.
130      * Subclasses can override <code>createEditor</code> to return a
131      * component that contains the spinner's editor or null, if they're going
132      * to handle adding the editor to the <code>JSpinner</code> in an <code>installUI</code>
133      * override.
134      * <p>
135      * Typically this method would be overridden to wrap the editor with a
136      * container with a custom border, since one can't assume that the editors
137      * border can be set directly.
138      * <p>
139      * The <code>replaceEditor</code> method is called when the spinners
140      * editor is changed with <code>JSpinner.setEditor</code>. If you've
141      * overriden this method, then you'll probably want to override <code>replaceEditor</code>
142      * as well.
143      *
144      * @return the JSpinners editor JComponent, spinner.getEditor() by default
145      * @see #installUI
146      * @see #replaceEditor
147      * @see JSpinner#getEditor
148      */

149     protected JComponent JavaDoc createEditor() {
150         JComponent JavaDoc editor = spinner.getEditor();
151         configureEditor(editor);
152         return editor;
153     }
154
155
156     /**
157      * Create a <code>LayoutManager</code> that manages the <code>editor</code>,
158      * <code>nextButton</code>, and <code>previousButton</code> children
159      * of the JSpinner. These three children must be added with a constraint
160      * that identifies their role: "Editor", "Next", and "Previous". The
161      * default layout manager can handle the absence of any of these children.
162      *
163      * @return a LayoutManager for the editor, next button, and previous
164      * button.
165      * @see #createNextButton
166      * @see #createPreviousButton
167      * @see #createEditor
168      */

169     protected LayoutManager JavaDoc createLayout() {
170         return new ExtBasicSpinnerLayout();
171     }
172
173
174     /**
175      * Called by the <code>PropertyChangeListener</code> when the <code>JSpinner</code>
176      * editor property changes. It's the responsibility of this method to
177      * remove the old editor and add the new one. By default this operation is
178      * just:
179      *
180      * <pre>
181      * spinner.remove(oldEditor); spinner.add(newEditor, "Editor");
182      * </pre>
183      *
184      *
185      * The implementation of <code>replaceEditor</code> should be coordinated
186      * with the <code>createEditor</code> method.
187      *
188      * @see #createEditor
189      * @see #createPropertyChangeListener
190      */

191     protected void replaceEditor(JComponent JavaDoc oldEditor, JComponent JavaDoc newEditor) {
192         spinner.remove(oldEditor);
193         configureEditor(newEditor);
194         spinner.add(newEditor, "Editor");
195     }
196
197
198     /**
199      * Removes an obsolete Border from Default editors.
200      */

201     private void configureEditor(JComponent JavaDoc editor) {
202         if ((editor instanceof JSpinner.DefaultEditor JavaDoc)) {
203             JSpinner.DefaultEditor JavaDoc defaultEditor = (JSpinner.DefaultEditor JavaDoc) editor;
204             defaultEditor.getTextField().setBorder(EMPTY_BORDER);
205         }
206     }
207
208 }
Popular Tags