Wizard.java revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* ident "%Z%%M% %I% %E% SMI"
*
* Copyright 1998-2002 by Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
// Interface for notification of button events
interface WizButtonListener {
public void buttonPressed(int buttonId);
public static final int BACK = 0;
public static final int FORWARD = 1;
public static final int CANCEL = 2;
public static final int HELP = 3;
public static final int FINISH = 4;
}
/**
* This is a widget for presenting a multi-step task, such as an installation
* sequence or other task with many questions or multiple possible paths. This
* component displays as a dialog, with a contents outline on the left and a
* panel for display of each step on the right, with a set of control buttons
* at the bottom. Typical usage is to subclass this class and implement a set
* of WizardStep classes which provide the steps to be executed.
*
* @see WizardStep
*/
// Panel to manage display and processing of buttons at base of window
class WizButtonPanel extends JPanel {
// Adaptor class to catch all button presses and pass along to listeners
class ButtonAdaptor implements ActionListener {
public void actionPerformed(ActionEvent e) {
int buttonId = -1;
} else if (source == forwardButton) {
} else if (source == finishButton) {
}
while (en.hasMoreElements()) {
}
}
}
public WizButtonPanel() {
super();
setBorder(new EtchedBorder());
// Right justify the buttons
// Create event handler
adaptor = new ButtonAdaptor();
/*
* Construct back buttons; need 2 for the separate cards used
* in the button panel
*/
backButton = new PreviousButton();
backButton2 = new PreviousButton();
// Construct forward button
forwardButton = new NextButton();
/*
* Now create cards; we created two copies of buttons that
* needed to be on both cards
*/
// Finish panel replaces the forward button with the finish button
}
// Show the first step
public void showFirst() {
setForwardEnabled(false);
forwardButton.requestFocus(true);
}
// Show the last step
public void showLast() {
backButton.setEnabled(true);
setFinishEnabled(false);
finishButton.requestFocus(true);
}
// Show any other step
public void showMiddle() {
backButton.setEnabled(true);
setForwardEnabled(false);
cancelButton.requestFocus(true);
}
// Allow steps to control when user may advance to next step
public void setForwardEnabled(boolean state) {
}
// Allow the final step to control when a user may complete the task
public void setFinishEnabled(boolean state) {
}
public void addWizButtonListener(WizButtonListener l) {
listeners.addElement(l);
}
public void removeWizButtonListener(WizButtonListener l) {
}
}
/*
* Panel to display the list of steps; we use a very custom JList
* for this as it does reasonable rendering with minimal effort on our part.
*/
class WizContentsPanel extends JPanel {
// Data model for holding the description text displayed
class ContentsModel extends AbstractListModel {
public ContentsModel() {
}
}
public int getSize() {
}
}
}
// Class to render the cells in the list
class ContentsRenderer implements ListCellRenderer {
public ContentsRenderer() {
}
boolean cellHasFocus) {
// Format label properly for i18n
text.setWrapStyleWord(true);
text.setLineWrap(true);
// Selected component is opaque, others transparent
return stepBox;
}
}
/*
* This class is defined strictly so that we can prevent
* focus from ever reaching the steps list as it is a display-only
* use of JList, not intended for any sort of input by the user.
*/
super(m);
// Don't allow this list to be focused
setFocusable(false);
}
// Ignore mouse clicks so highlighted step can't be changed
protected void processMouseEvent(MouseEvent e) {
return;
}
// Ignore mouse drags, which can also change highlighting
protected void processMouseMotionEvent(MouseEvent e) {
return;
}
}
public WizContentsPanel() {
l.setLabelFor(this);
add(l);
model = new ContentsModel();
/*
* Wrap the list with scroll bars, vertical as necessary but
* never a horizontal one.
*/
}
// Index the steps using the description string
}
}
}
/*
* This class provides a simple card layout to display each step as needed.
*/
class WizStepPanel extends JPanel {
public WizStepPanel() {
// Make sure we have some space around the edges.
}
}
}
}
int stepNumber = 0;
/**
* Constructs a wizard with the specified owning frame and title.
*
* @param owner the owning frame
* @param title the wizard's title string
*/
// Put the buttons at the bottom.
buttonPanel = new WizButtonPanel();
/*
* The main display is designed to use a 7:11 ratio for the horizontal
* area consumed by the contents panel and the display panel,
* respectively. There's nothing particularly magical about the
* ratio, that's just the way the designer drew it.
*/
contentsPanel = new WizContentsPanel();
stepPanel = new WizStepPanel();
// Consume all space not needed for buttons
/*
* We manage the button interactions, but the steps get limited veto
* power
*/
public void buttonPressed(int buttonId) {
switch (buttonId) {
case BACK:
break;
case FORWARD:
showNextStep();
break;
case FINISH:
doFinish();
break;
case CANCEL:
doCancel();
break;
case HELP:
doHelp();
break;
default:
}
}
});
}
/**
* Override of setVisible to control size when displayed. Perhaps this
* should be relaxed, but subclasses can always do whatever they want.
* @param state make visible or not?
*/
public void setVisible(boolean state) {
if (state) {
}
super.setVisible(state);
}
/**
* Adds a step to the wizard. Steps <bold>must</bold> be added in the
* sequence they will be displayed when traversing forward.
* @param step a <code>WizardStep</code>
*/
}
// Deactivate currently active step
if (activeStep != null) {
// Step vetoed its deactivation. We honor its wishes.
return false;
}
}
/*
* Activate new step by updating contents, display area, and possibly
* buttons
*/
activeStep = step;
} else {
}
return true;
}
// Show some arbitrary step indexed by number.
throws ArrayIndexOutOfBoundsException {
}
/**
* Show the very first step.
*/
public void showFirstStep() {
stepNumber = 0;
}
/**
* Show the next step.
*/
public void showNextStep() {
++stepNumber;
try {
// Handle step vetoing deactivation
--stepNumber;
}
} catch (ArrayIndexOutOfBoundsException e) {
--stepNumber;
}
}
/**
* Show the previous step.
*/
public void showPreviousStep() {
--stepNumber;
try {
++stepNumber;
}
} catch (ArrayIndexOutOfBoundsException e) {
++stepNumber;
}
}
/**
* Show the last step.
*/
public void showLastStep() {
int saveStep = stepNumber;
try {
}
} catch (ArrayIndexOutOfBoundsException e) {
}
}
/**
* Control state of the forward button.
* @param state <code>true</code> to enable the button
*/
public void setForwardEnabled(boolean state) {
}
/**
* Control state of the finish button.
* @param state <code>true</code> to enable the button
*/
public void setFinishEnabled(boolean state) {
}
/**
* Handle user's press of the Cancel button. Subclasses can override for
* special cleanup needs.
*/
public void doCancel() {
fireActionPerformed("cancelled");
dispose();
}
/**
* Handle user's press of the Finish button. Subclasses should override
* to perform whatever processing needed to complete the wizard's task.
*/
public void doFinish() {
fireActionPerformed("finished");
dispose();
}
/**
* Handle user's press of the Help button. Does nothing by default,
* subclasses can override to provide help as desired.
*/
public void doHelp() {
}
/**
* Utility function to create a multi-line text display such as for the
* explanatory text that most wizard steps use.
* @param text the text to display
* @param rows the number of rows to use for displaying the text
* @param columns the number of columns to wrap text at. 45 is generally a
* good number for standard wizards with standard fonts
* @return a <code>JComponent</code> displaying the supplied text
*/
public static JComponent createTextArea(
// We extend JTextArea in order to make this behave more like a label
class MyTextArea extends JTextArea {
/*
* Create a text area with word-wrapping, no editing,
* and no background.
*/
setLineWrap(true);
setWrapStyleWord(true);
setEditable(false);
setOpaque(false);
setFocusable(false);
}
}
// Put it in a scrollpane to get sizing to happen
// Put empty borders on the subcomponents so that all we get is text
scrollPane.setBorder(b);
return scrollPane;
}
/**
* Add a listener for action events. Wizard fires an
* <code>ActionEvent</code> when user either cancels or finishes the wizard.
*/
public void addActionListener(ActionListener l) {
listeners.addElement(l);
}
/**
* Remove an action listener.
*/
public void removeActionListener(ActionListener l) {
}
/**
* Fire an action event.
*/
command);
l.actionPerformed(e);
}
}
}