2
* Copyright (c) 2002-2008 JGoodies Karsten Lentzsch. All Rights Reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions are met:
7
* o Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
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.
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.
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.
31
package com.jgoodies.forms.builder;
33
import javax.swing.JButton;
34
import javax.swing.JComponent;
35
import javax.swing.JPanel;
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;
44
* A non-visual builder that assists you in building consistent button stacks
45
* using the {@link FormLayout}.<p>
47
* This builder sets a hint for narrow margin for the gridded buttons.
48
* This can reduce the button stack's width if some buttons have long texts.
49
* For example, a stack with 'OK', 'Cancel', 'Configure…' will likely
50
* exceed the minimum button width. The narrow margins help getting narrow
52
* Note that some look&feels do not support the narrow margin feature,
53
* and conversely, others have only narrow margins. The JGoodies look&feels
54
* honor the setting, the Mac Aqua l&f uses narrow margins all the time.<p>
56
* <strong>Example:</strong><br>
57
* The following example builds a button stack with <i>Close, Up</i> and
58
* <i>Down</i>, where Up and Down are related, and Close is not related
59
* to the other buttons, which makes a wide gap for the unrelated and
60
* a smaller gap for the related buttons.
62
* private JPanel createCloseUpDownButtonStack(
63
* JButton close, JButton up, JButton down) {
64
* ButtonStackBuilder builder = new ButtonStackBuilder();
65
* builder.addGridded(close);
66
* builder.addUnrelatedGap();
67
* builder.addGridded(up);
68
* builder.addRelatedGap();
69
* builder.addGridded(down);
70
* return builder.getPanel();
74
* @author Karsten Lentzsch
75
* @version $Revision: 1.8 $
77
* @see ButtonBarBuilder
78
* @see com.jgoodies.forms.factories.ButtonBarFactory
79
* @see com.jgoodies.forms.util.LayoutStyle
81
public final class ButtonStackBuilder extends PanelBuilder {
84
* Specifies the FormLayout's the single button stack column.
86
private static final ColumnSpec[] COL_SPECS =
87
new ColumnSpec[] { FormFactory.BUTTON_COLSPEC };
90
* Specifies the rows of the initial FormLayout used in constructors.
92
private static final RowSpec[] ROW_SPECS =
96
* The client property key used to indicate that a button shall
97
* get narrow margins on the left and right hand side.<p>
99
* This optional setting will be honored by all JGoodies Look&Feel
100
* implementations. The Mac Aqua l&f uses narrow margins only.
101
* Other look&feel implementations will likely ignore this key
102
* and so may render a wider button margin.
104
private static final String NARROW_KEY = "jgoodies.isNarrow";
107
// Instance Creation ****************************************************
110
* Constructs a ButtonStackBuilder on a default JPanel
111
* using a preconfigured FormLayout as layout manager.
113
public ButtonStackBuilder() {
114
this(new JPanel(null));
119
* Constructs a ButtonStackBuilder on the given panel
120
* using a preconfigured FormLayout as layout manager.
122
* @param panel the layout container
124
public ButtonStackBuilder(JPanel panel) {
125
this(new FormLayout(COL_SPECS, ROW_SPECS), panel);
130
* Constructs a ButtonStackBuilder on the given panel and layout.
131
* The layout must have at least one column.
133
* @param layout the FormLayout used to layout
134
* @param panel the layout container
138
public ButtonStackBuilder(FormLayout layout, JPanel panel) {
139
super(layout, panel);
143
// Adding Components ****************************************************
146
* Adds a sequence of related buttons separated by a default gap.
148
* @param buttons an array of buttons to add
150
public void addButtons(JButton[] buttons) {
151
for (int i = 0; i < buttons.length; i++) {
152
addGridded(buttons[i]);
153
if (i < buttons.length - 1)
160
* Adds a fixed size component.
162
* @param component the component to add
164
public void addFixed(JComponent component) {
165
getLayout().appendRow(FormFactory.PREF_ROWSPEC);
172
* Adds a gridded component.
174
* @param component the component to add
176
public void addGridded(JComponent component) {
177
getLayout().appendRow(FormFactory.PREF_ROWSPEC);
178
getLayout().addGroupedRow(getRow());
179
component.putClientProperty(NARROW_KEY, Boolean.TRUE);
186
* Adds a glue that will be given the extra space,
187
* if this box is larger than its preferred size.
189
public void addGlue() {
196
* Adds the standard gap for related components.
198
public void addRelatedGap() {
199
appendRelatedComponentsGapRow();
205
* Adds the standard gap for unrelated components.
207
public void addUnrelatedGap() {
208
appendUnrelatedComponentsGapRow();
214
* Adds a strut of a specified size.
216
* @param size a constant that describes the gap
218
public void addStrut(ConstantSize size) {
219
getLayout().appendRow(new RowSpec(RowSpec.TOP,