KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > modeler > util > PanelFactory


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56
57 package org.objectstyle.cayenne.modeler.util;
58
59 import java.awt.BorderLayout JavaDoc;
60 import java.awt.Component JavaDoc;
61 import java.awt.FlowLayout JavaDoc;
62
63 import javax.swing.BorderFactory JavaDoc;
64 import javax.swing.JButton JavaDoc;
65 import javax.swing.JComponent JavaDoc;
66 import javax.swing.JPanel JavaDoc;
67 import javax.swing.JScrollPane JavaDoc;
68 import javax.swing.JTable JavaDoc;
69 import javax.swing.ListSelectionModel JavaDoc;
70
71
72 import com.jgoodies.forms.builder.DefaultFormBuilder;
73 import com.jgoodies.forms.layout.FormLayout;
74
75 /**
76  * Implements a set of utility methods for laying out components on the panels.
77  *
78  * @author Misha Shengaout
79  * @author Andrei Adamchik
80  */

81
82 // TODO: get rid of PanelFactory in favor of JGoodies Forms
83
public class PanelFactory {
84
85     /**
86      * Creates and returns a panel with right-centered buttons.
87      */

88     public static JPanel JavaDoc createButtonPanel(JButton JavaDoc[] buttons) {
89         JPanel JavaDoc panel = new JPanel JavaDoc();
90         panel.setBorder(BorderFactory.createEmptyBorder(3, 20, 3, 7));
91         panel.setLayout(new FlowLayout JavaDoc(FlowLayout.RIGHT));
92
93         for (int i = 0; i < buttons.length; i++) {
94             panel.add(buttons[i]);
95         }
96
97         return panel;
98     }
99
100     public static JPanel JavaDoc createForm(
101         String JavaDoc title,
102         String JavaDoc[] labels,
103         Component JavaDoc[] components) {
104         Component JavaDoc[] jlabels = new Component JavaDoc[labels.length];
105         for (int i = 0; i < labels.length; i++) {
106             jlabels[i] = CayenneWidgetFactory.createLabel(labels[i]);
107         }
108         return createForm(title, jlabels, components);
109     }
110
111     public static JPanel JavaDoc createForm(
112         Component JavaDoc[] leftComponents,
113         Component JavaDoc[] rightComponents) {
114         return createForm(null, leftComponents, rightComponents);
115     }
116
117     /**
118      * Create panel with aligned labels on the right and fields on the left.
119      */

120     public static JPanel JavaDoc createForm(
121         String JavaDoc title,
122         Component JavaDoc[] leftComponents,
123         Component JavaDoc[] rightComponents) {
124
125         if (leftComponents.length != rightComponents.length) {
126             throw new IllegalArgumentException JavaDoc(
127                 "Arrays must be the same size, instead got "
128                     + leftComponents.length
129                     + "and "
130                     + rightComponents.length);
131         }
132
133         int numRows = leftComponents.length;
134         if (numRows == 0) {
135             throw new IllegalArgumentException JavaDoc("Zero components.");
136         }
137
138         FormLayout layout = new FormLayout("right:100, 3dlu, left:300", "");
139         DefaultFormBuilder builder = new DefaultFormBuilder(layout);
140         builder.setDefaultDialogBorder();
141
142         if (title != null) {
143             builder.appendSeparator(title);
144         }
145
146         for (int i = 0; i < numRows; i++) {
147             builder.append(leftComponents[i], rightComponents[i]);
148             builder.nextLine();
149         }
150
151         return builder.getPanel();
152     }
153
154     /**
155      * Creates panel with table within scroll panel and buttons in the bottom.
156      * Also sets the resizing and selection policies of the table to
157      * AUTO_RESIZE_OFF and SINGLE_SELECTION respectively.
158      */

159     public static JPanel JavaDoc createTablePanel(JTable JavaDoc table, JButton JavaDoc[] buttons) {
160         JPanel JavaDoc panel = new JPanel JavaDoc();
161         panel.setLayout(new BorderLayout JavaDoc(5, 5));
162
163         // Create table with two columns and no rows.
164
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
165         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
166
167         // Panel to add space between table and EAST/WEST borders
168
panel.add(new JScrollPane JavaDoc(table), BorderLayout.CENTER);
169
170         // Add Add and Remove buttons
171
if (buttons != null) {
172             panel.add(createButtonPanel(buttons), BorderLayout.SOUTH);
173         }
174         return panel;
175     }
176
177     /** Creates panel with table within scroll panel and buttons in the bottom.
178       * Also sets the resizing and selection policies of the table to
179       * AUTO_RESIZE_OFF and SINGLE_SELECTION respectively.*/

180     public static JPanel JavaDoc createTablePanel(
181         JTable JavaDoc table,
182         JComponent JavaDoc[] components,
183         JButton JavaDoc[] buttons) {
184         JPanel JavaDoc panel = new JPanel JavaDoc();
185         panel.setLayout(new BorderLayout JavaDoc(5, 5));
186
187         JPanel JavaDoc temp_panel = new JPanel JavaDoc(new BorderLayout JavaDoc());
188
189         // Create table with two columns and no rows.
190
table.getSelectionModel().setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
191         table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
192         JScrollPane JavaDoc scroll_pane = new JScrollPane JavaDoc(table);
193         temp_panel.add(scroll_pane, BorderLayout.CENTER);
194
195         for (int i = 0; i < components.length; i++) {
196             JPanel JavaDoc temp = new JPanel JavaDoc(new BorderLayout JavaDoc());
197             temp.add(temp_panel, BorderLayout.CENTER);
198             temp.add(components[i], BorderLayout.SOUTH);
199             temp_panel = temp;
200         }
201
202         panel.add(temp_panel, BorderLayout.CENTER);
203
204         if (buttons != null) {
205             panel.add(createButtonPanel(buttons), BorderLayout.SOUTH);
206         }
207         return panel;
208     }
209
210 }
211
Popular Tags