1 14 15 package com.sun.facelets.tag.jsf; 16 17 import java.io.IOException ; 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.Collections ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Locale ; 24 25 import javax.faces.FacesException; 26 import javax.faces.component.UIComponent; 27 import javax.faces.component.UIViewRoot; 28 import javax.faces.context.FacesContext; 29 30 import com.sun.facelets.FaceletContext; 31 import com.sun.facelets.FaceletHandler; 32 import com.sun.facelets.tag.TagAttribute; 33 import com.sun.facelets.tag.TagAttributeException; 34 35 40 public final class ComponentSupport { 41 42 private final static String MARK_DELETED = "com.sun.facelets.MARK_DELETED"; 43 public final static String MARK_CREATED = "com.sun.facelets.MARK_ID"; 44 45 52 public static final void finalizeForDeletion(UIComponent c) { 53 c.getAttributes().remove(MARK_DELETED); 55 56 int sz = c.getChildCount(); 58 if (sz > 0) { 59 UIComponent cc = null; 60 List cl = c.getChildren(); 61 while (--sz >= 0) { 62 cc = (UIComponent) cl.get(sz); 63 if (cc.getAttributes().containsKey(MARK_DELETED)) { 64 cl.remove(sz); 65 } 66 } 67 } 68 69 if (c.getFacets().size() > 0) { 71 Collection col = c.getFacets().values(); 72 UIComponent fc; 73 for (Iterator itr = col.iterator(); itr.hasNext();) { 74 fc = (UIComponent) itr.next(); 75 if (fc.getAttributes().containsKey(MARK_DELETED)) { 76 itr.remove(); 77 } 78 } 79 } 80 } 81 82 83 92 public static final UIComponent findChild(UIComponent parent, String id) { 93 int sz = parent.getChildCount(); 94 if (sz > 0) { 95 UIComponent c = null; 96 List cl = parent.getChildren(); 97 while (--sz >= 0) { 98 c = (UIComponent) cl.get(sz); 99 if (id.equals(c.getId())) { 100 return c; 101 } 102 } 103 } 104 return null; 105 } 106 107 113 public static final UIComponent findChildByTagId(UIComponent parent, String id) { 114 int sz = parent.getChildCount(); 115 if (sz > 0) { 116 UIComponent c = null; 117 List cl = parent.getChildren(); 118 String cid = null; 119 while (--sz >= 0) { 120 c = (UIComponent) cl.get(sz); 121 cid = (String ) c.getAttributes().get(MARK_CREATED); 122 if (id.equals(cid)) { 123 return c; 124 } 125 } 126 } 127 return null; 128 } 129 130 142 public static final Locale getLocale(FaceletContext ctx, TagAttribute attr) 143 throws TagAttributeException { 144 Object obj = attr.getObject(ctx); 145 if (obj instanceof Locale ) { 146 return (Locale ) obj; 147 } 148 if (obj instanceof String ) { 149 String s = (String ) obj; 150 if (s.length() == 2) { 151 return new Locale (s); 152 } 153 if (s.length() == 5) { 154 return new Locale (s.substring(0, 2), s.substring(3, 5) 155 .toUpperCase()); 156 } 157 if (s.length() >= 7) { 158 return new Locale (s.substring(0, 2), s.substring(3, 5) 159 .toUpperCase(), s.substring(6, s.length())); 160 } 161 throw new TagAttributeException(attr, "Invalid Locale Specified: " 162 + s); 163 } else { 164 throw new TagAttributeException(attr, 165 "Attribute did not evaluate to a String or Locale: " + obj); 166 } 167 } 168 169 179 public static final UIViewRoot getViewRoot(FaceletContext ctx, 180 UIComponent parent) { 181 UIComponent c = parent; 182 do { 183 if (c instanceof UIViewRoot) { 184 return (UIViewRoot) c; 185 } else { 186 c = c.getParent(); 187 } 188 } while (c != null); 189 return ctx.getFacesContext().getViewRoot(); 190 } 191 192 199 public static final void markForDeletion(UIComponent c) { 200 c.getAttributes().put(MARK_DELETED, Boolean.TRUE); 202 203 int sz = c.getChildCount(); 205 if (sz > 0) { 206 UIComponent cc = null; 207 List cl = c.getChildren(); 208 while (--sz >= 0) { 209 cc = (UIComponent) cl.get(sz); 210 if (cc.getAttributes().containsKey(MARK_CREATED)) { 211 cc.getAttributes().put(MARK_DELETED, Boolean.TRUE); 212 } 213 } 214 } 215 216 if (c.getFacets().size() > 0) { 218 Collection col = c.getFacets().values(); 219 UIComponent fc; 220 for (Iterator itr = col.iterator(); itr.hasNext();) { 221 fc = (UIComponent) itr.next(); 222 if (fc.getAttributes().containsKey(MARK_CREATED)) { 223 fc.getAttributes().put(MARK_DELETED, Boolean.TRUE); 224 } 225 } 226 } 227 } 228 229 public final static void encodeRecursive(FacesContext context, 230 UIComponent viewToRender) throws IOException , FacesException { 231 if (viewToRender.isRendered()) { 232 viewToRender.encodeBegin(context); 233 if (viewToRender.getRendersChildren()) { 234 viewToRender.encodeChildren(context); 235 } else if (viewToRender.getChildCount() > 0) { 236 Iterator kids = viewToRender.getChildren().iterator(); 237 while (kids.hasNext()) { 238 UIComponent kid = (UIComponent) kids.next(); 239 encodeRecursive(context, kid); 240 } 241 } 242 viewToRender.encodeEnd(context); 243 } 244 } 245 246 public static void removeTransient(UIComponent c) { 247 UIComponent d, e; 248 if (c.getChildCount() > 0) { 249 for (Iterator itr = c.getChildren().iterator(); itr.hasNext();) { 250 d = (UIComponent) itr.next(); 251 if (d.getFacets().size() > 0) { 252 for (Iterator jtr = d.getFacets().values().iterator(); jtr 253 .hasNext();) { 254 e = (UIComponent) jtr.next(); 255 if (e.isTransient()) { 256 jtr.remove(); 257 } else { 258 removeTransient(e); 259 } 260 } 261 } 262 if (d.isTransient()) { 263 itr.remove(); 264 } else { 265 removeTransient(d); 266 } 267 } 268 } 269 if (c.getFacets().size() > 0) { 270 for (Iterator itr = c.getFacets().values().iterator(); itr 271 .hasNext();) { 272 d = (UIComponent) itr.next(); 273 if (d.isTransient()) { 274 itr.remove(); 275 } else { 276 removeTransient(d); 277 } 278 } 279 } 280 } 281 282 290 public final static boolean isNew(UIComponent component) { 291 return component != null && component.getParent() == null; 292 } 293 } 294 | Popular Tags |