All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ajax4jsf.framework.skin.SkinImpl Maven / Gradle / Ivy

Go to download

Ajax4jsf is an open source extension to the JavaServer Faces standard that adds AJAX capability to JSF applications without requiring the writing of any JavaScript.

The newest version!
/**
 * Licensed under the Common Development and Distribution License,
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.sun.com/cddl/
 *   
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

package org.ajax4jsf.framework.skin;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;

/**
 * Singleton ( in respect as collection of different skins ) for produce
 * instances properties for all used skins.
 * 
 * @author shura (latest modification by $Author: alexsmirnov $)
 * @version $Revision: 1.3 $ $Date: 2006/09/06 17:53:55 $
 * 
 */
public class SkinImpl implements Skin {


	public static final String RENDER_KIT_PARAMETER = "render.kit";
	
	public static final String REQUEST_HASH_CODE_PARAMETER = "org.ajax4jsf.skin.HASH_CODE"; 

	private Map skinParams = new HashMap();
	

	/**
	 * Skin can instantiate only by factory method.
	 * 
	 * @param skinName
	 */
	SkinImpl(Map properties) {
		skinParams = properties;
	}


	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.skin.Skin#getRenderKitId(javax.faces.context.FacesContext)
	 */
	public String getRenderKitId(FacesContext context) {
		return (String) getValueReference(context,skinParams.get(RENDER_KIT_PARAMETER));
	}

	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.skin.Skin#getParameter(javax.faces.context.FacesContext, java.lang.String)
	 */
	public Object getParameter(FacesContext context, String name) {
		return getValueReference(context,skinParams.get(name));
	}

	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.skin.Skin#getParameter(javax.faces.context.FacesContext, java.lang.String, java.lang.String, java.lang.Object)
	 */
	public Object getParameter(FacesContext context, String name,  Object defaultValue) {
		Object value = getValueReference(context, skinParams.get(name));
		if(null == value){
			value = defaultValue;
		}
		return value;
	}
	
	
	/**
	 * Calculate concrete value for property - if it stored as @see ValueBinding ,
	 * return interpreted value.
	 * @param context
	 * @param property
	 * @return
	 */
	private Object getValueReference(FacesContext context, Object property) {
		if (property instanceof ValueBinding) {
			ValueBinding value = (ValueBinding) property;
			return value.getValue(context);
		}
		return property;
	}


	public String toString() {
		return skinParams.toString();
	}


	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.skin.Skin#containsProperty(java.lang.String)
	 */
	public boolean containsProperty(String name) {
		// TODO Auto-generated method stub
		return skinParams.containsKey(name);
	}


	/* (non-Javadoc)
	 * @see org.ajax4jsf.framework.skin.Skin#hashCode(javax.faces.context.FacesContext)
	 */
	public int hashCode(FacesContext context) {
		Integer requestCode = (Integer) context.getExternalContext().getRequestMap().get(REQUEST_HASH_CODE_PARAMETER);
		if(null == requestCode){
			int hash = 0;
			for (Iterator iter = skinParams.keySet().iterator(); iter.hasNext();) {
				String key = (String) iter.next();
				hash = 31*hash + getParameter(context,key).hashCode();
			}
			requestCode = new Integer(hash);
			// store hash for this skin as request-skope parameter - not calculate on next calls for same request
			context.getExternalContext().getRequestMap().put(REQUEST_HASH_CODE_PARAMETER,requestCode);
		}
		return requestCode.intValue();
	}
	
	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy