001: /**
002: * Wizard Framework
003: * Copyright 2004 - 2005 Andrew Pietsch
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * $Id: WizardAction.java,v 1.9 2005/05/16 23:06:59 pietschy Exp $
020: */package org.pietschy.wizard;
021:
022: import javax.swing.*;
023: import java.beans.PropertyChangeListener;
024: import java.beans.PropertyChangeEvent;
025: import java.awt.event.ActionEvent;
026: import java.awt.event.KeyEvent;
027:
028: /**
029: * Base class for all Wizard actions.
030: */
031: public abstract class WizardAction extends AbstractAction implements
032: PropertyChangeListener {
033: protected Wizard wizard;
034: private WizardStep activeStep;
035:
036: protected WizardAction(String key, Wizard wizard, Icon icon) {
037: this (key, wizard);
038: putValue(Action.SMALL_ICON, icon);
039: }
040:
041: protected WizardAction(String key, Wizard wizard) {
042: super (I18n.getString(key + ".text"));
043: this .wizard = wizard;
044: getModel().addPropertyChangeListener(this );
045: activeStep = getModel().getActiveStep();
046: if (activeStep != null)
047: activeStep.addPropertyChangeListener(this );
048:
049: putValue(Action.MNEMONIC_KEY, new Integer(I18n.getMnemonic(key
050: + ".mnemonic")));
051:
052: updateState();
053: }
054:
055: protected Wizard getWizard() {
056: return wizard;
057: }
058:
059: public WizardModel getModel() {
060: return getWizard().getModel();
061: }
062:
063: public WizardStep getActiveStep() {
064: return activeStep;
065: }
066:
067: public final void actionPerformed(ActionEvent e) {
068: try {
069: doAction(e);
070: } catch (InvalidStateException ise) {
071: handleInvalideStateException(ise);
072: }
073: }
074:
075: protected void handleInvalideStateException(
076: InvalidStateException ise) {
077: if (ise.isShowUser()) {
078: JOptionPane.showMessageDialog(getWizard(),
079: ise.getMessage(), "Error",
080: JOptionPane.INFORMATION_MESSAGE);
081: }
082: }
083:
084: public abstract void doAction(ActionEvent e)
085: throws InvalidStateException;
086:
087: protected abstract void updateState();
088:
089: public void propertyChange(PropertyChangeEvent evt) {
090: if (evt.getPropertyName().equals("activeStep")) {
091: if (activeStep != null)
092: activeStep.removePropertyChangeListener(this );
093: activeStep = (WizardStep) evt.getNewValue();
094: activeStep.addPropertyChangeListener(this);
095: }
096:
097: updateState();
098: }
099:
100: }
|