1 13 package com.tonbeller.wcf.form; 14 15 import org.apache.log4j.Logger; 16 import org.jaxen.JaxenException; 17 import org.jaxen.dom.DOMXPath; 18 import org.w3c.dom.Document ; 19 import org.w3c.dom.Element ; 20 import org.w3c.dom.Text ; 21 22 import com.tonbeller.wcf.component.Component; 23 import com.tonbeller.wcf.component.ComponentSupport; 24 import com.tonbeller.wcf.controller.RequestContext; 25 import com.tonbeller.wcf.controller.RequestListener; 26 import com.tonbeller.wcf.utils.DomUtils; 27 import com.tonbeller.wcf.utils.SoftException; 28 29 32 33 public class ConfirmComponent extends ComponentSupport { 34 Document document; 35 Element title; 36 Element body; 37 RequestListener acceptHandler; 38 RequestListener cancelHandler; 39 public static final String SESSIONKEY = "confirmForm"; 40 public static final String BUNDLE = "com.tonbeller.wcf.form.resources"; 41 private static final Logger logger = Logger.getLogger(ConfirmComponent.class); 42 43 public static ConfirmComponent instance() { 44 return (ConfirmComponent) RequestContext.instance().getSession().getAttribute(SESSIONKEY); 45 } 46 47 public void initialize(RequestContext context) throws Exception { 48 super.initialize(context); 49 FormDocument.replaceI18n(context, document, BUNDLE); 50 } 51 52 public ConfirmComponent(String id, Component parent, Document document) { 53 super(id, parent); 54 this.document = document; 55 56 try { 57 title = (Element ) new DOMXPath("//title").selectSingleNode(document); 58 body = (Element ) new DOMXPath("//td[@id='body']").selectSingleNode(document); 59 } catch (JaxenException e) { 60 logger.error(null, e); 61 throw new SoftException(e); 62 } 63 64 getDispatcher().addRequestListener("confirm.accept", null, new RequestListener() { 65 public void request(RequestContext context) throws Exception { 66 setVisible(false); 67 if (acceptHandler != null) 68 acceptHandler.request(context); 69 } 70 }); 71 72 getDispatcher().addRequestListener("confirm.cancel", null, new RequestListener() { 73 public void request(RequestContext context) throws Exception { 74 setVisible(false); 75 if (cancelHandler != null) 76 cancelHandler.request(context); 77 } 78 }); 79 80 setVisible(false); 81 } 82 83 public Document render(RequestContext context) throws Exception { 84 return document; 85 } 86 87 94 public void show(String title, String text, RequestListener accept, RequestListener cancel) { 95 DomUtils.removeChildNodesExceptAttributes(body); 97 this.title.setAttribute("value", title); 98 DomUtils.setText(body, text); 99 this.acceptHandler = accept; 100 this.cancelHandler = cancel; 101 setVisible(true); 102 } 103 104 113 public void show(String title, String text1, String [] list, String text2, RequestListener accept, 114 RequestListener cancel) { 115 DomUtils.removeChildNodesExceptAttributes(body); 116 this.title.setAttribute("value", title); 117 118 Text t1 = document.createTextNode(text1); 119 body.appendChild(t1); 120 Element ul = document.createElement("ul"); 121 body.appendChild(ul); 122 for (int i = 0; i < list.length; i++) { 123 Element li = document.createElement("li"); 124 ul.appendChild(li); 125 li.appendChild(document.createTextNode(list[i])); 126 } 127 Text t2 = document.createTextNode(text2); 128 body.appendChild(t2); 129 130 this.acceptHandler = accept; 131 this.cancelHandler = cancel; 132 setVisible(true); 133 } 134 135 146 public void show(String title, String textA, String [] listA, String textB, String [] listB, 147 String text, RequestListener accept, RequestListener cancel) { 148 DomUtils.removeChildNodesExceptAttributes(body); 149 this.title.setAttribute("value", title); 150 151 appendGroup(document, textA, listA); 152 appendGroup(document, textB, listB); 153 154 Text t = document.createTextNode(text); 155 body.appendChild(t); 156 157 this.acceptHandler = accept; 158 this.cancelHandler = cancel; 159 setVisible(true); 160 } 161 162 private void appendGroup(Document doc, String text, String [] list) { 163 if (list == null || list.length == 0) 164 return; 165 166 Text t = document.createTextNode(text); 167 body.appendChild(t); 168 Element ul = document.createElement("ul"); 169 body.appendChild(ul); 170 for (int i = 0; i < list.length; i++) { 171 Element li = document.createElement("li"); 172 ul.appendChild(li); 173 li.appendChild(document.createTextNode(list[i])); 174 } 175 } 176 } 177 | Popular Tags |