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.BorderLayout;
034: import java.awt.Color;
035: import java.awt.Component;
036:
037: import javax.swing.*;
038: import javax.swing.border.Border;
039: import javax.swing.border.EmptyBorder;
040: import javax.swing.plaf.basic.BasicComboBoxRenderer;
041: import javax.swing.table.TableColumn;
042:
043: import com.jgoodies.forms.builder.DefaultFormBuilder;
044: import com.jgoodies.forms.builder.PanelBuilder;
045: import com.jgoodies.forms.layout.CellConstraints;
046: import com.jgoodies.forms.layout.FormLayout;
047:
048: /**
049: * Demonstrates JComboBoxes in editable and non-editable mode with
050: * different renderer margins, renderer types, and different list content.
051: * This tab makes it easier to check the alignment of font baselines,
052: * and proper preferred size computations.
053: *
054: * @author Karsten Lentzsch
055: * @version $Revision: 1.8 $
056: */
057: final class ComboBoxTab {
058:
059: private static final String TEST_STR = "A test string";
060:
061: private ListCellRenderer narrowMarginRenderer;
062: private ListCellRenderer wideMarginRenderer;
063: private ListCellRenderer noMarginRenderer;
064: private ListCellRenderer panelRenderer;
065:
066: // Initialization *********************************************************
067:
068: private void initRenderers() {
069: narrowMarginRenderer = new CustomMarginRenderer(
070: new EmptyBorder(1, 1, 1, 1));
071: wideMarginRenderer = new CustomMarginRenderer(new EmptyBorder(
072: 1, 5, 1, 5));
073: noMarginRenderer = new CustomMarginRenderer(new EmptyBorder(0,
074: 0, 0, 0));
075: panelRenderer = new PanelRenderer();
076: }
077:
078: // ************************************************************************
079:
080: /**
081: * Builds a panel using <code>FormLayout</code> that consists
082: * of rows of different Swing components all centered vertically.
083: *
084: * @return the built panel
085: */
086: JComponent build() {
087: initRenderers();
088:
089: FormLayout layout = new FormLayout("0:grow, pref, 0:grow",
090: "pref, 9dlu, pref, 2dlu, pref, 21dlu, pref, 2dlu, 50dlu");
091:
092: PanelBuilder builder = new PanelBuilder(layout);
093: builder.setDefaultDialogBorder();
094: builder.getPanel().setOpaque(false);
095: CellConstraints cc = new CellConstraints();
096:
097: builder.add(buildHelpPanel(), cc.xy(2, 1));
098: builder.addSeparator("Form", cc.xy(2, 3));
099: builder.add(buildTestPanel(), cc.xy(2, 5, "center, fill"));
100: builder.addTitle("Table", cc.xy(2, 7));
101: builder.add(buildTable(), cc.xy(2, 9));
102:
103: return builder.getPanel();
104: }
105:
106: private JComponent buildHelpPanel() {
107: return new JLabel(
108: "Font baselines shall be aligned and the visible value shall not be cropped.");
109: }
110:
111: private JComponent buildTestPanel() {
112: FormLayout layout = new FormLayout(
113: "right:pref, 3dlu, left:pref, 4dlu, left:pref");
114:
115: DefaultFormBuilder builder = new DefaultFormBuilder(layout);
116: builder.getPanel().setOpaque(false);
117:
118: // Header
119: builder.append("Renderer");
120: builder.append(createCenteredLabel("Editable"));
121: builder.append(createCenteredLabel("Non-Editable"));
122:
123: builder.append("Default");
124: builder.append(createComboBox(TEST_STR, true, null));
125: builder.append(createComboBox(TEST_STR, false, null));
126:
127: builder.append("Panel");
128: builder.append(createComboBox(TEST_STR, true, panelRenderer));
129: builder.append(createComboBox(TEST_STR, false, panelRenderer));
130:
131: builder.append("Narrow Margin");
132: builder.append(createComboBox(TEST_STR, true,
133: narrowMarginRenderer));
134: builder.append(createComboBox(TEST_STR, false,
135: narrowMarginRenderer));
136:
137: builder.append("No Margin");
138: builder
139: .append(createComboBox(TEST_STR, true, noMarginRenderer));
140: builder
141: .append(createComboBox(TEST_STR, false,
142: noMarginRenderer));
143:
144: builder.append("Wide Margin");
145: builder.append(createComboBox(TEST_STR, true,
146: wideMarginRenderer));
147: builder.append(createComboBox(TEST_STR, false,
148: wideMarginRenderer));
149:
150: return builder.getPanel();
151: }
152:
153: private JComponent buildTable() {
154: String[] columnNames = { "Text", "Editable", "Non-Editable" };
155:
156: Object[][] data = { { TEST_STR, TEST_STR, TEST_STR },
157: { TEST_STR, TEST_STR, TEST_STR },
158: { TEST_STR, TEST_STR, TEST_STR },
159: { TEST_STR, TEST_STR, TEST_STR },
160: { TEST_STR, TEST_STR, TEST_STR } };
161: JTable table = new JTable(data, columnNames);
162:
163: TableColumn editableColumn = table.getColumnModel()
164: .getColumn(1);
165: JComboBox editableCombo = createComboBox(TEST_STR, true, null);
166: editableColumn.setCellEditor(new DefaultCellEditor(
167: editableCombo));
168:
169: TableColumn nonEditableColumn = table.getColumnModel()
170: .getColumn(2);
171: JComboBox nonEditableCombo = createComboBox(TEST_STR, false,
172: null);
173: nonEditableColumn.setCellEditor(new DefaultCellEditor(
174: nonEditableCombo));
175:
176: return new JScrollPane(table);
177: }
178:
179: // Helper Code **********************************************************
180:
181: private JLabel createCenteredLabel(String text) {
182: return new JLabel(text, JLabel.CENTER);
183: }
184:
185: private JComboBox createComboBox(String selectedText,
186: boolean editable, ListCellRenderer renderer) {
187: String[] values = new String[] { selectedText, "1", "2", "3",
188: "4", "5", "Two", "Three", "Four", "Five", "Six",
189: "Seven" };
190: JComboBox combo = new JComboBox(values);
191: combo.setEditable(editable);
192: if (renderer != null) {
193: combo.setRenderer(renderer);
194: }
195: return combo;
196: }
197:
198: // Renderers **************************************************************
199:
200: private static final class CustomMarginRenderer extends
201: BasicComboBoxRenderer {
202:
203: private CustomMarginRenderer(Border border) {
204: setBorder(border);
205: }
206: }
207:
208: private static final class PanelRenderer extends JPanel implements
209: ListCellRenderer {
210:
211: private static Border noFocusBorder;
212:
213: private final JLabel label;
214:
215: private PanelRenderer() {
216: super (new BorderLayout());
217: if (noFocusBorder == null) {
218: noFocusBorder = new EmptyBorder(1, 1, 1, 1);
219: }
220: setOpaque(true);
221: setBorder(noFocusBorder);
222: label = new JLabel();
223: add(label, BorderLayout.CENTER);
224: }
225:
226: public Component getListCellRendererComponent(JList list,
227: Object value, int index, boolean isSelected,
228: boolean cellHasFocus) {
229: setComponentOrientation(list.getComponentOrientation());
230: if (isSelected) {
231: setBackground(list.getSelectionBackground());
232: label.setForeground(list.getSelectionForeground());
233: } else {
234: setBackground(list.getBackground());
235: label.setForeground(list.getForeground());
236: }
237:
238: if (value instanceof Icon) {
239: label.setIcon((Icon) value);
240: label.setText("");
241: } else {
242: label.setIcon(null);
243: label.setText((value == null) ? "" : value.toString());
244: }
245:
246: label.setEnabled(list.isEnabled());
247: label.setFont(list.getFont());
248: setBorder((cellHasFocus) ? UIManager
249: .getBorder("List.focusCellHighlightBorder")
250: : noFocusBorder);
251:
252: return this ;
253: }
254:
255: public void setForeground(Color c) {
256: if (label != null) {
257: label.setForeground(c);
258: }
259: }
260:
261: }
262:
263: }
|