001: /*
002: * Copyright (c) 2002-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.forms.tutorial.building;
032:
033: import java.awt.Component;
034: import java.awt.Insets;
035:
036: import javax.swing.*;
037:
038: import com.jgoodies.forms.builder.ButtonStackBuilder;
039: import com.jgoodies.forms.factories.Borders;
040: import com.jgoodies.forms.layout.CellConstraints;
041: import com.jgoodies.forms.layout.FormLayout;
042:
043: /**
044: * Demonstrates how to build button stacks using the ButtonStackBuilder.
045: *
046: * @author Karsten Lentzsch
047: * @version $Revision: 1.17 $
048: *
049: * @see ButtonStackBuilder
050: */
051: public final class ButtonStacksExample {
052:
053: public static void main(String[] args) {
054: try {
055: UIManager
056: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
057: } catch (Exception e) {
058: // Likely PlasticXP is not in the class path; ignore.
059: }
060: JFrame frame = new JFrame();
061: frame.setTitle("Forms Tutorial :: Button Stacks");
062: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
063: JComponent panel = new ButtonStacksExample().buildPanel();
064: frame.getContentPane().add(panel);
065: frame.pack();
066: frame.setVisible(true);
067: }
068:
069: public JComponent buildPanel() {
070: JTabbedPane tabbedPane = new JTabbedPane();
071: tabbedPane.putClientProperty("jgoodies.noContentBorder",
072: Boolean.TRUE);
073:
074: tabbedPane.add(buildButtonStackNoBuilder(), "No Builder");
075: tabbedPane.add(buildButtonStackWithBuilder(), "Builder");
076: tabbedPane.add(buildButtonStackRelated(), "Related");
077: tabbedPane.add(buildButtonStackUnrelated(), "Unrelated ");
078: tabbedPane.add(buildButtonStackMixedDefault(), "Mix");
079: tabbedPane.add(buildButtonStackMixedNarrow(), "Mix Narrow");
080: return tabbedPane;
081: }
082:
083: private Component buildButtonStackNoBuilder() {
084: JPanel buttonStack = new JPanel(
085: new FormLayout("p", "p, 4px, p"));
086: buttonStack.add(new JButton("Yes"), "1, 1");
087: buttonStack.add(new JButton("No"), "1, 3");
088:
089: return wrap(
090: buttonStack,
091: "This stack has been built without a ButtonStackBuilder.\n"
092: + " o The buttons have no minimum width and\n"
093: + " o The gaps use pixel sizes and do not scale with the font\n"
094: + " o The gaps may become inconsisten in a team.");
095: }
096:
097: private Component buildButtonStackWithBuilder() {
098: ButtonStackBuilder builder = new ButtonStackBuilder();
099: builder.addGridded(new JButton("Yes"));
100: builder.addRelatedGap();
101: builder.addGridded(new JButton("No"));
102: return wrap(
103: builder.getPanel(),
104: "This stack has been built with a ButtonStackBuilder.\n"
105: + " o The buttons have a minimum width and\n"
106: + " o The gap uses a logical size that follows a style guide.");
107: }
108:
109: private Component buildButtonStackRelated() {
110: ButtonStackBuilder builder = new ButtonStackBuilder();
111: builder.addGridded(new JButton("Related"));
112: builder.addRelatedGap();
113: builder.addGridded(new JButton("Related"));
114: builder.addRelatedGap();
115: builder.addGridded(new JButton("Related"));
116:
117: return wrap(builder.getPanel(),
118: "This stack uses the logical gap for related buttons.\n");
119: }
120:
121: private Component buildButtonStackUnrelated() {
122: ButtonStackBuilder builder = new ButtonStackBuilder();
123: builder.addGridded(new JButton("Unrelated"));
124: builder.addUnrelatedGap();
125: builder.addGridded(new JButton("Unrelated"));
126: builder.addUnrelatedGap();
127: builder.addGridded(new JButton("Unrelated"));
128:
129: return wrap(builder.getPanel(),
130: "This stack uses the logical gap for unrelated buttons.\n");
131: }
132:
133: private Component buildButtonStackMixedDefault() {
134: ButtonStackBuilder builder = new ButtonStackBuilder();
135: builder.addGridded(new JButton("OK"));
136: builder.addRelatedGap();
137: builder.addGridded(new JButton("Cancel"));
138: builder.addUnrelatedGap();
139: builder.addGridded(new JButton("Help"));
140: builder.addUnrelatedGap();
141: builder.addGlue();
142: builder.addFixed(new JButton("Copy to Clipboard"));
143:
144: return wrap(
145: builder.getPanel(),
146: "Demonstrates a glue (between Help and Copy),\n"
147: + "has related and unrelated buttons and\n"
148: + "a button with long label with the default margin.");
149: }
150:
151: private Component buildButtonStackMixedNarrow() {
152: ButtonStackBuilder builder = new ButtonStackBuilder();
153: builder.addGridded(new JButton("OK"));
154: builder.addRelatedGap();
155: builder.addGridded(new JButton("Cancel"));
156: builder.addUnrelatedGap();
157: builder.addGridded(new JButton("Help"));
158: builder.addUnrelatedGap();
159: builder.addGlue();
160: builder.addGridded(new JButton("Copy to Clipboard"));
161:
162: return wrap(
163: builder.getPanel(),
164: "Demonstrates a glue (between Help and Copy),\n"
165: + "has related and unrelated buttons and\n"
166: + "a button with long label with a narrow margin.\n\n"
167: + "Note that some look&feels do not support\n"
168: + "the narrow margin feature, and conversely,\n"
169: + "others have only narrow margins.");
170: }
171:
172: // Helper Code ************************************************************
173:
174: private static Component wrap(Component buttonStack, String text) {
175: JTextArea textArea = new JTextArea(text);
176: textArea.setMargin(new Insets(6, 10, 4, 6));
177: // Non-editable but shall use the editable background.
178: textArea.setEditable(false);
179: textArea.putClientProperty("JTextArea.infoBackground",
180: Boolean.TRUE);
181: Component textPane = new JScrollPane(textArea);
182:
183: FormLayout layout = new FormLayout("fill:100dlu:grow, 6dlu, p",
184: "fill:56dlu:grow");
185: JPanel panel = new JPanel(layout);
186: CellConstraints cc = new CellConstraints();
187: panel.setBorder(Borders.DIALOG_BORDER);
188: panel.add(textPane, cc.xy(1, 1));
189: panel.add(buttonStack, cc.xy(3, 1));
190: return panel;
191: }
192:
193: }
|