/*
* 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.autocomplete;
import javax.faces.component.UIInput;
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;
@ListenerFor(systemEventClass = PostAddToViewEvent.class)
public class AutoComplete extends UIInput implements org.primefaces.component.api.AjaxComponent {
public static final String COMPONENT_TYPE = "org.primefaces.component.AutoComplete";
public static final String COMPONENT_FAMILY = "org.primefaces.component";
private static final String DEFAULT_RENDERER = "org.primefaces.component.AutoCompleteRenderer";
private static final String OPTIMIZED_PACKAGE = "org.primefaces.component.";
protected enum PropertyKeys {
widgetVar
,completeMethod
,var
,itemLabel
,itemValue
,maxResults
,minQueryLength
,queryDelay
,forceSelection
,selectListener
,onSelectUpdate
,onstart
,oncomplete
,disabled;
String toString;
PropertyKeys(String toString) {
this.toString = toString;
}
PropertyKeys() {}
public String toString() {
return ((this.toString != null) ? this.toString : super.toString());
}
}
public AutoComplete() {
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 javax.el.MethodExpression getCompleteMethod() {
return (javax.el.MethodExpression) getStateHelper().eval(PropertyKeys.completeMethod, null);
}
public void setCompleteMethod(javax.el.MethodExpression _completeMethod) {
getStateHelper().put(PropertyKeys.completeMethod, _completeMethod);
handleAttribute("completeMethod", _completeMethod);
}
public java.lang.String getVar() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.var, null);
}
public void setVar(java.lang.String _var) {
getStateHelper().put(PropertyKeys.var, _var);
handleAttribute("var", _var);
}
public java.lang.String getItemLabel() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.itemLabel, null);
}
public void setItemLabel(java.lang.String _itemLabel) {
getStateHelper().put(PropertyKeys.itemLabel, _itemLabel);
handleAttribute("itemLabel", _itemLabel);
}
public java.lang.Object getItemValue() {
return (java.lang.Object) getStateHelper().eval(PropertyKeys.itemValue, null);
}
public void setItemValue(java.lang.Object _itemValue) {
getStateHelper().put(PropertyKeys.itemValue, _itemValue);
handleAttribute("itemValue", _itemValue);
}
public int getMaxResults() {
return (java.lang.Integer) getStateHelper().eval(PropertyKeys.maxResults, 10);
}
public void setMaxResults(int _maxResults) {
getStateHelper().put(PropertyKeys.maxResults, _maxResults);
handleAttribute("maxResults", _maxResults);
}
public int getMinQueryLength() {
return (java.lang.Integer) getStateHelper().eval(PropertyKeys.minQueryLength, 1);
}
public void setMinQueryLength(int _minQueryLength) {
getStateHelper().put(PropertyKeys.minQueryLength, _minQueryLength);
handleAttribute("minQueryLength", _minQueryLength);
}
public int getQueryDelay() {
return (java.lang.Integer) getStateHelper().eval(PropertyKeys.queryDelay, 300);
}
public void setQueryDelay(int _queryDelay) {
getStateHelper().put(PropertyKeys.queryDelay, _queryDelay);
handleAttribute("queryDelay", _queryDelay);
}
public boolean isForceSelection() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.forceSelection, false);
}
public void setForceSelection(boolean _forceSelection) {
getStateHelper().put(PropertyKeys.forceSelection, _forceSelection);
handleAttribute("forceSelection", _forceSelection);
}
public javax.el.MethodExpression getSelectListener() {
return (javax.el.MethodExpression) getStateHelper().eval(PropertyKeys.selectListener, null);
}
public void setSelectListener(javax.el.MethodExpression _selectListener) {
getStateHelper().put(PropertyKeys.selectListener, _selectListener);
handleAttribute("selectListener", _selectListener);
}
public java.lang.String getOnSelectUpdate() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.onSelectUpdate, null);
}
public void setOnSelectUpdate(java.lang.String _onSelectUpdate) {
getStateHelper().put(PropertyKeys.onSelectUpdate, _onSelectUpdate);
handleAttribute("onSelectUpdate", _onSelectUpdate);
}
public java.lang.String getOnstart() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.onstart, null);
}
public void setOnstart(java.lang.String _onstart) {
getStateHelper().put(PropertyKeys.onstart, _onstart);
handleAttribute("onstart", _onstart);
}
public java.lang.String getOncomplete() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.oncomplete, null);
}
public void setOncomplete(java.lang.String _oncomplete) {
getStateHelper().put(PropertyKeys.oncomplete, _oncomplete);
handleAttribute("oncomplete", _oncomplete);
}
public boolean isDisabled() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.disabled, false);
}
public void setDisabled(boolean _disabled) {
getStateHelper().put(PropertyKeys.disabled, _disabled);
handleAttribute("disabled", _disabled);
}
public void broadcast(javax.faces.event.FacesEvent event) throws javax.faces.event.AbortProcessingException {
super.broadcast(event);
FacesContext facesContext = FacesContext.getCurrentInstance();
MethodExpression me = getSelectListener();
if (me != null && event instanceof org.primefaces.event.SelectEvent) {
me.invoke(facesContext.getELContext(), new Object[] {event});
}
}
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, "/jquery/plugins/ui/jquery.ui.autocomplete.css")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/jquery/plugins/ui/jquery.ui.autocomplete.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/autocomplete/autocomplete.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/primefaces/autocomplete/autocomplete.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);
}
}
}
}
}