KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > panoptes > swing > I15dPanelBuilder


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 net.sf.panoptes.swing;
32
33 import java.util.MissingResourceException JavaDoc;
34 import java.util.ResourceBundle JavaDoc;
35
36 import javax.swing.JComponent JavaDoc;
37 import javax.swing.JLabel JavaDoc;
38 import javax.swing.JPanel JavaDoc;
39
40 import com.jgoodies.forms.builder.PanelBuilder;
41 import com.jgoodies.forms.layout.CellConstraints;
42 import com.jgoodies.forms.layout.FormLayout;
43
44 /**
45  * A general purpose panel builder that uses the {@link FormLayout}
46  * to layout <code>JPanel</code>s. In addition to its superclass
47  * {@link PanelBuilder} this class provides convenience behavior to map
48  * resource keys to their associated internationalized (i15d) strings
49  * when adding labels, titles and titled separators.
50  * <p>
51  * This class is not yet part of the binary Forms library;
52  * it comes with the Forms distributions as an extra.
53  * <b>The API is work in progress and may change without notice.</b>
54  * If you want to use this class, you may consider copying it into your codebase.
55  *
56  * @author Karsten Lentzsch
57  * @version $Revision: 1.1 $
58  * @see ResourceBundle
59  */

60 public class I15dPanelBuilder extends PanelBuilder {
61     
62     /**
63      * Holds the <code>ResourceBundle</code> used to lookup internationalized
64      * (i15d) String resources.
65      */

66     private final ResourceBundle JavaDoc bundle;
67     
68
69     // Instance Creation ****************************************************
70

71     /**
72      * Constructs an instance of <code>I15dPanelBuilder</code> for the given
73      * panel and layout.
74      *
75      * @param panel the layout container
76      * @param layout the <code>FormLayout</code> used to layout the container
77      * @param bundle the <code>ResourceBundle</code> used to lookup i15d
78      * strings
79      */

80     public I15dPanelBuilder(JPanel JavaDoc panel, FormLayout layout, ResourceBundle JavaDoc bundle){
81         super(panel, layout);
82         this.bundle = bundle;
83     }
84
85     /**
86      * Constructs an instance of <code>I15dPanelBuilder</code> for the given
87      * layout. Uses an instance of <code>JPanel</code> as layout container.
88      *
89      * @param layout the form layout used to layout the container
90      * @param bundle the resource bundle used to lookup i15d strings
91      */

92     public I15dPanelBuilder(FormLayout layout, ResourceBundle JavaDoc bundle){
93         this(new JPanel JavaDoc(), layout, bundle);
94     }
95
96
97     // Adding Labels and Separators *****************************************
98

99     /**
100      * Adds an internationalized (i15d) textual label to the form using the
101      * specified constraints.
102      *
103      * @param resourceKey the resource key for the label's text
104      * @param constraints the label's cell constraints
105      * @return the added label
106      */

107     public final JLabel JavaDoc addI15dLabel(String JavaDoc resourceKey, CellConstraints constraints) {
108         return addLabel(getI15dString(resourceKey), constraints);
109     }
110
111     /**
112      * Adds an internationalized (i15d) textual label to the form using the
113      * specified constraints.
114      *
115      * @param resourceKey the resource key for the label's text
116      * @param encodedConstraints a string representation for the constraints
117      * @return the added label
118      */

119     public final JLabel JavaDoc addI15dLabel(String JavaDoc resourceKey, String JavaDoc encodedConstraints) {
120         return addI15dLabel(resourceKey, new CellConstraints(encodedConstraints));
121     }
122     
123     /**
124      * Adds an internationalized (i15d) titled separator to the form using the
125      * specified constraints.
126      *
127      * @param resourceKey the resource key for the separator title
128      * @param constraints the separator's cell constraints
129      * @return the added titled separator
130      */

131     public final JComponent JavaDoc addI15dSeparator(String JavaDoc resourceKey, CellConstraints constraints) {
132         return addSeparator(getI15dString(resourceKey), constraints);
133     }
134     
135     /**
136      * Adds an internationalized (i15d) titled separator to the form using
137      * the specified constraints.
138      *
139      * @param resourceKey the resource key for the separator titel
140      * @param encodedConstraints a string representation for the constraints
141      * @return the added titled separator
142      */

143     public final JComponent JavaDoc addI15dSeparator(String JavaDoc resourceKey, String JavaDoc encodedConstraints) {
144         return addI15dSeparator(resourceKey, new CellConstraints(encodedConstraints));
145     }
146      
147     /**
148      * Adds a title to the form using the specified constraints.
149      *
150      * @param resourceKey the resource key for the separator title
151      * @param constraints the separator's cell constraints
152      * @return the added title label
153      */

154     public final JLabel JavaDoc addI15dTitle(String JavaDoc resourceKey, CellConstraints constraints) {
155         return addTitle(getI15dString(resourceKey), constraints);
156     }
157     
158     /**
159      * Adds a title to the form using the specified constraints.
160      *
161      * @param resourceKey the resource key for the separator titel
162      * @param encodedConstraints a string representation for the constraints
163      * @return the added title label
164      */

165     public final JLabel JavaDoc add15dTitle(String JavaDoc resourceKey, String JavaDoc encodedConstraints) {
166         return addI15dTitle(resourceKey, new CellConstraints(encodedConstraints));
167     }
168     
169     
170     // Helper Code **********************************************************
171

172     /**
173      * Looks up and answers the internationalized (i15d) string for the given
174      * resource key from the <code>ResourceBundle</code>.
175      *
176      * @param resourceKey the key to look for in the resource bundle
177      * @return the associated internationalized string, or the resource key
178      * itself in case of a missing resource
179      * @throws IllegalStateException if no <code>ResourceBundle</code>
180      * has been set
181      */

182     protected String JavaDoc getI15dString(String JavaDoc resourceKey) {
183         if (bundle == null)
184             throw new IllegalStateException JavaDoc("You must specify a ResourceBundle" +
185                 " before using the internationalization support.");
186         try {
187             return bundle.getString(resourceKey);
188         } catch (MissingResourceException JavaDoc mre) {
189             return resourceKey;
190         }
191     }
192     
193      
194
195 }
196
Popular Tags