1 16 package org.apache.myfaces.custom.aliasbean; 17 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import javax.faces.component.UIComponent; 25 import javax.faces.component.UIComponentBase; 26 import javax.faces.context.FacesContext; 27 import javax.faces.el.ValueBinding; 28 import javax.faces.event.AbortProcessingException; 29 import javax.faces.event.FacesEvent; 30 import javax.faces.event.FacesListener; 31 import javax.faces.event.PhaseId; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 36 81 public class AliasBean extends UIComponentBase { 82 private static final Log log = LogFactory.getLog(AliasBean.class); 83 84 public static final String COMPONENT_TYPE = "org.apache.myfaces.AliasBean"; 85 public static final String COMPONENT_FAMILY = "javax.faces.Data"; 86 private static final String DEFAULT_RENDERER_TYPE = "org.apache.myfaces.AliasBean"; 87 88 private String _valueExpression = null; 89 90 private String _aliasBeanExpression = null; 91 92 private transient FacesContext _context = null; 93 94 public AliasBean() { 95 setRendererType(DEFAULT_RENDERER_TYPE); 96 } 97 98 public String getFamily() { 99 return COMPONENT_FAMILY; 100 } 101 102 public void setAlias(String aliasBeanExpression){ 103 this._aliasBeanExpression = aliasBeanExpression; 104 } 105 106 public String getValue(){ 107 if (_valueExpression != null) 108 return _valueExpression; 109 ValueBinding vb = getValueBinding("value"); 110 return vb != null ? (String )vb.getValue(getFacesContext()) : null; 111 } 112 public void setValue(String valueExpression){ 113 this._valueExpression = valueExpression; 114 } 115 116 public Object saveState(FacesContext context) { 117 log.debug("saveState"); 118 119 _context = context; 120 121 Object values[] = new Object [3]; 122 values[0] = super.saveState(context); 123 values[1] = _valueExpression; 124 values[2] = _aliasBeanExpression; 125 return values; 126 } 127 128 public void restoreState(FacesContext context, Object state) { 129 log.debug("restoreState"); 130 131 _context = context; 132 133 Object values[] = (Object []) state; 134 super.restoreState(context, values[0]); 135 _valueExpression = (String ) values[1]; 136 _aliasBeanExpression = (String ) values[2]; 137 } 138 139 public Object processSaveState(FacesContext context) { 140 if (context == null) 141 throw new NullPointerException ("context"); 142 if (isTransient()) 143 return null; 144 145 makeAlias(context); 146 147 Map facetMap = null; 148 for (Iterator it = getFacets().entrySet().iterator(); it.hasNext();) { 149 Map.Entry entry = (Map.Entry ) it.next(); 150 if (facetMap == null) 151 facetMap = new HashMap (); 152 UIComponent component = (UIComponent) entry.getValue(); 153 if (!component.isTransient()) { 154 facetMap.put(entry.getKey(), component.processSaveState(context)); 155 } 156 } 157 List childrenList = null; 158 if (getChildCount() > 0) { 159 for (Iterator it = getChildren().iterator(); it.hasNext();) { 160 UIComponent child = (UIComponent) it.next(); 161 if (!child.isTransient()) { 162 if (childrenList == null) 163 childrenList = new ArrayList (getChildCount()); 164 childrenList.add(child.processSaveState(context)); 165 } 166 } 167 } 168 169 removeAlias(context); 170 171 return new Object [] { saveState(context), facetMap, childrenList }; 172 } 173 174 public void processRestoreState(FacesContext context, Object state) { 175 if (context == null) 176 throw new NullPointerException ("context"); 177 Object myState = ((Object []) state)[0]; 178 179 restoreState(context, myState); 180 makeAlias(context); 181 182 Map facetMap = (Map ) ((Object []) state)[1]; 183 List childrenList = (List ) ((Object []) state)[2]; 184 for (Iterator it = getFacets().entrySet().iterator(); it.hasNext();) { 185 Map.Entry entry = (Map.Entry ) it.next(); 186 Object facetState = facetMap.get(entry.getKey()); 187 if (facetState != null) { 188 ((UIComponent) entry.getValue()).processRestoreState(context, facetState); 189 } else { 190 context.getExternalContext().log("No state found to restore facet " + entry.getKey()); 191 } 192 } 193 if (getChildCount() > 0) { 194 int idx = 0; 195 for (Iterator it = getChildren().iterator(); it.hasNext();) { 196 UIComponent child = (UIComponent) it.next(); 197 Object childState = childrenList.get(idx++); 198 if (childState != null) { 199 child.processRestoreState(context, childState); 200 } else { 201 context.getExternalContext().log("No state found to restore child of component " + getId()); 202 } 203 } 204 } 205 206 removeAlias(context); 207 } 208 209 public void processValidators(FacesContext context) { 210 log.debug("processValidators"); 211 makeAlias(context); 212 super.processValidators(context); 213 removeAlias(context); 214 } 215 216 public void processDecodes(FacesContext context) { 217 log.debug("processDecodes"); 218 makeAlias(context); 219 super.processDecodes(context); 220 removeAlias(context); 221 } 222 223 public void processUpdates(FacesContext context) { 224 log.debug("processUpdates"); 225 makeAlias(context); 226 super.processUpdates(context); 227 removeAlias(context); 228 } 229 230 public void queueEvent(FacesEvent event) { 231 super.queueEvent(new FacesEventWrapper(event, this)); 232 } 233 234 public void broadcast(FacesEvent event) throws AbortProcessingException { 235 makeAlias(); 236 237 if (event instanceof FacesEventWrapper) { 238 FacesEvent originalEvent = ((FacesEventWrapper) event).getWrappedFacesEvent(); 239 originalEvent.getComponent().broadcast(originalEvent); 240 } else { 241 super.broadcast(event); 242 } 243 244 removeAlias(); 245 } 246 247 void makeAlias(FacesContext context) { 248 _context = context; 249 makeAlias(); 250 } 251 252 private void makeAlias() { 253 Object value; 255 256 ValueBinding valueVB = null; 257 if (_valueExpression == null) { 258 valueVB = getValueBinding("value"); 259 _valueExpression = valueVB.getExpressionString(); 260 } 261 262 if( valueVB == null ){ 263 if( _valueExpression.startsWith("#{") ){ 264 valueVB = _context.getApplication().createValueBinding(_valueExpression); 265 value = valueVB.getValue(_context); 266 }else{ 267 value = _valueExpression; 268 } 269 }else{ 270 value = valueVB.getValue(_context); 271 } 272 273 ValueBinding aliasVB; 275 if (_aliasBeanExpression == null) { 276 aliasVB = getValueBinding("alias"); 277 _aliasBeanExpression = aliasVB.getExpressionString(); 278 } else { 279 aliasVB = _context.getApplication().createValueBinding(_aliasBeanExpression); 280 } 281 282 aliasVB.setValue(_context, value); 283 284 log.debug("makeAlias: " + _valueExpression + " = " + _aliasBeanExpression); 285 } 286 287 void removeAlias(FacesContext context) { 288 _context = context; 289 removeAlias(); 290 } 291 292 private void removeAlias() { 293 log.debug("removeAlias: " + _valueExpression + " != " + _aliasBeanExpression); 294 295 ValueBinding aliasVB = getValueBinding("alias"); 296 aliasVB.setValue(_context, null); 297 } 298 299 private static class FacesEventWrapper extends FacesEvent { 300 private FacesEvent _wrappedFacesEvent; 301 302 public FacesEventWrapper(FacesEvent facesEvent, AliasBean redirectComponent) { 303 super(redirectComponent); 304 _wrappedFacesEvent = facesEvent; 305 } 306 307 public PhaseId getPhaseId() { 308 return _wrappedFacesEvent.getPhaseId(); 309 } 310 311 public void setPhaseId(PhaseId phaseId) { 312 _wrappedFacesEvent.setPhaseId(phaseId); 313 } 314 315 public void queue() { 316 _wrappedFacesEvent.queue(); 317 } 318 319 public String toString() { 320 return _wrappedFacesEvent.toString(); 321 } 322 323 public boolean isAppropriateListener(FacesListener faceslistener) { 324 return _wrappedFacesEvent.isAppropriateListener(faceslistener); 325 } 326 327 public void processListener(FacesListener faceslistener) { 328 _wrappedFacesEvent.processListener(faceslistener); 329 } 330 331 public FacesEvent getWrappedFacesEvent() { 332 return _wrappedFacesEvent; 333 } 334 } 335 } | Popular Tags |