KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > forms > tutorial > factories > FormFactoryExample


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 com.jgoodies.forms.tutorial.factories;
32
33 import java.awt.Component JavaDoc;
34
35 import javax.swing.*;
36
37 import com.jgoodies.forms.extras.DefaultFormBuilder;
38 import com.jgoodies.forms.factories.FormFactory;
39 import com.jgoodies.forms.layout.ColumnSpec;
40 import com.jgoodies.forms.layout.FormLayout;
41 import com.jgoodies.forms.layout.Sizes;
42
43 /**
44  * Demonstrates the use of Factories as provided by the Forms framework.
45  *
46  * @author Karsten Lentzsch
47  * @version $Revision: 1.5 $
48  * @see ButtonBarFactory
49  * @see WizardBarFactory
50  */

51 public final class FormFactoryExample {
52
53     public static void main(String JavaDoc[] args) {
54         try {
55             UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
56         } catch (Exception JavaDoc e) {
57             // Likely PlasticXP is not in the class path; ignore.
58
}
59         JFrame frame = new JFrame();
60         frame.setTitle("Forms Tutorial :: FormFactory");
61         frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
62         JComponent panel = new FormFactoryExample().buildPanel();
63         frame.getContentPane().add(panel);
64         frame.pack();
65         frame.show();
66     }
67
68     public JComponent buildPanel() {
69         JTabbedPane tabbedPane = new JTabbedPane();
70         tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
71
72         tabbedPane.add(buildDefaultForm(1, 1), "1 - 1");
73         tabbedPane.add(buildDefaultForm(1, 2), "1 - 2");
74         tabbedPane.add(buildDefaultForm(1, 3), "1 - 3");
75         tabbedPane.add(buildDefaultForm(2, 1), "2 - 1");
76         tabbedPane.add(buildDefaultForm(2, 2), "2 - 2");
77         tabbedPane.add(buildDefaultForm(3, 1), "3 - 1");
78         return tabbedPane;
79     }
80
81
82     private Component JavaDoc buildDefaultForm(int majorCols, int minorCols) {
83         FormLayout layout =
84             FormFactory.createColumnLayout(
85                 majorCols,
86                 minorCols,
87                 new ColumnSpec("right:p"),
88                 Sizes.DLUX9,
89                 Sizes.DLUX1);
90
91         DefaultFormBuilder builder = new DefaultFormBuilder(layout);
92         builder.setDefaultDialogBorder();
93         builder.setLeadingColumnOffset(1);
94
95         buildParagraph(builder, 4, majorCols, minorCols, "Propeller Shaft");
96         buildParagraph(builder, 3, majorCols, minorCols, "Intermediate Shaft");
97
98         return builder.getContainer();
99     }
100
101     private void buildParagraph(
102         DefaultFormBuilder builder,
103         int rows,
104         int majorCols,
105         int minorCols,
106         String JavaDoc text) {
107         builder.appendSeparator(text);
108         for (int row = 0; row < rows; row++) {
109             buildRow(builder, majorCols, minorCols);
110         }
111     }
112
113     private void buildRow(
114         DefaultFormBuilder builder,
115         int majorCols,
116         int minorCols) {
117         int charCols = 50 / (majorCols * (1 + minorCols));
118         for (int majorCol = 0; majorCol < majorCols; majorCol++) {
119             buildSection(builder, minorCols, charCols);
120         }
121         builder.nextLine();
122     }
123
124     private void buildSection(DefaultFormBuilder builder, int minorCols, int charCols) {
125         builder.append(nextLabel(), new JTextField(charCols));
126         for (int minorCol = 1; minorCol < minorCols; minorCol++) {
127             builder.append(new JTextField(charCols));
128         }
129     }
130
131     // Helper Code ************************************************************
132

133     private static int nextInt = 0;
134
135     private String JavaDoc nextLabel() {
136         if (nextInt++ == LABELS.length - 1)
137             nextInt = 0;
138         return LABELS[nextInt];
139     }
140     
141     private static final String JavaDoc[] LABELS = {
142         "da [mm]", "ds [mm]", "kl [cm]", "Weight [Kg]", "Size [mm]",
143         "da2 [mm]", "ds2 [mm]", "cv [cm]", "pl [cm]", "mt [mm]",
144         "ep [mm]", "cvn [mm]", "nz [cm]", "Power [kW]", "Length [cm]",
145         "R [cm]", "D [mm]", "PTI [kW]"
146     };
147
148 }
149
Popular Tags