001: /*
002: * Copyright (c) 2001-2007 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:
031: package com.jgoodies.looks.demo;
032:
033: import javax.swing.*;
034:
035: import com.jgoodies.forms.builder.DefaultFormBuilder;
036: import com.jgoodies.forms.builder.PanelBuilder;
037: import com.jgoodies.forms.factories.FormFactory;
038: import com.jgoodies.forms.layout.CellConstraints;
039: import com.jgoodies.forms.layout.FormLayout;
040: import com.jgoodies.forms.layout.Sizes;
041:
042: /**
043: * Consists of rows of centered components to check alignment
044: * of font baselines and centered perceived bounds.
045: *
046: * @author Karsten Lentzsch
047: * @version $Revision: 1.4 $
048: */
049: final class AlignmentTab {
050:
051: private static final String TEST_STR = "EEEEE";
052:
053: /**
054: * Builds a panel using <code>FormLayout</code> that consists
055: * of rows of different Swing components all centered vertically.
056: *
057: * @return the built panel
058: */
059: JComponent build() {
060: FormLayout layout = new FormLayout(
061: "0:grow, center:pref, 0:grow", "pref, 21dlu, pref");
062:
063: PanelBuilder builder = new PanelBuilder(layout);
064: builder.setDefaultDialogBorder();
065: builder.getPanel().setOpaque(false);
066:
067: builder.add(createHelpLabel(), new CellConstraints(2, 1));
068: builder.add(buildAlignmentTestPanel(),
069: new CellConstraints(2, 3));
070:
071: return builder.getPanel();
072: }
073:
074: private JComponent buildAlignmentTestPanel() {
075: FormLayout layout = new FormLayout(
076: "p, 2px, 38dlu, 2px, 38dlu, 2px, 38dlu, 2px, max(38dlu;p)");
077:
078: DefaultFormBuilder builder = new DefaultFormBuilder(layout);
079: builder.setLineGapSize(Sizes.pixel(1));
080: builder.getPanel().setOpaque(false);
081:
082: builder.append(createCenteredLabel("Label"));
083: builder.append(createCenteredLabel("Field"));
084: builder.append(createCenteredLabel("Combo"));
085: builder.append(createCenteredLabel("Choice"));
086: builder.append(createCenteredLabel("Button"));
087: builder.append(TEST_STR);
088: builder.append(new JTextField(TEST_STR));
089: builder.append(createComboBox(TEST_STR, true));
090: builder.append(createComboBox(TEST_STR, false));
091: builder.append(new JButton(TEST_STR));
092:
093: builder.appendRow(FormFactory.PARAGRAPH_GAP_ROWSPEC);
094: builder.nextLine(2);
095:
096: builder.append(createCenteredLabel("Label"));
097: builder.append(createCenteredLabel("Field"));
098: builder.append(createCenteredLabel("Combo"));
099: builder.append(createCenteredLabel("Spinner"));
100: builder.append(createCenteredLabel("Button"));
101: builder.append(TEST_STR);
102: builder.append(new JTextField(TEST_STR));
103: builder.append(createComboBox(TEST_STR, true));
104: builder.append(createSpinner(TEST_STR));
105: builder.append(new JButton(TEST_STR));
106:
107: builder.appendRow(FormFactory.PARAGRAPH_GAP_ROWSPEC);
108: builder.nextLine(2);
109:
110: builder.append(createCenteredLabel("Label"));
111: builder.append(createCenteredLabel("Field"));
112: builder.append(createCenteredLabel("Format"));
113: builder.append(createCenteredLabel("Pass"));
114: builder.append(createCenteredLabel("Button"));
115: builder.append(TEST_STR);
116: builder.append(new JTextField(TEST_STR));
117: JFormattedTextField field = new JFormattedTextField();
118: field.setText(TEST_STR);
119: builder.append(field);
120: builder.append(new JPasswordField(TEST_STR));
121: builder.append(new JButton(TEST_STR));
122:
123: builder.appendRow(FormFactory.PARAGRAPH_GAP_ROWSPEC);
124: builder.nextLine(2);
125:
126: builder.append(createCenteredLabel("Label"));
127: builder.append(createCenteredLabel("Field"));
128: builder.append(createCenteredLabel("Area"));
129: builder.append(createCenteredLabel("Pane"));
130: builder.append(createCenteredLabel("Button"));
131: builder.append(TEST_STR);
132: builder.append(new JTextField(TEST_STR));
133: builder.append(createWrappedTextArea(TEST_STR));
134: builder.append(createWrappedEditorPane(TEST_STR));
135: builder.append(new JButton(TEST_STR));
136:
137: return builder.getPanel();
138: }
139:
140: // Helper Code **********************************************************
141:
142: private JComponent createHelpLabel() {
143: return new JLabel(
144: "Texts shall be aligned, perceived bounds centered.");
145: }
146:
147: private JLabel createCenteredLabel(String text) {
148: return new JLabel(text, JLabel.CENTER);
149: }
150:
151: private JComboBox createComboBox(String selectedText,
152: boolean editable) {
153: JComboBox box = new JComboBox(
154: new String[] { selectedText, "1", "2", "3", "4", "5",
155: "Two", "Three", "Four", /* "This is a quite long label"*/});
156: box.setEditable(editable);
157: return box;
158: }
159:
160: private JComponent createWrappedTextArea(String text) {
161: JTextArea area = new JTextArea(text);
162: return new JScrollPane(area,
163: JScrollPane.VERTICAL_SCROLLBAR_NEVER,
164: JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
165: }
166:
167: private JComponent createWrappedEditorPane(String text) {
168: JEditorPane pane = new JEditorPane();
169: pane.setText(text);
170: return new JScrollPane(pane,
171: JScrollPane.VERTICAL_SCROLLBAR_NEVER,
172: JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
173: }
174:
175: private JSpinner createSpinner(String choice) {
176: JSpinner spinner = new JSpinner();
177: spinner.setModel(new SpinnerListModel(new String[] { choice,
178: choice + "1", choice + "2" }));
179: return spinner;
180: }
181:
182: }
|