KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.awt.GridLayout JavaDoc;
35
36 import javax.swing.Box JavaDoc;
37 import javax.swing.JButton JavaDoc;
38 import javax.swing.JComponent JavaDoc;
39 import javax.swing.JPanel JavaDoc;
40
41 import com.jgoodies.forms.builder.PanelBuilder;
42 import com.jgoodies.forms.layout.CellConstraints;
43 import com.jgoodies.forms.layout.FormLayout;
44 import com.jgoodies.looks.Options;
45
46 /**
47  * Demonstrates the JGoodies Looks <i>narrowMargin</i> option.
48  * Therefore it contains button rows that use different combinations
49  * of layout managers and narrow hints.
50  *
51  * @author Karsten Lentzsch
52  * @version $Revision: 1.5 $
53  */

54 final class NarrowTab {
55
56     /**
57      * Builds the panel.
58      */

59     JComponent JavaDoc build() {
60         FormLayout fl = new FormLayout(
61                 "7dlu, right:max(50dlu;pref), 4dlu, left:pref, 0:grow",
62                 "pref, 2dlu, pref, 4dlu, pref, 4dlu, pref, 11dlu, "
63               + "pref, 2dlu, pref, 4dlu, pref, 4dlu, pref, 11dlu, "
64               + "pref, 2dlu, pref, 4dlu, pref, 4dlu, pref, 0:grow");
65         PanelBuilder builder = new PanelBuilder(fl);
66         builder.setDefaultDialogBorder();
67
68         CellConstraints cc = new CellConstraints();
69         
70         // BoxLayout
71
builder.addSeparator("Unmodified Button Widths (BoxLayout)", cc.xyw(1, 1, 5));
72
73         builder.addLabel("No Narrow Hint:", cc.xy(2, 3));
74         builder.add(buildButtonBoxNoNarrow(), cc.xy(4, 3));
75
76         builder.addLabel("One Narrow Hint:", cc.xy(2, 5));
77         builder.add(buildButtonBoxOneNarrow(), cc.xy(4, 5));
78
79         builder.addLabel("All Narrow Hints:", cc.xy(2, 7));
80         builder.add(buildButtonBoxAllNarrow(), cc.xy(4, 7));
81
82         // DesignGridLayout
83
builder.addSeparator("Adjusted Button Widths (FormLayout)", cc.xyw(1, 9, 5));
84
85         builder.addLabel("No Narrow Hint:", cc.xy(2, 11));
86         builder.add(buildButtonFormNoNarrow(), cc.xy(4, 11));
87
88         builder.addLabel("One Narrow Hint:", cc.xy(2, 13));
89         builder.add(buildButtonFormOneNarrow(), cc.xy(4, 13));
90
91         builder.addLabel("All Narrow Hints:", cc.xy(2, 15));
92         builder.add(buildButtonFormAllNarrow(), cc.xy(4, 15));
93
94         // GridLayout
95
builder.addSeparator("Equalized Button Widths (GridLayout)", cc.xyw(1, 17, 5));
96
97         builder.addLabel("No Narrow Hint:", cc.xy(2, 19));
98         builder.add(buildButtonGridNoNarrow(), cc.xy(4, 19));
99
100         builder.addLabel("One Narrow Hint:", cc.xy(2, 21));
101         builder.add(buildButtonGridOneNarrow(), cc.xy(4, 21));
102
103         builder.addLabel("All Narrow Hints:", cc.xy(2, 23));
104         builder.add(buildButtonGridAllNarrow(), cc.xy(4, 23));
105
106         return builder.getPanel();
107     }
108
109     // Button FlowLayout Panels *********************************************
110

111     private Component JavaDoc buildButtonBoxNoNarrow() {
112         return buildButtonBox(createButtons());
113     }
114
115     private Component JavaDoc buildButtonBoxOneNarrow() {
116         return buildButtonBox(createButtonsWithOneNarrowHint());
117     }
118
119     private Component JavaDoc buildButtonBoxAllNarrow() {
120         return buildButtonBox(createNarrowHintedButtons());
121     }
122
123     private Component JavaDoc buildButtonBox(JButton JavaDoc[] buttons) {
124         Box JavaDoc box = Box.createHorizontalBox();
125         for (int i = 0; i < buttons.length; i++) {
126             box.add(buttons[i]);
127             box.add(Box.createHorizontalStrut(6));
128         }
129         return box;
130     }
131
132     // Button DesignGrids ***************************************************
133

134     private Component JavaDoc buildButtonFormNoNarrow() {
135         return buildButtonForm(createButtons());
136     }
137
138     private Component JavaDoc buildButtonFormOneNarrow() {
139         return buildButtonForm(createButtonsWithOneNarrowHint());
140     }
141
142     private Component JavaDoc buildButtonFormAllNarrow() {
143         return buildButtonForm(createNarrowHintedButtons());
144     }
145
146     private Component JavaDoc buildButtonForm(JButton JavaDoc[] buttons) {
147         FormLayout fl = new FormLayout(
148                 "pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref",
149                 "pref");
150         fl.setColumnGroups(new int[][]{{1, 3, 5, 7}});
151         JPanel JavaDoc panel = new JPanel JavaDoc(fl);
152         for (int i = 0; i < buttons.length; i++) {
153             panel.add(buttons[i], new CellConstraints(i * 2 + 1, 1));
154         }
155         return panel;
156     }
157
158     // Button Grids *********************************************************
159

160     private Component JavaDoc buildButtonGridNoNarrow() {
161         return buildButtonGrid(createButtons());
162     }
163
164     private Component JavaDoc buildButtonGridOneNarrow() {
165         return buildButtonGrid(createButtonsWithOneNarrowHint());
166     }
167
168     private Component JavaDoc buildButtonGridAllNarrow() {
169         return buildButtonGrid(createNarrowHintedButtons());
170     }
171
172     private Component JavaDoc buildButtonGrid(JButton JavaDoc[] buttons) {
173         JPanel JavaDoc grid = new JPanel JavaDoc(new GridLayout JavaDoc(1, 4, 6, 0));
174         for (int i = 0; i < buttons.length; i++) {
175             grid.add(buttons[i]);
176         }
177         return grid;
178     }
179
180     // Helper Code **********************************************************
181

182     /**
183      * Creates and returns a <code>JButton</code> with a narrow hint set.
184      */

185     private JButton JavaDoc createNarrowButton(String JavaDoc text) {
186         JButton JavaDoc button = new JButton JavaDoc(text);
187         button.putClientProperty(Options.IS_NARROW_KEY, Boolean.TRUE);
188         return button;
189     }
190
191     /**
192      * Creates and answers an array of buttons that have no narrow hints set.
193      */

194     private JButton JavaDoc[] createButtons() {
195         return new JButton JavaDoc[] {
196             new JButton JavaDoc("Add..."),
197             new JButton JavaDoc("Remove"),
198             new JButton JavaDoc("Up"),
199             new JButton JavaDoc("Down"),
200             new JButton JavaDoc("A Long Label")};
201     }
202
203     /**
204      * Creates and answers an array of buttons, where only the last should be narrow.
205      */

206     private JButton JavaDoc[] createButtonsWithOneNarrowHint() {
207         return new JButton JavaDoc[] {
208             new JButton JavaDoc("Add..."),
209             new JButton JavaDoc("Remove"),
210             new JButton JavaDoc("Up"),
211             new JButton JavaDoc("Down"),
212             createNarrowButton("A Long Label")};
213     }
214
215     /**
216      * Creates and answers an array of buttons that have no narrow hints set.
217      */

218     private JButton JavaDoc[] createNarrowHintedButtons() {
219         return new JButton JavaDoc[] {
220             createNarrowButton("Add..."),
221             createNarrowButton("Remove"),
222             createNarrowButton("Up"),
223             createNarrowButton("Down"),
224             createNarrowButton("A Long Label")};
225     }
226
227
228 }
Popular Tags