KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.awt.Component JavaDoc;
34
35 import javax.swing.*;
36 import javax.swing.text.JTextComponent JavaDoc;
37
38 import com.jgoodies.forms.builder.DefaultFormBuilder;
39 import com.jgoodies.forms.builder.PanelBuilder;
40 import com.jgoodies.forms.factories.FormFactory;
41 import com.jgoodies.forms.layout.CellConstraints;
42 import com.jgoodies.forms.layout.ColumnSpec;
43 import com.jgoodies.forms.layout.FormLayout;
44 import com.jgoodies.forms.layout.Sizes;
45
46 /**
47  * Presents a larger set of Swing components in different states and
48  * configurations.
49  *
50  * @author Karsten Lentzsch
51  * @version $Revision: 1.8 $
52  */

53 final class StatesTab {
54
55     /**
56      * Builds and returns the states panel.
57      */

58     JComponent build() {
59         FormLayout layout = new FormLayout(
60                 "right:max(50dlu;pref), 4dlu, pref");
61         DefaultFormBuilder builder = new DefaultFormBuilder(layout);
62         builder.setDefaultDialogBorder();
63
64         builder.append("Standard:", buildButtonRow(true, true));
65         builder.append("No Content:", buildButtonRow(true, false));
66         builder.append("No Border:", buildButtonRow(false, true));
67         builder.append("Radio Button:", buildRadioButtonRow());
68         builder.append("Check Box:", buildCheckBoxRow());
69         builder.append("Combo Box:", buildComboBoxRow());
70         builder.append("Text Field:", buildTextRow(JTextField.class, false));
71         builder.append("Formatted Field:", buildTextRow(JFormattedTextField.class, false));
72         builder.append("Password:", buildTextRow(JPasswordField.class, false));
73         builder.append("Text Area:", buildTextRow(JTextArea.class, true));
74         builder.append("Editor Pane:", buildTextRow(JEditorPane.class, true));
75         builder.append("Spinner:", buildSpinnerRow());
76         
77         return builder.getPanel();
78     }
79
80
81     // Button Rows **********************************************************
82

83     private JComponent buildButtonRow(
84         boolean borderPainted,
85         boolean contentAreaFilled) {
86         JButton button = new JButton("Standard");
87         button.setDefaultCapable(true);
88
89         return buildButtonRow(
90             new AbstractButton[] {
91                 button,
92                 new JToggleButton("Selected"),
93                 new JButton("Disabled"),
94                 new JToggleButton("Selected")},
95             borderPainted,
96             contentAreaFilled);
97     }
98
99     private JComponent buildCheckBoxRow() {
100         return buildButtonRow(
101             new AbstractButton[] {
102                 new JCheckBox("Deselected"),
103                 new JCheckBox("Selected"),
104                 new JCheckBox("Disabled"),
105                 new JCheckBox("Selected")},
106             false,
107             false);
108     }
109
110     private JComponent buildRadioButtonRow() {
111         return buildButtonRow(
112             new AbstractButton[] {
113                 new JRadioButton("Deselected"),
114                 new JRadioButton("Selected"),
115                 new JRadioButton("Disabled"),
116                 new JRadioButton("Selected")},
117             false,
118             false);
119     }
120
121     private JComponent buildButtonRow(
122         AbstractButton[] buttons,
123         boolean borderPainted,
124         boolean contentAreaFilled) {
125         buttons[1].setSelected(true);
126         buttons[2].setEnabled(false);
127         buttons[3].setEnabled(false);
128         buttons[3].setSelected(true);
129         for (int i = 0; i < buttons.length; i++) {
130             buttons[i].setBorderPainted(borderPainted);
131             buttons[i].setContentAreaFilled(contentAreaFilled);
132         }
133
134         return buildGrid(buttons[0],
135                           buttons[1],
136                           buttons[2],
137                           buttons[3],
138                           FormFactory.BUTTON_COLSPEC);
139     }
140
141     // Text Rows ************************************************************
142

143     /**
144      * Creates and returns a bar with 4 text components.
145      * These are created using the given class;
146      * they are wrapped in a <code>JScrollpane</code> iff the
147      * wrap flag is set.
148      */

149     private JComponent buildTextRow(Class JavaDoc textComponentClass, boolean wrap) {
150         JTextComponent JavaDoc[] components = new JTextComponent JavaDoc[4];
151         for (int i = 0; i < 4; i++) {
152             try {
153                 components[i] =
154                     (JTextComponent JavaDoc) textComponentClass.newInstance();
155             } catch (InstantiationException JavaDoc e) {
156                 // Won't happen in the context we're using this.
157
} catch (IllegalAccessException JavaDoc e) {
158                 // Won't happen in the context we're using this.
159
}
160         }
161         components[0].setText("Editable");
162
163         components[1].setText("Uneditable");
164         components[1].setEditable(false);
165
166         components[2].setText("Disabled");
167         components[2].setEnabled(false);
168
169         components[3].setText("Uneditable");
170         components[3].setEditable(false);
171         components[3].setEnabled(false);
172
173         return wrap
174             ? buildGrid(
175                 wrapWithScrollPane(components[0]),
176                 wrapWithScrollPane(components[1]),
177                 wrapWithScrollPane(components[2]),
178                 wrapWithScrollPane(components[3]))
179             : buildGrid(
180                 components[0],
181                 components[1],
182                 components[2],
183                 components[3]);
184     }
185     
186     private Component JavaDoc wrapWithScrollPane(Component JavaDoc c) {
187         return new JScrollPane(
188             c,
189             ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER,
190             ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
191     }
192
193     // Misc *****************************************************************
194

195     private JComponent buildComboBoxRow() {
196         return buildGrid(
197             createComboBox("Editable", true, true),
198             createComboBox("Uneditable", true, false),
199             createComboBox("Disabled", false, true),
200             createComboBox("Uneditable", false, false));
201     }
202
203     private JComboBox createComboBox(
204         String JavaDoc text,
205         boolean enabled,
206         boolean editable) {
207         JComboBox box =
208             new JComboBox(new String JavaDoc[] { text, "Two", "Three", "Four", "A Quite Long Label" });
209         box.setEnabled(enabled);
210         box.setEditable(editable);
211         //box.setRenderer(new CustomComboBoxRenderer());
212
//Dimension prefSize = box.getPreferredSize();
213
//box.setPreferredSize(new Dimension(80, prefSize.height));
214
return box;
215     }
216
217     private JComponent buildSpinnerRow() {
218         return buildGrid(createSpinner(true),
219                          new JPanel(null),
220                          createSpinner(false),
221                          new JPanel(null));
222     }
223
224     private JComponent createSpinner(boolean enabled) {
225         JSpinner spinner = new JSpinner();
226         spinner.setEnabled(enabled);
227         return spinner;
228     }
229
230
231     // Custom ComboBox Editor ***********************************************
232

233 // private static class CustomComboBoxRenderer implements ListCellRenderer {
234
//
235
// private static final Border EMPTY_BORDER = new EmptyBorder(1,1,1,1);
236
//
237
// private final JLabel label = new JLabel();
238
//
239
// public Component getListCellRendererComponent(
240
// JList list,
241
// Object value,
242
// int index,
243
// boolean isSelected,
244
// boolean cellHasFocus) {
245
// label.setBackground(isSelected
246
// ? list.getSelectionBackground()
247
// : list.getBackground());
248
// label.setForeground(isSelected
249
// ? list.getSelectionForeground()
250
// : list.getForeground());
251
//
252
// label.setFont(list.getFont());
253
// label.setBorder(EMPTY_BORDER);
254
//
255
// if (value instanceof Icon) {
256
// label.setIcon((Icon) value);
257
// } else {
258
// label.setText(value == null ? "" : value.toString());
259
// }
260
// label.setOpaque(true);
261
// return label;
262
// }
263
//
264
// }
265

266     
267     // Helper Code **********************************************************
268

269     private JComponent buildGrid(
270                 Component JavaDoc c1,
271                 Component JavaDoc c2,
272                 Component JavaDoc c3,
273                 Component JavaDoc c4) {
274          return buildGrid(c1, c2, c3, c4,
275                             new ColumnSpec(ColumnSpec.DEFAULT,
276                                             Sizes.dluX(20),
277                                             ColumnSpec.DEFAULT_GROW));
278     }
279
280     private JComponent buildGrid(
281                 Component JavaDoc c1,
282                 Component JavaDoc c2,
283                 Component JavaDoc c3,
284                 Component JavaDoc c4,
285                 ColumnSpec colSpec) {
286         FormLayout layout = new FormLayout("", "pref");
287         for (int i = 0; i < 4; i++) {
288             layout.appendColumn(colSpec);
289             layout.appendColumn(FormFactory.RELATED_GAP_COLSPEC);
290         }
291         PanelBuilder builder = new PanelBuilder(layout);
292         CellConstraints cc = new CellConstraints();
293         builder.add(c1, cc.xy(1, 1));
294         builder.add(c2, cc.xy(3, 1));
295         builder.add(c3, cc.xy(5, 1));
296         builder.add(c4, cc.xy(7, 1));
297         return builder.getPanel();
298     }
299
300
301 }
Popular Tags