Throw an exception when the node is not connected.
[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$
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         @Override
60         protected void frameInit() {
61                 super.frameInit();
62                 setResizable(false);
63                 addWindowListener(this);
64                 setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
65                 createActions();
66
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();
72
73                 JPanel contentPane = new JPanel(new BorderLayout(12, 12));
74                 contentPane.setBorder(new EmptyBorder(12, 12, 12, 12));
75
76                 JPanel topPanel = new JPanel(new BorderLayout(12, 12));
77                 contentPane.add(topPanel, BorderLayout.PAGE_START);
78
79                 topPanel.add(pageIcon, BorderLayout.LINE_START);
80
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);
85
86                 pagePanel = new JPanel(new BorderLayout(12, 12));
87                 contentPane.add(pagePanel, BorderLayout.CENTER);
88
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);
95
96                 setContentPane(contentPane);
97         }
98
99         @Override
100         public void pack() {
101                 super.pack();
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());
105         }
106
107         private void createActions() {
108                 previousAction = new AbstractAction("Previous") {
109                         public void actionPerformed(ActionEvent actionEvent) {
110                                 actionPrevious();
111                         }
112                 };
113
114                 nextAction = new AbstractAction("Next") {
115                         public void actionPerformed(ActionEvent actionEvent) {
116                                 actionNext();
117                         }
118                 };
119
120                 quitAction = new AbstractAction("Quit") {
121                         public void actionPerformed(ActionEvent actionEvent) {
122                                 actionQuit();
123                         }
124                 };
125         }
126
127         public void addWizardListener(WizardListener wizardListener) {
128                 wizardListeners.add(wizardListener);
129         }
130
131         public void removeWizardListener(WizardListener wizardListener) {
132                 wizardListeners.remove(wizardListener);
133         }
134
135         protected void fireWizardPreviousPressed() {
136                 for (WizardListener wizardListener: wizardListeners) {
137                         wizardListener.wizardPreviousPressed(this);
138                 }
139         }
140
141         protected void fireWizardNextPressed() {
142                 for (WizardListener wizardListener: wizardListeners) {
143                         wizardListener.wizardNextPressed(this);
144                 }
145         }
146
147         protected void fireWizardQuitPressed() {
148                 for (WizardListener wizardListener: wizardListeners) {
149                         wizardListener.wizardQuitPressed(this);
150                 }
151         }
152
153         public void setIcon(Icon icon) {
154                 pageIcon.setIcon(icon);
155         }
156
157         public void setPage(TWizardPage page) {
158                 setVisible(false);
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);
164                         }
165                 }
166                 pagePanel.removeAll();
167                 pagePanel.add(page, BorderLayout.CENTER);
168                 page.pageAdded(this);
169                 pack();
170                 setTitle(page.getHeading());
171                 setVisible(true);
172         }
173
174         public TWizardPage getPage() {
175                 return (TWizardPage) pagePanel.getComponent(0);
176         }
177
178         public void setPreviousEnabled(boolean previousEnabled) {
179                 previousAction.setEnabled(previousEnabled);
180         }
181
182         public void setPreviousName(String previousName) {
183                 previousAction.putValue(Action.NAME, previousName);
184         }
185
186         public void setNextEnabled(boolean nextEnabled) {
187                 nextAction.setEnabled(nextEnabled);
188         }
189
190         public void setNextName(String nextName) {
191                 nextAction.putValue(Action.NAME, nextName);
192         }
193
194         public void setQuitEnabled(boolean quitEnabled) {
195                 quitAction.setEnabled(quitEnabled);
196         }
197
198         public void setQuitName(String quitName) {
199                 quitAction.putValue(Action.NAME, quitName);
200         }
201
202         protected void actionPrevious() {
203                 fireWizardPreviousPressed();
204         }
205
206         protected void actionNext() {
207                 fireWizardNextPressed();
208         }
209
210         protected void actionQuit() {
211                 fireWizardQuitPressed();
212         }
213
214         //
215         // INTERFACE WindowListener
216         //
217
218         /**
219          * {@inheritDoc}
220          */
221         public void windowOpened(WindowEvent e) {
222         }
223
224         /**
225          * {@inheritDoc}
226          */
227         public void windowClosing(WindowEvent e) {
228                 fireWizardQuitPressed();
229         }
230
231         /**
232          * {@inheritDoc}
233          */
234         public void windowClosed(WindowEvent e) {
235         }
236
237         /**
238          * {@inheritDoc}
239          */
240         public void windowIconified(WindowEvent e) {
241         }
242
243         /**
244          * {@inheritDoc}
245          */
246         public void windowDeiconified(WindowEvent e) {
247         }
248
249         /**
250          * {@inheritDoc}
251          */
252         public void windowActivated(WindowEvent e) {
253         }
254
255         /**
256          * {@inheritDoc}
257          */
258         public void windowDeactivated(WindowEvent e) {
259         }
260
261 }