1
/* ========================================================================
2
* JCommon : a free general purpose class library for the Java(tm) platform
3
* ========================================================================
5
* (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
7
* Project Info: http://www.jfree.org/jcommon/index.html
9
* This library is free software; you can redistribute it and/or modify it
10
* under the terms of the GNU Lesser General Public License as published by
11
* the Free Software Foundation; either version 2.1 of the License, or
12
* (at your option) any later version.
14
* This library is distributed in the hope that it will be useful, but
15
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17
* License for more details.
19
* You should have received a copy of the GNU Lesser General Public
20
* License along with this library; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
24
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
25
* in the United States and other countries.]
30
* (C) Copyright 2000-2004, by Object Refinery Limited.
32
* Original Author: David Gilbert (for Object Refinery Limited);
35
* $Id: WizardDialog.java,v 1.4 2005/10/18 13:18:34 mungady Exp $
37
* Changes (from 26-Oct-2001)
38
* --------------------------
39
* 26-Oct-2001 : Changed package to com.jrefinery.ui.*;
40
* 14-Oct-2002 : Fixed errors reported by Checkstyle (DG);
46
import java.awt.BorderLayout;
47
import java.awt.Container;
48
import java.awt.event.ActionEvent;
49
import java.awt.event.ActionListener;
50
import java.util.ArrayList;
52
import javax.swing.BorderFactory;
53
import javax.swing.JButton;
54
import javax.swing.JDialog;
55
import javax.swing.JFrame;
56
import javax.swing.JPanel;
59
* A dialog that presents the user with a sequence of steps for completing a task. The dialog
60
* contains "Next" and "Previous" buttons, allowing the user to navigate through the task.
62
* When the user backs up by one or more steps, the dialog keeps the completed steps so that
63
* they can be reused if the user doesn't change anything - this handles the cases where the user
64
* backs up a few steps just to review what has been completed.
66
* But if the user changes some options in an earlier step, then the dialog may have to discard
67
* the later steps and have them repeated.
69
* THIS CLASS IS NOT WORKING CORRECTLY YET.
72
* @author David Gilbert
74
public class WizardDialog extends JDialog implements ActionListener {
76
/** The end result of the wizard sequence. */
77
private Object result;
79
/** The current step in the wizard process (starting at step zero). */
82
/** A reference to the current panel. */
83
private WizardPanel currentPanel;
85
/** A list of references to the panels the user has already seen - used for navigating through
86
the steps that have already been completed. */
87
private java.util.List panels;
89
/** A handy reference to the "previous" button. */
90
private JButton previousButton;
92
/** A handy reference to the "next" button. */
93
private JButton nextButton;
95
/** A handy reference to the "finish" button. */
96
private JButton finishButton;
98
/** A handy reference to the "help" button. */
99
private JButton helpButton;
102
* Standard constructor - builds and returns a new WizardDialog.
104
* @param owner the owner.
105
* @param modal modal?
106
* @param title the title.
107
* @param firstPanel the first panel.
109
public WizardDialog(final JDialog owner, final boolean modal,
110
final String title, final WizardPanel firstPanel) {
112
super(owner, title + " : step 1", modal);
114
this.currentPanel = firstPanel;
116
this.panels = new ArrayList();
117
this.panels.add(firstPanel);
118
setContentPane(createContent());
123
* Standard constructor - builds a new WizardDialog owned by the specified JFrame.
125
* @param owner the owner.
126
* @param modal modal?
127
* @param title the title.
128
* @param firstPanel the first panel.
130
public WizardDialog(final JFrame owner, final boolean modal,
131
final String title, final WizardPanel firstPanel) {
133
super(owner, title + " : step 1", modal);
135
this.currentPanel = firstPanel;
137
this.panels = new ArrayList();
138
this.panels.add(firstPanel);
139
setContentPane(createContent());
143
* Returns the result of the wizard sequence.
145
* @return the result.
147
public Object getResult() {
152
* Returns the total number of steps in the wizard sequence, if this number is known. Otherwise
153
* this method returns zero. Subclasses should override this method unless the number of steps
156
* @return the number of steps.
158
public int getStepCount() {
163
* Returns true if it is possible to back up to the previous panel, and false otherwise.
167
public boolean canDoPreviousPanel() {
168
return (this.step > 0);
172
* Returns true if there is a 'next' panel, and false otherwise.
176
public boolean canDoNextPanel() {
177
return this.currentPanel.hasNextPanel();
181
* Returns true if it is possible to finish the sequence at this point (possibly with defaults
182
* for the remaining entries).
186
public boolean canFinish() {
187
return this.currentPanel.canFinish();
191
* Returns the panel for the specified step (steps are numbered from zero).
193
* @param step the current step.
197
public WizardPanel getWizardPanel(final int step) {
198
if (step < this.panels.size()) {
199
return (WizardPanel) this.panels.get(step);
209
* @param event the event.
211
public void actionPerformed(final ActionEvent event) {
212
final String command = event.getActionCommand();
213
if (command.equals("nextButton")) {
216
else if (command.equals("previousButton")) {
219
else if (command.equals("finishButton")) {
225
* Handles a click on the "previous" button, by displaying the previous panel in the sequence.
227
public void previous() {
229
final WizardPanel previousPanel = getWizardPanel(this.step - 1);
230
// tell the panel that we are returning
231
previousPanel.returnFromLaterStep();
232
final Container content = getContentPane();
233
content.remove(this.currentPanel);
234
content.add(previousPanel);
235
this.step = this.step - 1;
236
this.currentPanel = previousPanel;
237
setTitle("Step " + (this.step + 1));
244
* Displays the next step in the wizard sequence.
248
WizardPanel nextPanel = getWizardPanel(this.step + 1);
249
if (nextPanel != null) {
250
if (!this.currentPanel.canRedisplayNextPanel()) {
251
nextPanel = this.currentPanel.getNextPanel();
255
nextPanel = this.currentPanel.getNextPanel();
258
this.step = this.step + 1;
259
if (this.step < this.panels.size()) {
260
this.panels.set(this.step, nextPanel);
263
this.panels.add(nextPanel);
266
final Container content = getContentPane();
267
content.remove(this.currentPanel);
268
content.add(nextPanel);
270
this.currentPanel = nextPanel;
271
setTitle("Step " + (this.step + 1));
278
* Finishes the wizard.
280
public void finish() {
281
this.result = this.currentPanel.getResult();
286
* Enables/disables the buttons according to the current step. A good idea would be to ask the
287
* panels to return the status...
289
private void enableButtons() {
290
this.previousButton.setEnabled(this.step > 0);
291
this.nextButton.setEnabled(canDoNextPanel());
292
this.finishButton.setEnabled(canFinish());
293
this.helpButton.setEnabled(false);
297
* Checks, whether the user cancelled the dialog.
301
public boolean isCancelled() {
306
* Creates a panel containing the user interface for the dialog.
310
public JPanel createContent() {
312
final JPanel content = new JPanel(new BorderLayout());
313
content.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
314
content.add((JPanel) this.panels.get(0));
315
final L1R3ButtonPanel buttons = new L1R3ButtonPanel("Help", "Previous", "Next", "Finish");
317
this.helpButton = buttons.getLeftButton();
318
this.helpButton.setEnabled(false);
320
this.previousButton = buttons.getRightButton1();
321
this.previousButton.setActionCommand("previousButton");
322
this.previousButton.addActionListener(this);
323
this.previousButton.setEnabled(false);
325
this.nextButton = buttons.getRightButton2();
326
this.nextButton.setActionCommand("nextButton");
327
this.nextButton.addActionListener(this);
328
this.nextButton.setEnabled(true);
330
this.finishButton = buttons.getRightButton3();
331
this.finishButton.setActionCommand("finishButton");
332
this.finishButton.addActionListener(this);
333
this.finishButton.setEnabled(false);
335
buttons.setBorder(BorderFactory.createEmptyBorder(4, 0, 0, 0));
336
content.add(buttons, BorderLayout.SOUTH);