jSite: First commit : verion 4.0 (written by Bombe)
[jSite.git] / src / de / todesbaum / util / swing / TWizard.java
1 /*
2  * todesbaum-lib - 
3  * Copyright (C) 2006 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package de.todesbaum.util.swing;
21
22 import java.awt.BorderLayout;
23 import java.awt.Dimension;
24 import java.awt.FlowLayout;
25 import java.awt.Font;
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;
32
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;
42
43 /**
44  * @author David Roden <droden@gmail.com>
45  * @version $Id: TWizard.java 426 2006-03-29 18:02:50Z bombe $
46  */
47 public class TWizard extends JFrame implements WindowListener {
48         
49         protected List<WizardListener> wizardListeners = new ArrayList<WizardListener>();
50         
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;
58         
59         protected void frameInit() {
60                 super.frameInit();
61                 setResizable(false);
62                 addWindowListener(this);
63                 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
64                 createActions();
65                 
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();
71                 
72                 JPanel contentPane = new JPanel(new BorderLayout(12, 12));
73                 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
74                 
75                 JPanel topPanel = new JPanel(new BorderLayout(12, 12));
76                 contentPane.add(topPanel, BorderLayout.PAGE_START);
77                 
78                 topPanel.add(pageIcon, BorderLayout.LINE_START);
79                 
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);
84                 
85                 pagePanel = new JPanel(new BorderLayout(12, 12));
86                 contentPane.add(pagePanel, BorderLayout.CENTER);
87                 
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);
94                 
95                 setContentPane(contentPane);
96         }
97         
98         @Override
99         public void pack() {
100                 super.pack();
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());
104         }
105         
106         private void createActions() {
107                 previousAction = new AbstractAction("Previous") {
108                         public void actionPerformed(ActionEvent actionEvent) {
109                                 actionPrevious();
110                         }
111                 };
112                 
113                 nextAction = new AbstractAction("Next") {
114                         public void actionPerformed(ActionEvent actionEvent) {
115                                 actionNext();
116                         }
117                 };
118                 
119                 quitAction = new AbstractAction("Quit") {
120                         public void actionPerformed(ActionEvent actionEvent) {
121                                 actionQuit();
122                         }
123                 };
124         }
125         
126         public void addWizardListener(WizardListener wizardListener) {
127                 wizardListeners.add(wizardListener);
128         }
129         
130         public void removeWizardListener(WizardListener wizardListener) {
131                 wizardListeners.remove(wizardListener);
132         }
133         
134         protected void fireWizardPreviousPressed() {
135                 for (WizardListener wizardListener: wizardListeners) {
136                         wizardListener.wizardPreviousPressed(this);
137                 }
138         }
139         
140         protected void fireWizardNextPressed() {
141                 for (WizardListener wizardListener: wizardListeners) {
142                         wizardListener.wizardNextPressed(this);
143                 }
144         }
145         
146         protected void fireWizardQuitPressed() {
147                 for (WizardListener wizardListener: wizardListeners) {
148                         wizardListener.wizardQuitPressed(this);
149                 }
150         }
151         
152         public void setIcon(Icon icon) {
153                 pageIcon.setIcon(icon);
154         }
155         
156         public void setPage(TWizardPage page) {
157                 setVisible(false);
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);
163                         }
164                 }
165                 pagePanel.removeAll();
166                 pagePanel.add(page, BorderLayout.CENTER);
167                 page.pageAdded(this);
168                 pack();
169                 setTitle(page.getHeading());
170                 setVisible(true);
171         }
172         
173         public TWizardPage getPage() {
174                 return (TWizardPage) pagePanel.getComponent(0);
175         }
176         
177         public void setPreviousEnabled(boolean previousEnabled) {
178                 previousAction.setEnabled(previousEnabled);
179         }
180         
181         public void setPreviousName(String previousName) {
182                 previousAction.putValue(Action.NAME, previousName);
183         }
184         
185         public void setNextEnabled(boolean nextEnabled) {
186                 nextAction.setEnabled(nextEnabled);
187         }
188         
189         public void setNextName(String nextName) {
190                 nextAction.putValue(Action.NAME, nextName);
191         }
192         
193         public void setQuitEnabled(boolean quitEnabled) {
194                 quitAction.setEnabled(quitEnabled);
195         }
196         
197         public void setQuitName(String quitName) {
198                 quitAction.putValue(Action.NAME, quitName);
199         }
200         
201         protected void actionPrevious() {
202                 fireWizardPreviousPressed();
203         }
204
205         protected void actionNext() {
206                 fireWizardNextPressed();
207         }
208         
209         protected void actionQuit() {
210                 fireWizardQuitPressed();
211         }
212         
213         //
214         // INTERFACE WindowListener
215         //
216
217         /**
218          * {@inheritDoc}
219          */
220         public void windowOpened(WindowEvent e) {
221         }
222
223         /**
224          * {@inheritDoc}
225          */
226         public void windowClosing(WindowEvent e) {
227                 fireWizardQuitPressed();
228         }
229
230         /**
231          * {@inheritDoc}
232          */
233         public void windowClosed(WindowEvent e) {
234         }
235
236         /**
237          * {@inheritDoc}
238          */
239         public void windowIconified(WindowEvent e) {
240         }
241
242         /**
243          * {@inheritDoc}
244          */
245         public void windowDeiconified(WindowEvent e) {
246         }
247
248         /**
249          * {@inheritDoc}
250          */
251         public void windowActivated(WindowEvent e) {
252         }
253
254         /**
255          * {@inheritDoc}
256          */
257         public void windowDeactivated(WindowEvent e) {
258         }
259
260 }