001: /**
002: * Wizard Framework
003: * Copyright 2004 - 2005 Andrew Pietsch
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * $Id: ButtonBar.java,v 1.12 2006/05/13 19:52:51 pietschy Exp $
020: */package org.pietschy.wizard;
021:
022: import javax.swing.*;
023: import java.beans.PropertyChangeListener;
024: import java.beans.PropertyChangeEvent;
025: import java.awt.*;
026: import java.awt.event.FocusListener;
027: import java.awt.event.FocusEvent;
028:
029: /**
030: * The component that holds the wizards buttons. Subclasses may override {@link #layoutButtons layoutButtons()} to
031: * customize the look of the wizrad.
032: *
033: * @see Wizard#createButtonBar()
034: * @see #layoutButtons
035: */
036: public class ButtonBar extends JPanel {
037:
038: public static final int RELATED_GAP = Wizard.BORDER_WIDTH / 2;
039: public static final int UNRELATED_GAP = Wizard.BORDER_WIDTH;
040:
041: private Wizard wizard;
042: private JButton lastButton;
043: private JButton nextButton;
044: private JButton previousButton;
045: private JButton finishButton;
046: private JButton cancelButton;
047: private JButton closeButton;
048: private JButton helpButton;
049:
050: protected Component lastButtonGap = Box
051: .createHorizontalStrut(RELATED_GAP);
052: protected Component helpButtonGap = Box
053: .createHorizontalStrut(UNRELATED_GAP);
054:
055: public ButtonBar(Wizard wizard) {
056: this .wizard = wizard;
057: this .wizard.getModel().addPropertyChangeListener("lastVisible",
058: new PropertyChangeListener() {
059: public void propertyChange(PropertyChangeEvent evt) {
060: configureLastButton();
061: }
062: });
063:
064: this .wizard.addPropertyChangeListener("helpBroker",
065: new PropertyChangeListener() {
066: public void propertyChange(PropertyChangeEvent evt) {
067: configureHelpButton();
068: }
069: });
070:
071: previousButton = new JButton(wizard.getPreviousAction());
072: previousButton.putClientProperty("JButton.buttonType", "text");
073: previousButton.putClientProperty("Quaqua.Button.style", "push");
074:
075: nextButton = new JButton(wizard.getNextAction());
076: nextButton.setHorizontalTextPosition(SwingConstants.LEADING);
077: nextButton.putClientProperty("JButton.buttonType", "text");
078: nextButton.putClientProperty("Quaqua.Button.style", "push");
079:
080: lastButton = new JButton(wizard.getLastAction());
081: finishButton = new JButton(wizard.getFinishAction());
082: cancelButton = new JButton(wizard.getCancelAction());
083: closeButton = new JButton(wizard.getCloseAction());
084: helpButton = new JButton(wizard.getHelpAction());
085:
086: setBorder(BorderFactory.createEmptyBorder(Wizard.BORDER_WIDTH,
087: Wizard.BORDER_WIDTH, Wizard.BORDER_WIDTH,
088: Wizard.BORDER_WIDTH));
089:
090: showCloseButton(false);
091:
092: equalizeButtonWidths(helpButton, previousButton, nextButton,
093: lastButton, finishButton, cancelButton, closeButton);
094: layoutButtons(helpButton, previousButton, nextButton,
095: lastButton, finishButton, cancelButton, closeButton);
096:
097: configureLastButton();
098: configureHelpButton();
099: }
100:
101: private void configureLastButton() {
102: setLastVisible(wizard.getModel().isLastVisible());
103: }
104:
105: private void configureHelpButton() {
106: setHelpVisible(wizard.getHelpBroker() != null);
107: }
108:
109: /**
110: * Called by the constructor to add the buttons to the button bar. This may be overridden to alter
111: * the layout of the bar. Subclasses that override this method are responsible for setting the layout
112: * manager using {@link #setLayout(LayoutManager)}.
113: *
114: * @param helpButton the help button.
115: * @param previousButton the previous button.
116: * @param nextButton the next button
117: * @param lastButton the last button
118: * @param finishButton the showCloseButton button
119: * @param cancelButton the cancel button.
120: * @param closeButton the close button.
121: */
122: protected void layoutButtons(JButton helpButton,
123: JButton previousButton, JButton nextButton,
124: JButton lastButton, JButton finishButton,
125: JButton cancelButton, JButton closeButton) {
126: setLayout(new BoxLayout(this , BoxLayout.LINE_AXIS));
127: add(helpButton);
128: add(helpButtonGap);
129: add(Box.createHorizontalGlue());
130: add(previousButton);
131: add(Box.createHorizontalStrut(RELATED_GAP));
132: add(nextButton);
133: add(lastButtonGap);
134: add(lastButton);
135: add(Box.createHorizontalStrut(RELATED_GAP));
136: add(finishButton);
137: add(Box.createHorizontalStrut(UNRELATED_GAP));
138: add(cancelButton);
139: add(closeButton);
140:
141: }
142:
143: /**
144: * Call prior to {@link #layoutButtons(javax.swing.JButton, javax.swing.JButton, javax.swing.JButton, javax.swing.JButton, javax.swing.JButton, javax.swing.JButton, javax.swing.JButton)}
145: * to make all the buttons the same width.
146: * @param helpButton
147: * @param previousButton
148: * @param nextButton
149: * @param lastButton
150: * @param finishButton
151: * @param cancelButton
152: * @param closeButton
153: */
154: protected void equalizeButtonWidths(JButton helpButton,
155: JButton previousButton, JButton nextButton,
156: JButton lastButton, JButton finishButton,
157: JButton cancelButton, JButton closeButton) {
158: // make sure that every button has the same size
159: Dimension d = new Dimension();
160: JButton[] buttons = new JButton[] { helpButton, previousButton,
161: nextButton, lastButton, finishButton, cancelButton,
162: closeButton };
163: for (int i = 0; i < buttons.length; i++) {
164: Dimension buttonDim = buttons[i].getPreferredSize();
165: if (buttonDim.width > d.width)
166: d.width = (int) buttonDim.width;
167: if (buttonDim.height > d.height)
168: d.height = (int) buttonDim.height;
169: }
170:
171: for (int i = 0; i < buttons.length; i++) {
172: buttons[i].setPreferredSize(d);
173: }
174: }
175:
176: public void showCloseButton(boolean showClose) {
177: previousButton.setVisible(!showClose);
178: nextButton.setVisible(!showClose);
179: lastButton.setVisible(!showClose);
180: finishButton.setVisible(!showClose);
181: cancelButton.setVisible(!showClose);
182: closeButton.setVisible(showClose);
183: }
184:
185: private void setLastVisible(boolean visible) {
186: lastButton.setVisible(visible);
187: lastButtonGap.setVisible(visible);
188: revalidate();
189: repaint();
190: }
191:
192: private void setHelpVisible(boolean visible) {
193: helpButton.setVisible(visible);
194: helpButtonGap.setVisible(visible);
195: revalidate();
196: repaint();
197: }
198: }
|