home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2003 March / DPPCPRO0303.ISO / Components / Microsoft ASP / _SETUP.1 / ASPWizard.jar / asp / wizard / WizardManager.class (.txt) < prev    next >
Encoding:
Java Class File  |  1998-11-20  |  16.4 KB  |  565 lines

  1. package asp.wizard;
  2.  
  3. import asp.netobjects.nfx.util.ExceptionHandler;
  4. import asp.netobjects.nfx.util.ExternalError;
  5. import asp.netobjects.nfx.util.InternalError;
  6. import asp.netobjects.nfx.wizard.Wizard;
  7. import asp.netobjects.nfx.wizard.WizardPage;
  8. import asp.netobjects.nfx.wizard.WizardView;
  9. import asp.util.EResourceUtil;
  10. import asp.util.ResourceUtil;
  11. import asp.wizard.def.DefCollection;
  12. import asp.wizard.def.DefConnection;
  13. import asp.wizard.def.DefList;
  14. import asp.wizard.def.DefPage;
  15. import asp.wizard.def.DefQuery;
  16. import asp.wizard.def.LayoutGenerator;
  17. import asp.wizard.util.UiUtil;
  18. import com.netobjects.nfc.api.DSiteView;
  19. import com.sun.java.swing.ImageIcon;
  20. import com.sun.java.swing.JFrame;
  21. import java.awt.Component;
  22. import java.awt.Dialog;
  23. import java.awt.Font;
  24. import java.awt.Frame;
  25. import java.awt.Window;
  26. import java.awt.event.WindowEvent;
  27. import java.net.URL;
  28. import java.util.Enumeration;
  29. import java.util.Hashtable;
  30. import java.util.Vector;
  31.  
  32. public class WizardManager extends Wizard {
  33.    public static final int WIDTH = 540;
  34.    public static final int HEIGHT = 400;
  35.    public static final int TEMPLATE_LIST = 0;
  36.    public static final int TEMPLATE_DETAIL = 1;
  37.    public static final int TEMPLATE_FINISH = 2;
  38.    protected static final int NUM_OF_TEMPLATES = 3;
  39.    private static final String ID_LBL_TITLE = "window.title";
  40.    private static final String IMG_DIR = "image";
  41.    private static final String BUTTON_CANCEL = "button.cancel";
  42.    private static final String BUTTON_BACK = "button.back";
  43.    private static final String BUTTON_NEXT = "button.next";
  44.    private static final String BUTTON_FINISH = "button.finish";
  45.    public static final String TITLE_GENERATE = "window.title.generate";
  46.    private SeqTemplate[] _seqTemplates = new SeqTemplate[3];
  47.    private Vector _models = new Vector();
  48.    private DefPage _firstDefPage;
  49.    private Hashtable _modelDependents = new Hashtable();
  50.    private ResourceUtil _resourceUtil;
  51.    private boolean _modelSkippingEnabled = true;
  52.    private DSiteView _siteView;
  53.    private boolean _finished = false;
  54.    private boolean _closed = false;
  55.  
  56.    public static void main(String[] args) {
  57.       UiUtil.lafSetup();
  58.       JFrame f = createMainFrame();
  59.       WizardManager wm = new WizardManager(f, (String)null, (String)null, 540, 400, new AspWizardExceptionHandler(f));
  60.       Window win = ((Wizard)wm).getView();
  61.       win.addWindowListener(new 1());
  62.       ((Wizard)wm).setVisible(true);
  63.    }
  64.  
  65.    public static JFrame createMainFrame() {
  66.       JFrame result = new JFrame();
  67.       ((Component)result).setFont(new Font("Dialog", 0, 11));
  68.       return result;
  69.    }
  70.  
  71.    public WizardManager(JFrame parent, String title, String codeBase, int width, int height, ExceptionHandler handler) {
  72.       super(parent, title, codeBase, width, height, handler);
  73.       this.init_internal();
  74.    }
  75.  
  76.    public WizardManager(JFrame parent, String title, String codeBase, ExceptionHandler handler) {
  77.       super(parent, title, codeBase, handler);
  78.       this.init_internal();
  79.    }
  80.  
  81.    protected void init_internal() {
  82.       this.initResourceUtil();
  83.       this.initView();
  84.       this.initTemplates();
  85.       this.initSequence();
  86.       ((Wizard)this).enableButtons();
  87.  
  88.       try {
  89.          this.initialize();
  90.       } catch (Exception e) {
  91.          System.err.println(((Throwable)e).getMessage());
  92.       }
  93.  
  94.    }
  95.  
  96.    private void initResourceUtil() {
  97.       try {
  98.          this._resourceUtil = new ResourceUtil("asp.wizard.res", this.getClass());
  99.       } catch (EResourceUtil e) {
  100.          System.err.println(((Throwable)e).getMessage());
  101.       }
  102.  
  103.    }
  104.  
  105.    private void initView() {
  106.       WizardView wv = ((Wizard)this).getView();
  107.       if (wv != null) {
  108.          ((Dialog)wv).setTitle(this._resourceUtil.getString("window.title"));
  109.          URL u = this.getClass().getResource("image/transicon.gif");
  110.          if (u != null) {
  111.             ImageIcon icon = new ImageIcon(u);
  112.             if (wv instanceof Frame) {
  113.                ((Frame)wv).setIconImage(icon.getImage());
  114.             }
  115.          }
  116.  
  117.          wv.getCancelButton().setText(this._resourceUtil.getString("button.cancel"));
  118.          wv.getBackButton().setText(this._resourceUtil.getString("button.back"));
  119.          wv.getNextButton().setText(this._resourceUtil.getString("button.next"));
  120.          wv.getFinishButton().setText(this._resourceUtil.getString("button.finish"));
  121.          UiUtil.centerComponentInScreen(wv);
  122.          ((Dialog)wv).setResizable(false);
  123.       }
  124.  
  125.       ((Window)wv).addWindowListener(new 2(this));
  126.    }
  127.  
  128.    private void handleWindowClosing(WindowEvent e) {
  129.       WizDbManager.releaseInstance();
  130.       this._closed = true;
  131.    }
  132.  
  133.    private void handleWindowClosed(WindowEvent e) {
  134.       WizDbManager.releaseInstance();
  135.       this._closed = true;
  136.    }
  137.  
  138.    private void initTemplates() {
  139.       this._seqTemplates[0] = new SeqTemplateList(this, 0);
  140.       this._seqTemplates[1] = new SeqTemplateDetail(this, 1);
  141.       this._seqTemplates[2] = new SeqTemplateFinish(this, 2);
  142.    }
  143.  
  144.    private void initSequence() {
  145.       try {
  146.          WizardModelAbstract model = new WizardModelIntro(this, (String)null, (String)null, (ImageIcon)null, (ExceptionHandler)null);
  147.          this.add(model);
  148.          model.initialize(1);
  149.       } catch (InternalError e) {
  150.          System.err.println(((Throwable)e).getMessage());
  151.       } catch (ExternalError e) {
  152.          System.err.println(((Throwable)e).getMessage());
  153.       }
  154.  
  155.    }
  156.  
  157.    private boolean isValidTemplateId(int templateId) {
  158.       boolean result = false;
  159.       switch (templateId) {
  160.          case 0:
  161.          case 1:
  162.          case 2:
  163.             result = true;
  164.             break;
  165.          default:
  166.             result = false;
  167.       }
  168.  
  169.       return result;
  170.    }
  171.  
  172.    public void add(WizardPage page) throws InternalError {
  173.       if (page != null) {
  174.          if (!((WizardModelAbstract)page).isRegisteredWithWizard()) {
  175.             super.add(page);
  176.          }
  177.  
  178.          if (this._models.indexOf(page) == -1) {
  179.             this._models.addElement(page);
  180.          }
  181.  
  182.       }
  183.    }
  184.  
  185.    public void addSequence(WizardPage afterNode, WizardPage seqOwner, DefPage assocPage, int nextTemplateId, DefPage parentPage) {
  186.       if (parentPage != null) {
  187.          parentPage.addChildPage(assocPage);
  188.       }
  189.  
  190.       if (this.isValidTemplateId(nextTemplateId)) {
  191.          try {
  192.             WizardPage seqHead = this._seqTemplates[nextTemplateId].addSequence(afterNode, assocPage);
  193.             this._modelDependents.put(seqOwner, seqHead);
  194.          } catch (ESeqTemplate e) {
  195.             System.err.println(((Throwable)e).getMessage());
  196.          }
  197.       }
  198.  
  199.    }
  200.  
  201.    public void removeSequence(WizardPage headNode) {
  202.       if (headNode != null) {
  203.          WizardModelAbstract model = (WizardModelAbstract)headNode;
  204.          if (this.isValidTemplateId(model.getTemplateId())) {
  205.             SeqTemplate seqTemplate = this._seqTemplates[model.getTemplateId()];
  206.             if (seqTemplate.isSeqComplete(model)) {
  207.                int startIndex = this._models.indexOf(headNode);
  208.                int endIndex = startIndex + seqTemplate.getLength() - 1;
  209.                WizardModelAbstract prevModel = (WizardModelAbstract)this.getPreviousModel(model);
  210.                DefPage parentDefPage = prevModel.getDefPage();
  211.                DefPage currDefPage = model.getDefPage();
  212.  
  213.                for(int i = endIndex; i >= startIndex; --i) {
  214.                   this.removeModel(i);
  215.                }
  216.  
  217.                if (currDefPage != null) {
  218.                   this.removeDefPage(prevModel, model.getTemplateId(), currDefPage);
  219.                }
  220.             }
  221.          }
  222.  
  223.       }
  224.    }
  225.  
  226.    protected void removeModelDependent(WizardPage model) {
  227.       WizardPage dependent = (WizardPage)this._modelDependents.get(model);
  228.       if (dependent != null) {
  229.          this.removeSequence(dependent);
  230.          this._modelDependents.remove(model);
  231.       }
  232.  
  233.    }
  234.  
  235.    protected void removeModel(int index) {
  236.       this.removeModelDependent((WizardPage)this._models.elementAt(index));
  237.       Hashtable hash = ((Wizard)this).getPageIndex();
  238.       if (hash != null) {
  239.          hash.remove(this._models.elementAt(index));
  240.       }
  241.  
  242.       this._models.removeElementAt(index);
  243.    }
  244.  
  245.    public WizardPage getNextModel(WizardPage currModel) {
  246.       WizardPage result = null;
  247.       if (currModel == null) {
  248.          return result;
  249.       } else {
  250.          int curridx = this._models.indexOf(currModel);
  251.          if (curridx < 0) {
  252.             return result;
  253.          } else {
  254.             if (curridx < this._models.size() - 1) {
  255.                result = (WizardPage)this._models.elementAt(curridx + 1);
  256.             }
  257.  
  258.             return result;
  259.          }
  260.       }
  261.    }
  262.  
  263.    public WizardPage getPreviousModel(WizardPage currModel) {
  264.       WizardPage result = null;
  265.       if (currModel == null) {
  266.          return result;
  267.       } else {
  268.          int curridx = this._models.indexOf(currModel);
  269.          if (curridx < 0) {
  270.             return result;
  271.          } else {
  272.             if (curridx > 0 && curridx < this._models.size()) {
  273.                result = (WizardPage)this._models.elementAt(curridx - 1);
  274.             }
  275.  
  276.             return result;
  277.          }
  278.       }
  279.    }
  280.  
  281.    void insertModels(WizardPage afterNode, Vector newseq) {
  282.       int nodeindex = this._models.indexOf(afterNode);
  283.       if (nodeindex >= this._models.size() - 1) {
  284.          Enumeration e = newseq.elements();
  285.  
  286.          while(e.hasMoreElements()) {
  287.             this._models.addElement(e.nextElement());
  288.          }
  289.       } else {
  290.          int i = nodeindex;
  291.          Enumeration e = newseq.elements();
  292.  
  293.          while(e.hasMoreElements()) {
  294.             Vector var10000 = this._models;
  295.             Object var10001 = e.nextElement();
  296.             ++i;
  297.             var10000.insertElementAt(var10001, i);
  298.          }
  299.       }
  300.  
  301.    }
  302.  
  303.    public DefPage getFirstDefPage() {
  304.       return this._firstDefPage;
  305.    }
  306.  
  307.    public void setFirstDefPage(DefPage firstDefPage) {
  308.       this._firstDefPage = firstDefPage;
  309.    }
  310.  
  311.    public String getReport(WizardModelAbstract currPage) {
  312.       String result = null;
  313.       int prevTemplateId = ((WizardModelAbstract)this.getPreviousModel(currPage)).getTemplateId();
  314.       if (prevTemplateId == 1) {
  315.          result = "A detail page containing:\n\t- MSDBQuery1\n\t- MSDBConnection1\n\t- MSDBDetail\nhas been created.";
  316.       } else if (prevTemplateId == 0) {
  317.          result = "A list page containing:\n\t- MSDBQuery1\n\t- MSDBConnection1\n\t- MSDBList\nhas been created.";
  318.       }
  319.  
  320.       return result;
  321.    }
  322.  
  323.    private int getSequenceHeadIndex(WizardPage model) {
  324.       int result = -1;
  325.       WizardModelAbstract wm = (WizardModelAbstract)model;
  326.       int modelIndex = this._models.indexOf(wm);
  327.       if (modelIndex != -1) {
  328.          int tid = wm.getTemplateId();
  329.          int currIndex = modelIndex;
  330.          boolean found = false;
  331.          WizardModelAbstract wmprev = null;
  332.  
  333.          while(!found) {
  334.             wmprev = (WizardModelAbstract)this._models.elementAt(currIndex - 1);
  335.             Class tailClass = this._seqTemplates[tid].getClassOfLastModel();
  336.             found = wmprev.getTemplateId() != tid || tailClass == wmprev.getClass();
  337.             if (!found) {
  338.                --currIndex;
  339.             }
  340.  
  341.             if (currIndex < 0) {
  342.                break;
  343.             }
  344.          }
  345.  
  346.          if (found) {
  347.             result = currIndex;
  348.          }
  349.       }
  350.  
  351.       return result;
  352.    }
  353.  
  354.    public WizardPage getSequenceHead(WizardPage model) {
  355.       WizardPage result = null;
  356.       int headIndex = this.getSequenceHeadIndex(model);
  357.       if (headIndex != -1) {
  358.          result = (WizardPage)this._models.elementAt(headIndex);
  359.       }
  360.  
  361.       return result;
  362.    }
  363.  
  364.    private int getModelIndexOfClassLeftOf(Class modelClass, int headIndex) {
  365.       int result = -1;
  366.       boolean found = false;
  367.  
  368.       for(int i = headIndex; i >= 0 && !found; --i) {
  369.          found = modelClass == this._models.elementAt(i).getClass();
  370.          if (found) {
  371.             result = i;
  372.          }
  373.       }
  374.  
  375.       return result;
  376.    }
  377.  
  378.    private int getModelIndexOfClassRightOf(Class modelClass, int headIndex) {
  379.       int result = -1;
  380.       boolean found = false;
  381.  
  382.       for(int i = headIndex; i != -1 && i < this._models.size() && !found; ++i) {
  383.          found = modelClass == this._models.elementAt(i).getClass();
  384.          if (found) {
  385.             result = i;
  386.          }
  387.       }
  388.  
  389.       return result;
  390.    }
  391.  
  392.    public WizardPage getModelOfClassLeftOf(Class modelClass, WizardPage headModel) {
  393.       WizardPage result = null;
  394.       int headIndex = this._models.indexOf(headModel);
  395.       int foundIndex = this.getModelIndexOfClassLeftOf(modelClass, headIndex);
  396.       if (foundIndex != -1) {
  397.          result = (WizardPage)this._models.elementAt(foundIndex);
  398.       }
  399.  
  400.       return result;
  401.    }
  402.  
  403.    public WizardPage getModelOfClassRightOf(Class modelClass, WizardPage headModel) {
  404.       WizardPage result = null;
  405.       int headIndex = this._models.indexOf(headModel);
  406.       int foundIndex = this.getModelIndexOfClassRightOf(modelClass, headIndex);
  407.       if (foundIndex != -1) {
  408.          result = (WizardPage)this._models.elementAt(foundIndex);
  409.       }
  410.  
  411.       return result;
  412.    }
  413.  
  414.    public WizardPage getModelOfClass(Class modelClass, WizardPage model) {
  415.       WizardPage result = null;
  416.       int headIndex = this.getSequenceHeadIndex(model);
  417.       int modelIndex = this.getModelIndexOfClassRightOf(modelClass, headIndex);
  418.       if (modelIndex != -1) {
  419.          result = (WizardPage)this._models.elementAt(modelIndex);
  420.       }
  421.  
  422.       return result;
  423.    }
  424.  
  425.    public DefConnection getDefConnection(WizardModelAbstract model) {
  426.       DefConnection result = null;
  427.       int tid = model.getTemplateId();
  428.       if (this.isValidTemplateId(tid)) {
  429.          SeqTemplate seqTemplate = this._seqTemplates[tid];
  430.          result = seqTemplate.getDefConnection(model);
  431.       }
  432.  
  433.       return result;
  434.    }
  435.  
  436.    public DefQuery getDefQuery(WizardModelAbstract model) {
  437.       DefQuery result = null;
  438.       int tid = model.getTemplateId();
  439.       if (this.isValidTemplateId(tid)) {
  440.          SeqTemplate seqTemplate = this._seqTemplates[tid];
  441.          result = seqTemplate.getDefQuery(model);
  442.       }
  443.  
  444.       return result;
  445.    }
  446.  
  447.    public DefList getDefList(WizardModelAbstract model) {
  448.       DefList result = null;
  449.       int tid = model.getTemplateId();
  450.       if (this.isValidTemplateId(tid)) {
  451.          SeqTemplate seqTemplate = this._seqTemplates[tid];
  452.          result = seqTemplate.getDefList(model);
  453.       }
  454.  
  455.       return result;
  456.    }
  457.  
  458.    public boolean isModelSkippingEnabled() {
  459.       return this._modelSkippingEnabled;
  460.    }
  461.  
  462.    public void setModelSkippingEnabled(boolean value) {
  463.       this._modelSkippingEnabled = value;
  464.    }
  465.  
  466.    public void setDSiteView(DSiteView sv) {
  467.       this._siteView = sv;
  468.    }
  469.  
  470.    public void finish() throws ExternalError, InternalError {
  471.       this._finished = true;
  472.    }
  473.  
  474.    public boolean isFinished() {
  475.       return this._finished;
  476.    }
  477.  
  478.    public boolean isClosed() {
  479.       return this._closed;
  480.    }
  481.  
  482.    public void processDefPages() {
  483.       if (this._firstDefPage != null && this._siteView != null) {
  484.          try {
  485.             LayoutGenerator gen = new LayoutGenerator(this._siteView);
  486.             gen.addLayoutGeneratorListener(new GenProgressDisplay());
  487.             gen.generate(this._firstDefPage);
  488.          } catch (Exception e) {
  489.             AspWizardExceptionHandler.showMessage(0, ((Throwable)e).getMessage());
  490.          }
  491.       } else if (this._firstDefPage == null) {
  492.          String msg = "finish(): _firstDefPage is null";
  493.          AspWizardExceptionHandler.showMessage(-2, msg);
  494.       } else if (this._siteView == null) {
  495.          String msg = "finish(): _siteView is not specified";
  496.          AspWizardExceptionHandler.showMessage(-2, msg);
  497.       }
  498.  
  499.    }
  500.  
  501.    public void initialize() throws InternalError, ExternalError {
  502.       super.initialize();
  503.       WizardPage model = (WizardPage)((Wizard)this).getPageIndex().get("1");
  504.       if (model != null) {
  505.          model.initialize(1);
  506.          if (!model.getView().isVisible()) {
  507.             model.getView().setVisible(true);
  508.          }
  509.       }
  510.  
  511.       ((Wizard)this).enableButtons();
  512.    }
  513.  
  514.    public DefPage createDefPage(WizardPage currModel, int nextSeqTemplateId) {
  515.       DefPage result = null;
  516.       if (this.isValidTemplateId(nextSeqTemplateId)) {
  517.          result = this._seqTemplates[nextSeqTemplateId].createDefPage();
  518.       }
  519.  
  520.       return result;
  521.    }
  522.  
  523.    private void removeDefPage(WizardPage currModel, int nextSeqTemplateId, DefPage toRemove) {
  524.       if (this.isValidTemplateId(nextSeqTemplateId)) {
  525.          this._seqTemplates[nextSeqTemplateId].reclaimDefPageId();
  526.          ((DefCollection)toRemove).removeAllElements();
  527.          DefPage currPage = ((WizardModelAbstract)currModel).getDefPage();
  528.          if (currPage != null) {
  529.             currPage.removeChildPage(toRemove);
  530.          }
  531.       }
  532.  
  533.    }
  534.  
  535.    public void updateNextDefPageId(WizardPage currModel, int nextSeqTemplateId) {
  536.       if (this.isValidTemplateId(nextSeqTemplateId)) {
  537.          this._seqTemplates[nextSeqTemplateId].updateNextDefPageId();
  538.       }
  539.  
  540.    }
  541.  
  542.    public boolean isUsePreviousQuery(WizardPage model) {
  543.       boolean result = false;
  544.       if (((WizardModelAbstract)model).getTemplateId() == 1) {
  545.          WizardPage headModel = this.getSequenceHead(model);
  546.          WizardPage prevHeadModel = this.getPreviousModel(headModel);
  547.          if (prevHeadModel != null) {
  548.             result = !(prevHeadModel instanceof WizardModelIntro);
  549.          }
  550.       }
  551.  
  552.       return result;
  553.    }
  554.  
  555.    // $FF: synthetic method
  556.    static void access$0(WizardManager $0, WindowEvent $1) {
  557.       $0.handleWindowClosing($1);
  558.    }
  559.  
  560.    // $FF: synthetic method
  561.    static void access$1(WizardManager $0, WindowEvent $1) {
  562.       $0.handleWindowClosed($1);
  563.    }
  564. }
  565.