home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / VCAFE.3.0A / Main.bin / WizardController.java < prev    next >
Text File  |  1998-11-12  |  14KB  |  758 lines

  1.  
  2. package com.symantec.itools.frameworks.wizard;
  3.  
  4.  
  5. import com.sun.java.swing.JPanel;
  6. import java.awt.Image;
  7. import java.io.InputStream;
  8. import java.io.IOException;
  9. import java.net.URL;
  10. import java.util.Enumeration;
  11. import java.util.Vector;
  12. import com.symantec.itools.swing.icons.ImageIcon;
  13.  
  14.  
  15. /**
  16.  * @author Symantec Internet Tools Division
  17.  * @version 1.0
  18.  * @since VCafe 3.0
  19.  */
  20.  
  21.  
  22. public class WizardController
  23. {
  24.     protected static final int NULL_PANEL = -2;
  25.  
  26.     /**
  27.      * @since VCafe 3.0
  28.      */
  29.     protected Wizard     wizard;
  30.  
  31.     /**
  32.      * @since VCafe 3.0
  33.      */
  34.     protected Vector     panels;
  35.  
  36.     /**
  37.      * @since VCafe 3.0
  38.      */
  39.     protected int        currentPanelIndex;
  40.  
  41.     /**
  42.      * @since VCafe 3.0
  43.      */
  44.     protected JPanel currentPanel;
  45.  
  46.     /**
  47.      * @since VCafe 3.0
  48.      */
  49.     protected String     title;
  50.  
  51.     {
  52.         currentPanelIndex = -1;
  53.         panels            = new Vector();
  54.     }
  55.  
  56.     public WizardController()
  57.     {
  58.     }
  59.  
  60.     public WizardController(Wizard w)
  61.     {
  62.         wizard = w;
  63.         wizard.setWizardController(this);
  64.     }
  65.  
  66.     public void setWizard(Wizard w)
  67.     {
  68.         wizard = w;
  69.         wizard.setWizardController(this);
  70.     }
  71.  
  72.     /**
  73.      * @since VCafe 3.0
  74.      */
  75.  
  76.     public void init()
  77.     {
  78.         if(panels.size() > 0)
  79.         {
  80.             currentPanelIndex = firstPanelIndex();
  81.             currentPanel      = (JPanel)panels.elementAt(currentPanelIndex);
  82.  
  83.             if(wizard != null)
  84.             {
  85.                 if(currentPanelIndex > 0)
  86.                 {
  87.                     wizard.setBackEnabled(true);
  88.                 }
  89.                 else
  90.                 {
  91.                     wizard.setBackEnabled(false);
  92.                 }
  93.  
  94.                 wizard.setCancelEnabled(true);
  95.                 wizard.setHelpEnabled(true);
  96.  
  97.                 if(panels.size() > 1 + currentPanelIndex)
  98.                 {
  99.                     wizard.setNextEnabled(true);
  100.                     wizard.setFinishEnabled(false);
  101.                 }
  102.                 else
  103.                 {
  104.                     wizard.setNextEnabled(false);
  105.                     wizard.setFinishEnabled(true);
  106.                 }
  107.  
  108.                 wizard.showPanel(currentPanel);
  109.             }
  110.  
  111.             if(currentPanel instanceof WizardPage)
  112.             {
  113.                 ((WizardPage)currentPanel).entering();
  114.             }
  115.         }
  116.         else
  117.         {
  118.             if(wizard != null)
  119.             {
  120.                 wizard.setBackEnabled(false);
  121.                 wizard.setNextEnabled(false);
  122.                 wizard.setCancelEnabled(true);
  123.                 wizard.setHelpEnabled(true);
  124.                 wizard.setFinishEnabled(true);
  125.             }
  126.         }
  127.     }
  128.  
  129.     /**
  130.      * @param panel TODO
  131.      * @since VCafe 3.0
  132.      */
  133.  
  134.     public void addPanel(JPanel panel)
  135.     {
  136.         if(panel instanceof WizardPage)
  137.         {
  138.             ((WizardPage)panel).setWizardController(this);
  139.         }
  140.  
  141.         panels.addElement(panel);
  142.     }
  143.  
  144.     /**
  145.      * @param panel TODO
  146.      * @param index TODO
  147.      * @since VCafe 3.0
  148.      */
  149.  
  150.     public boolean addPanel(JPanel panel, int index)
  151.     {
  152.         if(panel instanceof WizardPage)
  153.         {
  154.             ((WizardPage)panel).setWizardController(this);
  155.         }
  156.  
  157.         if(index < 0)
  158.         {
  159.             panels.addElement(panel);
  160.         }
  161.         else
  162.         {
  163.             panels.insertElementAt(panel, index);
  164.         }
  165.  
  166.         return true;
  167.     }
  168.  
  169.     /**
  170.      * @param panel TODO
  171.      * @since VCafe 3.0
  172.      */
  173.  
  174.     public boolean removePanel(JPanel panel)
  175.     {
  176.         return panels.removeElement(panel);
  177.     }
  178.  
  179.     /**
  180.      * @param index TODO
  181.      * @since VCafe 3.0
  182.      */
  183.  
  184.     public boolean removePanel(int index)
  185.     {
  186.         if (index >= 0 && index < panels.size())
  187.         {
  188.             panels.removeElementAt(index);
  189.             return true;
  190.         }
  191.         else
  192.             return false;
  193.     }
  194.  
  195.     /**
  196.      * @since VCafe 3.0
  197.      */
  198.  
  199.     public Wizard getWizard()
  200.     {
  201.         return wizard;
  202.     }
  203.  
  204.     /**
  205.      * @since VCafe 3.0
  206.      */
  207.  
  208.     public void backPanel()
  209.     {
  210.         if(currentPanelIndex > 0)
  211.         {
  212.             currentPanelIndex = computePreviousPanelIndex(currentPanelIndex);
  213.             currentPanel = (JPanel)panels.elementAt(currentPanelIndex);
  214.  
  215.             if(currentPanelIndex > 0 && wizard != null)
  216.             {
  217.                 wizard.setBackEnabled(true);
  218.             }
  219.         }
  220.  
  221.         if(wizard != null)
  222.         {
  223.             if(currentPanelIndex == 0)
  224.             {
  225.                 wizard.setBackEnabled(false);
  226.             }
  227.  
  228.             if(panels.size() > currentPanelIndex+1)
  229.             {
  230.                 wizard.setNextEnabled(true);
  231.             }
  232.  
  233.             wizard.setFinishEnabled(false);
  234.             wizard.showPanel(currentPanel);
  235.         }
  236.  
  237.         if(currentPanel instanceof WizardPage)
  238.         {
  239.             ((WizardPage)currentPanel).entering();
  240.         }
  241.     }
  242.  
  243.     /**
  244.      * @since VCafe 3.0
  245.      */
  246.     public void nextPanel()
  247.     {
  248.         if(!(currentPanel instanceof WizardPage) || (((WizardPage)currentPanel).exiting()))
  249.         {
  250.             currentPanelIndex = computeNextPanelIndex(currentPanelIndex);
  251.  
  252.             if(wizard != null)
  253.             {
  254.                 wizard.setBackEnabled(true);
  255.             }
  256.  
  257.             if(currentPanelIndex < panels.size())
  258.             {
  259.                 currentPanel = (JPanel)panels.elementAt(currentPanelIndex);
  260.             }
  261.         }
  262.  
  263.         if(currentPanelIndex == panels.size() - 1)
  264.         {
  265.             if(wizard != null)
  266.             {
  267.                 wizard.setBackEnabled(true);
  268.                 wizard.setNextEnabled(false);
  269.                 wizard.setFinishEnabled(true);
  270.             }
  271.         }
  272.         
  273.         if(wizard != null)
  274.         {
  275.             wizard.showPanel(currentPanel);
  276.         }
  277.  
  278.         if(currentPanel instanceof WizardPage)
  279.         {
  280.             ((WizardPage)currentPanel).entering();
  281.         }
  282.     }
  283.  
  284.     /**
  285.      * @since VCafe 3.0
  286.      */
  287.  
  288.     protected int firstPanelIndex()
  289.     {
  290.         return 0;
  291.     }
  292.  
  293.     /**
  294.      * @param currentIndex TODO
  295.      * @since VCafe 3.0
  296.      */
  297.  
  298.     protected int computeNextPanelIndex(int currentIndex)
  299.     {
  300.         int max;
  301.  
  302.         max = panels.size();
  303.  
  304.         if(max > currentIndex + 1)
  305.         {
  306.             return currentIndex + 1;
  307.         }
  308.  
  309.         return NULL_PANEL;
  310.     }
  311.  
  312.     /**
  313.      * @param currentIndex TODO
  314.      * @since VCafe 3.0
  315.      */
  316.  
  317.     protected int computePreviousPanelIndex(int currentIndex)
  318.     {
  319.         if (currentIndex > 0)
  320.         {
  321.             return currentIndex - 1;
  322.         }
  323.  
  324.         return NULL_PANEL;
  325.     }
  326.  
  327.     /**
  328.      * @param goHere TODO
  329.      * @exception java.lang.ArrayIndexOutOfBoundsException
  330.      * @since VCafe 3.0
  331.      */
  332.     protected int gotoPanel(JPanel goHere)
  333.         throws ArrayIndexOutOfBoundsException
  334.     {
  335.         int newIndex;
  336.  
  337.         newIndex = panels.indexOf(goHere);
  338.  
  339.         if((!(currentPanel instanceof WizardPage)) || (((WizardPage)currentPanel).exiting()))
  340.         {
  341.             currentPanelIndex = newIndex;
  342.             currentPanel = (JPanel)panels.elementAt(currentPanelIndex);
  343.  
  344.             if(currentPanel instanceof WizardPage)
  345.             {
  346.                 ((WizardPage)currentPanel).entering();
  347.             }
  348.  
  349.             if(wizard != null)
  350.             {
  351.                 wizard.showPanel(currentPanel);
  352.             }
  353.         }
  354.  
  355.         return newIndex;
  356.     }
  357.  
  358.     /**
  359.      * @since VCafe 3.0
  360.      */
  361.  
  362.     public void finish()
  363.     {
  364.         if((!(currentPanel instanceof WizardPage)) || ((WizardPage)currentPanel).exiting())
  365.         {
  366.             for(Enumeration e = panels.elements(); e.hasMoreElements();)
  367.             {
  368.                 JPanel panel;
  369.  
  370.                 panel = (JPanel)e.nextElement();
  371.  
  372.                 if(panel instanceof WizardPage)
  373.                 {
  374.                     ((WizardPage)panel).finish();
  375.                 }
  376.             }
  377.         }
  378.     }
  379.  
  380.     /**
  381.      * @since VCafe 3.0
  382.      */
  383.  
  384.     public void cancel()
  385.     {
  386.         if((!(currentPanel instanceof WizardPage)) || (((WizardPage)currentPanel).exiting()))
  387.         {
  388.             for(Enumeration e = panels.elements(); e.hasMoreElements();)
  389.             {
  390.                 JPanel panel;
  391.  
  392.                 panel = (JPanel)e.nextElement();
  393.  
  394.                 if(panel instanceof WizardPage)
  395.                 {
  396.                     ((WizardPage)panel).cancel();
  397.                 }
  398.             }
  399.         }
  400.     }
  401.  
  402.     /**
  403.      * @since VCafe 3.0
  404.      */
  405.  
  406.     public void cancelWizard()
  407.     {
  408.         if(wizard != null)
  409.         {
  410.             wizard.cancel();
  411.         }
  412.     }
  413.  
  414.     /**
  415.      * @since VCafe 3.0
  416.      */
  417.  
  418.     public void help()
  419.     {
  420.     }
  421.  
  422.     /**
  423.      * @param str TODO
  424.      * @since VCafe 3.0
  425.      */
  426.  
  427.     public void setWizardTitle(String str)
  428.     {
  429.         title = str;
  430.     }
  431.  
  432.     /**
  433.      * @since VCafe 3.0
  434.      */
  435.  
  436.     public String getWizardTitle()
  437.     {
  438.         return (title);
  439.     }
  440.  
  441.     /**
  442.      * @param dataName TODO
  443.      * @since VCafe 3.0
  444.      */
  445.  
  446.     public Object getData(String dataName)
  447.     {
  448.         for(Enumeration e = panels.elements(); e.hasMoreElements();)
  449.         {
  450.             JPanel panel;
  451.  
  452.             panel = (JPanel)e.nextElement();
  453.  
  454.             if(panel instanceof WizardPage)
  455.             {
  456.                 Object data;
  457.  
  458.                 data = ((WizardPage)panel).getData(dataName);
  459.  
  460.                 if(data != null)
  461.                 {
  462.                     return (data);
  463.                 }
  464.             }
  465.         }
  466.  
  467.         return (null);
  468.     }
  469.  
  470.     /**
  471.      * @since VCafe 3.0
  472.      */
  473.  
  474.     public Vector getSummary()
  475.     {
  476.         Vector summaries;
  477.  
  478.         summaries = new Vector();
  479.  
  480.         for(Enumeration e = panels.elements(); e.hasMoreElements();)
  481.         {
  482.             JPanel panel;
  483.  
  484.             panel = (JPanel)e.nextElement();
  485.  
  486.             if(panel instanceof WizardSummaryPage)
  487.             {
  488.                 Summary s;
  489.  
  490.                 s = ((WizardSummaryPage)panel).getSummary();
  491.  
  492.                 if(s != null)
  493.                 {
  494.                     summaries.addElement(s);
  495.                 }
  496.             }
  497.         }
  498.  
  499.         return (summaries);
  500.     }
  501.  
  502.     public Vector getWizardSummary()
  503.     {
  504.         Vector summaries;
  505.  
  506.         summaries = new Vector();
  507.  
  508.         int max = panels.size();
  509.         for(int i = 0; i < max; i++)
  510.         {
  511.             JPanel panel;
  512.  
  513.             panel = (JPanel)panels.elementAt(i);
  514.  
  515.             if(panel instanceof WizardSummaryPage)
  516.             {
  517.                 WizardSummary s;
  518.  
  519.                 s = ((WizardSummaryPage)panel).getWizardSummary();
  520.  
  521.                 if(s != null)
  522.                 {
  523.                     summaries.addElement(s);
  524.                 }
  525.             }
  526.         }
  527.  
  528.         return (summaries);
  529.     }
  530.  
  531.     /**
  532.      * @param f TODO
  533.      * @since VCafe 3.0
  534.      */
  535.  
  536.     public void setBackEnabled(boolean f)
  537.     {
  538.         if(currentPanelIndex > 0 && wizard != null)
  539.         {
  540.             wizard.setBackEnabled(f);
  541.         }
  542.     }
  543.  
  544.     /**
  545.      * @param f TODO
  546.      * @since VCafe 3.0
  547.      */
  548.  
  549.     public void setNextEnabled(boolean f)
  550.     {
  551.         if(currentPanelIndex < panels.size() && wizard != null)
  552.         {
  553.             wizard.setNextEnabled(f);
  554.         }
  555.     }
  556.  
  557.     /**
  558.      * @param f TODO
  559.      * @since VCafe 3.0
  560.      */
  561.  
  562.     public void setFinishEnabled(boolean f)
  563.     {
  564.         if(wizard != null)
  565.         {
  566.             wizard.setFinishEnabled(f);
  567.         }
  568.     }
  569.  
  570.     /**
  571.      * @param f TODO
  572.      * @since VCafe 3.0
  573.      */
  574.  
  575.     public void setCancelEnabled(boolean f)
  576.     {
  577.         if(wizard != null)
  578.         {
  579.             wizard.setCancelEnabled(f);
  580.         }
  581.     }
  582.  
  583.     /**
  584.      * @param f TODO
  585.      * @since VCafe 3.0
  586.      */
  587.  
  588.     public void setHelpEnabled(boolean f)
  589.     {
  590.         if(wizard != null)
  591.         {
  592.             wizard.setHelpEnabled(f);
  593.         }
  594.     }
  595.  
  596.     /**
  597.      * @since VCafe 3.0
  598.      */
  599.  
  600.     public boolean isBackEnabled()
  601.     {
  602.         if(wizard != null)
  603.         {
  604.             return (wizard.isBackEnabled());
  605.         }
  606.  
  607.         return (false);
  608.     }
  609.  
  610.     /**
  611.      * @since VCafe 3.0
  612.      */
  613.  
  614.     public boolean isNextEnabled()
  615.     {
  616.         if(wizard != null)
  617.         {
  618.             return (wizard.isNextEnabled());
  619.         }
  620.  
  621.         return (false);
  622.     }
  623.  
  624.     /**
  625.      * @since VCafe 3.0
  626.      */
  627.  
  628.     public boolean isFinishEnabled()
  629.     {
  630.         if(wizard != null)
  631.         {
  632.             return (wizard.isFinishEnabled());
  633.         }
  634.  
  635.         return (false);
  636.     }
  637.  
  638.     /**
  639.      * @since VCafe 3.0
  640.      */
  641.  
  642.     public boolean isCancelEnabled()
  643.     {
  644.         if(wizard != null)
  645.         {
  646.             return (wizard.isCancelEnabled());
  647.         }
  648.  
  649.         return (false);
  650.     }
  651.  
  652.     /**
  653.      * @since VCafe 3.0
  654.      */
  655.  
  656.     public boolean isHelpEnabled()
  657.     {
  658.         if(wizard != null)
  659.         {
  660.             return (wizard.isHelpEnabled());
  661.         }
  662.  
  663.         return (false);
  664.     }
  665.  
  666.     /**
  667.      * @since VCafe 3.0
  668.      */
  669.  
  670.     public void setBackFocus()
  671.     {
  672.         if(wizard != null)
  673.         {
  674.             wizard.setBackFocus();
  675.         }
  676.     }
  677.  
  678.     /**
  679.      * @since VCafe 3.0
  680.      */
  681.  
  682.     public void setNextFocus()
  683.     {
  684.         if(wizard != null)
  685.         {
  686.             wizard.setNextFocus();
  687.         }
  688.     }
  689.  
  690.     /**
  691.      * @since VCafe 3.0
  692.      */
  693.  
  694.     public void setFinishFocus()
  695.     {
  696.         if(wizard != null)
  697.         {
  698.             wizard.setFinishFocus();
  699.         }
  700.     }
  701.  
  702.     /**
  703.      * @since VCafe 3.0
  704.      */
  705.  
  706.     public void setCancelFocus()
  707.     {
  708.         if(wizard != null)
  709.         {
  710.             wizard.setCancelFocus();
  711.         }
  712.     }
  713.  
  714.     /**
  715.      * @since VCafe 3.0
  716.      */
  717.  
  718.     public void setHelpFocus()
  719.     {
  720.         if(wizard != null)
  721.         {
  722.             wizard.setHelpFocus();
  723.         }
  724.     }
  725.     
  726.     public void setImage(Image image)
  727.     {
  728.         if(wizard != null) 
  729.         {
  730.             wizard.setImage(image);
  731.         }
  732.     }
  733.     
  734.     public void setImage(URL url)
  735.     {
  736.         if(wizard != null) 
  737.         {
  738.             wizard.setImage(url);
  739.         }
  740.     }
  741.     
  742.     public void setImage(InputStream stream)
  743.         throws IOException
  744.     {
  745.         if(wizard != null) 
  746.         {
  747.             wizard.setImage(stream);
  748.         }
  749.     }
  750.     
  751.     public void setImage(ImageIcon icon)
  752.     {
  753.         if(wizard != null)
  754.         {
  755.             wizard.setImage(icon);
  756.         }
  757.     }
  758. }