1 20 package org.enhydra.barracuda.core.comp; 21 22 import java.util.*; 23 24 import org.apache.log4j.*; 25 import org.w3c.dom.*; 26 import org.w3c.dom.html.*; 27 28 import org.enhydra.barracuda.core.comp.model.*; 29 import org.enhydra.barracuda.core.comp.renderer.*; 30 import org.enhydra.barracuda.core.comp.renderer.html.*; 31 import org.enhydra.barracuda.core.comp.renderer.xml.*; 32 import org.enhydra.barracuda.core.util.dom.DOMUtil; 33 import org.enhydra.barracuda.core.view.*; 34 import org.enhydra.barracuda.plankton.*; 35 36 46 public class BTemplate extends BComponent { 47 48 protected static final Logger logger = Logger.getLogger(BTemplate.class.getName()); 50 51 52 protected HashMap templateModels = new HashMap(); 54 private LocalModelListener callback = new LocalModelListener(); 56 59 62 private TemplateModel lnkTemplateModel; 63 64 65 69 public BTemplate() { 70 this (null, null); 71 } 72 73 80 public BTemplate(TemplateModel imodel) { 81 this (imodel, null); 82 } 83 84 97 BTemplate(TemplateModel imodel, TemplateView iview) { 98 if (imodel!=null) addModel(imodel); 99 if (iview!=null) this.addView(iview); 100 } 101 102 103 104 105 109 static { 110 HTMLRendererFactory rfHTML = new HTMLRendererFactory(); 111 installRendererFactory(rfHTML, BTemplate.class, HTMLElement.class); 112 installRendererFactory(rfHTML, BTemplate.class, HTMLDocument.class); 113 114 XMLRendererFactory rfXML = new XMLRendererFactory(); 115 installRendererFactory(rfXML, BTemplate.class, Node.class); 116 } 117 118 121 static class HTMLRendererFactory implements RendererFactory { 122 public Renderer getInstance() {return new HTMLTemplateRenderer();} 123 } 124 125 128 static class XMLRendererFactory implements RendererFactory { 129 public Renderer getInstance() {return new XMLTemplateRenderer();} 130 } 131 132 133 134 141 public void addModel(TemplateModel imodel) { 142 if (imodel==null) return; 144 145 imodel.removeModelListener(callback); 149 150 templateModels.put(imodel.getName(), imodel); 152 invalidate(); 153 154 imodel.addModelListener(callback); 156 } 157 158 163 public void addModels(List ilist) { 164 if (ilist==null) return; 166 167 Iterator it = ilist.iterator(); 169 while (it.hasNext()) { 170 Object o = it.next(); 171 if (o instanceof TemplateModel) addModel((TemplateModel) o); 172 } 173 } 174 175 180 public void removeModel(TemplateModel imodel) { 181 if (imodel==null) return; 183 184 templateModels.remove(imodel.getName()); 186 invalidate(); 187 188 imodel.removeModelListener(callback); 190 } 191 192 197 public void removeModel(String modelName) { 198 removeModel(getModel(modelName)); 199 } 200 201 205 public void removeAllModels() { 206 Iterator it = templateModels.entrySet().iterator(); 207 while (it.hasNext()) { 208 Map.Entry entry = (Map.Entry) it.next(); 209 TemplateModel model = (TemplateModel) entry.getValue(); 210 211 it.remove(); 213 214 model.removeModelListener(callback); 216 } 217 218 invalidate(); 219 } 220 221 226 public TemplateModel getModel(String modelName) { 227 return (TemplateModel) templateModels.get(modelName); 228 } 229 230 235 public List getModels() { 236 return new ArrayList(templateModels.values()); 237 } 238 239 247 262 263 264 265 270 public void destroyCycle() { 271 super.destroyCycle(); 273 274 removeAllModels(); templateModels = null; 280 callback = null; } 282 283 284 285 286 295 class LocalModelListener implements ModelListener { 296 public void modelChanged(Model m) { 298 invalidate(); 299 } 300 } 301 } | Popular Tags |