KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > examples > xmlc > TemplateTableScreen


1 package org.enhydra.barracuda.examples.xmlc;
2
3 import java.io.*;
4 import java.util.*;
5 import javax.servlet.*;
6 import javax.servlet.http.*;
7
8 import org.w3c.dom.*;
9 import org.w3c.dom.html.*;
10
11 import org.apache.log4j.Logger;
12
13 import org.enhydra.xml.xmlc.*;
14 import org.enhydra.xml.io.OutputOptions;
15
16 import org.enhydra.barracuda.core.comp.*;
17 import org.enhydra.barracuda.core.util.dom.*;
18 import org.enhydra.barracuda.core.event.*;
19 import org.enhydra.barracuda.core.event.helper.*;
20 import org.enhydra.barracuda.plankton.data.*;
21 import org.enhydra.barracuda.core.util.dom.*;
22 import org.enhydra.barracuda.core.helper.servlet.ScriptDetector;
23 import org.enhydra.barracuda.plankton.http.*;
24 import org.enhydra.barracuda.core.view.*;
25 import org.enhydra.barracuda.examples.xmlc.data.*;
26 import org.enhydra.barracuda.examples.xmlc.events.*;
27
28
29 /**
30  * A sample screen
31  */

32 public class TemplateTableScreen extends DefaultEventGateway {
33
34     //public constants
35
protected static final Logger logger = Logger.getLogger(TemplateTableScreen.class.getName());
36
37     //private constants
38
private static String JavaDoc TEST_SCREEN = TemplateTableScreen.class+".TestScreen";
39
40     //xmlc factory
41
// private static XMLCFactory xmlcFactory = null;
42

43     //set up our factories
44
private ListenerFactory getTestFactory = new EventForwardingFactory(new RenderCompTest3());
45     private ListenerFactory renderTestFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new RenderTestHandler();} public String JavaDoc getListenerID() {return getID(RenderTestHandler.class);}};
46     
47     /**
48      * Public constructor
49      */

50     public TemplateTableScreen() {
51         //specify who's interested in what
52
specifyLocalEventInterests(getTestFactory, CompTest3.class);
53         specifyLocalEventInterests(renderTestFactory, RenderCompTest3.class);
54
55         //set up the XMLCFactory
56
//xmlcFactory = new XMLCStdFactory(this.getClass().getClassLoader(), new StreamXMLCLogger());
57
}
58
59
60
61
62     //------------------------------------------------------------
63
// Model 2 - View Event Handlers
64
//------------------------------------------------------------
65
/**
66      * RenderTestHandler - handle the request to render the test
67      * screen
68      */

69     class RenderTestHandler extends DefaultBaseEventListener {
70         public void handleViewEvent(ViewEventContext context) throws EventException, ServletException, IOException {
71             //unpack the necessary entities from the context
72
ViewContext vc = new DefaultViewContext(context); //used for both rendering and preparing the ScriptDetector
73

74             //figure out which page we're after
75
String JavaDoc pageName = ""+context.getRequest().getParameter("page");
76             Class JavaDoc pageCl = CompEx3HTML.class;
77             if (pageName.equals("CompEx3a")) pageCl = CompEx3aHTML.class;
78             else if (pageName.equals("CompEx3b")) pageCl = CompEx3bHTML.class;
79 // XMLObject page = xmlcFactory.create(pageCl);
80

81             //load the localized DOM template
82
Document page = DefaultDOMLoader.getGlobalInstance().getDOM(pageCl, vc.getViewCapabilities().getClientLocale());
83
84             //create the component and bind it to the proper page (note that we're
85
//doing this on a per-request basis...if we wanted to optimize we could
86
//store the component hierarchy on a per-session basis)
87
BComponent wcRoot = new BComponent();
88             BTemplate wcTemplate = new BTemplate();
89 // wcTemplate.addView(new DefaultTemplateView(page.getDocument().getElementById("UserReport")));
90
wcTemplate.addView(new DefaultTemplateView(page.getDocumentElement())); //id "UserReport" is attached to the HTML element. There is only on HTML element anyway, no need to use an id
91
wcTemplate.addModel(new UserReportModel("org.enhydra.barracuda.examples.xmlc.UserReport"));
92             wcTemplate.addModel(new UserDataModel());
93             wcRoot.addChild(wcTemplate);
94             
95             //grab the component hierarchy and render it
96
try {wcRoot.render(vc);}
97             catch (RenderException e) {logger.debug ("Error rendering views:", e);}
98
99             //now adjust the outgoing page (this is critical to make sure we can accurately detect client scripting)
100
ScriptDetector.prepareClientResp((HTMLDocument) page, vc);
101            
102             //now render the DOM
103
//get the output options
104
OutputOptions oo = DefaultDOMWriter.getDefaultOutputOptions(page.getOwnerDocument());
105
106             //add doctype - need to do here because XMLC can't yet add the one embedded in the html to the DOM
107
oo.setOmitDocType(false);
108             oo.setPublicId("-//W3C//DTD HTML 4.01 Transitional//EN");
109             oo.setSystemId("http://www.w3.org/TR/html401/loose.dtd");
110                         
111             //now render the DOM
112
// new DefaultDOMWriter().write(page, context.getResponse());
113
new DefaultDOMWriter(oo).write(page, context.getResponse());
114         }
115     }
116
117
118     //--------------- UserReportModel ------------------------
119
/**
120      * UserReport Model
121      */

122     class UserReportModel extends DefaultPropertiesModel {
123
124 // CompEx3FooterHTML footer = (CompEx3FooterHTML) xmlcFactory.create(CompEx3FooterHTML.class);
125
// Node footerNode = footer.getElementUserReportFooter();
126

127         /**
128          * Instantiate the model by pointing it to the given
129          * properties file (fully qualified name)
130          */

131         public UserReportModel(String JavaDoc propFileName) {
132             super(propFileName);
133         }
134
135         /**
136          * This is where you would return an item based on a given key.
137          */

138         public Object JavaDoc getItem(String JavaDoc key) {
139             ViewContext vc = getViewContext();
140             Object JavaDoc result = null;
141             if (key.equals("Footer")) {
142                 Document footer = null;
143                 //load the localized DOM template
144
try {footer = DefaultDOMLoader.getGlobalInstance().getDOM(CompEx3FooterHTML.class, vc.getViewCapabilities().getClientLocale());}
145                 catch (IOException ioe) {TemplateTableScreen.logger.fatal ("Fatal Error loading DOM template:", ioe);}
146                 Node footerNode = ((CompEx3FooterHTML)footer).getElementUserReportFooter();
147                 result = vc.getElementFactory().getDocument().importNode(footerNode, true);
148             }
149             else result = (String JavaDoc) super.getItem(key);
150             if (result==null) result = " ";
151             return result;
152         }
153
154     }
155     
156     //--------------- UserDataModel --------------------------
157
/**
158      * UserData Model
159      */

160     class UserDataModel extends AbstractTemplateModel implements IterativeModel {
161
162         //data structures
163
UsersList users = null;
164         UserData ud = null;
165         Iterator it = null;
166
167         //template nodes
168
boolean useSelectionImages = true;
169         Node selNode = null;
170         Node unselNode = null;
171
172         /**
173          * Return the name of this model
174          */

175         public String JavaDoc getName() {return "UserData";}
176
177         /**
178          * This is where you would return an item based on a given key
179          */

180         public Object JavaDoc getItem(String JavaDoc key) {
181             ViewContext vc = getViewContext();
182             Object JavaDoc result = null;
183             if (key.equals("Selected")) {
184                 if (useSelectionImages) {
185                     ElementFactory ef = vc.getElementFactory();
186                     selNode = ef.getElement("SelectedImageCell");
187                     unselNode = ef.getElement("UnselectedImageCell");
188                     useSelectionImages = false; //so we don't continually look for them if they're not there
189
}
190                 boolean selected = ((Boolean JavaDoc) ud.get(UserData.SELECTED)).booleanValue();
191                 if (selNode!=null && selected) result = selNode.cloneNode(true);
192                 else if (unselNode!=null && !selected) result = unselNode.cloneNode(true);
193                 else result = (selected ? "Yes" : "No");
194             }
195             else if (key.equals("FirstName")) result = (String JavaDoc) ud.get(UserData.FIRST_NAME);
196             else if (key.equals("LastName")) result = (String JavaDoc) ud.get(UserData.LAST_NAME);
197             else if (key.equals("Gender")) result = (((Boolean JavaDoc) ud.get(UserData.GENDER)).booleanValue() ? "Male" : "Female");
198             else if (key.equals("Age")) result = ""+ud.get(UserData.AGE);
199             else if (key.equals("Phone")) result = (String JavaDoc) ud.get(UserData.PHONE);
200             else if (key.equals("Email")) {
201 // result = (String) ud.get(UserData.EMAIL);
202
String JavaDoc email = (String JavaDoc) ud.get(UserData.EMAIL);
203                 result = new BLink(email, "mailto:"+email, vc);
204             }
205             else if (key.equals("Notes")) result = (String JavaDoc) ud.get(UserData.NOTES);
206             else result = (String JavaDoc) super.getItem(key);
207             if (result==null) result = " ";
208             return result;
209         }
210
211         /**
212          * Process any special directives
213          */

214         public boolean processDirective(TemplateDirective td) {
215             if (td.getCommand().equals("Veto_If_Null")) {
216                 String JavaDoc data = td.getKeyName();
217                 if (data!=null) {
218                     if (null!=ud) {
219                         StringTokenizer st = new StringTokenizer(data,",");
220                         while (st.hasMoreTokens()) {
221                             String JavaDoc key = st.nextToken();
222                             Object JavaDoc value = ud.get(key);
223                             if (value==null || value.toString().length()<1) return false; //don't allow this directive
224
}
225                     } else {
226                         return false; //don't allow this directive
227
}
228                 }
229             }
230             return true; //by default, allow all directives
231
}
232         
233         /**
234          * Prepare for iteration
235          */

236         public void preIterate() {
237             if (users==null) users = UsersList.getSampleInstance();
238             it = users.iterator();
239         }
240         
241         /**
242          * Return true if there are more rows in the data model
243          */

244         public boolean hasNext() {
245             return (it!=null && it.hasNext());
246         }
247         
248         /**
249          * Load the next row in the model
250          */

251         public void loadNext() {
252             ud = (UserData) it.next();
253         }
254         
255         /**
256          * Post iteration cleanup
257          */

258         public void postIterate() {
259             it = null;
260         }
261     }
262
263 }
264
265
Popular Tags