~ubuntu-branches/ubuntu/trusty/libjgoodies-forms-java/trusty

« back to all changes in this revision

Viewing changes to src/core/com/jgoodies/forms/builder/ButtonStackBuilder.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2008-02-25 10:57:07 UTC
  • mfrom: (1.2.1 upstream) (2.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080225105707-pe51fdbcq1dt3vi6
Tags: 1.2.0-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2002-2008 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;
 
34
import javax.swing.JComponent;
 
35
import javax.swing.JPanel;
 
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}.<p>
 
46
 *
 
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&hellip;' will likely
 
50
 * exceed the minimum button width. The narrow margins help getting narrow
 
51
 * stacks.
 
52
 * Note that some look&amp;feels do not support the narrow margin feature,
 
53
 * and conversely, others have only narrow margins. The JGoodies look&amp;feels
 
54
 * honor the setting, the Mac Aqua l&amp;f uses narrow margins all the time.<p>
 
55
 *
 
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.
 
61
 * <pre>
 
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();
 
71
 * }
 
72
 * </pre>
 
73
 *
 
74
 * @author      Karsten Lentzsch
 
75
 * @version $Revision: 1.8 $
 
76
 *
 
77
 * @see ButtonBarBuilder
 
78
 * @see com.jgoodies.forms.factories.ButtonBarFactory
 
79
 * @see com.jgoodies.forms.util.LayoutStyle
 
80
 */
 
81
public final class ButtonStackBuilder extends PanelBuilder {
 
82
 
 
83
    /**
 
84
     * Specifies the FormLayout's the single button stack column.
 
85
     */
 
86
    private static final ColumnSpec[] COL_SPECS =
 
87
        new ColumnSpec[] { FormFactory.BUTTON_COLSPEC };
 
88
 
 
89
    /**
 
90
     * Specifies the rows of the initial FormLayout used in constructors.
 
91
     */
 
92
    private static final RowSpec[] ROW_SPECS =
 
93
        new RowSpec[]{};
 
94
 
 
95
    /**
 
96
     * The client property key used to indicate that a button shall
 
97
     * get narrow margins on the left and right hand side.<p>
 
98
     *
 
99
     * This optional setting will be honored by all JGoodies Look&amp;Feel
 
100
     * implementations. The Mac Aqua l&amp;f uses narrow margins only.
 
101
     * Other look&amp;feel implementations will likely ignore this key
 
102
     * and so may render a wider button margin.
 
103
     */
 
104
    private static final String NARROW_KEY = "jgoodies.isNarrow";
 
105
 
 
106
 
 
107
    // Instance Creation ****************************************************
 
108
 
 
109
    /**
 
110
     * Constructs a ButtonStackBuilder  on a default JPanel
 
111
     * using a preconfigured FormLayout as layout manager.
 
112
     */
 
113
    public ButtonStackBuilder() {
 
114
        this(new JPanel(null));
 
115
    }
 
116
 
 
117
 
 
118
    /**
 
119
     * Constructs a ButtonStackBuilder on the given panel
 
120
     * using a preconfigured FormLayout as layout manager.
 
121
     *
 
122
     * @param panel   the layout container
 
123
     */
 
124
    public ButtonStackBuilder(JPanel panel) {
 
125
        this(new FormLayout(COL_SPECS, ROW_SPECS), panel);
 
126
    }
 
127
 
 
128
 
 
129
    /**
 
130
     * Constructs a ButtonStackBuilder on the given panel and layout.
 
131
     * The layout must have at least one column.
 
132
     *
 
133
     * @param layout  the FormLayout used to layout
 
134
     * @param panel   the layout container
 
135
     *
 
136
     * @since 1.2
 
137
     */
 
138
    public ButtonStackBuilder(FormLayout layout, JPanel panel) {
 
139
        super(layout, panel);
 
140
    }
 
141
 
 
142
 
 
143
    // Adding Components ****************************************************
 
144
 
 
145
    /**
 
146
     * Adds a sequence of related buttons separated by a default gap.
 
147
     *
 
148
     * @param buttons  an array of buttons to add
 
149
     */
 
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)
 
154
                addRelatedGap();
 
155
        }
 
156
    }
 
157
 
 
158
 
 
159
    /**
 
160
     * Adds a fixed size component.
 
161
     *
 
162
     * @param component  the component to add
 
163
     */
 
164
    public void addFixed(JComponent component) {
 
165
        getLayout().appendRow(FormFactory.PREF_ROWSPEC);
 
166
        add(component);
 
167
        nextRow();
 
168
    }
 
169
 
 
170
 
 
171
    /**
 
172
     * Adds a gridded component.
 
173
     *
 
174
     * @param component  the component to add
 
175
     */
 
176
    public void addGridded(JComponent component) {
 
177
        getLayout().appendRow(FormFactory.PREF_ROWSPEC);
 
178
        getLayout().addGroupedRow(getRow());
 
179
        component.putClientProperty(NARROW_KEY, Boolean.TRUE);
 
180
        add(component);
 
181
        nextRow();
 
182
    }
 
183
 
 
184
 
 
185
    /**
 
186
     * Adds a glue that will be given the extra space,
 
187
     * if this box is larger than its preferred size.
 
188
     */
 
189
    public void addGlue() {
 
190
        appendGlueRow();
 
191
        nextRow();
 
192
    }
 
193
 
 
194
 
 
195
    /**
 
196
     * Adds the standard gap for related components.
 
197
     */
 
198
    public void addRelatedGap() {
 
199
        appendRelatedComponentsGapRow();
 
200
        nextRow();
 
201
    }
 
202
 
 
203
 
 
204
    /**
 
205
     * Adds the standard gap for unrelated components.
 
206
     */
 
207
    public void addUnrelatedGap() {
 
208
        appendUnrelatedComponentsGapRow();
 
209
        nextRow();
 
210
    }
 
211
 
 
212
 
 
213
    /**
 
214
     * Adds a strut of a specified size.
 
215
     *
 
216
     * @param size  a constant that describes the gap
 
217
     */
 
218
    public void addStrut(ConstantSize size) {
 
219
        getLayout().appendRow(new RowSpec(RowSpec.TOP,
 
220
                                          size,
 
221
                                          RowSpec.NO_GROW));
 
222
        nextRow();
 
223
    }
 
224
 
 
225
 
 
226
}