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 java.awt.Component;
034:
035: import javax.swing.*;
036: import javax.swing.JSpinner.DefaultEditor;
037: import javax.swing.text.JTextComponent;
038:
039: import com.jgoodies.forms.builder.DefaultFormBuilder;
040: import com.jgoodies.forms.builder.PanelBuilder;
041: import com.jgoodies.forms.factories.FormFactory;
042: import com.jgoodies.forms.layout.CellConstraints;
043: import com.jgoodies.forms.layout.ColumnSpec;
044: import com.jgoodies.forms.layout.FormLayout;
045: import com.jgoodies.forms.layout.Sizes;
046: import com.jgoodies.looks.Options;
047:
048: /**
049: * Presents a larger set of Swing components in different states and
050: * configurations.
051: *
052: * @author Karsten Lentzsch
053: * @version $Revision: 1.3 $
054: */
055: final class StateTab {
056:
057: /**
058: * Builds and returns the states panel.
059: */
060: JComponent build() {
061: FormLayout layout = new FormLayout(
062: "right:max(50dlu;pref), 4dlu, pref");
063: DefaultFormBuilder builder = new DefaultFormBuilder(layout);
064: builder.setDefaultDialogBorder();
065: builder.getPanel().setOpaque(false);
066:
067: builder.append("Standard:", buildButtonRow(true, true));
068: builder.append("No Content:", buildButtonRow(true, false));
069: builder.append("No Border:", buildButtonRow(false, true));
070: builder.append("Radio Button:", buildRadioButtonRow());
071: builder.append("Check Box:", buildCheckBoxRow());
072: builder.append("Combo Box:", buildComboBoxRow());
073: builder.append("Text Field:", buildTextRow(JTextField.class,
074: false));
075: builder.append("Formatted Field:", buildTextRow(
076: JFormattedTextField.class, false));
077: builder.append("Text Area:",
078: buildTextRow(JTextArea.class, true));
079: builder.append("Editor Pane:", buildTextRow(JEditorPane.class,
080: true));
081: builder.append("Password:", buildTextRow(JPasswordField.class,
082: false));
083: builder.append("Spinner:", buildSpinnerRow());
084:
085: return builder.getPanel();
086: }
087:
088: // Button Rows **********************************************************
089:
090: private JComponent buildButtonRow(boolean borderPainted,
091: boolean contentAreaFilled) {
092: JButton button = new JButton("Standard");
093: button.setDefaultCapable(true);
094:
095: return buildButtonRow(new AbstractButton[] { button,
096: new JToggleButton("Selected"), new JButton("Disabled"),
097: new JToggleButton("Selected") }, borderPainted,
098: contentAreaFilled);
099: }
100:
101: private JComponent buildCheckBoxRow() {
102: return buildButtonRow(new AbstractButton[] {
103: new JCheckBox("Deselected"), new JCheckBox("Selected"),
104: new JCheckBox("Disabled"), new JCheckBox("Selected") },
105: false, false);
106: }
107:
108: private JComponent buildRadioButtonRow() {
109: return buildButtonRow(new AbstractButton[] {
110: new JRadioButton("Deselected"),
111: new JRadioButton("Selected"),
112: new JRadioButton("Disabled"),
113: new JRadioButton("Selected") }, false, false);
114: }
115:
116: private JComponent buildButtonRow(AbstractButton[] buttons,
117: boolean borderPainted, boolean contentAreaFilled) {
118: buttons[1].setSelected(true);
119: buttons[2].setEnabled(false);
120: buttons[3].setEnabled(false);
121: buttons[3].setSelected(true);
122: for (int i = 0; i < buttons.length; i++) {
123: buttons[i].setBorderPainted(borderPainted);
124: buttons[i].setContentAreaFilled(contentAreaFilled);
125: }
126:
127: return buildGrid(buttons[0], buttons[1], buttons[2],
128: buttons[3], FormFactory.BUTTON_COLSPEC);
129: }
130:
131: // Text Rows ************************************************************
132:
133: /**
134: * Creates and returns a bar with 4 text components.
135: * These are created using the given class;
136: * they are wrapped in a <code>JScrollpane</code> iff the
137: * wrap flag is set.
138: */
139: private JComponent buildTextRow(Class textComponentClass,
140: boolean wrap) {
141: JTextComponent[] components = new JTextComponent[4];
142: for (int i = 0; i < 4; i++) {
143: try {
144: components[i] = (JTextComponent) textComponentClass
145: .newInstance();
146: } catch (InstantiationException e) {
147: // Won't happen in the context we're using this.
148: } catch (IllegalAccessException e) {
149: // Won't happen in the context we're using this.
150: }
151: }
152: components[0].setText("Editable");
153:
154: components[1].setText("Uneditable");
155: components[1].setEditable(false);
156:
157: components[2].setText("Disabled");
158: components[2].setEnabled(false);
159:
160: components[3].setText("Uneditable");
161: components[3].setEditable(false);
162: components[3].setEnabled(false);
163:
164: return wrap ? buildGrid(wrapWithScrollPane(components[0]),
165: wrapWithScrollPane(components[1]),
166: wrapWithScrollPane(components[2]),
167: wrapWithScrollPane(components[3])) : buildGrid(
168: components[0], components[1], components[2],
169: components[3]);
170: }
171:
172: private Component wrapWithScrollPane(Component c) {
173: return new JScrollPane(c,
174: ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
175: ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
176: }
177:
178: // Misc *****************************************************************
179:
180: private JComponent buildComboBoxRow() {
181: return buildGrid(createComboBox("Editable", true, true),
182: createComboBox("Uneditable", true, false),
183: createComboBox("Disabled", false, true),
184: createComboBox("Uneditable", false, false));
185: }
186:
187: private JComboBox createComboBox(String text, boolean enabled,
188: boolean editable) {
189: JComboBox box = new JComboBox(new String[] { text, "Two",
190: "Three", "Four", "A Quite Long Label" });
191: box.setEnabled(enabled);
192: box.setEditable(editable);
193: box.putClientProperty(
194: Options.COMBO_POPUP_PROTOTYPE_DISPLAY_VALUE_KEY,
195: "A Quite Long Label");
196: return box;
197: }
198:
199: private JComponent buildSpinnerRow() {
200: return buildGrid(createSpinner(true, true), createSpinner(true,
201: false), createSpinner(false, true), createSpinner(
202: false, false));
203: }
204:
205: private JComponent createSpinner(boolean enabled, boolean editable) {
206: JSpinner spinner = new JSpinner();
207: spinner.setValue(new Integer(123));
208: spinner.setEnabled(enabled);
209: JComponent editor = spinner.getEditor();
210: if (editor instanceof DefaultEditor) {
211: ((DefaultEditor) editor).getTextField().setEditable(
212: editable);
213: }
214: return spinner;
215: }
216:
217: // Custom ComboBox Editor ***********************************************
218:
219: // private static class CustomComboBoxRenderer implements ListCellRenderer {
220: //
221: // private static final Border EMPTY_BORDER = new EmptyBorder(1,1,1,1);
222: //
223: // private final JLabel label = new JLabel();
224: //
225: // public Component getListCellRendererComponent(
226: // JList list,
227: // Object value,
228: // int index,
229: // boolean isSelected,
230: // boolean cellHasFocus) {
231: // label.setBackground(isSelected
232: // ? list.getSelectionBackground()
233: // : list.getBackground());
234: // label.setForeground(isSelected
235: // ? list.getSelectionForeground()
236: // : list.getForeground());
237: //
238: // label.setFont(list.getFont());
239: // label.setBorder(EMPTY_BORDER);
240: //
241: // if (value instanceof Icon) {
242: // label.setIcon((Icon) value);
243: // } else {
244: // label.setText(value == null ? "" : value.toString());
245: // }
246: // label.setOpaque(true);
247: // return label;
248: // }
249: //
250: // }
251:
252: // Helper Code **********************************************************
253:
254: private JComponent buildGrid(Component c1, Component c2,
255: Component c3, Component c4) {
256: return buildGrid(c1, c2, c3, c4, new ColumnSpec(
257: ColumnSpec.DEFAULT, Sizes.dluX(20),
258: ColumnSpec.DEFAULT_GROW));
259: }
260:
261: private JComponent buildGrid(Component c1, Component c2,
262: Component c3, Component c4, ColumnSpec colSpec) {
263: FormLayout layout = new FormLayout("", "pref");
264: for (int i = 0; i < 4; i++) {
265: layout.appendColumn(colSpec);
266: layout.appendColumn(FormFactory.RELATED_GAP_COLSPEC);
267: }
268: PanelBuilder builder = new PanelBuilder(layout);
269: builder.getPanel().setOpaque(false);
270: CellConstraints cc = new CellConstraints();
271: builder.add(c1, cc.xy(1, 1));
272: builder.add(c2, cc.xy(3, 1));
273: builder.add(c3, cc.xy(5, 1));
274: builder.add(c4, cc.xy(7, 1));
275: return builder.getPanel();
276: }
277:
278: }
|