1 package com.tonbeller.wcf.wizard; 2 3 import java.util.ArrayList ; 4 import java.util.List ; 5 6 import org.apache.log4j.Logger; 7 import org.w3c.dom.Document ; 8 9 import com.tonbeller.wcf.component.Component; 10 import com.tonbeller.wcf.component.ComponentSupport; 11 import com.tonbeller.wcf.controller.RequestContext; 12 import com.tonbeller.wcf.wizard.WizardPage.WizardPagePosition; 13 14 17 public class WizardComponent extends ComponentSupport { 18 private List pages = new ArrayList (); 19 private WizardPage current = null; 20 21 private static Logger logger = Logger.getLogger(WizardComponent.class); 22 23 private PageListener pageListener = new PageListener() { 24 public void onNext(RequestContext context) throws Exception { 25 setCurrentPageIndex(getCurrentPageIndex() + 1); 26 } 27 28 public void onBack(RequestContext context) throws Exception { 29 setCurrentPageIndex(getCurrentPageIndex() - 1); 30 } 31 32 public void onFinish(RequestContext context) throws Exception { 33 int pageCnt = pages.size(); 35 int cur = getCurrentPageIndex(); 36 for(int i=cur+1; i<pageCnt; i++) { 37 WizardPage page = (WizardPage) pages.get(i); 38 page.pageSkipped(); 39 } 40 41 setCurrentPageIndex(0); 42 } 43 44 public void onCancel(RequestContext context) throws Exception { 45 setCurrentPageIndex(0); 46 } 47 }; 48 49 public WizardComponent(String id, Component parent) { 50 super(id, parent); 51 } 52 53 public int getPageCount() { 54 return pages.size(); 55 } 56 57 public int getCurrentPageIndex() { 58 return pages.indexOf(current); 59 } 60 61 public void setCurrentPageIndex(int page) { 62 if(page>=pages.size()) 63 throw new IllegalArgumentException ("Invalid index " + page); 64 65 current = (WizardPage) pages.get(page); 66 adjustVisibility(); 67 } 68 69 public WizardPage getCurrentPage() { 70 return current; 71 } 72 73 public void setCurrentPage(WizardPage page) { 74 if (!pages.contains(page)) 75 throw new IllegalArgumentException ("page must be registered with the wizard"); 76 this.current = page; 77 adjustVisibility(); 78 } 79 80 public void addPage(WizardPage page) { 81 pages.add(page); 82 page.addPageListener(pageListener); 83 int cnt = pages.size(); 84 for(int i=0; i<cnt; i++) { 85 WizardPage wp = (WizardPage) pages.get(i); 86 if(cnt==1) 87 wp.pageAdded(WizardPagePosition.SINGLE_PAGE); 88 else if(i==0) 89 wp.pageAdded(WizardPagePosition.FIRST_PAGE); 90 else if(i==cnt-1) 91 wp.pageAdded(WizardPagePosition.LAST_PAGE); 92 else 93 wp.pageAdded(WizardPagePosition.MIDDLE_PAGE); 94 } 95 } 96 97 public void removePage(WizardPage page) { 98 page.removePageListener(pageListener); 99 pages.remove(page); 100 } 101 102 public void initialize(RequestContext context) throws Exception { 103 super.initialize(context); 104 setCurrentPageIndex(0); 105 } 106 107 public Document render(RequestContext context) throws Exception { 108 return current.render(context); 109 } 110 111 private void adjustVisibility() { 112 int pageCnt = pages.size(); 113 for(int i=0; i<pageCnt; i++) { 114 WizardPage p = (WizardPage) pages.get(i); 115 p.setVisible(p==current && this.isVisible()); 116 } 117 } 118 119 public WizardPage getPage(int idx) { 120 return (WizardPage) pages.get(idx); 121 } 122 123 } | Popular Tags |