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.ButtonBarBuilder;
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 bars using a ButtonBarBuilder.
045: *
046: * @author Karsten Lentzsch
047: * @version $Revision: 1.21 $
048: *
049: * @see ButtonBarBuilder
050: * @see com.jgoodies.forms.factories.ButtonBarFactory
051: */
052: public final class ButtonBarsExample {
053:
054: public static void main(String[] args) {
055: try {
056: UIManager
057: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
058: } catch (Exception e) {
059: // Likely PlasticXP is not in the class path; ignore.
060: }
061: JFrame frame = new JFrame();
062: frame.setTitle("Forms Tutorial :: Button Bars");
063: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
064: JComponent panel = new ButtonBarsExample().buildPanel();
065: frame.getContentPane().add(panel);
066: frame.pack();
067: frame.setVisible(true);
068: }
069:
070: public JComponent buildPanel() {
071: JTabbedPane tabbedPane = new JTabbedPane();
072: tabbedPane.putClientProperty("jgoodies.noContentBorder",
073: Boolean.TRUE);
074:
075: tabbedPane.add(buildButtonBar1Panel(), "No Builder");
076: tabbedPane.add(buildButtonBar2Panel(), "Builder");
077: tabbedPane.add(buildButtonBar3Panel(), "Related");
078: tabbedPane.add(buildButtonBar4Panel(), "Unrelated ");
079: tabbedPane.add(buildButtonMixedBar1Panel(), "Mix");
080: tabbedPane.add(buildButtonMixedBar2Panel(), "Mix Narrow");
081: return tabbedPane;
082: }
083:
084: private Component buildButtonBar1Panel() {
085: JPanel buttonBar = new JPanel(new FormLayout(
086: "0:grow, p, 4px, p", "p"));
087: buttonBar.add(new JButton("Yes"), "2, 1");
088: buttonBar.add(new JButton("No"), "4, 1");
089:
090: return wrap(
091: buttonBar,
092: "This bar has been built without a ButtonBarBuilder:\n"
093: + " o buttons have no minimum widths,\n"
094: + " o the button order is fixed left-to-right,\n"
095: + " o gaps may be inconsistent between team members.");
096: }
097:
098: private Component buildButtonBar2Panel() {
099: ButtonBarBuilder builder = new ButtonBarBuilder();
100: builder.addGlue();
101: builder.addGriddedButtons(new JButton[] { new JButton("Yes"),
102: new JButton("No") });
103: return wrap(
104: builder.getPanel(),
105: "This bar has been built with a ButtonBarBuilder:\n"
106: + " o buttons have a minimum widths,\n"
107: + " o the button order honors the platform default,\n"
108: + " o the button gap is a logical size that follows a style guide.");
109: }
110:
111: private Component buildButtonBar3Panel() {
112: ButtonBarBuilder builder = new ButtonBarBuilder();
113: builder.addGlue();
114: builder.addGriddedButtons(new JButton[] { new JButton("One"),
115: new JButton("Two"), new JButton("Three") });
116: return wrap(builder.getPanel(),
117: "This bar uses the logical gap for related buttons.\n");
118: }
119:
120: private Component buildButtonBar4Panel() {
121: ButtonBarBuilder builder = new ButtonBarBuilder();
122: builder.addGlue();
123: builder.addGridded(new JButton("One"));
124: builder.addUnrelatedGap();
125: builder.addGridded(new JButton("Two"));
126: builder.addUnrelatedGap();
127: builder.addGridded(new JButton("Three"));
128:
129: return wrap(
130: builder.getPanel(),
131: "This bar uses the logical gap for unrelated buttons.\n"
132: + "It is a little bit wider than the related gap.");
133: }
134:
135: private Component buildButtonMixedBar1Panel() {
136: ButtonBarBuilder builder = new ButtonBarBuilder();
137: builder.addGridded(new JButton("Help"));
138: builder.addGlue();
139: builder.addUnrelatedGap();
140: builder.addFixed(new JButton("Copy to Clipboard"));
141: builder.addUnrelatedGap();
142: builder.addGriddedButtons(new JButton[] { new JButton("OK"),
143: new JButton("Cancel") });
144: return wrap(
145: builder.getPanel(),
146: "Demonstrates a glue (between Help and the rest),\n"
147: + "has related and unrelated buttons and an ungridded button\n"
148: + "with a default margin (Copy to Clipboard).");
149: }
150:
151: private Component buildButtonMixedBar2Panel() {
152: ButtonBarBuilder builder = new ButtonBarBuilder();
153: builder.addGridded(new JButton("Help"));
154: builder.addGlue();
155: builder.addUnrelatedGap();
156: builder.addFixedNarrow(new JButton("Copy to Clipboard"));
157: builder.addUnrelatedGap();
158: builder.addGriddedButtons(new JButton[] { new JButton("OK"),
159: new JButton("Cancel") });
160: return wrap(
161: builder.getPanel(),
162: "Demonstrates a glue (between Help and the rest),\n"
163: + "has related and unrelated buttons and an ungridded button\n"
164: + "with a narrow margin (Copy to Clipboard).\n\n"
165: + "Note that some look&feels do not support the narrow margin\n"
166: + "feature, and conversely, others have only narrow margins.");
167: }
168:
169: // Helper Code ************************************************************
170:
171: private static Component wrap(Component buttonBar, String text) {
172: JTextArea textArea = new JTextArea(text);
173: textArea.setMargin(new Insets(6, 10, 4, 6));
174: // Non-editable but shall use the editable background.
175: textArea.setEditable(false);
176: textArea.putClientProperty("JTextArea.infoBackground",
177: Boolean.TRUE);
178: Component textPane = new JScrollPane(textArea);
179:
180: FormLayout layout = new FormLayout("fill:100dlu:grow",
181: "fill:56dlu:grow, 4dlu, p");
182: JPanel panel = new JPanel(layout);
183: CellConstraints cc = new CellConstraints();
184: panel.setBorder(Borders.DIALOG_BORDER);
185: panel.add(textPane, cc.xy(1, 1));
186: panel.add(buttonBar, cc.xy(1, 3));
187: return panel;
188: }
189:
190: }
|