1 23 24 package com.sun.enterprise.tools.guiframework.view; 25 26 import com.iplanet.jato.RequestContext; 27 import com.iplanet.jato.RequestManager; 28 import com.iplanet.jato.model.Model; 29 import com.iplanet.jato.view.View; 30 31 import com.sun.enterprise.tools.guiframework.exception.FrameworkException; 32 import com.sun.enterprise.tools.guiframework.view.descriptors.ViewDescriptor; 33 import com.sun.enterprise.tools.guiframework.exception.FrameworkError; 34 import com.sun.enterprise.tools.guiframework.event.descriptors.EventDescriptor; 35 36 import com.sun.web.ui.model.wizard.WizardEvent; 37 import com.sun.web.ui.model.wizard.WizardInterface; 38 import com.sun.web.ui.model.wizard.WizardInterfaceExt; 39 import com.sun.web.ui.model.CCWizardWindowModelInterface; 40 41 import java.lang.reflect.Field ; 42 import java.util.ArrayList ; 43 import java.util.Iterator ; 44 import java.util.List ; 45 import java.util.EventObject ; 46 47 48 60 61 public class DescriptorWizardImpl implements WizardInterface, java.io.Serializable , WizardInterfaceExt { 62 63 69 public DescriptorWizardImpl(RequestContext ctx, String name) { 70 if (name == null) { 72 throw new FrameworkException("Wizard name cannot be null! It " 73 + "is required to find the ViewDescriptor!"); 74 } 75 setName(name); 76 ViewDescriptor viewDesc = 77 ViewDescriptorManager.getInstance().getViewDescriptor(name); 78 if (viewDesc == null) { 79 throw new FrameworkException("ViewDescriptor for wizard '" + name 80 + "' not found!"); 81 } 82 setViewDescriptor(viewDesc); 83 } 85 86 private List sequence = new ArrayList (); 87 88 91 protected void initialize(ViewDescriptor desc) { 92 93 boolean firstBranch = false; 94 95 setResourceBundle("" + desc.getParameter(RESOURCE_BUNDLE)); 97 setTitle("" + desc.getParameter(WIZARD_TITLE)); 98 99 List pages = desc.getChildDescriptors(); 101 Iterator it = pages.iterator(); 102 while (it.hasNext()) { 103 WizardPage wizardPage = new WizardPage((ViewDescriptor) it.next()); 104 addWizardPage(wizardPage); 105 if (!wizardPage.isSubStep()) { 107 sequence.add(wizardPage); 108 } 109 110 if (wizardPage.isResultsPage()) { 111 showResultsPage = true; 112 } 113 } 114 } 115 116 117 121 public RequestContext getRequestContext() { 122 return RequestManager.getRequestContext(); 123 } 124 125 126 132 protected void setViewDescriptor(ViewDescriptor desc) { 133 _viewDesc = desc; 134 initialize(desc); 135 } 136 137 138 143 public ViewDescriptor getViewDescriptor() { 144 return _viewDesc; 145 } 146 147 148 151 protected void addWizardPage(WizardPage wizardPage) { 152 getWizardPages().add(wizardPage); 153 } 154 155 156 159 protected void setWizardPages(List pages) { 160 _wizardPages = pages; 161 } 162 163 164 167 public List getWizardPages() { 168 return _wizardPages; 169 } 170 171 172 175 public WizardPage getWizardPage(String pageId) { 176 return getWizardPage(pageIdToPage(pageId)); 177 } 178 179 180 183 public WizardPage getWizardPage(int index) { 184 return (WizardPage) sequence.get(index); 186 } 187 188 189 190 194 197 public static WizardInterface create(RequestContext ctx) { 198 String wizardName = 201 ctx.getRequest().getParameter(WIZARD_WINDOW_PARAMETER_NAME); 202 203 return new DescriptorWizardImpl(ctx, wizardName); 204 } 205 206 207 213 public String getName() { 214 return _wizName; 215 } 216 217 218 222 public void setName(String name) { 223 _wizName = name; 224 } 225 226 227 231 public Model getPageModel(String pageId) { 232 WizardPage page = getWizardPage(pageId); 233 return page.getModel(); 234 } 235 236 237 241 public Class getPageClass(String pageId) { 242 getRequestContext().getRequest().setAttribute( 244 DescriptorCCWizardPage.VIEW_DESCRIPTOR_NAME, 245 getWizardPage(pageId).getName()); 246 247 return DescriptorCCWizardPage.class; 249 } 250 251 252 255 public String getPageName(String pageId) { 256 return getWizardPage(pageId).getName(); 259 } 260 261 262 267 public String getFirstPageId() { 268 return pageToPageId(0); 271 } 272 273 274 277 public String getNextPageId(String pageId) { 278 if (isFinishPageId(pageId) && !showResultsPage) { 281 return null; 282 } 283 return pageToPageId(pageIdToPage(pageId) + 1); 284 } 285 286 287 292 public String getResourceBundle() { 293 return _resourceBundle; 296 } 297 298 299 302 public void setResourceBundle(String resBundle) { 303 _resourceBundle = resBundle; 304 } 305 306 309 public String getTitle() { 310 return _title; 311 } 312 313 314 317 public void setTitle(String title) { 318 _title = title; 319 } 320 321 322 328 public String getStepTitle(String pageId) { 329 return getWizardPage(pageId).getStepText(); 330 } 331 332 333 340 public String [] getFuturePages(String currentPageId) { 341 if (isResultsPage(currentPageId)) { 343 return null; 344 } 345 346 int page = pageIdToPage(currentPageId) + 1; 347 int howMany = sequence.size() - page; 348 357 358 String [] futurePages = new String [howMany]; 359 for (int cnt = 0; cnt < howMany; ++cnt, ++page) { 360 futurePages[cnt] = pageToPageId(page); 361 } 362 return futurePages; 363 } 364 365 369 public String [] getFutureSteps(String currentPageId) { 370 int page = pageIdToPage(currentPageId) + 1; 372 int howMany = sequence.size() - page; 374 String [] futureSteps = new String [howMany]; 376 for (int i = 0; i < howMany; ++i, ++page) { 377 380 futureSteps[i] = ((WizardPage) sequence.get(page)).getStepText(); 381 } 382 return futureSteps; 384 } 385 386 387 390 public String getStepInstruction(String pageId) { 391 return getWizardPage(pageId).getStepInstructions(); 394 } 395 396 397 400 public String getStepText(String pageId) { 401 return getWizardPage(pageId).getStepText(); 402 } 403 404 405 409 public String [] getStepHelp(String pageId) { 410 return getWizardPage(pageId).getStepHelp(); 411 } 412 413 414 418 public boolean isFinishPageId(String pageId) { 419 int currentPage = pageIdToPage(pageId); 422 return ((WizardPage) sequence.get(currentPage)).isFinishPage(); 423 } 424 425 426 432 public boolean hasPreviousPageId(String pageId) { 433 return !(getFirstPageId().equals(pageId)); 434 } 435 436 437 442 public String getCancelPrompt(String pageId) { 443 return getWizardPage(pageId).getCancelPrompt(); 444 } 445 446 447 450 public String toString() { 451 return this.getClass().getName() + "(" + getName() + ") with " 452 + getWizardPages().size() + " pages."; 453 } 454 455 456 463 public boolean done(String wizardName) { 464 471 ViewDescriptor viewDesc = getViewDescriptor(); 472 EventObject event = new EventObject (wizardName); 473 Object result = null; 474 try { 475 result = DescriptorViewHelper.dispatchEvent(getRequestContext(), 477 null, viewDesc, 478 viewDesc.getEventDescriptor( 479 EventDescriptor.TYPES.END_WIZARD), 480 event); 481 } catch (FrameworkException ex) { 482 throw new FrameworkError( 484 ex, 485 ex.getResponsibleViewDescriptor(), 486 ex.getResponsibleView()); 487 } catch (Exception ex) { 488 throw new FrameworkError(ex, viewDesc, (View) null); 489 } 490 491 boolean retVal = true; 492 if (result instanceof Boolean ) { 493 retVal = ((Boolean ) result).booleanValue(); 494 } 495 return retVal; 496 } 497 498 499 511 public boolean nextStep(WizardEvent wizardEvent) { 512 RequestContext rc = getRequestContext(); 516 try { 517 boolean continueWizard = DescriptorViewHelper.nextWizardStep( 518 (DescriptorContainerView) wizardEvent.getView(), wizardEvent); 519 String pageId = wizardEvent.getPageId(); 520 WizardPage wizPage = getWizardPage(pageId); 521 Model model = wizPage.getModel(); 522 523 if (!continueWizard) { 524 String errorMessage = (String ) model.getValue(ERROR_MESSAGE); 526 int severity = ((Integer ) model.getValue(SEVERITY)).intValue(); 527 wizardEvent.setErrorMessage(errorMessage); 528 wizardEvent.setSeverity(severity); 529 return false; 530 } 531 532 if (wizPage.isBranchStep()) { 533 535 536 boolean showSteps = new Boolean ( 537 (String ) model.getValue(SHOW_STEPS)).booleanValue(); 538 if (showSteps) { 539 List pages = getWizardPages(); 541 int currentPage = pageIdToPage(pageId); 542 sequence.subList(currentPage + 1, sequence.size()).clear(); 543 int startIdx = currentPage + 1; 544 for (int i = startIdx; i < pages.size(); i++, startIdx++) { 545 WizardPage page = (WizardPage) pages.get(i); 546 if (page.isSubStep()) { 547 sequence.add(page); 548 } else { 549 break; 550 } 551 } 552 for (int i = startIdx; i < pages.size(); i++) { 553 WizardPage page = (WizardPage) pages.get(i); 554 if (!page.isSubStep()) { 555 sequence.add(page); 556 } 557 } 558 } else { 559 int startIdx = pageIdToPage(pageId) + 1; 562 List removes = new ArrayList (); 563 for (int i = startIdx; i < sequence.size(); i++) { 564 WizardPage page = (WizardPage) sequence.get(i); 565 if (page.isSubStep()) { 566 removes.add(page); 567 } else { 568 break; 569 } 570 } 571 sequence.removeAll(removes); 572 } 573 } 574 return continueWizard; 575 } catch (Exception ex) { 576 throw new FrameworkError( 577 ex, 578 getViewDescriptor(), 579 wizardEvent.getView()); 580 } 581 } 582 583 584 596 public boolean previousStep(WizardEvent wizardEvent) { 597 RequestContext rc = getRequestContext(); 601 try { 602 return DescriptorViewHelper.prevWizardStep( 603 (DescriptorContainerView) wizardEvent.getView(), 604 wizardEvent); 605 } catch (Exception ex) { 606 throw new FrameworkError( 607 ex, 608 getViewDescriptor(), 609 wizardEvent.getView()); 610 } 611 } 612 613 614 629 public boolean gotoStep(WizardEvent wizardEvent) { 630 RequestContext rc = getRequestContext(); 631 try { 632 return DescriptorViewHelper.goToWizardStep( 633 (DescriptorContainerView) wizardEvent.getView(), 634 wizardEvent); 635 } catch (Exception ex) { 636 throw new FrameworkError( 637 ex, 638 getViewDescriptor(), 639 wizardEvent.getView()); 640 } 641 } 642 643 644 659 public boolean finishStep(WizardEvent wizardEvent) { 660 RequestContext rc = getRequestContext(); 661 try { 662 boolean result = DescriptorViewHelper.finishWizardStep( 663 (DescriptorContainerView) wizardEvent.getView(), wizardEvent); 664 if (!result) { 665 stopWizardFromClosing(wizardEvent.getView().getParent()); 666 } 667 return result; 668 } catch (Throwable ex) { 669 stopWizardFromClosing(wizardEvent.getView().getParent()); 670 throw new FrameworkError( 671 ex, 672 getViewDescriptor(), 673 wizardEvent.getView()); 674 } 675 } 676 677 678 683 private void stopWizardFromClosing(View view) { 684 if (!(view instanceof com.sun.web.ui.view.wizard.CCWizard)) { 685 return; 686 } try { 687 Field [] fields = view.getClass().getDeclaredFields(); 688 int idx; 689 for (idx = 0; idx < fields.length; idx++) { 690 if (fields[idx].getName().equals("isFinishRequest")) { 691 break; 692 } 693 } 694 fields[idx].setAccessible(true); 695 fields[idx].setBoolean(view, false); 696 } catch (Exception ex) { 697 } 699 } 700 701 702 712 public boolean cancelStep(WizardEvent wizardEvent) { 713 RequestContext rc = getRequestContext(); 714 try { 715 return DescriptorViewHelper.cancelWizardStep( 716 (DescriptorContainerView) wizardEvent.getView(), 717 wizardEvent); 718 } catch (Exception ex) { 719 throw new FrameworkError( 720 ex, 721 getViewDescriptor(), 722 wizardEvent.getView()); 723 } 724 } 725 726 727 742 public boolean warnOnRevisitStep() { 743 return false; 744 } 745 746 747 750 private String pageToPageId(int page) { 751 return Integer.toString(page + 1); 752 } 753 754 755 758 private int pageIdToPage(String pageId) { 759 return Integer.parseInt(pageId) - 1; 760 } 761 762 public boolean canBeStepLink(String pageId) { 763 return true; 764 } 765 766 public void closeStep(WizardEvent wizardEvent) { 767 } 768 769 public String getPlaceholderText(String pageId) { 770 774 if (isResultsPage(pageId)) { 775 return null; 776 } 777 return getWizardPage(pageId).getPlaceHolderText(); 778 } 779 780 public String getResultsPageId(String pageId) { 781 if (showResultsPage) { 782 return (new Integer (sequence.size())).toString(); 783 } 784 return null; 785 } 786 787 public boolean helpTab(WizardEvent wizardEvent) { 788 return true; 789 } 790 791 public boolean isSubstep(String pageId) { 792 if (isResultsPage(pageId)) { 794 return false; 795 } 796 return getWizardPage(pageId).isSubStep(); 797 } 798 799 public boolean stepTab(WizardEvent wizardEvent) { 800 return true; 801 } 802 803 805 protected boolean isResultsPage(String pageId) { 806 return ((WizardPage) sequence.get( 807 pageIdToPage(pageId))).isResultsPage(); 808 } 809 812 protected class WizardPage implements java.io.Serializable { 813 public WizardPage(ViewDescriptor viewDesc) { 814 initialize(viewDesc); 815 } 816 817 protected void initialize(ViewDescriptor viewDesc) { 818 setName(viewDesc.getName()); 819 setStepText("" + viewDesc.getParameter(STEP_TEXT)); 820 setStepInstructions("" + viewDesc.getParameter(STEP_INSTRUCTIONS)); 821 setCancelPrompt("" + viewDesc.getParameter(CANCEL_PROMPT)); 822 setPageModelName((String ) viewDesc.getParameter(PAGE_MODEL_NAME)); 823 setPageModelClass((String ) viewDesc.getParameter(PAGE_MODEL_CLASS)); 824 825 List stepHelp = (List ) viewDesc.getParameter(STEP_HELP); 827 if (stepHelp != null) { 828 setStepHelp((String []) stepHelp.toArray( 829 new String [stepHelp.size()])); 830 } 831 832 String resultsPage = (String ) viewDesc.getParameter(RESULTS_PAGE); 833 if ((resultsPage != null) && resultsPage.equalsIgnoreCase("true")) { 834 _resultsPage = true; 835 } 836 837 String finishPage = (String ) viewDesc.getParameter(FINISH_PAGE); 838 if ((finishPage != null) && finishPage.equalsIgnoreCase("true")) { 839 _finishPage = true; 840 } 841 842 String branchStep = (String ) viewDesc.getParameter(BRANCH_STEP); 843 if ((branchStep != null) && branchStep.equalsIgnoreCase("true")) { 844 _branchStep = true; 845 String placeHolderText = 846 (String ) viewDesc.getParameter(PLACE_HOLDER_TEXT); 847 setPlaceHolderText(placeHolderText); 848 } 849 850 String subStep = (String ) viewDesc.getParameter(SUB_STEP); 851 if ((subStep != null) && subStep.equalsIgnoreCase("true")) { 852 if (_branchStep) { 853 throw new FrameworkException( 854 viewDesc.getName() 855 + " cannot be a branchStep as well as a subStep"); 856 } 857 _subStep = true; 858 } 859 } 860 861 862 865 public void setName(String name) { 866 _name = name; 867 } 868 869 870 873 public String getName() { 874 return _name; 875 } 876 877 878 881 public void setStepText(String stepText) { 882 _stepText = stepText; 883 } 884 885 886 889 public String getStepText() { 890 return _stepText; 891 } 892 893 894 897 public void setStepInstructions(String stepInstructions) { 898 _stepInstructions = stepInstructions; 899 } 900 901 902 905 public String getStepInstructions() { 906 return _stepInstructions; 907 } 908 909 910 913 public void setStepHelp(String [] stepHelp) { 914 _stepHelp = stepHelp; 915 } 916 917 918 921 public String [] getStepHelp() { 922 return _stepHelp; 923 } 924 925 926 929 public void setCancelPrompt(String cancelPrompt) { 930 _cancelPrompt = cancelPrompt; 931 } 932 933 934 937 public String getCancelPrompt() { 938 return _cancelPrompt; 939 } 940 941 942 947 public void setPageModelName(String modelName) { 948 if (modelName == null) { 949 return; 950 } 951 _modelName = modelName; 952 } 953 954 955 958 public String getPageModelName() { 959 return _modelName; 960 } 961 962 963 968 public void setPageModelClass(String modelClass) { 969 if (modelClass == null) { 970 return; 971 } 972 _modelClass = modelClass; 973 } 974 975 976 979 public String getPageModelClass() { 980 return _modelClass; 981 } 982 983 984 987 public Model getModel() { 988 try { 989 return getRequestContext().getModelManager().getModel( 992 getPageModelClass(), getPageModelName(), true, true); 993 } catch (ClassNotFoundException ex) { 994 throw new FrameworkException( 995 "Unable to get to model for wizard '" + this.getName() 996 + "', page '" + getName() + "'.", 997 ex, null, null); 998 } 999 } 1000 1001 1004 public boolean isResultsPage() { 1005 return _resultsPage; 1006 } 1007 1008 public boolean isFinishPage() { 1009 return _finishPage; 1010 } 1011 1012 public void setPlaceHolderText(String text) { 1013 if ((text == null) || (text.length() == 0)) { 1014 _placeHolderText = "Steps determined by choices"; 1015 } else { 1016 _placeHolderText = text; 1017 } 1018 } 1019 1020 public String getPlaceHolderText() { 1021 return _placeHolderText; 1022 } 1023 1024 public boolean isSubStep() { 1025 return _subStep; 1026 } 1027 1028 public boolean isBranchStep() { 1029 return _branchStep; 1030 } 1031 1032 public String toString() { 1033 return _name; 1034 } 1035 1038 public static final String PAGE_NAME = "pageName"; 1039 1040 1043 public static final String CANCEL_PROMPT = "cancelPrompt"; 1044 1045 1049 public static final String STEP_HELP = "stepHelp"; 1050 1051 1054 public static final String STEP_INSTRUCTIONS = "stepInstructions"; 1055 1056 1059 public static final String STEP_TEXT = "stepText"; 1060 1061 1064 public static final String PAGE_MODEL_CLASS = "wizardPageModelClass"; 1065 1066 1069 public static final String PAGE_MODEL_NAME = "wizardPageModelName"; 1070 1071 1074 public static final String RESULTS_PAGE = "resultsPage"; 1075 1076 1080 public static final String FINISH_PAGE = "finishPage"; 1081 public static final String BRANCH_STEP = "branchStep"; 1082 public static final String SUB_STEP = "subStep"; 1083 public static final String PLACE_HOLDER_TEXT = "placeHolderText"; 1084 1085 private String _cancelPrompt = ""; 1086 private String _name = ""; 1087 private String [] _stepHelp = {""}; 1088 private String _stepInstructions = ""; 1089 private String _stepText = ""; 1090 private String _modelClass = "com.iplanet.jato.model.DefaultModel"; 1091 private String _modelName = DescriptorCCWizardPage.WIZARD_MODEL; 1092 private boolean _resultsPage = false; 1093 private boolean _finishPage = false; 1094 private boolean _subStep = false; 1095 private String _placeHolderText = null; 1096 private boolean _branchStep = false; 1097 } 1098 1099 1100 1104 public static final String RESOURCE_BUNDLE = "resourceBundle"; 1105 1106 1110 public static final String WIZARD_TITLE = "title"; 1111 1112 1113 1116 public static final String WIZARD_WINDOW_PARAMETER_NAME = 1117 "WizardWindow." + CCWizardWindowModelInterface.WIZARD_NAME; 1118 1119 private ViewDescriptor _viewDesc = null; 1120 private String _wizName = null; 1121 private List _wizardPages = new ArrayList (); 1122 private boolean showResultsPage = false; 1123 1124 private String _resourceBundle = "test.Resources"; 1125 private String _title = "WizardWindow"; 1126 1127 public static final String ERROR_MESSAGE = "errorMessage"; 1128 public static final String SEVERITY = "severity"; 1129 public static final String SHOW_STEPS = "showSteps"; 1130} 1131 | Popular Tags |