001: /*
002: * Copyright (c) 2003 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030: package net.sourceforge.squirrel_sql.client.gui.builders;
031:
032: import java.util.MissingResourceException;
033: import java.util.ResourceBundle;
034: import javax.swing.JComponent;
035: import javax.swing.JLabel;
036: import javax.swing.JPanel;
037: import com.jgoodies.forms.builder.PanelBuilder;
038: import com.jgoodies.forms.layout.CellConstraints;
039: import com.jgoodies.forms.layout.FormLayout;
040:
041: /**
042: * A general purpose panel builder that uses the {@link FormLayout}
043: * to layout <code>JPanel</code>s. In addition to its superclass
044: * {@link PanelBuilder} this class provides convenience behavior to map
045: * resource keys to their associated internationalized (i15d) strings
046: * when adding labels, titles and titled separators.
047: * <p>
048: * This class is not yet part of the binary Forms library;
049: * it comes with the Forms distributions as an extra.
050: * <b>The API is work in progress and may change without notice.</b>
051: * If you want to use this class, you may consider copying it into your codebase.
052: *
053: * @author Karsten Lentzsch
054: * @see ResourceBundle
055: */
056: public class I15dPanelBuilder extends PanelBuilder {
057: /**
058: * Holds the <code>ResourceBundle</code> used to lookup internationalized
059: * (i15d) String resources.
060: */
061: private final ResourceBundle bundle;
062:
063: // Instance Creation ****************************************************
064: /**
065: * Constructs an instance of <code>I15dPanelBuilder</code> for the given
066: * panel and layout.
067: *
068: * @param panel the layout container
069: * @param layout the <code>FormLayout</code> used to layout the container
070: * @param bundle the <code>ResourceBundle</code> used to lookup i15d
071: * strings
072: */
073: public I15dPanelBuilder(JPanel panel, FormLayout layout,
074: ResourceBundle bundle) {
075: super (panel, layout);
076: this .bundle = bundle;
077: }
078:
079: /**
080: * Constructs an instance of <code>I15dPanelBuilder</code> for the given
081: * layout. Uses an instance of <code>JPanel</code> as layout container.
082: *
083: * @param layout the form layout used to layout the container
084: * @param bundle the resource bundle used to lookup i15d strings
085: */
086: public I15dPanelBuilder(FormLayout layout, ResourceBundle bundle) {
087: this (new JPanel(), layout, bundle);
088: }
089:
090: // Adding Labels and Separators *****************************************
091: /**
092: * Adds an internationalized (i15d) textual label to the form using the
093: * specified constraints.
094: *
095: * @param resourceKey the resource key for the label's text
096: * @param constraints the label's cell constraints
097: * @return the added label
098: */
099: public final JLabel addI15dLabel(String resourceKey,
100: CellConstraints constraints) {
101: return addLabel(getI15dString(resourceKey), constraints);
102: }
103:
104: /**
105: * Adds an internationalized (i15d) textual label to the form using the
106: * specified constraints.
107: *
108: * @param resourceKey the resource key for the label's text
109: * @param encodedConstraints a string representation for the constraints
110: * @return the added label
111: */
112: public final JLabel addI15dLabel(String resourceKey,
113: String encodedConstraints) {
114: return addI15dLabel(resourceKey, new CellConstraints(
115: encodedConstraints));
116: }
117:
118: /**
119: * Adds an internationalized (i15d) titled separator to the form using the
120: * specified constraints.
121: *
122: * @param resourceKey the resource key for the separator title
123: * @param constraints the separator's cell constraints
124: * @return the added titled separator
125: */
126: public final JComponent addI15dSeparator(String resourceKey,
127: CellConstraints constraints) {
128: return addSeparator(getI15dString(resourceKey), constraints);
129: }
130:
131: /**
132: * Adds an internationalized (i15d) titled separator to the form using
133: * the specified constraints.
134: *
135: * @param resourceKey the resource key for the separator titel
136: * @param encodedConstraints a string representation for the constraints
137: * @return the added titled separator
138: */
139: public final JComponent addI15dSeparator(String resourceKey,
140: String encodedConstraints) {
141: return addI15dSeparator(resourceKey, new CellConstraints(
142: encodedConstraints));
143: }
144:
145: /**
146: * Adds a title to the form using the specified constraints.
147: *
148: * @param resourceKey the resource key for the separator title
149: * @param constraints the separator's cell constraints
150: * @return the added title label
151: */
152: public final JLabel addI15dTitle(String resourceKey,
153: CellConstraints constraints) {
154: return addTitle(getI15dString(resourceKey), constraints);
155: }
156:
157: /**
158: * Adds a title to the form using the specified constraints.
159: *
160: * @param resourceKey the resource key for the separator titel
161: * @param encodedConstraints a string representation for the constraints
162: * @return the added title label
163: */
164: public final JLabel add15dTitle(String resourceKey,
165: String encodedConstraints) {
166: return addI15dTitle(resourceKey, new CellConstraints(
167: encodedConstraints));
168: }
169:
170: // Helper Code **********************************************************
171: /**
172: * Looks up and answers the internationalized (i15d) string for the given
173: * resource key from the <code>ResourceBundle</code>.
174: *
175: * @param resourceKey the key to look for in the resource bundle
176: * @return the associated internationalized string, or the resource key
177: * itself in case of a missing resource
178: * @throws IllegalStateException if no <code>ResourceBundle</code>
179: * has been set
180: */
181: protected String getI15dString(String resourceKey) {
182: if (bundle == null)
183: throw new IllegalStateException(
184: "You must specify a ResourceBundle"
185: + " before using the internationalization support.");
186: try {
187: return bundle.getString(resourceKey);
188: } catch (MissingResourceException mre) {
189: return resourceKey;
190: }
191: }
192: }
|