package simtools.ui;
import java.awt.Dimension;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
/**
* Type
* <br><b>Summary:</b><br>
* A WizardManager controls wizard UI configuration, according to:
* <ul>
* <li>The current step
* <li>The current settings
* </ul>
*
* Thus, it determine whether or not it is possible to finish, to go forward.
* A wizard manager provides next and previous steps.
*/
public abstract class WizardManager{
/**<b>(Map)</b> settings: The settings map. Shall contain at final step
* all settings needed to produce what has to be produced.
*
* Note: settings shall contain no duplicate keys.
*/
protected Map settings;
/**<b>(WizardPage)</b> currentPage: The currentPage.*/
protected WizardPage currentPage;
/**<b>(Stack)</b> pageHistory: The pageHistory.*/
protected Stack pageHistory;
/**
* The object built on finish operation;
*/
protected Object wizardObject;
public WizardManager(){
currentPage=null;
pageHistory = new Stack();
settings = new HashMap();
wizardObject = null;
}
/**
* @return the maximumn size to allocate for pages in wizard displayer
*/
//TODO could be improved --> use of CardLayout...
public Dimension getMaximumnPageSize(){
return currentPage.getPreferredSize();
}
/**
* Method getCurrentStep
* <br><b>Summary:</b><br>
* @return <b>(WizardPage)</b> A WizardPage. The current Wizard page or null if no current page.
*/
public WizardPage getCurrentStep(){
return currentPage;
}
/**
* Method getPreviousStep
* <br><b>Summary:</b><br>
* @return <b>(WizardPage)</b> A WizardPage. The previous Wizard page or null if no previous page.
*/
public WizardPage getPreviousStep(){
if (pageHistory.isEmpty())
return null;
return (WizardPage)pageHistory.peek();
}
/**
* Method processToNextStep
* <br><b>Summary:</b><br>
* Update setting and compute the next page to dispay
*/
public void processToNextStep(){
// Get info from current panel and update settings
if (currentPage!=null){
Map map = currentPage.getInformation();
if (map!=null)
settings.putAll(map);
// Update history
pageHistory.push(currentPage);
}
// Compute the next step according to current state and settings
currentPage = next();
// Update current page if needed
currentPage.setPageFromSettings(settings);
}
/**
* Method processToPreviousStep
* <br><b>Summary:</b><br>
* get previous page, restore previous settings
*
*/
public void processToPreviousStep(){
currentPage= (WizardPage)pageHistory.pop();
// All info contained in this page shall be withdrawn from wizard manager settings
if (currentPage!=null){
Map map = currentPage.getInformation();
if (map!=null){
for (Iterator it = map.keySet().iterator(); it.hasNext(); ) {
settings.remove(it.next());
}
}
}
}
// What has to be implemented !!!
/**
* Method getAllPagesTitle
* <br><b>Summary:</b><br>
* @return <b>(String[])</b> A list of pages titles that describes the differents steps to
* terminate the wizart.
*/
public abstract String[] getStepsTitles();
/**
* Method canFinish
* <br><b>Summary:</b><br>
* @return <b>(boolean)</b> Return true if a next step is available
*/
public abstract boolean canNext();
/**
* Method getNextStep
* <br><b>Summary:</b><br>
* Compute next step in function of current state and settings
* @return <b>(WizardPage)</b> the next page.
*/
protected abstract WizardPage next();
/**
* Method canFinish
* <br><b>Summary:</b><br>
* @return <b>(boolean)</b> Return true if wizard process can terminate
*/
public abstract boolean canFinish();
/**
* Method finish
* <br><b>Summary:</b><br>
* process finish action
*/
public void finish(){
// Get info from current panel and update settings
if (currentPage!=null){
Map map = currentPage.getInformation();
if (map!=null)
settings.putAll(map);
}
// Apply what have to be applied using the whole wizard settings
wizardObject = processFinish();
}
/**
* Method processFinish
* <br><b>Summary:</b><br>
* Instantiate whatever object (if any) the wizard creates from its
* gathered data.
* @return <b>(Object)</b> An object composed based on what the user entered in the wizard. Return null if
* production cannot be accomplished
*/
protected abstract Object processFinish();
public Object getWizardObject(){
return wizardObject;
}
public Map getSettings() {
return settings;
}
}