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

« back to all changes in this revision

Viewing changes to src/core/com/jgoodies/forms/builder/ButtonBarBuilder2.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.Action;
 
34
import javax.swing.JButton;
 
35
import javax.swing.JComponent;
 
36
import javax.swing.JPanel;
 
37
 
 
38
import com.jgoodies.forms.factories.Borders;
 
39
import com.jgoodies.forms.factories.FormFactory;
 
40
import com.jgoodies.forms.layout.ColumnSpec;
 
41
import com.jgoodies.forms.layout.ConstantSize;
 
42
import com.jgoodies.forms.layout.FormLayout;
 
43
import com.jgoodies.forms.layout.RowSpec;
 
44
import com.jgoodies.forms.util.LayoutStyle;
 
45
 
 
46
/**
 
47
 * A non-visual builder for building consistent button bars that comply
 
48
 * with popular style guides. Utilizes the JGoodies {@link FormLayout}
 
49
 * and honors the platform's {@link LayoutStyle} regarding button sizes,
 
50
 * gap widths, and the default button order.<p>
 
51
 *
 
52
 * This is an improved version of the older {@link ButtonBarBuilder}.
 
53
 * The ButtonBarBuilder2 has a simpler, safer, and more convenient API,
 
54
 * see below for a comparison.<p>
 
55
 *
 
56
 * <strong>ButtonBarBuilder2 vs. ButtonBarBuilder:</strong><br>
 
57
 * ButtonBarBuilder2 uses only 3 component types that can be added:
 
58
 * <em>button</em>, <em>standard</em>, and <em>growing button</em>, where
 
59
 * ButtonBarBuilder has <em>button</em>, <em>fixed</em>, and <em>growing</em>.
 
60
 * Also, the ButtonBarBuilder2 doesn't group buttons.
 
61
 * The layout of the ButtonBarBuilder and ButtonBarBuilder2 is the same
 
62
 * if all buttons are smaller than {@link LayoutStyle#getDefaultButtonWidth()}.
 
63
 * If some buttons are wider, ButtonBarBuilder2 will make only these buttons
 
64
 * wider, where the old ButtonBarBuilder makes all (gridded) buttons wider.
 
65
 *
 
66
 * <p>
 
67
 * <strong>Examples:</strong><pre>
 
68
 * // Build a right-aligned bar for: OK, Cancel, Apply
 
69
 * ButtonBarBuilder2 builder = new ButtonBarBuilder2();
 
70
 * builder.addGlue();
 
71
 * builder.addButton(okButton);
 
72
 * builder.addRelatedGap();
 
73
 * builder.addButton(cancelButton);
 
74
 * builder.addRelatedGap();
 
75
 * builder.addButton(applyButton);
 
76
 * return builder.getPanel();
 
77
 *
 
78
 * // Add a sequence of related buttons
 
79
 * ButtonBarBuilder2 builder = new ButtonBarBuilder2();
 
80
 * builder.addGlue();
 
81
 * builder.addButton(okButton, cancelButton, applyButton);
 
82
 * return builder.getPanel();
 
83
 *
 
84
 * // Add a sequence of related buttons for given Actions
 
85
 * ButtonBarBuilder2 builder = new ButtonBarBuilder2();
 
86
 * builder.addGlue();
 
87
 * builder.addButton(okAction, cancelAction, applyAction);
 
88
 * return builder.getPanel();
 
89
 * </pre>
 
90
 *
 
91
 * Buttons are added to a builder individually or as a sequence.
 
92
 *
 
93
 * To honor the platform's button order (left-to-right vs. right-to-left)
 
94
 * this builder uses the <em>leftToRightButtonOrder</em> property.
 
95
 * It is initialized with the current LayoutStyle's button order,
 
96
 * which in turn is left-to-right on most platforms and right-to-left
 
97
 * on the Mac OS X. Builder methods that create sequences of buttons
 
98
 * (e.g. {@link #addButton(JComponent[])} honor the button order.
 
99
 * If you want to ignore the default button order, you can either
 
100
 * add individual buttons, or create a ButtonBarBuilder2 instance
 
101
 * with the order set to left-to-right. For the latter see
 
102
 * {@link #createLeftToRightBuilder()}. Also see the button order
 
103
 * example below.<p>
 
104
 *
 
105
 * <strong>Example:</strong><br>
 
106
 * The following example builds a button bar with <i>Help</i> button on the
 
107
 * left-hand side and <i>OK, Cancel, Apply</i> buttons on the right-hand side.
 
108
 * <pre>
 
109
 * private JPanel createHelpOKCancelApplyBar(
 
110
 *         JButton help, JButton ok, JButton cancel, JButton apply) {
 
111
 *     ButtonBarBuilder2 builder = new ButtonBarBuilder2();
 
112
 *     builder.addButton(help);
 
113
 *     builder.addUnrelatedGap();
 
114
 *     builder.addGlue();
 
115
 *     builder.addButton(new JButton[]{ok, cancel, apply});
 
116
 *     return builder.getPanel();
 
117
 * }
 
118
 * </pre><p>
 
119
 *
 
120
 * <strong>Button Order Example:</strong><br>
 
121
 * The following example builds three button bars where one honors
 
122
 * the platform's button order and the other two ignore it.
 
123
 * <pre>
 
124
 * public JComponent buildPanel() {
 
125
 *     FormLayout layout = new FormLayout("pref");
 
126
 *     DefaultFormBuilder rowBuilder = new DefaultFormBuilder(layout);
 
127
 *     rowBuilder.setDefaultDialogBorder();
 
128
 *
 
129
 *     rowBuilder.append(buildButtonSequence(new ButtonBarBuilder2()));
 
130
 *     rowBuilder.append(buildButtonSequence(ButtonBarBuilder2.createLeftToRightBuilder()));
 
131
 *     rowBuilder.append(buildIndividualButtons(new ButtonBarBuilder2()));
 
132
 *
 
133
 *     return rowBuilder.getPanel();
 
134
 * }
 
135
 *
 
136
 * private Component buildButtonSequence(ButtonBarBuilder2 builder) {
 
137
 *     builder.addButton(new JButton[] {
 
138
 *             new JButton("One"),
 
139
 *             new JButton("Two"),
 
140
 *             new JButton("Three")
 
141
 *     });
 
142
 *     return builder.getPanel();
 
143
 * }
 
144
 *
 
145
 * private Component buildIndividualButtons(ButtonBarBuilder2 builder) {
 
146
 *     builder.addButton(new JButton("One"));
 
147
 *     builder.addRelatedGap();
 
148
 *     builder.addButton(new JButton("Two"));
 
149
 *     builder.addRelatedGap();
 
150
 *     builder.addButton(new JButton("Three"));
 
151
 *     return builder.getPanel();
 
152
 * }
 
153
 * </pre>
 
154
 *
 
155
 * @author      Karsten Lentzsch
 
156
 * @version $Revision: 1.9 $
 
157
 *
 
158
 * @see ButtonStackBuilder
 
159
 * @see com.jgoodies.forms.factories.ButtonBarFactory
 
160
 * @see com.jgoodies.forms.util.LayoutStyle
 
161
 */
 
162
public class ButtonBarBuilder2 extends AbstractButtonPanelBuilder {
 
163
 
 
164
    /**
 
165
     * Specifies the columns of the initial FormLayout used in constructors.
 
166
     */
 
167
    private static final ColumnSpec[] COL_SPECS  =
 
168
        new ColumnSpec[]{};
 
169
 
 
170
    /**
 
171
     * Specifies the FormLayout's the single button bar row.
 
172
     */
 
173
    private static final RowSpec[] ROW_SPECS  =
 
174
        new RowSpec[]{ RowSpec.decode("center:pref") };
 
175
 
 
176
 
 
177
    /**
 
178
     * The client property key used to indicate that a button shall
 
179
     * get narrow margins on the left and right hand side.<p>
 
180
     *
 
181
     * This optional setting will be honored by all JGoodies Look&amp;Feel
 
182
     * implementations. The Mac Aqua l&amp;f uses narrow margins only.
 
183
     * Other look&amp;feel implementations will likely ignore this key
 
184
     * and so may render a wider button margin.
 
185
     */
 
186
    private static final String NARROW_KEY = "jgoodies.isNarrow";
 
187
 
 
188
 
 
189
    /**
 
190
     * Describes how sequences of buttons are added to the button bar:
 
191
     * left-to-right or right-to-left. This setting is initialized using
 
192
     * the current {@link LayoutStyle}'s button order. It is honored
 
193
     * only by builder methods that build sequences of button, for example
 
194
     * {@link #addButton(JComponent[])}, and ignored if you add
 
195
     * individual button, for example using {@link #addButton(JComponent)}.
 
196
     *
 
197
     * @see #isLeftToRight()
 
198
     * @see #setLeftToRight(boolean)
 
199
     * @see #addGrowing(JComponent[])
 
200
     */
 
201
    private boolean leftToRight;
 
202
 
 
203
 
 
204
    // Instance Creation ****************************************************
 
205
 
 
206
    /**
 
207
     * Constructs an empty ButtonBarBuilder2 on a JPanel.
 
208
     */
 
209
    public ButtonBarBuilder2() {
 
210
        this(new JPanel(null));
 
211
    }
 
212
 
 
213
 
 
214
    /**
 
215
     * Constructs an empty ButtonBarBuilder2 on the given panel.
 
216
     *
 
217
     * @param panel  the layout container
 
218
     */
 
219
    public ButtonBarBuilder2(JPanel panel) {
 
220
        super(new FormLayout(COL_SPECS, ROW_SPECS), panel);
 
221
        leftToRight = LayoutStyle.getCurrent().isLeftToRightButtonOrder();
 
222
        setOpaque(false);
 
223
    }
 
224
 
 
225
 
 
226
    /**
 
227
     * Creates and returns a ButtonBarBuilder2 with
 
228
     * a left to right button order.
 
229
     *
 
230
     * @return a button bar builder with button order set to left-to-right
 
231
     */
 
232
    public static ButtonBarBuilder2 createLeftToRightBuilder() {
 
233
        ButtonBarBuilder2 builder = new ButtonBarBuilder2();
 
234
        builder.setLeftToRightButtonOrder(true);
 
235
        return builder;
 
236
    }
 
237
 
 
238
 
 
239
    // Accessing Properties *************************************************
 
240
 
 
241
    /**
 
242
     * Returns whether button sequences will be ordered from
 
243
     * left to right or from right to left.
 
244
     *
 
245
     * @return true if button sequences are ordered from left to right
 
246
     *
 
247
     * @see LayoutStyle#isLeftToRightButtonOrder()
 
248
     */
 
249
    public boolean isLeftToRightButtonOrder() {
 
250
        return leftToRight;
 
251
    }
 
252
 
 
253
 
 
254
    /**
 
255
     * Sets the order for button sequences to either left to right,
 
256
     * or right to left.
 
257
     *
 
258
     * @param newButtonOrder  true if button sequences shall be ordered
 
259
     *     from left to right
 
260
     *
 
261
     * @see LayoutStyle#isLeftToRightButtonOrder()
 
262
     */
 
263
    public void setLeftToRightButtonOrder(boolean newButtonOrder) {
 
264
        leftToRight = newButtonOrder;
 
265
    }
 
266
 
 
267
 
 
268
    // Default Borders ******************************************************
 
269
 
 
270
    /**
 
271
     * Sets a default border that has a gap in the bar's north.
 
272
     */
 
273
    public void setDefaultButtonBarGapBorder() {
 
274
        setBorder(Borders.BUTTON_BAR_GAP_BORDER);
 
275
    }
 
276
 
 
277
 
 
278
    // Spacing ****************************************************************
 
279
 
 
280
    /**
 
281
     * Adds a glue that will be given the extra space,
 
282
     * if this button bar is larger than its preferred size.
 
283
     */
 
284
    public void addGlue() {
 
285
        appendGlueColumn();
 
286
        nextColumn();
 
287
    }
 
288
 
 
289
 
 
290
    /**
 
291
     * Adds the standard horizontal gap for related components.
 
292
     *
 
293
     * @see LayoutStyle#getRelatedComponentsPadX()
 
294
     */
 
295
    public void addRelatedGap() {
 
296
        appendRelatedComponentsGapColumn();
 
297
        nextColumn();
 
298
    }
 
299
 
 
300
 
 
301
    /**
 
302
     * Adds the standard horizontal gap for unrelated components.
 
303
     *
 
304
     * @see LayoutStyle#getUnrelatedComponentsPadX()
 
305
     */
 
306
    public void addUnrelatedGap() {
 
307
        appendUnrelatedComponentsGapColumn();
 
308
        nextColumn();
 
309
    }
 
310
 
 
311
 
 
312
    /**
 
313
     * Adds a horizontal strut of the specified width.
 
314
     * For related and unrelated components use {@link #addRelatedGap()}
 
315
     * and {@link #addUnrelatedGap()} respectively.
 
316
     *
 
317
     * @param width  describes the gap width
 
318
     *
 
319
     * @see ColumnSpec#createGap(ConstantSize)
 
320
     */
 
321
    public void addStrut(ConstantSize width) {
 
322
        getLayout().appendColumn(ColumnSpec.createGap(width));
 
323
        nextColumn();
 
324
    }
 
325
 
 
326
 
 
327
    /**
 
328
     * Adds a command button component that has a minimum width
 
329
     * specified by the {@link LayoutStyle#getDefaultButtonWidth()}.<p>
 
330
     *
 
331
     * Although a JButton is expected, any JComponent is accepted
 
332
     * to allow custom button component types.
 
333
     *
 
334
     * @param button  the component to add
 
335
     *
 
336
     * @throws NullPointerException if {@code button} is {@code null}
 
337
     */
 
338
    public void addButton(JComponent button) {
 
339
        button.putClientProperty(NARROW_KEY, Boolean.TRUE);
 
340
        getLayout().appendColumn(FormFactory.BUTTON_COLSPEC);
 
341
        add(button);
 
342
        nextColumn();
 
343
    }
 
344
 
 
345
 
 
346
    /**
 
347
     * Adds a sequence of related button components.
 
348
     * Each button has the minimum width as specified by
 
349
     * {@link LayoutStyle#getDefaultButtonWidth()}. The gap width between
 
350
     * the buttons is {@link LayoutStyle#getRelatedComponentsPadX()}.<p>
 
351
     *
 
352
     * This method is equivalent to
 
353
     * <code>addButton(new JComponent[]{button1, button2});</code>
 
354
     *
 
355
     * @param button1    the first button to add
 
356
     * @param button2    the second button to add
 
357
     *
 
358
     * @throws NullPointerException if a button is {@code null}
 
359
     *
 
360
     * @see #addButton(JComponent[])
 
361
     */
 
362
    public void addButton(
 
363
            JComponent button1,
 
364
            JComponent button2) {
 
365
        addButton(new JComponent[]{button1, button2});
 
366
    }
 
367
 
 
368
 
 
369
    /**
 
370
     * Adds a sequence of related button components.
 
371
     * Each button has the minimum width as specified by
 
372
     * {@link LayoutStyle#getDefaultButtonWidth()}. The gap width between
 
373
     * the buttons is {@link LayoutStyle#getRelatedComponentsPadX()}.<p>
 
374
     *
 
375
     * This method is equivalent to
 
376
     * <code>addButton(new JComponent[]{button1, button2, button3});</code>
 
377
     *
 
378
     * @param button1    the first button to add
 
379
     * @param button2    the second button to add
 
380
     * @param button3    the third button to add
 
381
     *
 
382
     * @throws NullPointerException if a button is {@code null}
 
383
     *
 
384
     * @see #addButton(JComponent[])
 
385
     */
 
386
    public void addButton(
 
387
            JComponent button1,
 
388
            JComponent button2,
 
389
            JComponent button3) {
 
390
        addButton(new JComponent[]{button1, button2, button3});
 
391
    }
 
392
 
 
393
 
 
394
    /**
 
395
     * Adds a sequence of related button components.
 
396
     * Each button has the minimum width as specified by
 
397
     * {@link LayoutStyle#getDefaultButtonWidth()}. The gap width between
 
398
     * the buttons is {@link LayoutStyle#getRelatedComponentsPadX()}.<p>
 
399
     *
 
400
     * This method is equivalent to
 
401
     * <code>addButton(new JComponent[]{button1, button2, button3, button4});</code>
 
402
     *
 
403
     * @param button1    the first button to add
 
404
     * @param button2    the second button to add
 
405
     * @param button3    the third button to add
 
406
     * @param button4    the fourth button to add
 
407
     *
 
408
     * @throws NullPointerException if a button is {@code null}
 
409
     *
 
410
     * @see #addButton(JComponent[])
 
411
     */
 
412
    public void addButton(
 
413
            JComponent button1,
 
414
            JComponent button2,
 
415
            JComponent button3,
 
416
            JComponent button4) {
 
417
        addButton(new JComponent[]{button1, button2, button3, button4});
 
418
    }
 
419
 
 
420
 
 
421
    /**
 
422
     * Adds a sequence of related button components.
 
423
     * Each button has the minimum width as specified by
 
424
     * {@link LayoutStyle#getDefaultButtonWidth()}. The gap width between
 
425
     * the buttons is {@link LayoutStyle#getRelatedComponentsPadX()}.<p>
 
426
     *
 
427
     * This method is equivalent to
 
428
     * <code>addButton(new JComponent[]{button1, button2, button3, button4, button5});</code>
 
429
     *
 
430
     * @param button1    the first button to add
 
431
     * @param button2    the second button to add
 
432
     * @param button3    the third button to add
 
433
     * @param button4    the fourth button to add
 
434
     * @param button5    the fifth button to add
 
435
     *
 
436
     * @throws NullPointerException if a button is {@code null}
 
437
     *
 
438
     * @see #addButton(JComponent[])
 
439
     */
 
440
    public void addButton(
 
441
            JComponent button1,
 
442
            JComponent button2,
 
443
            JComponent button3,
 
444
            JComponent button4,
 
445
            JComponent button5) {
 
446
        addButton(new JComponent[]{button1, button2, button3, button4, button5});
 
447
    }
 
448
 
 
449
 
 
450
    /**
 
451
     * Adds a sequence of related button components.
 
452
     * Each button has the minimum width as specified by
 
453
     * {@link LayoutStyle#getDefaultButtonWidth()}. The gap width between
 
454
     * the buttons is {@link LayoutStyle#getRelatedComponentsPadX()}.<p>
 
455
     *
 
456
     * Uses this builder's button order (left-to-right vs. right-to-left).
 
457
     * If you  want to use a fixed order, add individual buttons instead.<p>
 
458
     *
 
459
     * Although JButtons are expected, general JComponents are accepted
 
460
     * to allow custom button component types.
 
461
     *
 
462
     * @param buttons  an array of buttons to add
 
463
     *
 
464
     * @throws NullPointerException if the button array or a button is {@code null}
 
465
     * @throws IllegalArgumentException if the button array is empty
 
466
     *
 
467
     * @see #addButton(JComponent)
 
468
     */
 
469
    public void addButton(JComponent[] buttons) {
 
470
        if (buttons == null)
 
471
            throw new NullPointerException("The button array must not be null.");
 
472
        int length = buttons.length;
 
473
        if (length == 0)
 
474
            throw new IllegalArgumentException("The button array must not be empty.");
 
475
        for (int i = 0; i < length; i++) {
 
476
            int index = leftToRight ? i : length -1 - i;
 
477
            addButton(buttons[index]);
 
478
            if (i < buttons.length - 1)
 
479
                addRelatedGap();
 
480
        }
 
481
    }
 
482
 
 
483
 
 
484
    /**
 
485
     * Adds a JButton for the given Action that has a minimum width
 
486
     * specified by the {@link LayoutStyle#getDefaultButtonWidth()}.
 
487
     *
 
488
     * @param action  the action that describes the button to add
 
489
     *
 
490
     * @throws NullPointerException if {@code action} is {@code null}
 
491
     *
 
492
     * @see #addButton(JComponent)
 
493
     */
 
494
    public void addButton(Action action) {
 
495
        if (action == null)
 
496
            throw new NullPointerException("The button Action must not be null.");
 
497
        addButton(new JButton(action));
 
498
    }
 
499
 
 
500
 
 
501
    /**
 
502
     * Adds a sequence of related JButtons built from the given Actions.
 
503
     * Each button has the minimum width as specified by
 
504
     * {@link LayoutStyle#getDefaultButtonWidth()}. The gap width between
 
505
     * the buttons is {@link LayoutStyle#getRelatedComponentsPadX()}.<p>
 
506
     *
 
507
     * This method is equivalent to
 
508
     * <code>addButton(new Action[]{action1, action2});</code>
 
509
     *
 
510
     * @param action1    describes the first button to add
 
511
     * @param action2    describes the second button to add
 
512
     *
 
513
     * @throws NullPointerException if an Action is {@code null}
 
514
     *
 
515
     * @see #addButton(Action[])
 
516
     * @see #addButton(JComponent[])
 
517
     */
 
518
    public void addButton(
 
519
            Action action1,
 
520
            Action action2) {
 
521
        addButton(new Action[]{action1, action2});
 
522
    }
 
523
 
 
524
 
 
525
    /**
 
526
     * Adds a sequence of related JButtons built from the given Actions.
 
527
     * Each button has the minimum width as specified by
 
528
     * {@link LayoutStyle#getDefaultButtonWidth()}. The gap width between
 
529
     * the buttons is {@link LayoutStyle#getRelatedComponentsPadX()}.<p>
 
530
     *
 
531
     * This method is equivalent to
 
532
     * <code>addButton(new Action[]{action1, action2, action3});</code>
 
533
     *
 
534
     * @param action1    describes the first button to add
 
535
     * @param action2    describes the second button to add
 
536
     * @param action3    describes the third button to add
 
537
     *
 
538
     * @throws NullPointerException if an Action is {@code null}
 
539
     *
 
540
     * @see #addButton(Action[])
 
541
     * @see #addButton(JComponent[])
 
542
     */
 
543
    public void addButton(
 
544
            Action action1,
 
545
            Action action2,
 
546
            Action action3) {
 
547
        addButton(new Action[]{action1, action2, action3});
 
548
    }
 
549
 
 
550
 
 
551
    /**
 
552
     * Adds a sequence of related JButtons built from the given Actions.
 
553
     * Each button has the minimum width as specified by
 
554
     * {@link LayoutStyle#getDefaultButtonWidth()}. The gap width between
 
555
     * the buttons is {@link LayoutStyle#getRelatedComponentsPadX()}.<p>
 
556
     *
 
557
     * This method is equivalent to
 
558
     * <code>addButton(new Action[]{action1, action2, action3, action4});</code>
 
559
     *
 
560
     * @param action1    describes the first button to add
 
561
     * @param action2    describes the second button to add
 
562
     * @param action3    describes the third button to add
 
563
     * @param action4    describes the fourth button to add
 
564
     *
 
565
     * @throws NullPointerException if an Action is {@code null}
 
566
     *
 
567
     * @see #addButton(Action[])
 
568
     * @see #addButton(JComponent[])
 
569
     */
 
570
    public void addButton(
 
571
            Action action1,
 
572
            Action action2,
 
573
            Action action3,
 
574
            Action action4) {
 
575
        addButton(new Action[]{action1, action2, action3, action4});
 
576
    }
 
577
 
 
578
 
 
579
    /**
 
580
     * Adds a sequence of related JButtons built from the given Actions.
 
581
     * Each button has the minimum width as specified by
 
582
     * {@link LayoutStyle#getDefaultButtonWidth()}. The gap width between
 
583
     * the buttons is {@link LayoutStyle#getRelatedComponentsPadX()}.<p>
 
584
     *
 
585
     * This method is equivalent to
 
586
     * <code>addButton(new Action[]{action1, action2, action3, action4, action5});</code>
 
587
     *
 
588
     * @param action1    describes the first button to add
 
589
     * @param action2    describes the second button to add
 
590
     * @param action3    describes the third button to add
 
591
     * @param action4    describes the fourth button to add
 
592
     * @param action5    describes the fifth button to add
 
593
     *
 
594
     * @throws NullPointerException if an Action is {@code null}
 
595
     *
 
596
     * @see #addButton(Action[])
 
597
     * @see #addButton(JComponent[])
 
598
     */
 
599
    public void addButton(
 
600
            Action action1,
 
601
            Action action2,
 
602
            Action action3,
 
603
            Action action4,
 
604
            Action action5) {
 
605
        addButton(new Action[]{action1, action2, action3, action4, action5});
 
606
    }
 
607
 
 
608
 
 
609
    /**
 
610
     * Adds a sequence of related JButtons built from the given Actions
 
611
     * that are separated by the default gap as specified by
 
612
     * {@link LayoutStyle#getRelatedComponentsPadX()}.<p>
 
613
     *
 
614
     * Uses this builder's button order (left-to-right vs. right-to-left).
 
615
     * If you  want to use a fixed order, add individual Actions instead.
 
616
     *
 
617
     * @param actions   the Actions that describe the buttons to add
 
618
     *
 
619
     * @throws NullPointerException if the Action array or an Action is {@code null}
 
620
     * @throws IllegalArgumentException if the Action array is empty
 
621
     *
 
622
     * @see #addButton(JComponent[])
 
623
     */
 
624
    public void addButton(Action[] actions) {
 
625
        if (actions == null)
 
626
            throw new NullPointerException("The Action array must not be null.");
 
627
        int length = actions.length;
 
628
        if (length == 0)
 
629
            throw new IllegalArgumentException("The Action array must not be empty.");
 
630
        JButton[] buttons = new JButton[length];
 
631
        for (int i = 0; i < length; i++) {
 
632
            buttons[i] = new JButton(actions[i]);
 
633
        }
 
634
        addButton(buttons);
 
635
    }
 
636
 
 
637
 
 
638
    // Other ******************************************************************
 
639
 
 
640
    /**
 
641
     * Adds a button or other component that grows if the container grows.
 
642
     * The component's initial size (before it grows) is specified
 
643
     * by the {@link LayoutStyle#getDefaultButtonWidth()}.
 
644
     *
 
645
     * @param component  the component to add
 
646
     */
 
647
    public void addGrowing(JComponent component) {
 
648
        getLayout().appendColumn(FormFactory.GROWING_BUTTON_COLSPEC);
 
649
        component.putClientProperty(NARROW_KEY, Boolean.TRUE);
 
650
        add(component);
 
651
        nextColumn();
 
652
    }
 
653
 
 
654
 
 
655
    /**
 
656
     * Adds a sequence of related growing buttons
 
657
     * where each is separated by a default gap.
 
658
     * Honors this builder's button order. If you
 
659
     * want to use a fixed left to right order,
 
660
     * add individual buttons.
 
661
     *
 
662
     * @param buttons  an array of buttons to add
 
663
     *
 
664
     * @see LayoutStyle
 
665
     */
 
666
    public void addGrowing(JComponent[] buttons) {
 
667
        int length = buttons.length;
 
668
        for (int i = 0; i < length; i++) {
 
669
            int index = leftToRight ? i : length -1 - i;
 
670
            addGrowing(buttons[index]);
 
671
            if (i < buttons.length - 1)
 
672
                addRelatedGap();
 
673
        }
 
674
    }
 
675
 
 
676
 
 
677
    /**
 
678
     * Adds a fixed size component with narrow margin. Unlike the gridded
 
679
     * components, this component keeps its individual preferred dimension.
 
680
     *
 
681
     * @param component  the component to add
 
682
     */
 
683
    public void addFixed(JComponent component) {
 
684
        component.putClientProperty(NARROW_KEY, Boolean.TRUE);
 
685
        getLayout().appendColumn(FormFactory.PREF_COLSPEC);
 
686
        add(component);
 
687
        nextColumn();
 
688
    }
 
689
 
 
690
 
 
691
}