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

« back to all changes in this revision

Viewing changes to src/core/com/jgoodies/forms/factories/Borders.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.factories;
 
32
 
 
33
import java.awt.Component;
 
34
import java.awt.Insets;
 
35
 
 
36
import javax.swing.border.AbstractBorder;
 
37
import javax.swing.border.Border;
 
38
 
 
39
import com.jgoodies.forms.layout.ConstantSize;
 
40
import com.jgoodies.forms.layout.Sizes;
 
41
import com.jgoodies.forms.util.LayoutStyle;
 
42
 
 
43
/**
 
44
 * Provides constants and factory methods for <code>Border</code>s that use
 
45
 * instances of {@link ConstantSize} to define the margins.<p>
 
46
 *
 
47
 * <strong>Examples:</strong><br>
 
48
 * <pre>
 
49
 * Borders.DLU2_BORDER
 
50
 * Borders.createEmptyBorder(Sizes.DLUY4, Sizes.DLUX2, Sizes.DLUY4, Sizes.DLUX2);
 
51
 * Borders.createEmptyBorder("4dlu, 2dlu, 4dlu, 2dlu");
 
52
 * </pre>
 
53
 *
 
54
 * @author  Karsten Lentzsch
 
55
 * @version $Revision: 1.10 $
 
56
 *
 
57
 * @see     Border
 
58
 * @see     Sizes
 
59
 */
 
60
public final class Borders {
 
61
 
 
62
    private Borders() {
 
63
        // Overrides default constructor; prevents instantiation.
 
64
    }
 
65
 
 
66
 
 
67
    // Constant Borders *****************************************************
 
68
 
 
69
    /**
 
70
     * A prepared and reusable EmptyBorder without gaps.
 
71
     */
 
72
    public static final Border EMPTY_BORDER =
 
73
        new javax.swing.border.EmptyBorder(0, 0, 0, 0);
 
74
 
 
75
 
 
76
    /**
 
77
     * A prepared and reusable Border with 2dlu on all sides.
 
78
     */
 
79
    public static final Border DLU2_BORDER =
 
80
        createEmptyBorder(Sizes.DLUY2,
 
81
                          Sizes.DLUX2,
 
82
                          Sizes.DLUY2,
 
83
                          Sizes.DLUX2);
 
84
 
 
85
 
 
86
    /**
 
87
     * A prepared and reusable Border with 4dlu on all sides.
 
88
     */
 
89
    public static final Border DLU4_BORDER =
 
90
        createEmptyBorder(Sizes.DLUY4,
 
91
                          Sizes.DLUX4,
 
92
                          Sizes.DLUY4,
 
93
                          Sizes.DLUX4);
 
94
 
 
95
 
 
96
    /**
 
97
     * A prepared and reusable Border with 7dlu on all sides.
 
98
     */
 
99
    public static final Border DLU7_BORDER =
 
100
        createEmptyBorder(Sizes.DLUY7,
 
101
                          Sizes.DLUX7,
 
102
                          Sizes.DLUY7,
 
103
                          Sizes.DLUX7);
 
104
 
 
105
 
 
106
    /**
 
107
     * A prepared Border with 14dlu on all sides.
 
108
     */
 
109
    public static final Border DLU14_BORDER =
 
110
        createEmptyBorder(Sizes.DLUY14,
 
111
                          Sizes.DLUX14,
 
112
                          Sizes.DLUY14,
 
113
                          Sizes.DLUX14);
 
114
 
 
115
 
 
116
    /**
 
117
     * A prepared Border with 21dlu on all sides.
 
118
     *
 
119
     * @since 1.2
 
120
     */
 
121
    public static final Border DLU21_BORDER =
 
122
        createEmptyBorder(Sizes.DLUY21,
 
123
                          Sizes.DLUX21,
 
124
                          Sizes.DLUY21,
 
125
                          Sizes.DLUX21);
 
126
 
 
127
 
 
128
    /**
 
129
     * A standardized Border that describes the gap between a component
 
130
     * and a button bar in its bottom.
 
131
     */
 
132
    public static final Border BUTTON_BAR_GAP_BORDER =
 
133
        createEmptyBorder(
 
134
            LayoutStyle.getCurrent().getButtonBarPad(),
 
135
            Sizes.dluX(0),
 
136
            Sizes.dluY(0),
 
137
            Sizes.dluX(0));
 
138
 
 
139
 
 
140
    /**
 
141
     * A standardized Border that describes the border around
 
142
     * a dialog content that has no tabs.
 
143
     *
 
144
     * @see #TABBED_DIALOG_BORDER
 
145
     */
 
146
    public static final Border DIALOG_BORDER =
 
147
        createEmptyBorder(
 
148
            LayoutStyle.getCurrent().getDialogMarginY(),
 
149
            LayoutStyle.getCurrent().getDialogMarginX(),
 
150
            LayoutStyle.getCurrent().getDialogMarginY(),
 
151
            LayoutStyle.getCurrent().getDialogMarginX()
 
152
        );
 
153
 
 
154
 
 
155
    /**
 
156
     * A standardized Border that describes the border around
 
157
     * a dialog content that uses tabs.
 
158
     *
 
159
     * @see #DIALOG_BORDER
 
160
     */
 
161
    public static final Border TABBED_DIALOG_BORDER =
 
162
        createEmptyBorder(
 
163
                LayoutStyle.getCurrent().getTabbedDialogMarginY(),
 
164
                LayoutStyle.getCurrent().getTabbedDialogMarginX(),
 
165
                LayoutStyle.getCurrent().getTabbedDialogMarginY(),
 
166
                LayoutStyle.getCurrent().getTabbedDialogMarginX()
 
167
        );
 
168
 
 
169
 
 
170
    // Factory Methods ******************************************************
 
171
 
 
172
    /**
 
173
     * Creates and returns an <code>EmptyBorder</code> with the specified
 
174
     * gaps.
 
175
     *
 
176
     * @param top               the top gap
 
177
     * @param left              the left-hand side gap
 
178
     * @param bottom    the bottom gap
 
179
     * @param right     the right-hand side gap
 
180
     * @return an <code>EmptyBorder</code> with the specified gaps
 
181
     *
 
182
     * @throws NullPointerException if top, left, bottom, or right is {@code null}
 
183
     *
 
184
     * @see #createEmptyBorder(String)
 
185
     */
 
186
    public static Border createEmptyBorder(ConstantSize top,   ConstantSize left,
 
187
                                            ConstantSize bottom, ConstantSize right) {
 
188
        return new EmptyBorder(top, left, bottom, right);
 
189
    }
 
190
 
 
191
    /**
 
192
     * Creates and returns a <code>Border</code> using sizes as specified by
 
193
     * the given string. This string is a comma-separated encoding of
 
194
     * 4 <code>ConstantSize</code>s.
 
195
     *
 
196
     * @param encodedSizes       top, left, bottom, right gap encoded as String
 
197
     * @return an <code>EmptyBorder</code> with the specified gaps
 
198
     *
 
199
     * @see #createEmptyBorder(ConstantSize, ConstantSize, ConstantSize, ConstantSize)
 
200
     */
 
201
    public static Border createEmptyBorder(String encodedSizes) {
 
202
        String[] token = encodedSizes.split("\\s*,\\s*");
 
203
        int tokenCount = token.length;
 
204
        if (token.length != 4) {
 
205
            throw new IllegalArgumentException(
 
206
                "The border requires 4 sizes, but \"" + encodedSizes +
 
207
                "\" has " + tokenCount + ".");
 
208
        }
 
209
        ConstantSize top    = Sizes.constant(token[0], false);
 
210
        ConstantSize left   = Sizes.constant(token[1], true);
 
211
        ConstantSize bottom = Sizes.constant(token[2], false);
 
212
        ConstantSize right  = Sizes.constant(token[3], true);
 
213
        return createEmptyBorder(top, left, bottom, right);
 
214
    }
 
215
 
 
216
 
 
217
    /**
 
218
     * An empty border that uses 4 instances of {@link ConstantSize}
 
219
     * to define the top, left, bottom and right gap.
 
220
     */
 
221
    public static final class EmptyBorder extends AbstractBorder {
 
222
 
 
223
        private final ConstantSize top;
 
224
        private final ConstantSize left;
 
225
        private final ConstantSize bottom;
 
226
        private final ConstantSize right;
 
227
 
 
228
        private EmptyBorder(
 
229
                ConstantSize top,
 
230
                ConstantSize left,
 
231
                ConstantSize bottom,
 
232
                ConstantSize right) {
 
233
            if (   (top == null)
 
234
                || (left == null)
 
235
                || (bottom == null)
 
236
                || (right == null)) {
 
237
                throw new NullPointerException("The top, left, bottom, and right must not be null.");
 
238
            }
 
239
            this.top    = top;
 
240
            this.left   = left;
 
241
            this.bottom = bottom;
 
242
            this.right  = right;
 
243
        }
 
244
 
 
245
        /**
 
246
         * Returns the insets of the border.
 
247
         *
 
248
         * @param c      the component for which this border insets value applies
 
249
         * @param insets the insets to be reinitialized
 
250
         * @return the <code>insets</code> object
 
251
         */
 
252
        public Insets getBorderInsets(Component c, Insets insets) {
 
253
            insets.top    = top.getPixelSize(c);
 
254
            insets.left   = left.getPixelSize(c);
 
255
            insets.bottom = bottom.getPixelSize(c);
 
256
            insets.right  = right.getPixelSize(c);
 
257
            return insets;
 
258
        }
 
259
 
 
260
        /**
 
261
         * Returns the insets of the border.
 
262
         *
 
263
         * @param c the component for which this border insets value applies
 
264
         * @return the border's Insets
 
265
         */
 
266
        public Insets getBorderInsets(Component c) {
 
267
            return getBorderInsets(c, new Insets(0, 0, 0, 0));
 
268
        }
 
269
 
 
270
        /**
 
271
         * Returns this border's top size.
 
272
         *
 
273
         * @return this border's top size
 
274
         */
 
275
        public ConstantSize top() {
 
276
            return top;
 
277
        }
 
278
 
 
279
        /**
 
280
         * Returns this border's left size.
 
281
         *
 
282
         * @return this border's left size
 
283
         */
 
284
        public ConstantSize left() {
 
285
            return left;
 
286
        }
 
287
 
 
288
        /**
 
289
         * Returns this border's bottom size.
 
290
         *
 
291
         * @return this border's bottom size
 
292
         */
 
293
        public ConstantSize bottom() {
 
294
            return bottom;
 
295
        }
 
296
 
 
297
        /**
 
298
         * Returns this border's right size.
 
299
         *
 
300
         * @return this border's right size
 
301
         */
 
302
        public ConstantSize right() {
 
303
            return right;
 
304
        }
 
305
 
 
306
    }
 
307
 
 
308
 
 
309
}
 
310