home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1999 February / CDW0299.iso / Demos / Cafe / Source.bin / SimpleWizardController.java < prev    next >
Encoding:
Java Source  |  1998-03-18  |  15.3 KB  |  566 lines

  1. /*
  2.  * SimpleWizardController.java
  3.  */
  4.  
  5. package symantec.itools.awt;
  6.  
  7. import java.awt.Component;
  8. import java.util.Vector;
  9.  
  10. /**
  11.  * SimpleWizardController implements the WizardController interface.
  12.  * It is the default WizardController used by Wizard and provides
  13.  * a simple behaviour that can be easily overriden. It manages a stack
  14.  * containing the history of pages shown, and provides consistent
  15.  * defaults for the navigation buttons.
  16.  */
  17.  
  18. public class SimpleWizardController implements WizardController
  19. {
  20.     /**
  21.      * Create a SimpleWizardController.
  22.      *
  23.      * @param wizard
  24.      * A WizardInterface to talk to.
  25.      */
  26.     public SimpleWizardController(WizardInterface wizard)
  27.     {
  28.         this.wizard = wizard;
  29.     }
  30.  
  31.     /**
  32.      * Called before the first page is shown.
  33.      * Default behavior: none.
  34.      */
  35.     public void doPrepare()
  36.     {
  37.         // do nothing
  38.     }
  39.  
  40.     /**
  41.      * Called before a page is shown.
  42.      * Default behavior: add the page to the history stack if the action is
  43.      * NEXT, remove the last page from the history stack if the action is
  44.      * PREVIOUS.
  45.      *
  46.      * @param comp
  47.      * The page that will be shown.
  48.      * @param action
  49.      * The action that has led to this page, either PREVIOUS or NEXT. If it is
  50.      * the first page of the wizard, the action is NEXT.
  51.      * @see #getPreviousPage
  52.      */
  53.     public void preparePage(Component comp, int action)
  54.     {
  55.         // Don't maintain a stack at design time
  56.  
  57.         if (java.beans.Beans.isDesignTime())
  58.             return;
  59.  
  60.         if (action == NEXT)
  61.         {
  62.             stack.addElement(comp);
  63.             //System.err.println("Added element, comp = " + comp.hashCode());
  64.         }
  65.         else if (action == PREVIOUS)
  66.         {
  67.             if (stack.size() > 0) {
  68.                 //System.err.println("preparePage removeElementAt");
  69.                 stack.removeElementAt(stack.size() - 1);
  70.                 //System.err.println("Removed element, comp = " + comp.hashCode());
  71.             }
  72.         }
  73.     }
  74.  
  75.     /**
  76.      * Called after a page is shown.
  77.      * Default behavior: none.
  78.      *
  79.      * @param comp
  80.      * The page that has been shown.
  81.      */
  82.     public void pageShown(Component comp)
  83.     {
  84.         // do nothing
  85.     }
  86.  
  87.     /**
  88.      * Try to validate a page.
  89.      * Default behavior: return the state of the button associated with
  90.      * the action, using the isXxxEnabled methods. For example, if the
  91.      * controller has the Previous button enabled, this method will return
  92.      * true, meaning that it is valid to go back.
  93.      * If the result is true, then the Wizard will proceed with the
  94.      * action, else the state will not change.
  95.      *
  96.      * @param comp
  97.      * The page that must be validated.
  98.      * @param target
  99.      * The page that will be shown if the action is PREVIOUS or NEXT, null
  100.      * otherwise.
  101.      * @param action
  102.      * The action that must be validated, PREVIOUS, NEXT, FINISH, CANCEL or HELP.
  103.      * @return
  104.      * True if the action is validated, false otherwise.
  105.      */
  106.     public boolean validatePage(Component comp, Component target, int action)
  107.     {
  108.         switch(action)
  109.         {
  110.             case NEXT:
  111.                 return isNextEnabled();
  112.             case PREVIOUS:
  113.                 return isPreviousEnabled();
  114.             case FINISH:
  115.                 return isFinishEnabled();
  116.             case CANCEL:
  117.                 return isCancelEnabled();
  118.             case HELP:
  119.                 return isHelpEnabled();
  120.             default:
  121.                 return false;
  122.         }
  123.     }
  124.  
  125.     /**
  126.      * Called before the page is hidden.
  127.      * Default behavior: none.
  128.      *
  129.      * @param comp
  130.      * The page that has been shown.
  131.      */
  132.     public void pageHidden(Component comp)
  133.     {
  134.         // do nothing
  135.     }
  136.  
  137.     /**
  138.      * Called when the FINISH action has been validated.
  139.      * Default behavior: none.
  140.      */
  141.     public void doFinish()
  142.     {
  143.         // do nothing
  144.     }
  145.  
  146.     /**
  147.      * Called when the CANCEL action has been validated.
  148.      * Default behavior: none.
  149.      */
  150.     public void doCancel()
  151.     {
  152.         // do nothing
  153.     }
  154.  
  155.     /**
  156.      * Called when the HELP action has been validated.
  157.      * Default behavior: none.
  158.      */
  159.     public void doHelp()
  160.     {
  161.         // do nothing
  162.     }
  163.  
  164.     /**
  165.      * Tells if the Previous button must be enabled.
  166.      * Default behavior: if setPreviousEnabled has been called,
  167.      * return that value. Else return true if the getPreviousPage method
  168.      * returns a page, false otherwise.
  169.      *
  170.      * @return
  171.      * True if the Previous button must be enabled, false otherwise.
  172.      */
  173.     public boolean isPreviousEnabled()
  174.     {
  175.         if (previousEnabled == 0)
  176.             return false;
  177.         else if (previousEnabled == 1)
  178.             return true;
  179.  
  180.         return (getPreviousPage() != null);
  181.     }
  182.  
  183.     /**
  184.      * Tells if the Next button must be enabled.
  185.      * Default behavior: if setNextEnabled has been called,
  186.      * return that value. Else return true if the getNextPage method
  187.      * returns a page, false otherwise.
  188.      *
  189.      * @return
  190.      * True if the Next button must be enabled, false otherwise.
  191.      */
  192.     public boolean isNextEnabled()
  193.     {
  194.         if (nextEnabled == 0)
  195.             return false;
  196.         else if (nextEnabled == 1)
  197.             return true;
  198.  
  199.         return (getNextPage() != null);
  200.     }
  201.  
  202.     /**
  203.      * Tells if the Finish button must be enabled.
  204.      * Default behavior: if setFinishEnabled has been called,
  205.      * return that value. Else return true if the current page
  206.      * is the last page in the Wizard container.
  207.      *
  208.      * @return
  209.      * True if the Finish button must be enabled, false otherwise.
  210.      */
  211.     public boolean isFinishEnabled()
  212.     {
  213.         if (finishEnabled == 0)
  214.             return false;
  215.         else if (finishEnabled == 1)
  216.             return true;
  217.  
  218.         int current = wizard.getSelectedIndex();
  219.         int count = wizard.getPageCount();
  220.  
  221.         if (!java.beans.Beans.isDesignTime())
  222.         {
  223.             if ((count > 0) && (current != -1))
  224.                 return (current == (count - 1));
  225.             else
  226.                 return false;
  227.         }
  228.         else
  229.         {
  230.             return false;
  231.         }
  232.     }
  233.  
  234.     /**
  235.      * Tells if the Cancel button must be enabled.
  236.      * Default behavior: if setCancelEnabled has been called,
  237.      * return that value. Else return true.
  238.      *
  239.      * @return
  240.      * True if the Cancel button must be enabled, false otherwise.
  241.      */
  242.     public boolean isCancelEnabled()
  243.     {
  244.         if (cancelEnabled == 0)
  245.             return false;
  246.         else if (cancelEnabled == 1)
  247.             return true;
  248.  
  249.         if (java.beans.Beans.isDesignTime())
  250.             return false;
  251.         else
  252.             return true;
  253.     }
  254.  
  255.     /**
  256.      * Tells if the Help button must be enabled.
  257.      * Default behavior: if setHelpEnabled has been called,
  258.      * return that value. Else return true.
  259.      *
  260.      * @return
  261.      * True if the Help button must be enabled, false otherwise.
  262.      */
  263.     public boolean isHelpEnabled()
  264.     {
  265.         if (helpEnabled == 0)
  266.             return false;
  267.         else if (helpEnabled == 1)
  268.             return true;
  269.  
  270.         if (java.beans.Beans.isDesignTime())
  271.             return false;
  272.         else
  273.             return true;
  274.     }
  275.  
  276.     /**
  277.      * Return the previous page.
  278.      * Default behavior: if a previous page has been set with
  279.      * setPreviousPage, return this page. If a previous page index has
  280.      * been set with setPreviousPageIndex, return the associated page.
  281.      * Else, return the next to last element on the stack. If there is none,
  282.      * return null.
  283.      * This method is called before and after a validatePage, and by the
  284.      * default implementation of isPreviousEnabled.
  285.      *
  286.      * @return
  287.      * The previous page to show, null if there is none.
  288.      * @see #setPreviousPage
  289.      * @see #setPreviousPageIndex
  290.      * @see #preparePage
  291.      */
  292.     public Component getPreviousPage()
  293.     {
  294.         if (!java.beans.Beans.isDesignTime())
  295.         {
  296.             if (previousPage != null)
  297.                 return previousPage;
  298.             else if (previousPageIndex != -1)
  299.                 return wizard.getComponentAt(previousPageIndex);
  300.             else if (stack.size() > 1)
  301.                 return (Component)stack.elementAt(stack.size() - 2);
  302.             else
  303.                 return null;
  304.         }
  305.         else
  306.         {
  307.             int current = wizard.getSelectedIndex();
  308.             int count = wizard.getPageCount();
  309.  
  310.             if ((count > 0) && (current > 0))
  311.                 return wizard.getComponentAt(current - 1);
  312.             else
  313.                 return null;
  314.  
  315.         }
  316.     }
  317.  
  318.     /**
  319.      * Return the next page.
  320.      * Default behavior: if a next page has been set with
  321.      * setNextPage, return this page. If a next page index has
  322.      * been set with setNextPageIndex, return the associated page.
  323.      * Else, return the next page in the Wizard components order,
  324.      * page 0 if no page has been shown.
  325.      * If there is none, return null.
  326.      * This method is called before and after a validatePage, and by the
  327.      * default implementation of isNextEnabled.
  328.      *
  329.      * @return
  330.      * The next page to show, null if there is none.
  331.      * @see #setNextPage
  332.      * @see #setNextPageIndex
  333.      */
  334.     public Component getNextPage()
  335.     {
  336.         int current = wizard.getSelectedIndex();
  337.         int count = wizard.getPageCount();
  338.  
  339.         if (!java.beans.Beans.isDesignTime())
  340.         {
  341.             if (nextPage != null)
  342.                 return nextPage;
  343.             else if (nextPageIndex != -1)
  344.                 return wizard.getComponentAt(nextPageIndex);
  345.             else if ((count > 0) && (current != -1) && (current != (count - 1)))
  346.                 return wizard.getComponentAt(current + 1);
  347.             else if ((count > 0) && (current == -1))
  348.                 return wizard.getComponentAt(0);
  349.             else
  350.                 return null;
  351.         }
  352.         else
  353.         {
  354.             if ((count > 0) && (current != -1) && (current != (count - 1)))
  355.                 return wizard.getComponentAt(current + 1);
  356.             else
  357.                 return null;
  358.         }
  359.     }
  360.  
  361.     // Explicit chain information provided by interactions
  362.  
  363.     /**
  364.      * Set an index for the previous page.
  365.      * This index is used by the default implementation of getPreviousPage.
  366.      * It is reset before a new page is prepared.
  367.      *
  368.      * @see #getPreviousPage
  369.      * @see #resetChainInfo
  370.      */
  371.     public void setPreviousPageIndex(int index)
  372.     {
  373.         previousPageIndex = index;
  374.     }
  375.  
  376.     /**
  377.      * Set an index for the next page.
  378.      * This index is used by the default implementation of getNextPage.
  379.      * It is reset before a new page is prepared.
  380.      *
  381.      * @see #getNextPage
  382.      * @see #resetChainInfo
  383.      */
  384.     public void setNextPageIndex(int index)
  385.     {
  386.         nextPageIndex = index;
  387.     }
  388.  
  389.     /**
  390.      * Set a previous page.
  391.      * This page is used by the default implementation of getPreviousPage.
  392.      * It is reset before a new page is prepared.
  393.      *
  394.      * @see #getPreviousPage
  395.      * @see #resetChainInfo
  396.      */
  397.     public void setPreviousPage(Component comp)
  398.     {
  399.         previousPage = comp;
  400.     }
  401.  
  402.     /**
  403.      * Set a next page.
  404.      * This page is used by the default implementation of getNextPage.
  405.      * It is reset before a new page is prepared.
  406.      *
  407.      * @see #getNextPage
  408.      * @see #resetChainInfo
  409.      */
  410.     public void setNextPage(Component comp)
  411.     {
  412.         nextPage = comp;
  413.     }
  414.  
  415.     /**
  416.      * Set the state of the Previous button.
  417.      * This state is used by the default implementation of isPreviousEnabled.
  418.      * It is reset before a new page is prepared.
  419.      *
  420.      * @see #isPreviousEnabled
  421.      * @see #resetChainInfo
  422.      */
  423.     public void setPreviousEnabled(boolean status)
  424.     {
  425.         previousEnabled = status ? 1 : 0;
  426.         wizard.updateButtonsState();
  427.     }
  428.  
  429.     /**
  430.      * Set the state of the Next button.
  431.      * This state is used by the default implementation of isNextEnabled.
  432.      * It is reset before a new page is prepared.
  433.      *
  434.      * @see #isNextEnabled
  435.      * @see #resetChainInfo
  436.      */
  437.     public void setNextEnabled(boolean status)
  438.     {
  439.         nextEnabled = status ? 1 : 0;
  440.         wizard.updateButtonsState();
  441.     }
  442.  
  443.     /**
  444.      * Set the state of the Finish button.
  445.      * This state is used by the default implementation of isFinishEnabled.
  446.      * It is reset before a new page is prepared.
  447.      *
  448.      * @see #isFinishEnabled
  449.      * @see #resetChainInfo
  450.      */
  451.     public void setFinishEnabled(boolean status)
  452.     {
  453.         finishEnabled = status ? 1 : 0;
  454.         wizard.updateButtonsState();
  455.     }
  456.  
  457.     /**
  458.      * Set the state of the Cancel button.
  459.      * This state is used by the default implementation of isCancelEnabled.
  460.      * It is reset before a new page is prepared.
  461.      *
  462.      * @see #isCancelEnabled
  463.      * @see #resetChainInfo
  464.      */
  465.     public void setCancelEnabled(boolean status)
  466.     {
  467.         cancelEnabled = status ? 1 : 0;
  468.         wizard.updateButtonsState();
  469.     }
  470.  
  471.     /**
  472.      * Set the state of the Help button.
  473.      * This state is used by the default implementation of isHelpEnabled.
  474.      * It is reset before a new page is prepared.
  475.      *
  476.      * @see #isHelpEnabled
  477.      * @see #resetChainInfo
  478.      */
  479.     public void setHelpEnabled(boolean status)
  480.     {
  481.         helpEnabled = status ? 1 : 0;
  482.         wizard.updateButtonsState();
  483.     }
  484.  
  485.     /**
  486.      * Reset the chain information set by setPreviousPageIndex,
  487.      * setNextPageIndex, setPreviousPage, setNextPage, and
  488.      * setXxxEnabled. This is called before a new page is preared.
  489.      *
  490.      * @see #setPreviousPageIndex
  491.      * @see #setNextPageIndex
  492.      * @see #setPreviousPage
  493.      * @see #setNextPage
  494.      */
  495.     public void resetChainInfo()
  496.     {
  497.         previousPage = null;
  498.         previousPageIndex = -1;
  499.         nextPage = null;
  500.         nextPageIndex = -1;
  501.  
  502.         previousEnabled = -1;
  503.         nextEnabled = -1;
  504.         finishEnabled = -1;
  505.         cancelEnabled = -1;
  506.         helpEnabled = -1;
  507.     }
  508.  
  509.     // Member variables
  510.  
  511.     /**
  512.      * The WizardInterface.
  513.      */
  514.     protected WizardInterface wizard;
  515.     /**
  516.      * The stack containing the history of the pages shown.
  517.      *
  518.      * @see #preparePage
  519.      * @see #getPreviousPage
  520.      */
  521.     protected Vector stack = new Vector();
  522.  
  523.     /**
  524.      * The index of the previous ("back") page, or -1 if none.
  525.      * @see #setPreviousPageIndex
  526.      */
  527.     protected int previousPageIndex = -1;
  528.     /**
  529.      * The index of the next page, or -1 if none.
  530.      * @see #setNextPageIndex
  531.      */
  532.     protected int nextPageIndex = -1;
  533.  
  534.     /**
  535.      * The previous ("back") page, or <code>null</code> if none.
  536.      * @see #setPreviousPage
  537.      */
  538.     protected Component previousPage;
  539.     /**
  540.      * The next page, or <code>null</code> if none.
  541.      * @see #setNextPage
  542.      */
  543.     protected Component nextPage;
  544.  
  545.     /**
  546.      * True if the Previous button is enabled.
  547.      */
  548.     protected int previousEnabled = -1;
  549.     /**
  550.      * True if the Next button is enabled.
  551.      */
  552.     protected int nextEnabled = -1;
  553.     /**
  554.      * True if the Finish button is enabled.
  555.      */
  556.     protected int finishEnabled = -1;
  557.     /**
  558.      * True if the Cancel button is enabled.
  559.      */
  560.     protected int cancelEnabled = -1;
  561.     /**
  562.      * True if the Help button is enabled.
  563.      */
  564.     protected int helpEnabled = -1;
  565. }
  566.