/*
* 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.tree;
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.event.NodeSelectEvent;
import org.primefaces.event.NodeExpandEvent;
import org.primefaces.event.NodeCollapseEvent;
import javax.faces.component.UIComponent;
import java.util.Map;
import java.util.HashMap;
import org.primefaces.model.TreeNode;
@ListenerFor(systemEventClass = PostAddToViewEvent.class)
public class Tree extends UIComponentBase implements org.primefaces.component.api.AjaxComponent {
public static final String COMPONENT_TYPE = "org.primefaces.component.Tree";
public static final String COMPONENT_FAMILY = "org.primefaces.component";
private static final String DEFAULT_RENDERER = "org.primefaces.component.TreeRenderer";
private static final String OPTIMIZED_PACKAGE = "org.primefaces.component.";
protected enum PropertyKeys {
widgetVar
,value
,var
,dynamic
,cache
,expandAnim
,collapseAnim
,nodeSelectListener
,nodeExpandListener
,nodeCollapseListener
,onNodeClick
,expanded
,update
,onselectStart
,onselectComplete
,selection
,style
,styleClass
,propagateSelectionUp
,propagateSelectionDown
,selectionMode;
String toString;
PropertyKeys(String toString) {
this.toString = toString;
}
PropertyKeys() {}
public String toString() {
return ((this.toString != null) ? this.toString : super.toString());
}
}
public Tree() {
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.Object getValue() {
return (java.lang.Object) getStateHelper().eval(PropertyKeys.value, null);
}
public void setValue(java.lang.Object _value) {
getStateHelper().put(PropertyKeys.value, _value);
handleAttribute("value", _value);
}
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 boolean isDynamic() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.dynamic, false);
}
public void setDynamic(boolean _dynamic) {
getStateHelper().put(PropertyKeys.dynamic, _dynamic);
handleAttribute("dynamic", _dynamic);
}
public boolean isCache() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.cache, true);
}
public void setCache(boolean _cache) {
getStateHelper().put(PropertyKeys.cache, _cache);
handleAttribute("cache", _cache);
}
public java.lang.String getExpandAnim() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.expandAnim, null);
}
public void setExpandAnim(java.lang.String _expandAnim) {
getStateHelper().put(PropertyKeys.expandAnim, _expandAnim);
handleAttribute("expandAnim", _expandAnim);
}
public java.lang.String getCollapseAnim() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.collapseAnim, null);
}
public void setCollapseAnim(java.lang.String _collapseAnim) {
getStateHelper().put(PropertyKeys.collapseAnim, _collapseAnim);
handleAttribute("collapseAnim", _collapseAnim);
}
public javax.el.MethodExpression getNodeSelectListener() {
return (javax.el.MethodExpression) getStateHelper().eval(PropertyKeys.nodeSelectListener, null);
}
public void setNodeSelectListener(javax.el.MethodExpression _nodeSelectListener) {
getStateHelper().put(PropertyKeys.nodeSelectListener, _nodeSelectListener);
handleAttribute("nodeSelectListener", _nodeSelectListener);
}
public javax.el.MethodExpression getNodeExpandListener() {
return (javax.el.MethodExpression) getStateHelper().eval(PropertyKeys.nodeExpandListener, null);
}
public void setNodeExpandListener(javax.el.MethodExpression _nodeExpandListener) {
getStateHelper().put(PropertyKeys.nodeExpandListener, _nodeExpandListener);
handleAttribute("nodeExpandListener", _nodeExpandListener);
}
public javax.el.MethodExpression getNodeCollapseListener() {
return (javax.el.MethodExpression) getStateHelper().eval(PropertyKeys.nodeCollapseListener, null);
}
public void setNodeCollapseListener(javax.el.MethodExpression _nodeCollapseListener) {
getStateHelper().put(PropertyKeys.nodeCollapseListener, _nodeCollapseListener);
handleAttribute("nodeCollapseListener", _nodeCollapseListener);
}
public java.lang.String getOnNodeClick() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.onNodeClick, null);
}
public void setOnNodeClick(java.lang.String _onNodeClick) {
getStateHelper().put(PropertyKeys.onNodeClick, _onNodeClick);
handleAttribute("onNodeClick", _onNodeClick);
}
public boolean isExpanded() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.expanded, false);
}
public void setExpanded(boolean _expanded) {
getStateHelper().put(PropertyKeys.expanded, _expanded);
handleAttribute("expanded", _expanded);
}
public java.lang.String getUpdate() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.update, null);
}
public void setUpdate(java.lang.String _update) {
getStateHelper().put(PropertyKeys.update, _update);
handleAttribute("update", _update);
}
public java.lang.String getOnselectStart() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.onselectStart, null);
}
public void setOnselectStart(java.lang.String _onselectStart) {
getStateHelper().put(PropertyKeys.onselectStart, _onselectStart);
handleAttribute("onselectStart", _onselectStart);
}
public java.lang.String getOnselectComplete() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.onselectComplete, null);
}
public void setOnselectComplete(java.lang.String _onselectComplete) {
getStateHelper().put(PropertyKeys.onselectComplete, _onselectComplete);
handleAttribute("onselectComplete", _onselectComplete);
}
public java.lang.Object getSelection() {
return (java.lang.Object) getStateHelper().eval(PropertyKeys.selection, null);
}
public void setSelection(java.lang.Object _selection) {
getStateHelper().put(PropertyKeys.selection, _selection);
handleAttribute("selection", _selection);
}
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 boolean isPropagateSelectionUp() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.propagateSelectionUp, false);
}
public void setPropagateSelectionUp(boolean _propagateSelectionUp) {
getStateHelper().put(PropertyKeys.propagateSelectionUp, _propagateSelectionUp);
handleAttribute("propagateSelectionUp", _propagateSelectionUp);
}
public boolean isPropagateSelectionDown() {
return (java.lang.Boolean) getStateHelper().eval(PropertyKeys.propagateSelectionDown, false);
}
public void setPropagateSelectionDown(boolean _propagateSelectionDown) {
getStateHelper().put(PropertyKeys.propagateSelectionDown, _propagateSelectionDown);
handleAttribute("propagateSelectionDown", _propagateSelectionDown);
}
public java.lang.String getSelectionMode() {
return (java.lang.String) getStateHelper().eval(PropertyKeys.selectionMode, null);
}
public void setSelectionMode(java.lang.String _selectionMode) {
getStateHelper().put(PropertyKeys.selectionMode, _selectionMode);
handleAttribute("selectionMode", _selectionMode);
}
private Map<String,UITreeNode> nodes;
public void broadcast(javax.faces.event.FacesEvent event) throws javax.faces.event.AbortProcessingException {
super.broadcast(event);
FacesContext facesContext = FacesContext.getCurrentInstance();
MethodExpression me = null;
if(event instanceof NodeSelectEvent) {
me = getNodeSelectListener();
} else if(event instanceof NodeExpandEvent) {
me = getNodeExpandListener();
} else if(event instanceof NodeCollapseEvent) {
me = getNodeCollapseListener();
}
if (me != null) {
me.invoke(facesContext.getELContext(), new Object[] {event});
}
}
public UITreeNode getUITreeNodeByType(String type) {
if(nodes == null) {
nodes = new HashMap<String,UITreeNode>();
for(UIComponent child : getChildren()) {
UITreeNode uiTreeNode = (UITreeNode) child;
nodes.put(uiTreeNode.getType(), uiTreeNode);
}
}
UITreeNode node = nodes.get(type);
if(node == null)
throw new javax.faces.FacesException("Unsupported tree node type:" + type);
else
return node;
}
public void processUpdates(FacesContext context) {
super.processUpdates(context);
Object selection = this.getSelection();
if(selection != null) {
this.getValueExpression("selection").setValue(context.getELContext(), selection);
setSelection(null);
}
}
public boolean hasAjaxListener() {
return getNodeSelectListener() != null || getNodeExpandListener() != null || getNodeCollapseListener() != null;
}
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, "/yui/treeview/assets/skins/sam/treeview.css")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/yui/treeview/assets/skins/sam/treeview.css");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/yui/utilities/utilities.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/yui/utilities/utilities.js");
viewroot.addComponentResource(facesContext, resource, "head");
}
if(!resourceExists(facesContext, "/yui/treeview/treeview-min.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/yui/treeview/treeview-min.js");
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, "/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/treeview/treeview.js")) {
resource = (Resource) facesContext.getApplication().createComponent("org.primefaces.component.Resource");
resource.setName("/primefaces/treeview/treeview.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);
}
}
}
}
}