KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > forms > builder > ButtonStackBuilder


1 /*
2  * Copyright (c) 2003 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.forms.builder;
32
33 import javax.swing.JButton JavaDoc;
34 import javax.swing.JComponent JavaDoc;
35 import javax.swing.JPanel JavaDoc;
36
37 import com.jgoodies.forms.factories.FormFactory;
38 import com.jgoodies.forms.layout.ColumnSpec;
39 import com.jgoodies.forms.layout.ConstantSize;
40 import com.jgoodies.forms.layout.FormLayout;
41 import com.jgoodies.forms.layout.RowSpec;
42
43 /**
44  * A non-visual builder that assists you in building consistent button stacks
45  * using the {@link FormLayout}.
46  *
47  * <p>
48  * <b>Example:</b><br>
49  * The following example builds a button stack with <i>Close, Up</i> and
50  * <i>Down</i>, where Up and Down are related, and Close is not related
51  * to the other buttons, which makes a wide gap for the unrelated and
52  * a smaller gap for the related buttons.
53  * <pre>
54  * private JPanel createCloseUpDownButtonStack(
55  * JButton close, JButton up, JButton down) {
56  * ButtonStackBuilder builder = new ButtonStackBuilder();
57  * builder.addGridded(close);
58  * builder.addUnrelatedGap();
59  * builder.addGridded(up);
60  * builder.addRelatedGap();
61  * builder.addGridded(down);
62  * return builder.getPanel();
63  * }
64  * </pre>
65  *
66  * @author Karsten Lentzsch
67  * @version $Revision: 1.2 $
68  */

69 public final class ButtonStackBuilder extends PanelBuilder {
70     
71     private static final ColumnSpec[] COL_SPECS =
72         new ColumnSpec[] { FormFactory.BUTTON_COLSPEC };
73         
74     private static final RowSpec[] ROW_SPECS =
75         new RowSpec[]{};
76     
77     private static final String JavaDoc NARROW_KEY = "jgoodies.isNarrow";
78     
79     
80     // Instance Creation ****************************************************
81

82     /**
83      * Constructs an instance of <code>ButtonStackBuilder</code> on the given
84      * panel.
85      *
86      * @param panel the layout container
87      */

88     public ButtonStackBuilder(JPanel JavaDoc panel) {
89         super(panel, new FormLayout(COL_SPECS, ROW_SPECS));
90     }
91
92     /**
93      * Constructs an instance of <code>ButtonStackBuilder</code> on a default
94      * <code>JPanel</code>.
95      */

96     public ButtonStackBuilder() {
97         this(new JPanel JavaDoc());
98     }
99
100
101     // Adding Components ****************************************************
102

103     /**
104      * Adds a sequence of related buttons separated by a default gap.
105      *
106      * @param buttons an array of buttons to add
107      */

108     public void addButtons(JButton JavaDoc[] buttons) {
109         for (int i = 0; i < buttons.length; i++) {
110             addGridded(buttons[i]);
111             if (i < buttons.length - 1)
112                 addRelatedGap();
113         }
114     }
115
116     /**
117      * Adds a fixed size component.
118      *
119      * @param component the component to add
120      */

121     public void addFixed(JComponent JavaDoc component) {
122         getLayout().appendRow(FormFactory.PREF_ROWSPEC);
123         add(component);
124         nextRow();
125     }
126
127     /**
128      * Adds a gridded component.
129      *
130      * @param component the component to add
131      */

132     public void addGridded(JComponent JavaDoc component) {
133         getLayout().appendRow(FormFactory.PREF_ROWSPEC);
134         getLayout().addGroupedRow(getRow());
135         add(component);
136         nextRow();
137     }
138
139     /**
140      * Adds a gridded narrow component.
141      *
142      * @param component the component to add
143      */

144     public void addGriddedNarrow(JComponent JavaDoc component) {
145         component.putClientProperty(NARROW_KEY, Boolean.TRUE);
146         addGridded(component);
147     }
148
149     /**
150      * Adds a glue that will be given the extra space,
151      * if this box is larger than its preferred size.
152      */

153     public void addGlue() {
154         appendGlueRow();
155         nextRow();
156     }
157
158     /**
159      * Adds the standard gap for related components.
160      */

161     public void addRelatedGap() {
162         appendRelatedComponentsGapRow();
163         nextRow();
164     }
165
166     /**
167      * Adds the standard gap for unrelated components.
168      */

169     public void addUnrelatedGap() {
170         appendUnrelatedComponentsGapRow();
171         nextRow();
172     }
173
174     /**
175      * Adds a strut of a specified size.
176      *
177      * @param size a constant that describes the gap
178      */

179     public void addStrut(ConstantSize size) {
180         getLayout().appendRow(new RowSpec(RowSpec.TOP,
181                                           size,
182                                           RowSpec.NO_GROW));
183         nextRow();
184     }
185     
186
187 }
188
Popular Tags