KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jgoodies > forms > tutorial > building > ButtonStacksExample


1 /*
2  * Copyright (c) 2003 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.forms.tutorial.building;
32
33 import java.awt.Component JavaDoc;
34
35 import javax.swing.*;
36
37 import com.jgoodies.forms.builder.ButtonStackBuilder;
38 import com.jgoodies.forms.factories.Borders;
39 import com.jgoodies.forms.layout.CellConstraints;
40 import com.jgoodies.forms.layout.FormLayout;
41
42 /**
43  * Demonstrates how to build button stacks using the ButtonStackBuilder.
44  *
45  * @author Karsten Lentzsch
46  * @version $Revision: 1.6 $
47  * @see ButtonStackBuilder
48  */

49 public final class ButtonStacksExample {
50     
51     public static void main(String JavaDoc[] args) {
52         try {
53             UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel");
54         } catch (Exception JavaDoc e) {
55             // Likely PlasticXP is not in the class path; ignore.
56
}
57         JFrame frame = new JFrame();
58         frame.setTitle("Forms Tutorial :: Button Stacks");
59         frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
60         JComponent panel = new ButtonStacksExample().buildPanel();
61         frame.getContentPane().add(panel);
62         frame.pack();
63         frame.show();
64     }
65
66
67     public JComponent buildPanel() {
68         JTabbedPane tabbedPane = new JTabbedPane();
69         tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE);
70
71         tabbedPane.add(buildButtonStackNoBuilder(), "No Builder");
72         tabbedPane.add(buildButtonStackWithBuilder(), "With Builder");
73         tabbedPane.add(buildButtonStackRelated(), "Related");
74         tabbedPane.add(buildButtonStackUnrelated(), "Unrelated ");
75         tabbedPane.add(buildButtonStackMixedDefault(), "Mix Default");
76         tabbedPane.add(buildButtonStackMixedNarrow(), "Mix Narrow");
77         return tabbedPane;
78     }
79     
80     private Component JavaDoc buildButtonStackNoBuilder() {
81         JPanel buttonStack = new JPanel(
82             new FormLayout("p", "p, 4px, p"));
83         buttonStack.add(new JButton("Yes"), "1, 1");
84         buttonStack.add(new JButton("No"), "1, 3");
85         
86         return wrap(buttonStack,
87             "\nThis stack has been built without a ButtonStackBuilder.\n" +
88             " o The buttons have no minimum width and\n" +
89             " o The gaps use pixel sizes and do not scale with the font\n" +
90             " o The gaps may become inconsisten in a team.");
91     }
92
93     private Component JavaDoc buildButtonStackWithBuilder() {
94         ButtonStackBuilder builder = new ButtonStackBuilder();
95         builder.addGridded(new JButton("Yes"));
96         builder.addRelatedGap();
97         builder.addGridded(new JButton("No"));
98         return wrap(builder.getPanel(),
99             "\nThis stack has been built with a ButtonStackBuilder.\n" +
100             " o The buttons have a minimum width and\n" +
101             " o The button gap is a logical size that follows a style guide.");
102     }
103     
104     private Component JavaDoc buildButtonStackRelated() {
105         ButtonStackBuilder builder = new ButtonStackBuilder();
106         builder.addGridded(new JButton("Related"));
107         builder.addRelatedGap();
108         builder.addGridded(new JButton("Related"));
109         builder.addRelatedGap();
110         builder.addGridded(new JButton("Related"));
111
112         return wrap(builder.getPanel(),
113             "\nThis stack uses the logical gap for related buttons.\n");
114     }
115     
116     private Component JavaDoc buildButtonStackUnrelated() {
117         ButtonStackBuilder builder = new ButtonStackBuilder();
118         builder.addGridded(new JButton("Unrelated"));
119         builder.addUnrelatedGap();
120         builder.addGridded(new JButton("Unrelated"));
121         builder.addUnrelatedGap();
122         builder.addGridded(new JButton("Unrelated"));
123
124         return wrap(builder.getPanel(),
125             "\nThis stack uses the logical gap for unrelated buttons.\n");
126     }
127     
128     private Component JavaDoc buildButtonStackMixedDefault() {
129         ButtonStackBuilder builder = new ButtonStackBuilder();
130         builder.addGridded(new JButton("OK"));
131         builder.addRelatedGap();
132         builder.addGridded(new JButton("Cancel"));
133         builder.addUnrelatedGap();
134         builder.addGridded(new JButton("Help"));
135         builder.addUnrelatedGap();
136         builder.addGlue();
137         builder.addFixed(new JButton("Copy to Clipboard"));
138
139         return wrap(builder.getPanel(),
140             "\nDemonstrates a glue (between Help and Copy),\n" +
141             "has related and unrelated buttons and\n" +
142             "a button with long label with the default margin.");
143     }
144     
145     private Component JavaDoc buildButtonStackMixedNarrow() {
146         ButtonStackBuilder builder = new ButtonStackBuilder();
147         builder.addGridded(new JButton("OK"));
148         builder.addRelatedGap();
149         builder.addGridded(new JButton("Cancel"));
150         builder.addUnrelatedGap();
151         builder.addGridded(new JButton("Help"));
152         builder.addUnrelatedGap();
153         builder.addGlue();
154         builder.addGriddedNarrow(new JButton("Copy to Clipboard"));
155
156         return wrap(builder.getPanel(),
157             "\nDemonstrates a glue (between Help and Copy),\n" +
158             "has related and unrelated buttons and\n" +
159             "a button with long label with a narrow margin.");
160     }
161     
162     
163     // Helper Code ************************************************************
164

165     private Component JavaDoc wrap(Component JavaDoc buttonStack, String JavaDoc text) {
166         Component JavaDoc textPane = new JScrollPane(new JTextArea(text));
167         
168         FormLayout layout = new FormLayout(
169                         "fill:p:grow, 6dlu, p",
170                         "fill:p:grow");
171         JPanel panel = new JPanel(layout);
172         CellConstraints cc = new CellConstraints();
173         panel.setBorder(Borders.DIALOG_BORDER);
174         panel.add(textPane, cc.xy(1, 1));
175         panel.add(buttonStack, cc.xy(3, 1));
176         return panel;
177     }
178
179     
180 }
181
182
Popular Tags