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;
60 protected void frameInit() {
63 addWindowListener(this);
64 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
67 pageIcon = new JLabel();
68 pageIcon.setVerticalAlignment(SwingConstants.TOP);
69 pageHeading = new JLabel();
70 pageHeading.setFont(pageHeading.getFont().deriveFont(pageHeading.getFont().getSize() * 2.0f).deriveFont(Font.BOLD));
71 pageDescription = new JLabel();
73 JPanel contentPane = new JPanel(new BorderLayout(12, 12));
74 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
76 JPanel topPanel = new JPanel(new BorderLayout(12, 12));
77 contentPane.add(topPanel, BorderLayout.PAGE_START);
79 topPanel.add(pageIcon, BorderLayout.LINE_START);
81 JPanel textPanel = new JPanel(new BorderLayout(12, 12));
82 topPanel.add(textPanel, BorderLayout.CENTER);
83 textPanel.add(pageHeading, BorderLayout.PAGE_START);
84 textPanel.add(pageDescription, BorderLayout.CENTER);
86 pagePanel = new JPanel(new BorderLayout(12, 12));
87 contentPane.add(pagePanel, BorderLayout.CENTER);
89 JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING, 12, 12));
90 buttonPanel.setBorder(new EmptyBorder(-12, -12, -12, -12));
91 buttonPanel.add(new JButton(previousAction));
92 buttonPanel.add(new JButton(nextAction));
93 buttonPanel.add(new JButton(quitAction));
94 contentPane.add(buttonPanel, BorderLayout.PAGE_END);
96 setContentPane(contentPane);
102 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
103 setLocation((screenSize.width - getWidth()) / 2, (screenSize.height - getHeight()) / 2);
104 // System.out.println("resized to: " + getWidth() + "x" + getHeight());
107 private void createActions() {
108 previousAction = new AbstractAction("Previous") {
109 public void actionPerformed(ActionEvent actionEvent) {
114 nextAction = new AbstractAction("Next") {
115 public void actionPerformed(ActionEvent actionEvent) {
120 quitAction = new AbstractAction("Quit") {
121 public void actionPerformed(ActionEvent actionEvent) {
127 public void addWizardListener(WizardListener wizardListener) {
128 wizardListeners.add(wizardListener);
131 public void removeWizardListener(WizardListener wizardListener) {
132 wizardListeners.remove(wizardListener);
135 protected void fireWizardPreviousPressed() {
136 for (WizardListener wizardListener: wizardListeners) {
137 wizardListener.wizardPreviousPressed(this);
141 protected void fireWizardNextPressed() {
142 for (WizardListener wizardListener: wizardListeners) {
143 wizardListener.wizardNextPressed(this);
147 protected void fireWizardQuitPressed() {
148 for (WizardListener wizardListener: wizardListeners) {
149 wizardListener.wizardQuitPressed(this);
153 public void setIcon(Icon icon) {
154 pageIcon.setIcon(icon);
157 public void setPage(TWizardPage page) {
159 pageHeading.setText(page.getHeading());
160 pageDescription.setText(page.getDescription());
161 if (pagePanel.getComponentCount() > 0) {
162 if (pagePanel.getComponent(0) instanceof TWizardPage) {
163 ((TWizardPage) pagePanel.getComponent(0)).pageDeleted(this);
166 pagePanel.removeAll();
167 pagePanel.add(page, BorderLayout.CENTER);
168 page.pageAdded(this);
170 setTitle(page.getHeading());
174 public TWizardPage getPage() {
175 return (TWizardPage) pagePanel.getComponent(0);
178 public void setPreviousEnabled(boolean previousEnabled) {
179 previousAction.setEnabled(previousEnabled);
182 public void setPreviousName(String previousName) {
183 previousAction.putValue(Action.NAME, previousName);
186 public void setNextEnabled(boolean nextEnabled) {
187 nextAction.setEnabled(nextEnabled);
190 public void setNextName(String nextName) {
191 nextAction.putValue(Action.NAME, nextName);
194 public void setQuitEnabled(boolean quitEnabled) {
195 quitAction.setEnabled(quitEnabled);
198 public void setQuitName(String quitName) {
199 quitAction.putValue(Action.NAME, quitName);
202 protected void actionPrevious() {
203 fireWizardPreviousPressed();
206 protected void actionNext() {
207 fireWizardNextPressed();
210 protected void actionQuit() {
211 fireWizardQuitPressed();
215 // INTERFACE WindowListener
221 public void windowOpened(WindowEvent e) {
227 public void windowClosing(WindowEvent e) {
228 fireWizardQuitPressed();
234 public void windowClosed(WindowEvent e) {
240 public void windowIconified(WindowEvent e) {
246 public void windowDeiconified(WindowEvent e) {
252 public void windowActivated(WindowEvent e) {
258 public void windowDeactivated(WindowEvent e) {