/*
* Copyright 2009 Prime Technology.
*
* 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.primefaces.component.wizard;
import javax.faces.component.UIComponentBase;
import javax.faces.context.FacesContext;
import javax.el.ValueExpression;
import javax.el.MethodExpression;
import javax.faces.render.Renderer;
import java.io.IOException;
import org.primefaces.renderkit.PartialRenderer;
import org.primefaces.component.resource.Resource;
import javax.faces.component.UIComponent;
import javax.faces.event.ListenerFor;
import javax.faces.event.PostAddToViewEvent;
import javax.faces.event.ComponentSystemEvent;
import javax.faces.event.AbortProcessingException;
import java.util.List;
import java.util.ArrayList;
import org.primefaces.component.tabview.Tab;
import org.primefaces.event.FlowEvent;
@ListenerFor(systemEventClass = PostAddToViewEvent.class)
public class Wizard extends UIComponentBase implements org.primefaces.component.api.AjaxComponent,javax.faces.component.NamingContainer {
public static final String COMPONENT_TYPE = "org.primefaces.component.Wizard";
public static final String COMPONENT_FAMILY = "org.primefaces.component";
private static final String DEFAULT_RENDERER = "org.primefaces.component.WizardRenderer";
private static final String OPTIMIZED_PACKAGE = "org.primefaces.component.";
protected enum PropertyKeys {
widgetVar
,step
,style
,styleClass
,flowListener
,showNavBar
,onback
,onnext
,nextLabel
,backLabel
,effect
,effectSpeed;
String toString;
PropertyKeys(String toString) {
this.toString = toString;
}
PropertyKeys() {}
public String toString() {
return ((this.toString != null) ? this.toString : super.toString());
}
}
public Wizard() {
setRendererType(DEFAULT_RENDERER);
}
public String getFamily() {
return COMPONENT_FAMILY;
}
public java.lang.String getWidgetVar() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.widgetVar, null);
}
public void setWidgetVar(java.lang.String _widgetVar) {
getStateHelper().put(PropertyKeys.widgetVar, _widgetVar);
handleAttribute("widgetVar", _widgetVar);
}
public java.lang.String getStep() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.step, null);
}
public void setStep(java.lang.String _step) {
getStateHelper().put(PropertyKeys.step, _step);
handleAttribute("step", _step);
}
public java.lang.String getStyle() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.style, null);
}
public void setStyle(java.lang.String _style) {
getStateHelper().put(PropertyKeys.style, _style);
handleAttribute("style", _style);
}
public java.lang.String getStyleClass() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.styleClass, null);
}
public void setStyleClass(java.lang.String _styleClass) {
getStateHelper().put(PropertyKeys.styleClass, _styleClass);
handleAttribute("styleClass", _styleClass);
}
public javax.el.MethodExpression getFlowListener() {
return (javax.el.MethodExpression) getStateHelper().eval(PropertyKeys.flowListener, null);
}
public void setFlowListener(javax.el.MethodExpression _flowListener) {
getStateHelper().put(PropertyKeys.flowListener, _flowListener);
handleAttribute("flowListener", _flowListener);
}
public boolean isShowNavBar() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.showNavBar, true);
}
public void setShowNavBar(boolean _showNavBar) {
getStateHelper().put(PropertyKeys.showNavBar, _showNavBar);
handleAttribute("showNavBar", _showNavBar);
}
public java.lang.String getOnback() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.onback, null);
}
public void setOnback(java.lang.String _onback) {
getStateHelper().put(PropertyKeys.onback, _onback);
handleAttribute("onback", _onback);
}
public java.lang.String getOnnext() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.onnext, null);
}
public void setOnnext(java.lang.String _onnext) {
getStateHelper().put(PropertyKeys.onnext, _onnext);
handleAttribute("onnext", _onnext);
}
public java.lang.String getNextLabel() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.nextLabel, "Next");
}
public void setNextLabel(java.lang.String _nextLabel) {
getStateHelper().put(PropertyKeys.nextLabel, _nextLabel);
handleAttribute("nextLabel", _nextLabel);
}
public java.lang.String getBackLabel() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.backLabel, "Back");
}
public void setBackLabel(java.lang.String _backLabel) {
getStateHelper().put(PropertyKeys.backLabel, _backLabel);
handleAttribute("backLabel", _backLabel);
}
public boolean isEffect() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.effect, true);
}
public void setEffect(boolean _effect) {
getStateHelper().put(PropertyKeys.effect, _effect);
handleAttribute("effect", _effect);
}
public java.lang.String getEffectSpeed() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.effectSpeed, "normal");
}
public void setEffectSpeed(java.lang.String _effectSpeed) {
getStateHelper().put(PropertyKeys.effectSpeed, _effectSpeed);
handleAttribute("effectSpeed", _effectSpeed);
}
private Tab tabToProcess;
public void processDecodes(FacesContext facesContext) {
if(isWizardRequest(facesContext)) {
//If flow goes back, skip to rendering
if(isBackRequest(facesContext)) {
facesContext.renderResponse();
} else {
getStepToProcess(facesContext).processDecodes(facesContext);
}
} else {
super.processDecodes(facesContext);
}
}
public void processValidators(FacesContext facesContext) {
if(isWizardRequest(facesContext)) {
getStepToProcess(facesContext).processValidators(facesContext);
} else {
super.processValidators(facesContext);
}
}
public void processUpdates(FacesContext facesContext) {
if(isWizardRequest(facesContext)) {
getStepToProcess(facesContext).processUpdates(facesContext);
} else {
super.processUpdates(facesContext);
}
}
public Tab getStepToProcess(FacesContext facesContext) {
if(tabToProcess == null) {
String currentStepId = facesContext.getExternalContext().getRequestParameterMap().get(getClientId(facesContext) + "_currentStep");
for(javax.faces.component.UIComponent child : getChildren()) {
if(child.getId().equals(currentStepId)) {
tabToProcess = (Tab) child;
break;
}
}
}
return tabToProcess;
}
private boolean isWizardRequest(FacesContext facesContext) {
return facesContext.getExternalContext().getRequestParameterMap().containsKey(getClientId(facesContext) + "_wizardRequest");
}
private boolean isBackRequest(FacesContext facesContext) {
return facesContext.getExternalContext().getRequestParameterMap().containsKey(getClientId(facesContext) + "_backRequest");
}
protected FacesContext getFacesContext() {
return FacesContext.getCurrentInstance();
}
public void encodePartially(FacesContext facesContext) throws IOException {
Renderer renderer = getRenderer(facesContext);
if(renderer instanceof PartialRenderer) {
((PartialRenderer)renderer).encodePartially(facesContext, this);
}
}
public boolean resourceExists(FacesContext facesContext, String name) {
java.util.List<UIComponent> resources = facesContext.getViewRoot().getComponentResources(facesContext, "head");
for(UIComponent component : resources) {
String value = component.toString();
if(value != null && value.equals(name))
return true;
}
return false;
}
@Override
public void processEvent(ComponentSystemEvent event) throws AbortProcessingException {
FacesContext facesContext = getFacesContext();
javax.faces.component.UIViewRoot viewroot = facesContext.getViewRoot();
Resource resource = null;
if(!resourceExists(facesContext, "/primefaces/wizard/wizard.css")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/primefaces/wizard/wizard.css");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/jquery/plugins/ui/jquery.ui.button.css")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/jquery/plugins/ui/jquery.ui.button.css");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/jquery/jquery.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/jquery/jquery.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/jquery/plugins/ui/jquery-ui.custom.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/jquery/plugins/ui/jquery-ui.custom.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/primefaces/core/core.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/primefaces/core/core.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/primefaces/wizard/wizard.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/primefaces/wizard/wizard.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
}
public void handleAttribute(String name, Object value) {
List<String> setAttributes = (List<String>) this.getAttributes().get("javax.faces.component.UIComponentBase.attributesThatAreSet");
if(setAttributes == null) {
String cname = this.getClass().getName();
if(cname != null && cname.startsWith(OPTIMIZED_PACKAGE)) {
setAttributes = new ArrayList<String>(6);
this.getAttributes().put("javax.faces.component.UIComponentBase.attributesThatAreSet", setAttributes);
}
}
if(setAttributes != null) {
if(value == null) {
ValueExpression ve = getValueExpression(name);
if(ve == null) {
setAttributes.remove(name);
} else if(!setAttributes.contains(name)) {
setAttributes.add(name);
}
}
}
}
}