problem making treeSelector custom composite component
dashti Jul 15, 2008 8:47 AMDear friends:
I'm trying to make a component like rich:Calendar but this time, instead of selecting a dare, we want to select an item from a tree.
I want to do this by reusing richfaces components.
I only put here the main part of code.
every thing is ok, but at the end tree doesn't work in ajax mode!!!
please help me !
here is my code:
HtmlTreeSelector.java
package com.cybermehr.component.html;
import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import javax.el.ELException;
import javax.el.ValueExpression;
import javax.faces.FacesException;
import javax.faces.application.Application;
import javax.faces.component.html.HtmlGraphicImage;
import javax.faces.component.html.HtmlInputText;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
import org.richfaces.component.UITree;
import org.richfaces.component.html.HtmlTree;
import org.richfaces.component.html.HtmlTreeNode;
import org.richfaces.component.state.TreeStateAdvisor;
import org.richfaces.model.TreeNode;
import com.cybermehr.component.UITreeSelector;
import com.cybermehr.helper.datastructure.TreeData;
import com.cybermehr.helper.datastructure.TreeHierarchy;
public class HtmlTreeSelector extends UITreeSelector
{
private boolean _ajaxSingle;
private boolean _ajaxSingleSet;
private boolean _bypassUpdates;
private boolean _bypassUpdatesSet;
private Object _data;
private String _eventsQueue;
private String _focus;
private String _icon;
private String _iconCollapsed;
private String _iconExpanded;
private String _iconLeaf;
private boolean _ignoreDupResponses;
private boolean _ignoreDupResponsesSet;
private boolean _limitToList;
private boolean _limitToListSet;
private String _nodeFace;
private String _onbeforedomupdate;
private String _oncomplete;
private String _ondblclick;
private Object _process;
private Object _reRender;
private int _requestDelay;
private boolean _requestDelaySet;
private String _status;
private String _switchType;
private int _timeout;
private boolean _timeoutSet;
private boolean _toggleOnClick;
private boolean _toggleOnClickSet;
private String _buttonClass;
private String _buttonIcon;
private String _inputClass;
private Integer _inputSize;
private String _inputStyle;
private String _layout;
private Object _roots;
private HtmlInputText inputBox;
private HtmlGraphicImage imageButton;
private HtmlTree insiderTree;
//private HtmlRecursiveTreeNodesAdaptor nodeAdaptor;
private HtmlTreeNode treeNode;
private HtmlOutputText treeNodeLabel;
public HtmlTreeSelector()
{
super();
Application app = FacesContext.getCurrentInstance().getApplication();
_buttonClass = null;
_buttonIcon = null;
_inputClass = null;
_inputSize = null;
_inputStyle = null;
_layout = null;
_roots = null;
if(getChildCount() == 0) {
setInputBox((HtmlInputText)app.createComponent(HtmlInputText.COMPONENT_TYPE));
setImageButton((HtmlGraphicImage) app.createComponent(HtmlGraphicImage.COMPONENT_TYPE));
setInsiderTree((HtmlTree) app.createComponent(HtmlTree.COMPONENT_TYPE));
// setNodeAdaptor((HtmlRecursiveTreeNodesAdaptor) app.createComponent(HtmlRecursiveTreeNodesAdaptor.COMPONENT_TYPE));
setTreeNode((HtmlTreeNode) app.createComponent(HtmlTreeNode.COMPONENT_TYPE));
setTreeNodeLabel((HtmlOutputText) app.createComponent(HtmlOutputText.COMPONENT_TYPE));
getTreeNode().getChildren().add(getTreeNodeLabel());
// getNodeAdaptor().getChildren().add(getTreeNode());
// getInsiderTree().getChildren().add(getNodeAdaptor());
getInsiderTree().getChildren().add(getTreeNode());
// getChildren().add(getInputBox());
// getChildren().add(getImageButton());
getChildren().add(getInsiderTree());
setTransient(false);
}
}
public boolean isAjaxSingle()
{
if(_ajaxSingleSet)
return _ajaxSingle;
ValueExpression ve = getValueExpression("ajaxSingle");
if(ve != null)
{
Boolean value = null;
try
{
value = (Boolean)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
if(null == value)
return _ajaxSingle;
else
return value.booleanValue();
} else
{
return _ajaxSingle;
}
}
public void setAjaxSingle(boolean _ajaxSingle)
{
this._ajaxSingle = _ajaxSingle;
_ajaxSingleSet = true;
}
public boolean isBypassUpdates()
{
if(_bypassUpdatesSet)
return _bypassUpdates;
ValueExpression ve = getValueExpression("bypassUpdates");
if(ve != null)
{
Boolean value = null;
try
{
value = (Boolean)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
if(null == value)
return _bypassUpdates;
else
return value.booleanValue();
} else
{
return _bypassUpdates;
}
}
public void setBypassUpdates(boolean _bypassUpdates)
{
this._bypassUpdates = _bypassUpdates;
_bypassUpdatesSet = true;
}
public Object getData()
{
if(_data != null)
return _data;
ValueExpression ve = getValueExpression("data");
if(ve != null)
{
Object value = null;
try
{
value = ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return null;
}
}
public void setData(Object _data)
{
this._data = _data;
}
public String getEventsQueue()
{
if(_eventsQueue != null)
return _eventsQueue;
ValueExpression ve = getValueExpression("eventsQueue");
if(ve != null)
{
String value = null;
try
{
value = (String)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return null;
}
}
public void setEventsQueue(String _eventsQueue)
{
this._eventsQueue = _eventsQueue;
}
public String getFocus()
{
if(_focus != null)
return _focus;
ValueExpression ve = getValueExpression("focus");
if(ve != null)
{
String value = null;
try
{
value = (String)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return null;
}
}
public void setFocus(String _focus)
{
this._focus = _focus;
}
public String getIcon()
{
if(_icon != null)
return _icon;
ValueExpression ve = getValueExpression("icon");
if(ve != null)
{
String value = null;
try
{
value = (String)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return null;
}
}
public void setIcon(String _icon)
{
this._icon = _icon;
}
public String getIconCollapsed()
{
if(_iconCollapsed != null)
return _iconCollapsed;
ValueExpression ve = getValueExpression("iconCollapsed");
if(ve != null)
{
String value = null;
try
{
value = (String)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return null;
}
}
public void setIconCollapsed(String _iconCollapsed)
{
this._iconCollapsed = _iconCollapsed;
}
public String getIconExpanded()
{
if(_iconExpanded != null)
return _iconExpanded;
ValueExpression ve = getValueExpression("iconExpanded");
if(ve != null)
{
String value = null;
try
{
value = (String)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return null;
}
}
public void setIconExpanded(String _iconExpanded)
{
this._iconExpanded = _iconExpanded;
}
public String getIconLeaf()
{
if(_iconLeaf != null)
return _iconLeaf;
ValueExpression ve = getValueExpression("iconLeaf");
if(ve != null)
{
String value = null;
try
{
value = (String)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return null;
}
}
public void setIconLeaf(String _iconLeaf)
{
this._iconLeaf = _iconLeaf;
}
public boolean isIgnoreDupResponses()
{
if(_ignoreDupResponsesSet)
return _ignoreDupResponses;
ValueExpression ve = getValueExpression("ignoreDupResponses");
if(ve != null)
{
Boolean value = null;
try
{
value = (Boolean)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
if(null == value)
return _ignoreDupResponses;
else
return value.booleanValue();
} else
{
return _ignoreDupResponses;
}
}
public void setIgnoreDupResponses(boolean _ignoreDupResponses)
{
this._ignoreDupResponses = _ignoreDupResponses;
_ignoreDupResponsesSet = true;
}
public boolean isLimitToList()
{
if(_limitToListSet)
return _limitToList;
ValueExpression ve = getValueExpression("limitToList");
if(ve != null)
{
Boolean value = null;
try
{
value = (Boolean)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
if(null == value)
return _limitToList;
else
return value.booleanValue();
} else
{
return _limitToList;
}
}
public void setLimitToList(boolean _limitToList)
{
this._limitToList = _limitToList;
_limitToListSet = true;
}
public String getNodeFace()
{
if(_nodeFace != null)
return _nodeFace;
ValueExpression ve = getValueExpression("nodeFace");
if(ve != null)
{
String value = null;
try
{
value = (String)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return null;
}
}
public void setNodeFace(String _nodeFace)
{
this._nodeFace = _nodeFace;
}
public String getOnbeforedomupdate()
{
if(_onbeforedomupdate != null)
return _onbeforedomupdate;
ValueExpression ve = getValueExpression("onbeforedomupdate");
if(ve != null)
{
String value = null;
try
{
value = (String)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return null;
}
}
public void setOnbeforedomupdate(String _onbeforedomupdate)
{
this._onbeforedomupdate = _onbeforedomupdate;
}
public String getOncomplete()
{
if(_oncomplete != null)
return _oncomplete;
ValueExpression ve = getValueExpression("oncomplete");
if(ve != null)
{
String value = null;
try
{
value = (String)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return null;
}
}
public void setOncomplete(String _oncomplete)
{
this._oncomplete = _oncomplete;
}
public String getOndblclick()
{
if(_ondblclick != null)
return _ondblclick;
ValueExpression ve = getValueExpression("ondblclick");
if(ve != null)
{
String value = null;
try
{
value = (String)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return null;
}
}
public Object getProcess()
{
if(_process != null)
return _process;
ValueExpression ve = getValueExpression("process");
if(ve != null)
{
Object value = null;
try
{
value = ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return null;
}
}
public void setProcess(Object _process)
{
this._process = _process;
}
public Object getReRender()
{
if(_reRender != null)
return _reRender;
ValueExpression ve = getValueExpression("reRender");
if(ve != null)
{
Object value = null;
try
{
value = ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return null;
}
}
public void setReRender(Object _reRender)
{
this._reRender = _reRender;
}
public int getRequestDelay()
{
if(_requestDelaySet)
return _requestDelay;
ValueExpression ve = getValueExpression("requestDelay");
if(ve != null)
{
Integer value = null;
try
{
value = (Integer)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
if(null == value)
return _requestDelay;
else
return value.intValue();
} else
{
return _requestDelay;
}
}
public void setRequestDelay(int _requestDelay)
{
this._requestDelay = _requestDelay;
_requestDelaySet = true;
}
public String getStatus()
{
if(_status != null)
return _status;
ValueExpression ve = getValueExpression("status");
if(ve != null)
{
String value = null;
try
{
value = (String)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return null;
}
}
public void setStatus(String _status)
{
this._status = _status;
}
public String getSwitchType()
{
if(_switchType != null)
return _switchType;
ValueExpression ve = getValueExpression("switchType");
if(ve != null)
{
String value = null;
try
{
value = (String)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return "ajax";
}
}
public void setSwitchType(String _switchType)
{
this._switchType = _switchType;
}
public int getTimeout()
{
if(_timeoutSet)
return _timeout;
ValueExpression ve = getValueExpression("timeout");
if(ve != null)
{
Integer value = null;
try
{
value = (Integer)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
if(null == value)
return _timeout;
else
return value.intValue();
} else
{
return _timeout;
}
}
public void setTimeout(int _timeout)
{
this._timeout = _timeout;
_timeoutSet = true;
}
public boolean isToggleOnClick()
{
if(_toggleOnClickSet)
return _toggleOnClick;
ValueExpression ve = getValueExpression("toggleOnClick");
if(ve != null)
{
Boolean value = null;
try
{
value = (Boolean)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
if(null == value)
return _toggleOnClick;
else
return value.booleanValue();
} else
{
return _toggleOnClick;
}
}
public void setToggleOnClick(boolean _toggleOnClick)
{
this._toggleOnClick = _toggleOnClick;
_toggleOnClickSet = true;
}
public String getButtonClass()
{
if(_buttonClass != null)
return _buttonClass;
ValueExpression ve = getValueExpression("buttonClass");
if(ve != null)
{
String value = null;
try
{
value = (String)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return null;
}
}
public void setButtonClass(String _buttonClass)
{
this._buttonClass = _buttonClass;
}
public String getButtonIcon()
{
if(_buttonIcon != null)
return _buttonIcon;
ValueExpression ve = getValueExpression("buttonIcon");
if(ve != null)
{
String value = null;
try
{
value = (String)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return null;
}
}
public void setButtonIcon(String _buttonIcon)
{
this._buttonIcon = _buttonIcon;
}
public String getInputClass()
{
if(_inputClass != null)
return _inputClass;
ValueExpression ve = getValueExpression("inputClass");
if(ve != null)
{
String value = null;
try
{
value = (String)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return null;
}
}
public void setInputClass(String _inputClass)
{
this._inputClass = _inputClass;
}
public int getInputSize()
{
if(_inputSize != null)
return _inputSize;
ValueExpression ve = getValueExpression("inputSize");
if(ve != null)
{
Integer value = null;
try
{
value = (Integer)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
if(null == value)
return _inputSize;
else
return value.intValue();
} else
{
return _inputSize;
}
}
public void setInputSize(int _inputSize)
{
this._inputSize = _inputSize;
}
public String getInputStyle()
{
if(_inputStyle != null)
return _inputStyle;
ValueExpression ve = getValueExpression("inputStyle");
if(ve != null)
{
String value = null;
try
{
value = (String)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return null;
}
}
public void setLayout(String _layout)
{
this._layout = _layout;
}
public String getLayout()
{
if(_layout != null)
return _layout;
ValueExpression ve = getValueExpression("layout");
if(ve != null)
{
String value = null;
try
{
value = (String)ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return null;
}
}
@Override
public Object getRoots()
{
if(_roots != null)
return _roots;
ValueExpression ve = getValueExpression("roots");
if(ve != null)
{
Object value = null;
try
{
value = ve.getValue(getFacesContext().getELContext());
}
catch(ELException e)
{
throw new FacesException(e);
}
return value;
} else
{
return null;
}
}
public void setRoots(Object _roots)
{
this._roots = _roots;
}
public void setInputStyle(String _inputStyle)
{
this._inputStyle = _inputStyle;
}
@Override
public String getFamily()
{
return UITreeSelector.COMPONENT_FAMILY;
}
public HtmlInputText getInputBox() {
return this.inputBox;
}
public void setInputBox(HtmlInputText inputBox) {
this.inputBox = inputBox;
}
public HtmlGraphicImage getImageButton() {
return this.imageButton;
}
public void setImageButton(HtmlGraphicImage imageButton) {
this.imageButton = imageButton;
}
public HtmlTree getInsiderTree() {
return this.insiderTree;
}
public void setInsiderTree(HtmlTree insiderTree) {
this.insiderTree = insiderTree;
}
// public HtmlRecursiveTreeNodesAdaptor getNodeAdaptor() {
// return this.nodeAdaptor;
// }
// public void setNodeAdaptor(HtmlRecursiveTreeNodesAdaptor nodeAdaptor) {
// this.nodeAdaptor = nodeAdaptor;
// }
public HtmlTreeNode getTreeNode() {
return this.treeNode;
}
public void setTreeNode(HtmlTreeNode treeNode) {
this.treeNode = treeNode;
}
public HtmlOutputText getTreeNodeLabel() {
return this.treeNodeLabel;
}
public void setTreeNodeLabel(HtmlOutputText treeNodeLabel) {
this.treeNodeLabel = treeNodeLabel;
}
@Override
public void encodeBegin(FacesContext context) throws IOException {
if(!isRendered()) {
return;
}
Application app = context.getApplication();
Long value = (Long) getValue();
TreeNode<TreeData> roots = (TreeNode<TreeData>)getRoots();
TreeNode<TreeData> selectedValue = TreeHierarchy.findTreeNode(roots, value);
Object currentInputTextValue = "";
if(selectedValue != null) {
currentInputTextValue = selectedValue.getData().getLabel();
}
getInputBox().setReadonly(true);
getInputBox().setValue(currentInputTextValue);
getImageButton().setStyle("vertical-align: middle;cursor: pointer;");
getImageButton().setStyleClass((new StringBuilder()).append("cyber-tree-button ").append(convertToString(getAttributes().get("buttonClass"))).toString());
String buttonIcon = (String) getAttributes().get("buttonIcon");
getImageButton().setValue(buttonIcon);
getInsiderTree().setSwitchType("ajax");
getInsiderTree().setAjaxSubmitSelection(true);
System.out.println("salam16");
getInsiderTree().setValue(getRoots());
getInsiderTree().setVar("item");
getTreeNodeLabel().setValueExpression("value", app.getExpressionFactory().createValueExpression(context.getELContext(),"#{item.label}", java.lang.String.class));
super.encodeBegin(context);
}
@Override
public Object saveState(FacesContext context)
{
Object state[] = new Object[8];
state[0] = super.saveState(context);
state[1] = _buttonClass;
state[2] = _buttonIcon;
state[3] = _inputClass;
state[4] = _inputSize;
state[5] = _inputStyle;
state[6] = _layout;
state[7] = _roots;
return state;
}
@Override
public void restoreState(FacesContext context, Object state)
{
Object states[] = (Object[])state;
super.restoreState(context, states[0]);
_buttonClass = (String)states[1];
_buttonIcon = (String)states[2];
_inputClass = (String)states[3];
_inputSize = (Integer)states[4];
_inputStyle = (String)states[5];
_layout = (String)states[6];
_roots = states[7];
}
@Override
public Object processSaveState(FacesContext context) {
Object superState = super.processSaveState(context);
return new Object[] {superState, new Integer(getChildCount())};
}
@Override
public void processRestoreState(FacesContext context, Object state) {
Object[] values = (Object[]) state;
Integer savedChildCount = (Integer) values[1];
for (int i = getChildCount() - savedChildCount.intValue(); i > 0; i--) {
getChildren().remove(0);
}
super.processRestoreState(context, values[0]);
}
private String convertToString(Object obj)
{
return obj != null ? obj.toString() : "";
}
private String convertToString(boolean b)
{
return String.valueOf(b);
}
private String convertToString(int b)
{
return b == 0x80000000 ? "" : String.valueOf(b);
}
private String convertToString(long b)
{
return b == 0x8000000000000000L ? "" : String.valueOf(b);
}
private boolean isEmpty(Object o)
{
if(null == o)
return true;
if(o instanceof String)
return 0 == ((String)o).length();
if(o instanceof Collection)
return 0 == ((Collection)o).size();
if(o instanceof Map)
return 0 == ((Map)o).size();
if(o.getClass().isArray())
return 0 == ((Object[])o).length;
else
return false;
}
private TreeStateAdvisor getMyStateAdvisor() {
final Object val = getValue();
return new TreeStateAdvisor() {
@Override
public Boolean adviseNodeOpened(UITree tree) {
TreeData node = (TreeData) tree.getRowData(tree.getRowKey());
if(node != null && node.getValue().equals(val)) {
return Boolean.TRUE;
}
return null;
}
@Override
public Boolean adviseNodeSelected(UITree tree) {
TreeData node = (TreeData) tree.getRowData(tree.getRowKey());
if(node != null && node.getValue().equals(val)) {
return Boolean.TRUE;
}
return null;
}
};
}
}