1 16 package javax.faces.component; 17 18 import java.util.ArrayList ; 19 import java.util.ConcurrentModificationException ; 20 import java.util.List ; 21 import java.util.ListIterator ; 22 import java.util.Locale ; 23 24 import javax.faces.context.FacesContext; 25 import javax.faces.el.ValueBinding; 26 import javax.faces.event.AbortProcessingException; 27 import javax.faces.event.FacesEvent; 28 import javax.faces.event.PhaseId; 29 import javax.faces.render.RenderKitFactory; 30 31 60 public class UIViewRoot 61 extends UIComponentBase 62 { 63 public static final String UNIQUE_ID_PREFIX = "_id"; 64 public static final int ANY_PHASE_ORDINAL = PhaseId.ANY_PHASE.getOrdinal(); 65 66 private int _uniqueIdCounter = 0; 67 68 private String _viewId = null; 69 private Locale _locale = null; 70 private List _events = null; 71 72 public String getViewId() 73 { 74 return _viewId; 75 } 76 77 public void setViewId(String viewId) 78 { 79 if (viewId == null) throw new NullPointerException ("viewId"); 80 _viewId = viewId; 81 } 82 83 public void queueEvent(FacesEvent event) 84 { 85 if (event == null) throw new NullPointerException ("event"); 86 if (_events == null) 87 { 88 _events = new ArrayList (); 89 } 90 _events.add(event); 91 } 92 93 private void _broadcastForPhase(PhaseId phaseId) 94 { 95 if (_events == null) return; 96 97 boolean abort = false; 98 99 int phaseIdOrdinal = phaseId.getOrdinal(); 100 for (ListIterator listiterator = _events.listIterator(); listiterator.hasNext();) 101 { 102 FacesEvent event = (FacesEvent) listiterator.next(); 103 int ordinal = event.getPhaseId().getOrdinal(); 104 if (ordinal == ANY_PHASE_ORDINAL || 105 ordinal == phaseIdOrdinal) 106 { 107 UIComponent source = event.getComponent(); 108 try 109 { 110 source.broadcast(event); 111 } 112 catch (AbortProcessingException e) 113 { 114 abort = true; 118 break; 119 } finally { 120 121 try 122 { 123 listiterator.remove(); 124 } 125 catch(ConcurrentModificationException cme) 126 { 127 int eventIndex = listiterator.previousIndex(); 128 _events.remove(eventIndex); 129 listiterator = _events.listIterator(); 130 } 131 } 132 } 133 } 134 135 if (abort) { 136 clearEvents(); 138 } 139 } 140 141 142 private void clearEvents() 143 { 144 _events = null; 145 } 146 147 148 public void processDecodes(FacesContext context) 149 { 150 if (context == null) throw new NullPointerException ("context"); 151 super.processDecodes(context); 152 _broadcastForPhase(PhaseId.APPLY_REQUEST_VALUES); 153 if (context.getRenderResponse() || context.getResponseComplete()) 154 { 155 clearEvents(); 156 } 157 } 158 159 public void processValidators(FacesContext context) 160 { 161 if (context == null) throw new NullPointerException ("context"); 162 super.processValidators(context); 163 _broadcastForPhase(PhaseId.PROCESS_VALIDATIONS); 164 if (context.getRenderResponse() || context.getResponseComplete()) 165 { 166 clearEvents(); 167 } 168 } 169 170 public void processUpdates(FacesContext context) 171 { 172 if (context == null) throw new NullPointerException ("context"); 173 super.processUpdates(context); 174 _broadcastForPhase(PhaseId.UPDATE_MODEL_VALUES); 175 if (context.getRenderResponse() || context.getResponseComplete()) 176 { 177 clearEvents(); 178 } 179 } 180 181 public void processApplication(FacesContext context) 182 { 183 if (context == null) throw new NullPointerException ("context"); 184 _broadcastForPhase(PhaseId.INVOKE_APPLICATION); 185 if (context.getRenderResponse() || context.getResponseComplete()) 186 { 187 clearEvents(); 188 } 189 } 190 191 public void encodeBegin(FacesContext context) 192 throws java.io.IOException 193 { 194 _uniqueIdCounter = 0; 195 clearEvents(); 196 super.encodeBegin(context); 197 } 198 199 public String createUniqueId() 200 { 201 return UNIQUE_ID_PREFIX + _uniqueIdCounter++; 202 } 203 204 public Locale getLocale() 205 { 206 if (_locale != null) return _locale; 207 ValueBinding vb = getValueBinding("locale"); 208 FacesContext facesContext = getFacesContext(); 209 if (vb == null) 210 { 211 return facesContext.getApplication().getViewHandler().calculateLocale(facesContext); 212 } 213 Object locale = vb.getValue(facesContext); 214 if (locale == null) 215 { 216 return facesContext.getApplication().getViewHandler().calculateLocale(facesContext); 217 } 218 if (locale instanceof Locale ) 219 { 220 return (Locale )locale; 221 } 222 else if (locale instanceof String ) 223 { 224 return getLocale((String )locale); 225 } 226 else 227 { 228 throw new IllegalArgumentException ("locale binding"); } 230 } 231 232 239 private static Locale getLocale(String locale){ 240 int cnt = 0; 241 int pos = 0; 242 int prev = 0; 243 244 String [] lv = new String [3]; 250 Locale l=null; 251 252 while((pos=locale.indexOf('_',prev))!=-1){ 253 lv[cnt++] = locale.substring(prev,pos); 254 prev = pos + 1; 255 } 256 257 lv[cnt++] = locale.substring(prev,locale.length()); 258 259 switch(cnt){ 260 case 1: 261 l = new Locale (lv[0]); 263 break; 264 case 2: 265 l = new Locale (lv[0],lv[1]); 267 break; 268 case 3: 269 l = new Locale (lv[0], lv[1], lv[2]); 271 break; 272 } 273 return l; 274 } 275 276 277 public void setLocale(Locale locale) 278 { 279 _locale = locale; 280 } 281 282 284 public static final String COMPONENT_TYPE = "javax.faces.ViewRoot"; 285 public static final String COMPONENT_FAMILY = "javax.faces.ViewRoot"; 286 private static final String DEFAULT_RENDERKITID = RenderKitFactory.HTML_BASIC_RENDER_KIT; 287 288 private String _renderKitId = null; 289 290 public String getFamily() 291 { 292 return COMPONENT_FAMILY; 293 } 294 295 296 public void setRenderKitId(String renderKitId) 297 { 298 _renderKitId = renderKitId; 299 } 300 301 public String getRenderKitId() 302 { 303 if (_renderKitId != null) return _renderKitId; 304 ValueBinding vb = getValueBinding("renderKitId"); 305 return vb != null ? (String )vb.getValue(getFacesContext()) : DEFAULT_RENDERKITID; 306 } 307 308 309 310 public Object saveState(FacesContext context) 311 { 312 Object values[] = new Object [4]; 313 values[0] = super.saveState(context); 314 values[1] = _locale; 315 values[2] = _renderKitId; 316 values[3] = _viewId; 317 return values; 318 } 319 320 public void restoreState(FacesContext context, Object state) 321 { 322 Object values[] = (Object [])state; 323 super.restoreState(context, values[0]); 324 _locale = (Locale )values[1]; 325 _renderKitId = (String )values[2]; 326 _viewId = (String )values[3]; 327 } 328 } | Popular Tags |