KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > looks > demo > AlignmentTab


1 /*
2  * Copyright (c) 2001-2005 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.looks.demo;
32
33 import javax.swing.*;
34
35 import com.jgoodies.forms.builder.DefaultFormBuilder;
36 import com.jgoodies.forms.builder.PanelBuilder;
37 import com.jgoodies.forms.factories.FormFactory;
38 import com.jgoodies.forms.layout.CellConstraints;
39 import com.jgoodies.forms.layout.FormLayout;
40 import com.jgoodies.forms.layout.Sizes;
41 import com.jgoodies.looks.Options;
42
43 /**
44  * Consists of rows of centered components to check alignment
45  * of font baselines and centered perceived bounds.
46  *
47  * @author Karsten Lentzsch
48  * @version $Revision: 1.11 $
49  */

50 final class AlignmentTab {
51     
52     private static final String JavaDoc TEST_STR = "EEEEE";
53
54     /**
55      * Builds a panel using <code>FormLayout</code> that consists
56      * of rows of different Swing components all centered vertically.
57      *
58      * @return the built panel
59      */

60     JComponent build() {
61         FormLayout layout = new FormLayout(
62                 "0:grow, center:pref, 0:grow",
63                 "pref, 21dlu, pref");
64         
65         PanelBuilder builder = new PanelBuilder(layout);
66         builder.setDefaultDialogBorder();
67         
68         builder.add(createHelpLabel(), new CellConstraints(2, 1));
69         builder.add(buildAlignmentTestPanel(), new CellConstraints(2, 3));
70         
71         return builder.getPanel();
72     }
73     
74     private JComponent buildAlignmentTestPanel() {
75         FormLayout layout = new FormLayout(
76                 "p, 2px, 38dlu, 2px, 38dlu, 2px, 38dlu, 2px, max(38dlu;p)");
77         
78         DefaultFormBuilder builder = new DefaultFormBuilder(layout);
79         builder.setLineGapSize(Sizes.pixel(1));
80         
81         builder.append(createCenteredLabel("Label"));
82         builder.append(createCenteredLabel("Field"));
83         builder.append(createCenteredLabel("Edit"));
84         builder.append(createCenteredLabel("Choice"));
85         builder.append(createCenteredLabel("Button"));
86         builder.append(TEST_STR);
87         builder.append(new JTextField(TEST_STR));
88         builder.append(createComboBox(TEST_STR, true));
89         builder.append(createComboBox(TEST_STR, false));
90         builder.append(createNarrowButton(TEST_STR));
91         
92         builder.appendRow(FormFactory.PARAGRAPH_GAP_ROWSPEC);
93         builder.nextLine(2);
94         
95         builder.append(createCenteredLabel("Label"));
96         builder.append(createCenteredLabel("Field"));
97         builder.append(createCenteredLabel("Spinner"));
98         builder.append(createCenteredLabel("Combo"));
99         builder.append(createCenteredLabel("Button"));
100         builder.append(TEST_STR);
101         builder.append(new JTextField(TEST_STR));
102         builder.append(createSpinner(TEST_STR));
103         builder.append(createComboBox(TEST_STR, true));
104         builder.append(createNarrowButton(TEST_STR));
105
106         builder.appendRow(FormFactory.PARAGRAPH_GAP_ROWSPEC);
107         builder.nextLine(2);
108         
109         builder.append(createCenteredLabel("Label"));
110         builder.append(createCenteredLabel("Field"));
111         builder.append(createCenteredLabel("Format"));
112         builder.append(createCenteredLabel("Pass"));
113         builder.append(createCenteredLabel("Button"));
114         builder.append(TEST_STR);
115         builder.append(new JTextField(TEST_STR));
116         builder.append(new JFormattedTextField(TEST_STR));
117         builder.append(new JPasswordField(TEST_STR));
118         builder.append(createNarrowButton(TEST_STR));
119         
120         builder.appendRow(FormFactory.PARAGRAPH_GAP_ROWSPEC);
121         builder.nextLine(2);
122         
123         builder.append(createCenteredLabel("Label"));
124         builder.append(createCenteredLabel("Field"));
125         builder.append(createCenteredLabel("Area"));
126         builder.append(createCenteredLabel("Pane"));
127         builder.append(createCenteredLabel("Button"));
128         builder.append(TEST_STR);
129         builder.append(new JTextField(TEST_STR));
130         builder.append(createWrappedTextArea(TEST_STR));
131         builder.append(createWrappedEditorPane(TEST_STR));
132         builder.append(createNarrowButton(TEST_STR));
133         
134         return builder.getPanel();
135     }
136
137
138     // Helper Code **********************************************************
139

140     private JComponent createHelpLabel() {
141         return new JLabel("Texts shall be aligned, perceived bounds centered.");
142     }
143     
144     private JLabel createCenteredLabel(String JavaDoc text) {
145         return new JLabel(text, JLabel.CENTER);
146     }
147
148     private JComboBox createComboBox(String JavaDoc selectedText, boolean editable) {
149         JComboBox box = new JComboBox(new String JavaDoc[] {
150             selectedText, "1", "2", "3", "4", "5", "Two", "Three", "Four", /* "This is a quite long label"*/ });
151         box.setEditable(editable);
152         return box;
153     }
154     
155     private JButton createNarrowButton(String JavaDoc text) {
156         JButton button = new JButton(text);
157         button.putClientProperty(Options.IS_NARROW_KEY, Boolean.TRUE);
158         return button;
159     }
160     
161     private JComponent createWrappedTextArea(String JavaDoc text) {
162         JTextArea area = new JTextArea(text);
163         return new JScrollPane(area,
164                 JScrollPane.VERTICAL_SCROLLBAR_NEVER,
165                 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
166     }
167
168     private JComponent createWrappedEditorPane(String JavaDoc text) {
169         JEditorPane pane = new JEditorPane();
170         pane.setText(text);
171         return new JScrollPane(pane,
172                 JScrollPane.VERTICAL_SCROLLBAR_NEVER,
173                 JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
174     }
175     
176     private JSpinner createSpinner(String JavaDoc choice) {
177         JSpinner spinner = new JSpinner();
178         spinner.setModel(new SpinnerListModel(new String JavaDoc[] {
179                 choice, choice + "1", choice + "2"}));
180         return spinner;
181     }
182
183
184 }
Popular Tags