KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > common > ExtBasicSpinnerLayout


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.common;
32
33 import java.awt.Component JavaDoc;
34 import java.awt.Container JavaDoc;
35 import java.awt.Dimension JavaDoc;
36 import java.awt.Insets JavaDoc;
37 import java.awt.LayoutManager JavaDoc;
38
39 import javax.swing.UIManager JavaDoc;
40
41
42 /**
43  * A simple layout manager for the editor and the next/previous buttons.
44  * See the BasicSpinnerUI javadoc for more information about exactly how
45  * the components are arranged.
46  *
47  * @author Karsten Lentzsch
48  * @version $Revision: 1.2 $
49  */

50 public final class ExtBasicSpinnerLayout implements LayoutManager JavaDoc {
51     
52     /**
53      * Used by the default LayoutManager class - SpinnerLayout for
54      * missing (null) editor/nextButton/previousButton children.
55      */

56     private static final Dimension JavaDoc ZERO_SIZE = new Dimension JavaDoc(0, 0);
57
58     
59     private Component JavaDoc nextButton = null;
60     private Component JavaDoc previousButton = null;
61     private Component JavaDoc editor = null;
62
63
64     public void addLayoutComponent(String JavaDoc name, Component JavaDoc c) {
65         if ("Next".equals(name)) {
66             nextButton = c;
67         } else if ("Previous".equals(name)) {
68             previousButton = c;
69         } else if ("Editor".equals(name)) {
70             editor = c;
71         }
72     }
73
74
75     public void removeLayoutComponent(Component JavaDoc c) {
76         if (c == nextButton) {
77             c = null;
78         } else if (c == previousButton) {
79             previousButton = null;
80         } else if (c == editor) {
81             editor = null;
82         }
83     }
84
85
86     private Dimension JavaDoc preferredSize(Component JavaDoc c) {
87         return (c == null) ? ZERO_SIZE : c.getPreferredSize();
88     }
89
90
91     public Dimension JavaDoc preferredLayoutSize(Container JavaDoc parent) {
92         Dimension JavaDoc nextD = preferredSize(nextButton);
93         Dimension JavaDoc previousD = preferredSize(previousButton);
94         Dimension JavaDoc editorD = preferredSize(editor);
95
96         Dimension JavaDoc size = new Dimension JavaDoc(editorD.width, editorD.height);
97         size.width += Math.max(nextD.width, previousD.width);
98         Insets JavaDoc insets = parent.getInsets();
99         size.width += insets.left + insets.right;
100         size.height += insets.top + insets.bottom;
101         return size;
102     }
103
104
105     public Dimension JavaDoc minimumLayoutSize(Container JavaDoc parent) {
106         return preferredLayoutSize(parent);
107     }
108
109
110     private void setBounds(Component JavaDoc c, int x, int y, int width, int height) {
111         if (c != null) {
112             c.setBounds(x, y, width, height);
113         }
114     }
115
116
117     public void layoutContainer(Container JavaDoc parent) {
118         int width = parent.getWidth();
119         int height = parent.getHeight();
120
121         Insets JavaDoc insets = parent.getInsets();
122         Dimension JavaDoc nextD = preferredSize(nextButton);
123         Dimension JavaDoc previousD = preferredSize(previousButton);
124         int buttonsWidth = Math.max(nextD.width, previousD.width);
125         int editorHeight = height - (insets.top + insets.bottom);
126
127         // The arrowButtonInsets value is used instead of the JSpinner's
128
// insets if not null. Defining this to be (0, 0, 0, 0) causes the
129
// buttons to be aligned with the outer edge of the spinner's
130
// border, and leaving it as "null" places the buttons completely
131
// inside the spinner's border.
132
Insets JavaDoc buttonInsets = UIManager
133                 .getInsets("Spinner.arrowButtonInsets");
134         if (buttonInsets == null) {
135             buttonInsets = insets;
136         }
137
138         /*
139          * Deal with the spinner's componentOrientation property.
140          */

141         int editorX, editorWidth, buttonsX;
142         if (parent.getComponentOrientation().isLeftToRight()) {
143             editorX = insets.left;
144             editorWidth = width - insets.left - buttonsWidth
145                     - buttonInsets.right;
146             buttonsX = width - buttonsWidth - buttonInsets.right;
147         } else {
148             buttonsX = buttonInsets.left;
149             editorX = buttonsX + buttonsWidth;
150             editorWidth = width - buttonInsets.left - buttonsWidth
151                     - insets.right;
152         }
153
154         int nextY = buttonInsets.top;
155         int nextHeight = (height / 2) + (height % 2) - nextY;
156         int previousY = buttonInsets.top + nextHeight;
157         int previousHeight = height - previousY - buttonInsets.bottom;
158
159         setBounds(editor, editorX, insets.top, editorWidth, editorHeight);
160         setBounds(nextButton, buttonsX, nextY, buttonsWidth, nextHeight);
161         setBounds(previousButton, buttonsX, previousY, buttonsWidth,
162                 previousHeight);
163     }
164 }
Popular Tags