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 import org.enhydra.xml.xmlc.*; 11 12 import org.enhydra.barracuda.core.comp.*; 13 import org.enhydra.barracuda.core.comp.helper.*; 14 import org.enhydra.barracuda.core.util.dom.*; 15 import org.enhydra.barracuda.core.event.*; 16 import org.enhydra.barracuda.core.util.dom.*; 17 import org.enhydra.barracuda.core.view.*; 18 import org.enhydra.barracuda.examples.xmlc.data.*; 19 20 24 public class CompEx6 extends ComponentGateway { 25 26 30 public Document handleDefault (BComponent root, ViewContext vc, HttpServletRequest req, HttpServletResponse resp) throws IOException { 31 Document page = DefaultDOMLoader.getGlobalInstance().getDOM(CompEx6HTML.class); 33 34 BTemplate templateComp = new BTemplate(new LocalTemplateModel(req)); 36 templateComp.addModel(new LocalIterativeModel("Cntr1",2)); 37 templateComp.addModel(new LocalIterativeModel("Cntr2",3)); 38 templateComp.setView(new DefaultTemplateView(page.getDocumentElement())); 39 root.addChild(templateComp); 40 41 return page; 43 } 44 45 51 public DOMWriter getDOMWriter() { 52 return new DefaultDOMWriter(true); 54 } 55 56 60 class LocalTemplateModel extends AbstractTemplateModel { 61 62 HttpServletRequest req = null; 63 int attr = 0; 64 int cntr = 0; 65 66 public LocalTemplateModel(HttpServletRequest ireq) { 67 req = ireq; 68 } 69 70 public String getName() {return "Test1";} 72 73 public Object getItem(String key) { 75 ViewContext vc = getViewContext(); 76 ViewCapabilities vcap = vc.getViewCapabilities(); 77 if (key.equals("NullItem")) { 78 return null; 79 } else if (key.equals("Link1")) { 80 BLink blink = new BLink("some text","someurl", vc); 81 return blink; 82 } else if (key.equals("Image1")) { 83 return "/Barracuda/images/key.gif"; 84 } else if (key.equals("Markup1")) { 85 try { 86 CompEx6aHTML snippetPage = (CompEx6aHTML) DefaultDOMLoader.getGlobalInstance().getDOM(CompEx6aHTML.class); 87 Node targetNode = snippetPage.getElementMarkupBlockID(); 88 return vc.getElementFactory().getDocument().importNode(targetNode, true); 89 } catch (IOException e) { 90 return "Unable to find "+key+" snippet:"+e; 91 } 92 } else if (key.equals("Text1")) { 93 return "Some text goes here..."; 94 } else if (key.startsWith("GetAttr")) { 95 BComponent comp = new BComponent(); 96 comp.setAttr("cntr", ""+(++attr)); 97 return comp; 99 } else { 100 return super.getItem(key); 101 } 102 } 103 } 104 105 108 class LocalIterativeModel extends AbstractTemplateModel implements IterativeModel { 109 110 String modelName = null; 111 int max = -1; 112 int cntr = -1; 113 114 public LocalIterativeModel(String imodelName, int imax) { 115 modelName = imodelName; 116 max = imax; 117 } 118 119 public String getName() {return modelName;} 121 122 public Object getItem(String key) { 124 ViewContext vc = getViewContext(); 125 ViewCapabilities vcap = vc.getViewCapabilities(); 126 if (key.equals("Cntr")) { 127 return new BText("[model "+modelName+"]."+"Iterative Item"+cntr); 128 } else { 129 return super.getItem(key); 130 } 131 } 132 133 134 public void preIterate() { 135 cntr = 0; 136 } 137 138 public void postIterate() { 139 cntr = 99; 140 } 141 142 public boolean hasNext() { 143 return (cntr<max); 144 } 145 146 public void loadNext() { 147 cntr++; 148 } 149 150 } 151 } 152 | Popular Tags |