KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > form > ConfirmComponent


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

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 JavaDoc;
19 import org.w3c.dom.Element JavaDoc;
20 import org.w3c.dom.Text JavaDoc;
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 /**
30  * a simple yes/no query component
31  */

32
33 public class ConfirmComponent extends ComponentSupport {
34   Document JavaDoc document;
35   Element JavaDoc title;
36   Element JavaDoc body;
37   RequestListener acceptHandler;
38   RequestListener cancelHandler;
39   public static final String JavaDoc SESSIONKEY = "confirmForm";
40   public static final String JavaDoc 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 JavaDoc {
48     super.initialize(context);
49     FormDocument.replaceI18n(context, document, BUNDLE);
50   }
51
52   public ConfirmComponent(String JavaDoc id, Component parent, Document JavaDoc document) {
53     super(id, parent);
54     this.document = document;
55
56     try {
57       title = (Element JavaDoc) new DOMXPath("//title").selectSingleNode(document);
58       body = (Element JavaDoc) 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 JavaDoc {
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 JavaDoc {
74         setVisible(false);
75         if (cancelHandler != null)
76           cancelHandler.request(context);
77       }
78     });
79
80     setVisible(false);
81   }
82
83   public Document JavaDoc render(RequestContext context) throws Exception JavaDoc {
84     return document;
85   }
86
87   /**
88    * creates a simple yes/no form
89    * @param title title of the form
90    * @param text text of the body
91    * @param accept action for OK button or null
92    * @param cancel action for Cancel button or null
93    */

94   public void show(String JavaDoc title, String JavaDoc text, RequestListener accept, RequestListener cancel) {
95     // remove stuff from previous call
96
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   /**
105    * crates a yes/no dialog with a list
106    * @param title title of the dialog
107    * @param text1 text before the list
108    * @param list the list items
109    * @param text2 text after the list
110    * @param accept action for OK button or null
111    * @param cancel action for Cancel button or null
112    */

113   public void show(String JavaDoc title, String JavaDoc text1, String JavaDoc[] list, String JavaDoc text2, RequestListener accept,
114       RequestListener cancel) {
115     DomUtils.removeChildNodesExceptAttributes(body);
116     this.title.setAttribute("value", title);
117
118     Text JavaDoc t1 = document.createTextNode(text1);
119     body.appendChild(t1);
120     Element JavaDoc ul = document.createElement("ul");
121     body.appendChild(ul);
122     for (int i = 0; i < list.length; i++) {
123       Element JavaDoc li = document.createElement("li");
124       ul.appendChild(li);
125       li.appendChild(document.createTextNode(list[i]));
126     }
127     Text JavaDoc t2 = document.createTextNode(text2);
128     body.appendChild(t2);
129
130     this.acceptHandler = accept;
131     this.cancelHandler = cancel;
132     setVisible(true);
133   }
134
135   /**
136    * crates a yes/no dialog with a list
137    * @param title title of the dialog
138    * @param textA text before list A
139    * @param listA the first group of list items
140    * @param textB text before list B
141    * @param listB the second group of list items
142    * @param text text after the list
143    * @param accept action for OK button or null
144    * @param cancel action for Cancel button or null
145    */

146   public void show(String JavaDoc title, String JavaDoc textA, String JavaDoc[] listA, String JavaDoc textB, String JavaDoc[] listB,
147       String JavaDoc 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 JavaDoc 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 JavaDoc doc, String JavaDoc text, String JavaDoc[] list) {
163     if (list == null || list.length == 0)
164       return;
165
166     Text JavaDoc t = document.createTextNode(text);
167     body.appendChild(t);
168     Element JavaDoc ul = document.createElement("ul");
169     body.appendChild(ul);
170     for (int i = 0; i < list.length; i++) {
171       Element JavaDoc li = document.createElement("li");
172       ul.appendChild(li);
173       li.appendChild(document.createTextNode(list[i]));
174     }
175   }
176 }
177
Popular Tags