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

org.ajax4jsf.ajax.UIAjaxStatus 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!
/**
 * Copyright 2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.ajax;

import java.io.Serializable;

import javax.faces.FacesException;
import javax.faces.component.UIComponent;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.faces.el.ValueBinding;

import org.ajax4jsf.framework.ajax.AjaxContainer;
import org.ajax4jsf.framework.renderer.AjaxRendererUtils;
import org.ajax4jsf.framework.util.message.Messages;


/**
 * 
 * Component for create request state status area.
 * can create 2 output text areas ( <span> ) with different text and styles.
 * if defined "start" or/and "stop" facets, it's use as conttent
 * for different state.
 * By default, "start" text/facet rendered with display = none style.
 * On client side, display style changed for elements on 
 * start-stop request.
 * @author shura (latest modification by $Author: alexsmirnov $)
 * @version $Revision: 1.3 $ $Date: 2006/11/30 15:42:13 $
 *
 */
public class UIAjaxStatus extends UIComponentBase
{

    private static final String COMPONENT_FAMILY = "javax.faces.Output";
    public static final String COMPONENT_TYPE = "org.ajax4jsf.components.AjaxStatus";
    

    /* (non-Javadoc)
     * @see javax.faces.component.UIComponent#getFamily()
     */
    public String getFamily()
    {
        return COMPONENT_FAMILY;
    }
    
    /**
     * id of form for wich status will displayed
     */
    private String _for = null;

    /**
     *
     * @param new value of id of form for wich status will displayed to set
     */
    public void setFor(String _for)
    {
        this._for = _for;
    }

    /**
     * @return value or result of valueBinding of id of form for wich status will displayed
     *
     */
    public String getFor()
    {
        return (String) getValueOrBinding(_for, "for");
    }
    
    /**
     * Text to output on start request
     */
    private String _startText = null;

    /**
     * setter method for property
     * @param new value of Text to output on start request to set
     */
    public void setStartText(String startText)
    {
        this._startText = startText;
    }

    /**
     * @return value or result of valueBinding of Text to output on start request
     */
    public String getStartText()
    {
        return (String) getValueOrBinding(_startText, "startText");
    }
    
    /**
     * Text to display on complete request
     */
    private String _stopText = null;

    /**
     * setter method for property
     * @param new value of Text to display on complete request to set
     */
    public void setStopText(String stopText)
    {
        this._stopText = stopText;
    }

    /**
     * @return value or result of valueBinding of Text to display on complete request
     */
    public String getStopText()
    {
        return (String) getValueOrBinding(_stopText, "stopText");
    }
    /**
     * Style for display on start request
     */
    private String _startStyle = null;

    /**
     * setter method for property
     * @param new value of Style for display on start request to set
     */
    public void setStartStyle(String startStyle)
    {
        this._startStyle = startStyle;
    }

    /**
     * @return value or result of valueBinding of Style for display on start request
     */
    public String getStartStyle()
    {
        return (String) getValueOrBinding(_startStyle, "startStyle");
    }
    /**
     * Style for displaying on complete
     */
    private String _stopStyle = null;

    /**
     * setter method for property
     * @param new value of Style for displaying on complete to set
     */
    public void setStopStyle(String stopStyle)
    {
        this._stopStyle = stopStyle;
    }

    /**
     * @return value or result of valueBinding of Style for displaying on complete
     */
    public String getStopStyle()
    {
        return (String) getValueOrBinding(_stopStyle, "stopStyle");
    }
    /**
     * Style class for display on request
     */
    private String _startStyleClass = null;

    /**
     * setter method for property
     * @param new value of Style class for display on request to set
     */
    public void setStartStyleClass(String startStyleClass)
    {
        this._startStyleClass = startStyleClass;
    }

    /**
     * @return value or result of valueBinding of Style class for display on request
     */
    public String getStartStyleClass()
    {
        return (String) getValueOrBinding(_startStyleClass, "startStyleClass");
    }
    /**
     * Style class for display on complete request
     */
    private String _stopStyleClass = null;

    /**
     * setter method for property
     * @param new value of Style class for display on complete request to set
     */
    public void setStopStyleClass(String stopStyleClass)
    {
        this._stopStyleClass = stopStyleClass;
    }

    
    /**
     * Force id to render in Html as is
     */
    private boolean _forceId = false;
    private boolean _forceIdSet = false;

    /**
     * setter method for property
     * @param new value of Force id to render in Html as is to set
     */
    public void setForceId(boolean forceId)
    {
        this._forceId = forceId;
        this._forceIdSet = true;
    }

    /**
     * @return value or result of valueBinding of Force id to render in Html as is
     */
    public boolean isForceId()
    {
        return  isValueOrBinding(_forceId, _forceIdSet, "forceId");
    }
    
    
    
    
	private String _clientId = null;

	/* (non-Javadoc)
	 * @see javax.faces.component.UIComponentBase#getClientId(javax.faces.context.FacesContext)
	 */
	public String getClientId(FacesContext context) {
        if (null == _clientId) {
			String forValue = getFor();
			UIComponent container;
			if (null != forValue) {
				container = findComponent(forValue);
				// 'for' attribute must be pointed to real container in view tree
				if (null == container || !(container instanceof AjaxContainer)) {
					throw new FacesException(
							Messages.getMessage(Messages.FOR_TARGETS_NO_AJAX_CONTAINER, getId()));
				}
				_clientId = container.getClientId(context) + ":status";

			} else if (isForceId()) {
				_clientId = getRenderer(context).convertClientId(context,
						this.getId());
			} else if (null !=(container = (UIComponent) AjaxRendererUtils.findAjaxContainer(context,this))) {
				_clientId = container.getClientId(context) + ":status";
			} else {
				_clientId = super.getClientId(context);
			}
		}
        return _clientId;
	}

	
	/* (non-Javadoc)
	 * reset clientId for calculate
	 * @see javax.faces.component.UIComponentBase#setId(java.lang.String)
	 */
	public void setId(String arg0) {
		super.setId(arg0);
		_clientId = null;
	}

	/* (non-Javadoc)
     * @see javax.faces.component.UIComponentBase#restoreState(javax.faces.context.FacesContext, java.lang.Object)
     */
    public void restoreState(FacesContext context, Object state)
    {
        State myState = (State) state;
        _for = myState._for;
        _startText = myState._startText;
        _stopText = myState._stopText;
        _startStyle = myState._startStyle;
        _stopStyle = myState._stopStyle;
        _startStyleClass = myState._startStyleClass;
        _stopStyleClass = myState._stopStyleClass;
        _forceId = myState._forceId;
        _forceIdSet = myState._forceIdSet;
        super.restoreState(context, myState.superState);
    }

    /* (non-Javadoc)
     * @see javax.faces.component.UIComponentBase#saveState(javax.faces.context.FacesContext)
     */
    public Object saveState(FacesContext context)
    {
        State state = new State();
        state._for = _for;
        state._startText = _startText;
        state._stopText = _stopText;
        state._startStyle = _startStyle;
        state._stopStyle = _stopStyle;
        state._startStyleClass = _startStyleClass;
        state._stopStyleClass = _stopStyleClass;
        state._forceId = _forceId;
        state._forceIdSet = _forceIdSet;
        state.superState = super.saveState(context);
        return state;
    }
    
    /**
     * @author shura (latest modification by $Author: alexsmirnov $)
     * @version $Revision: 1.3 $ $Date: 2006/11/30 15:42:13 $
     * Memento pattern state class for save-restore component.
     */
    public static class State implements Serializable {
        /**
         * 
         */
        private static final long serialVersionUID = 317266574102385358L;
        Object superState;
        String _for;
        String _startText;
        String _stopText;
        String _startStyle;
        String _stopStyle;
        String _startStyleClass;
        String _stopStyleClass;        
        boolean _forceIdSet;
        boolean _forceId;
    }

    /**
     * @return value or result of valueBinding of Style class for display on complete request
     */
    public String getStopStyleClass()
    {
        return (String) getValueOrBinding(_stopStyleClass, "stopStyleClass");
    }
    
    /**
     * @param field - value of field to get.
     * @param name - name of field, to get from ValueBinding
     * @return field or value of binding expression.
     */
    private Object getValueOrBinding(Object field, String name){
        if( null != field){
            return field;
        }
        ValueBinding vb = getValueBinding(name);
        if (null != vb) {
            return vb.getValue(getFacesContext());
        } else {
            return null;
        }

    }
    
    /**
     * @param field - value of field to get.
     * @param name - name of field, to get from ValueBinding
     * @return boolean value, based on field or valuebinding.
     */
    private boolean isValueOrBinding(boolean field, boolean fieldSet, String name){
        if( fieldSet ){
            return field;
        }
        ValueBinding vb = getValueBinding(name);
        if (null != vb) {
            return ((Boolean)vb.getValue(getFacesContext())).booleanValue();
        } else {
            return false;
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy