net.java.truelicense.swing.nexes.WizardController Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of truelicense-swing Show documentation
Show all versions of truelicense-swing Show documentation
The TrueLicense Swing module provides a graphical user interface for
consuming license keys.
/*
* Copyright (C) 2005-2013 Schlichtherle IT Services.
* All rights reserved. Use is subject to license terms.
*/
package net.java.truelicense.swing.nexes;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* The controller for the wizard component, which is responsible for reacting to
* events generated by pushing any of the three buttons, 'Next', 'Previous', and
* 'Cancel.' Based on what button is pressed, the controller will update the
* model to show a new panel and reset the state of the buttons as necessary.
*
* @author Robert Eckstein (original code)
* @author Christian Schlichtherle (revision)
*/
public class WizardController implements ActionListener {
private final Wizard wizard;
/**
* This constructor accepts a reference to the Wizard component that created
* it, which it uses to update the button components and access the
* WizardModel.
*
* @param wizard A callback to the Wizard component that created this controller.
*/
public WizardController(final Wizard wizard) {
this.wizard = wizard;
}
private WizardModel getModel() { return wizard.getModel(); }
/**
* Calling method for the action listener interface. This class listens for
* actions performed by the buttons in the Wizard class, and calls methods
* below to determine the correct course of action.
*
* @param evt The ActionEvent that occurred.
*/
@Override
public void actionPerformed(ActionEvent evt) {
final String command = evt.getActionCommand();
if (command.equals(Wizard.BACK_BUTTON_ACTION_COMMAND))
backButtonPressed();
else if (command.equals(Wizard.NEXT_BUTTON_ACTION_COMMAND))
nextButtonPressed();
else if (command.equals(Wizard.CANCEL_BUTTON_ACTION_COMMAND))
cancelButtonPressed();
}
private void backButtonPressed() {
final WizardPanelDescriptor
current = getModel().getCurrentPanelDescriptor();
assert null != current;
final String back = current.getBackPanelIdentifier();
wizard.setCurrentPanelIdentifier(back);
}
private void nextButtonPressed() {
final WizardPanelDescriptor
current = getModel().getCurrentPanelDescriptor();
assert null != current;
final String next = current.getNextPanelIdentifier();
if (WizardPanelDescriptor.FINISH.equals(next))
wizard.close(Wizard.FINISH_RETURN_CODE);
else
wizard.setCurrentPanelIdentifier(next);
}
private void cancelButtonPressed() {
wizard.close(Wizard.CANCEL_RETURN_CODE);
}
void resetButtonsToPanelRules() {
// Reset the buttons to support the original panel rules,
// including whether the next or back buttons are enabled or
// disabled, or if the panel is finishable.
final WizardModel model = getModel();
final WizardPanelDescriptor current = model.getCurrentPanelDescriptor();
assert null != current;
// If the panel in question has another panel behind it, enable
// the back button. Otherwise, disable it.
model.setBackButtonText(Wizard.DEFAULT_BACK_BUTTON_TEXT);
if (current.getBackPanelIdentifier() != null)
model.setBackButtonEnabled(Boolean.TRUE);
else
model.setBackButtonEnabled(Boolean.FALSE);
// If the panel in question has one or more panels in front of it,
// enable the next button. Otherwise, disable it.
//model.setNextButtonText(Wizard.DEFAULT_NEXT_BUTTON_TEXT);
if (current.getNextPanelIdentifier() != null)
model.setNextButtonEnabled(Boolean.TRUE);
else
model.setNextButtonEnabled(Boolean.FALSE);
// If the panel in question is the last panel in the series, change
// the Next button to Finish and enable it. Otherwise, set the text
// back to Next.
if (WizardPanelDescriptor.FINISH.equals(current.getNextPanelIdentifier())) {
model.setNextButtonText(Wizard.DEFAULT_FINISH_BUTTON_TEXT);
model.setNextButtonEnabled(Boolean.TRUE);
} else {
model.setNextButtonText(Wizard.DEFAULT_NEXT_BUTTON_TEXT);
}
}
}