1 20 package org.enhydra.barracuda.contrib.dbroggisch.repopulation; 21 22 23 import java.util.List ; 24 import java.util.Locale ; 25 import java.util.Iterator ; 26 import java.util.ArrayList ; 27 import javax.servlet.*; 28 import javax.servlet.http.*; 29 30 import org.enhydra.barracuda.core.comp.TemplateModel; 31 import org.enhydra.barracuda.core.forms.DefaultFormMap; 32 import org.enhydra.barracuda.core.forms.FormMap; 33 import org.enhydra.barracuda.core.forms.FormElement; 34 import org.enhydra.barracuda.core.forms.FormType; 35 import org.enhydra.barracuda.core.forms.ParseException; 36 import org.enhydra.barracuda.core.comp.ViewContext; 37 import org.enhydra.barracuda.core.comp.TemplateDirective; 38 import org.enhydra.barracuda.core.comp.model.ModelListener; 39 import org.enhydra.barracuda.core.comp.BInput; 40 import org.enhydra.barracuda.core.comp.BSelect; 41 import org.enhydra.barracuda.core.comp.BComponent; 42 import org.apache.log4j.*; 43 44 45 57 public class RepopulationFormMap extends ErrorFormMap implements TemplateModel { 58 protected static final Logger logger = Logger.getLogger(RepopulationFormMap.class.getName()); 59 60 64 protected String prefix; 65 66 70 protected String modelName; 71 72 protected ViewContext viewContext = null; 73 protected List listeners = new ArrayList (); 74 75 protected List _modelChilds = new ArrayList (); 76 77 public void defineElement(FormElement element) { 78 super.defineElement(element); 79 if(element instanceof ChildModelElement) { 80 _modelChilds.add(element); 81 } 82 } 83 84 public void defineElement(java.lang.String key, FormElement element) { 85 super.defineElement(key, element); 86 if(element instanceof ChildModelElement) { 87 _modelChilds.add(element); 88 } 89 } 90 91 public List getChildModels() { 92 List res = new ArrayList (); 93 for(Iterator it = _modelChilds.iterator(); it.hasNext();) { 94 ChildModelElement cme = (ChildModelElement)it.next(); 95 res.add(cme.getChildModel(modelName, prefix)); 96 } 97 return res; 98 } 99 100 105 public void setPrefix(String prefix) { 106 this.prefix = prefix; 107 } 108 109 110 115 public String getPrefix() { 116 return this.prefix; 117 } 118 119 120 121 128 public FormMap map(ServletRequest req) { 129 return super.map(req, prefix); 130 } 131 132 139 public FormMap map(ServletRequest req, String prefix) { 140 this.prefix = prefix; 141 return super.map(req, prefix); 142 } 143 144 151 public FormMap map(ServletRequest req, Locale loc) { 152 return super.map(req, prefix, loc); 153 } 154 155 157 163 public Object getItem(String key) 164 { 165 if(logger.isDebugEnabled()) { 166 logger.debug("getItem() with key: " + key); 167 } 168 if(getElement(key) != null) { 169 String name = prefix != null ? prefix + key: key; 170 BComponent bc = null; 171 FormElement el = getElement(key); 172 if(el instanceof RepopulationElement) { 173 Object res = ((RepopulationElement)el).render(getViewContext()); 174 if(!(res instanceof BComponent)) { 175 return res; 176 } 177 bc = (BComponent)res; 178 } 179 else { 180 BInput bi = new BInput(); 181 if(logger.isDebugEnabled() && el.getOrigVal() != null) { 182 logger.debug("Class: " + el.getOrigVal().getClass() + " Value: " + el.getOrigVal()); 183 } 184 bi.setValue((String )el.getOrigVal()); 185 bc = bi; 186 } 187 if(bc != null) { 188 bc.setName(name); 189 return bc; 190 } 191 } 192 return super.getErrorComponent(key); 193 } 194 195 public Object getItem(TemplateDirective td) { 196 return getItem(td.getKeyName()); 197 } 198 199 203 public void setViewContext(ViewContext vc) 204 { 205 viewContext = vc; 206 } 207 208 212 public ViewContext getViewContext() 213 { 214 return viewContext; 215 } 216 218 223 public String getName() 224 { 225 return modelName; 226 } 227 228 229 234 public void setName(String name) 235 { 236 modelName = name; 237 } 238 239 244 public boolean processDirective(TemplateDirective td) 245 { 246 return true; 248 } 249 250 252 256 public void addModelListener(ModelListener ml) 257 { 258 listeners.add(ml); 259 } 260 261 265 public void removeModelListener(ModelListener ml) 266 { 267 listeners.remove(ml); 268 } 269 270 275 public void fireModelChanged() { 276 Iterator it = listeners.iterator(); 277 ModelListener ml = null; 278 while (it.hasNext()) { 279 ml = (ModelListener) it.next(); 280 ml.modelChanged(this); 281 } 282 } 283 284 } 285 | Popular Tags |