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 32 public class TemplateTableScreen extends DefaultEventGateway { 33 34 protected static final Logger logger = Logger.getLogger(TemplateTableScreen.class.getName()); 36 37 private static String TEST_SCREEN = TemplateTableScreen.class+".TestScreen"; 39 40 43 private ListenerFactory getTestFactory = new EventForwardingFactory(new RenderCompTest3()); 45 private ListenerFactory renderTestFactory = new DefaultListenerFactory() {public BaseEventListener getInstance() {return new RenderTestHandler();} public String getListenerID() {return getID(RenderTestHandler.class);}}; 46 47 50 public TemplateTableScreen() { 51 specifyLocalEventInterests(getTestFactory, CompTest3.class); 53 specifyLocalEventInterests(renderTestFactory, RenderCompTest3.class); 54 55 } 58 59 60 61 62 69 class RenderTestHandler extends DefaultBaseEventListener { 70 public void handleViewEvent(ViewEventContext context) throws EventException, ServletException, IOException { 71 ViewContext vc = new DefaultViewContext(context); 74 String pageName = ""+context.getRequest().getParameter("page"); 76 Class pageCl = CompEx3HTML.class; 77 if (pageName.equals("CompEx3a")) pageCl = CompEx3aHTML.class; 78 else if (pageName.equals("CompEx3b")) pageCl = CompEx3bHTML.class; 79 81 Document page = DefaultDOMLoader.getGlobalInstance().getDOM(pageCl, vc.getViewCapabilities().getClientLocale()); 83 84 BComponent wcRoot = new BComponent(); 88 BTemplate wcTemplate = new BTemplate(); 89 wcTemplate.addView(new DefaultTemplateView(page.getDocumentElement())); wcTemplate.addModel(new UserReportModel("org.enhydra.barracuda.examples.xmlc.UserReport")); 92 wcTemplate.addModel(new UserDataModel()); 93 wcRoot.addChild(wcTemplate); 94 95 try {wcRoot.render(vc);} 97 catch (RenderException e) {logger.debug ("Error rendering views:", e);} 98 99 ScriptDetector.prepareClientResp((HTMLDocument) page, vc); 101 102 OutputOptions oo = DefaultDOMWriter.getDefaultOutputOptions(page.getOwnerDocument()); 105 106 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 new DefaultDOMWriter(oo).write(page, context.getResponse()); 114 } 115 } 116 117 118 122 class UserReportModel extends DefaultPropertiesModel { 123 124 127 131 public UserReportModel(String propFileName) { 132 super(propFileName); 133 } 134 135 138 public Object getItem(String key) { 139 ViewContext vc = getViewContext(); 140 Object result = null; 141 if (key.equals("Footer")) { 142 Document footer = null; 143 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 ) super.getItem(key); 150 if (result==null) result = " "; 151 return result; 152 } 153 154 } 155 156 160 class UserDataModel extends AbstractTemplateModel implements IterativeModel { 161 162 UsersList users = null; 164 UserData ud = null; 165 Iterator it = null; 166 167 boolean useSelectionImages = true; 169 Node selNode = null; 170 Node unselNode = null; 171 172 175 public String getName() {return "UserData";} 176 177 180 public Object getItem(String key) { 181 ViewContext vc = getViewContext(); 182 Object 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; } 190 boolean selected = ((Boolean ) 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 ) ud.get(UserData.FIRST_NAME); 196 else if (key.equals("LastName")) result = (String ) ud.get(UserData.LAST_NAME); 197 else if (key.equals("Gender")) result = (((Boolean ) 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 ) ud.get(UserData.PHONE); 200 else if (key.equals("Email")) { 201 String email = (String ) ud.get(UserData.EMAIL); 203 result = new BLink(email, "mailto:"+email, vc); 204 } 205 else if (key.equals("Notes")) result = (String ) ud.get(UserData.NOTES); 206 else result = (String ) super.getItem(key); 207 if (result==null) result = " "; 208 return result; 209 } 210 211 214 public boolean processDirective(TemplateDirective td) { 215 if (td.getCommand().equals("Veto_If_Null")) { 216 String data = td.getKeyName(); 217 if (data!=null) { 218 if (null!=ud) { 219 StringTokenizer st = new StringTokenizer(data,","); 220 while (st.hasMoreTokens()) { 221 String key = st.nextToken(); 222 Object value = ud.get(key); 223 if (value==null || value.toString().length()<1) return false; } 225 } else { 226 return false; } 228 } 229 } 230 return true; } 232 233 236 public void preIterate() { 237 if (users==null) users = UsersList.getSampleInstance(); 238 it = users.iterator(); 239 } 240 241 244 public boolean hasNext() { 245 return (it!=null && it.hasNext()); 246 } 247 248 251 public void loadNext() { 252 ud = (UserData) it.next(); 253 } 254 255 258 public void postIterate() { 259 it = null; 260 } 261 } 262 263 } 264 265 | Popular Tags |