3 * Copyright (C) 2006 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package de.todesbaum.util.swing;
22 import java.awt.BorderLayout;
23 import java.awt.Dimension;
24 import java.awt.FlowLayout;
26 import java.awt.Toolkit;
27 import java.awt.event.ActionEvent;
28 import java.awt.event.WindowEvent;
29 import java.awt.event.WindowListener;
30 import java.util.ArrayList;
31 import java.util.List;
33 import javax.swing.AbstractAction;
34 import javax.swing.Action;
35 import javax.swing.Icon;
36 import javax.swing.JButton;
37 import javax.swing.JFrame;
38 import javax.swing.JLabel;
39 import javax.swing.JPanel;
40 import javax.swing.SwingConstants;
41 import javax.swing.border.EmptyBorder;
44 * @author David Roden <droden@gmail.com>
47 public class TWizard extends JFrame implements WindowListener {
49 protected List<WizardListener> wizardListeners = new ArrayList<WizardListener>();
51 private Action previousAction;
52 private Action nextAction;
53 private Action quitAction;
54 private JLabel pageIcon;
55 private JPanel pagePanel;
56 private JLabel pageHeading;
57 private JLabel pageDescription;
59 protected void frameInit() {
62 addWindowListener(this);
63 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
66 pageIcon = new JLabel();
67 pageIcon.setVerticalAlignment(SwingConstants.TOP);
68 pageHeading = new JLabel();
69 pageHeading.setFont(pageHeading.getFont().deriveFont(pageHeading.getFont().getSize() * 2.0f).deriveFont(Font.BOLD));
70 pageDescription = new JLabel();
72 JPanel contentPane = new JPanel(new BorderLayout(12, 12));
73 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
75 JPanel topPanel = new JPanel(new BorderLayout(12, 12));
76 contentPane.add(topPanel, BorderLayout.PAGE_START);
78 topPanel.add(pageIcon, BorderLayout.LINE_START);
80 JPanel textPanel = new JPanel(new BorderLayout(12, 12));
81 topPanel.add(textPanel, BorderLayout.CENTER);
82 textPanel.add(pageHeading, BorderLayout.PAGE_START);
83 textPanel.add(pageDescription, BorderLayout.CENTER);
85 pagePanel = new JPanel(new BorderLayout(12, 12));
86 contentPane.add(pagePanel, BorderLayout.CENTER);
88 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
89 buttonPanel.setBorder(new EmptyBorder(-12, -12, -12, -12));
90 buttonPanel.add(new JButton(previousAction));
91 buttonPanel.add(new JButton(nextAction));
92 buttonPanel.add(new JButton(quitAction));
93 contentPane.add(buttonPanel, BorderLayout.PAGE_END);
95 setContentPane(contentPane);
101 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
102 setLocation((screenSize.width - getWidth()) / 2, (screenSize.height - getHeight()) / 2);
103 // System.out.println("resized to: " + getWidth() + "x" + getHeight());
106 private void createActions() {
107 previousAction = new AbstractAction("Previous") {
108 public void actionPerformed(ActionEvent actionEvent) {
113 nextAction = new AbstractAction("Next") {
114 public void actionPerformed(ActionEvent actionEvent) {
119 quitAction = new AbstractAction("Quit") {
120 public void actionPerformed(ActionEvent actionEvent) {
126 public void addWizardListener(WizardListener wizardListener) {
127 wizardListeners.add(wizardListener);
130 public void removeWizardListener(WizardListener wizardListener) {
131 wizardListeners.remove(wizardListener);
134 protected void fireWizardPreviousPressed() {
135 for (WizardListener wizardListener: wizardListeners) {
136 wizardListener.wizardPreviousPressed(this);
140 protected void fireWizardNextPressed() {
141 for (WizardListener wizardListener: wizardListeners) {
142 wizardListener.wizardNextPressed(this);
146 protected void fireWizardQuitPressed() {
147 for (WizardListener wizardListener: wizardListeners) {
148 wizardListener.wizardQuitPressed(this);
152 public void setIcon(Icon icon) {
153 pageIcon.setIcon(icon);
156 public void setPage(TWizardPage page) {
158 pageHeading.setText(page.getHeading());
159 pageDescription.setText(page.getDescription());
160 if (pagePanel.getComponentCount() > 0) {
161 if (pagePanel.getComponent(0) instanceof TWizardPage) {
162 ((TWizardPage) pagePanel.getComponent(0)).pageDeleted(this);
165 pagePanel.removeAll();
166 pagePanel.add(page, BorderLayout.CENTER);
167 page.pageAdded(this);
169 setTitle(page.getHeading());
173 public TWizardPage getPage() {
174 return (TWizardPage) pagePanel.getComponent(0);
177 public void setPreviousEnabled(boolean previousEnabled) {
178 previousAction.setEnabled(previousEnabled);
181 public void setPreviousName(String previousName) {
182 previousAction.putValue(Action.NAME, previousName);
185 public void setNextEnabled(boolean nextEnabled) {
186 nextAction.setEnabled(nextEnabled);
189 public void setNextName(String nextName) {
190 nextAction.putValue(Action.NAME, nextName);
193 public void setQuitEnabled(boolean quitEnabled) {
194 quitAction.setEnabled(quitEnabled);
197 public void setQuitName(String quitName) {
198 quitAction.putValue(Action.NAME, quitName);
201 protected void actionPrevious() {
202 fireWizardPreviousPressed();
205 protected void actionNext() {
206 fireWizardNextPressed();
209 protected void actionQuit() {
210 fireWizardQuitPressed();
214 // INTERFACE WindowListener
220 public void windowOpened(WindowEvent e) {
226 public void windowClosing(WindowEvent e) {
227 fireWizardQuitPressed();
233 public void windowClosed(WindowEvent e) {
239 public void windowIconified(WindowEvent e) {
245 public void windowDeiconified(WindowEvent e) {
251 public void windowActivated(WindowEvent e) {
257 public void windowDeactivated(WindowEvent e) {