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: DefaultTitleComponent.java,v 1.4 2005/05/16 23:06:57 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.util.Iterator;
027:
028: /**
029: * This class displays the details of the current {@link WizardStep}. It displays the steps name, summary
030: * and icon. During construction, the title component iterates over all of the wizards steps to determine
031: * its appropriate size.
032: */
033: public class DefaultTitleComponent extends JPanel {
034: private static final Color COLOR_GRAY_230 = new Color(230, 230, 230);
035:
036: private JLabel title;
037: private JEditorPane summary;
038: private JLabel iconLabel;
039:
040: private WizardModel model;
041:
042: private boolean gradientBackground = false;
043: private Color fadeColor = COLOR_GRAY_230;
044:
045: protected DefaultTitleComponent(Wizard wizard) {
046: model = wizard.getModel();
047:
048: setLayout(new BorderLayout());
049:
050: JPanel p = new JPanel(new BorderLayout());
051: p.setBackground(Color.WHITE);
052: title = new JLabel();
053: title.setFont(title.getFont().deriveFont(Font.BOLD));
054:
055: summary = new HTMLPane(false);
056: summary.setFont(title.getFont().deriveFont(Font.PLAIN));
057: summary.setEditable(false);
058: summary.setEnabled(false);
059: summary.setForeground(Color.BLACK);
060: summary.setDisabledTextColor(Color.BLACK);
061: summary.setBorder(BorderFactory.createEmptyBorder(
062: Wizard.BORDER_WIDTH / 2, Wizard.BORDER_WIDTH, 0,
063: Wizard.BORDER_WIDTH));
064:
065: // lock in the preferred size
066: summary.setText("<html>Blah</html>");
067: Dimension preferredSize = summary.getPreferredSize();
068: preferredSize.width = 0;
069: preferredSize.height *= 2;
070: summary.setPreferredSize(preferredSize);
071:
072: // determine the largest icon size.
073: int maxIconHeight = 0;
074: int maxIconWidth = 0;
075: for (Iterator iter = model.stepIterator(); iter.hasNext();) {
076: Icon icon = ((WizardStep) iter.next()).getIcon();
077: if (icon != null) {
078: maxIconWidth = Math.max(maxIconWidth, icon
079: .getIconWidth());
080: maxIconHeight = Math.max(maxIconHeight, icon
081: .getIconHeight());
082: }
083: }
084:
085: iconLabel = new JLabel();
086: Dimension maximumSize = new Dimension(maxIconWidth,
087: maxIconHeight);
088: iconLabel.setMaximumSize(maximumSize);
089: iconLabel.setPreferredSize(maximumSize);
090: if (iconLabel.getComponentOrientation().isLeftToRight())
091: iconLabel.setHorizontalAlignment(SwingConstants.RIGHT);
092: else
093: iconLabel.setHorizontalAlignment(SwingConstants.LEFT);
094: iconLabel.setVerticalAlignment(SwingConstants.CENTER);
095:
096: p.add(title, BorderLayout.PAGE_START);
097: p.add(summary, BorderLayout.CENTER);
098: p.setOpaque(false);
099:
100: add(p, BorderLayout.CENTER);
101: add(iconLabel, BorderLayout.LINE_END);
102:
103: setOpaque(true);
104: setBackground(Color.WHITE);
105:
106: setBorder(BorderFactory.createCompoundBorder(BorderFactory
107: .createEtchedBorder(), BorderFactory.createEmptyBorder(
108: Wizard.BORDER_WIDTH, Wizard.BORDER_WIDTH,
109: Wizard.BORDER_WIDTH, Wizard.BORDER_WIDTH)));
110:
111: model.addPropertyChangeListener(new PropertyChangeListener() {
112: public void propertyChange(PropertyChangeEvent evt) {
113: if (evt.getPropertyName().equals("activeStep")) {
114: WizardStep activeStep = DefaultTitleComponent.this .model
115: .getActiveStep();
116: title.setText(activeStep.getName());
117: summary.setText(activeStep.getSummary());
118: iconLabel.setIcon(activeStep.getIcon());
119: }
120: }
121: });
122: }
123:
124: /**
125: * Checks if the title is using a gradient background. Subclasses can control the appearance of the
126: * gradient by overriding {@link #prepareGradient()}.
127: *
128: * @return <tt>true</tt> if the background will use a gradient, <tt>false</tt> if the background will be painted
129: * using the background color.
130: * @see #prepareGradient()
131: */
132: public boolean isGradientBackground() {
133: return gradientBackground;
134: }
135:
136: /**
137: * Configures the title to use a gradient background. Subclasses can control the appearance of the
138: * gradient by overriding {@link #prepareGradient()}.
139: *
140: * @param gradientBackground <tt>true</tt> to paint a gradient background.
141: * @see #prepareGradient()
142: */
143: public void setGradientBackground(boolean gradientBackground) {
144: this .gradientBackground = gradientBackground;
145: }
146:
147: protected void paintComponent(Graphics g) {
148: if (isGradientBackground()) {
149: Paint gradientPaint = prepareGradient();
150:
151: Graphics2D g2 = (Graphics2D) g;
152: Paint oldPaint = g2.getPaint();
153:
154: g2.setPaint(gradientPaint);
155: g2.fillRect(0, 0, getWidth(), getHeight());
156: g2.setPaint(oldPaint);
157: } else {
158: super .paintComponent(g);
159: }
160: }
161:
162: /**
163: * Prepares the background paint for the component. By default it returns a {@link java.awt.GradientPaint} as
164: * per the following.
165: * <pre>
166: * new GradientPaint(getWidth()/2, 0, Color.WHITE,
167: * getWidth(), getHeight(), COLOR_GRAY_230))
168: * </pre>
169: *
170: * If this method returns null, the component will be filled with the current backgrount color.
171: *
172: * @return the background paint to use for this component or <tt>null</tt> to use the default background color.
173: */
174: protected Paint prepareGradient() {
175: return new GradientPaint(getWidth() / 2, 0, getBackground(),
176: getWidth(), getHeight(), getFadeColor());
177: }
178:
179: /**
180: * Gets the color the gradient will fade to. By default this is gray(230,230,230).
181: * @return the second color of the gradient. The first color is the background of the component.
182: */
183: private Color getFadeColor() {
184: return fadeColor;
185: }
186:
187: /**
188: * Sets the color the gradient will fade to.
189: * @param fadeColor the second color of the gradient. The first color is the background of the component.
190: */
191: public void setFadeColor(Color fadeColor) {
192: this.fadeColor = fadeColor;
193: }
194:
195: }
|