5 Replies Latest reply on Apr 12, 2011 11:13 AM by Anuj Kumar

    Couldn't remove a selected node from tree

    Anuj Kumar Newbie

      Hi All,

       

      I am going through the example of rich:tree for RichFaces 4, described here: http://richfaces-showcase.appspot.com/richfaces/component-sample.jsf?demo=tree&skin=blueSky

       

      I have created a selection listener as described in the source code given in the above example. Now, I want to remove the selected node. I have added a button to remove the selected node and render the tree. In the action listener, I remove the selected node from the list of nodes that is the value of the rich:tree.

       

      If I remove the node from the selected list, in the rendering phase it gives this exception-

       

      java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

              at java.util.ArrayList.RangeCheck(Unknown Source)

              at java.util.ArrayList.get(Unknown Source)

              at com.google.common.collect.Iterables.get(Iterables.java:634)

              at org.richfaces.model.SwingTreeNodeImpl.getChildAt(SwingTreeNodeImpl.java:70)

              at org.richfaces.model.SwingTreeNodeDataModelImpl.findChild(SwingTreeNodeDataModelImpl.java:

      77)

              at org.richfaces.model.SwingTreeNodeDataModelImpl.setupChildContext(SwingTreeNodeDataModelIm

      pl.java:95)

              at org.richfaces.model.SwingTreeNodeDataModelImpl.setupChildContext(SwingTreeNodeDataModelIm

      pl.java:40)

              at org.richfaces.model.NodesTreeSequenceKeyModel.setupKey(NodesTreeSequenceKeyModel.java:47)

       

       

              at org.richfaces.model.TreeSequenceKeyModel.setRowKey(TreeSequenceKeyModel.java:49)

              at org.richfaces.component.UIDataAdaptor.setRowKey(UIDataAdaptor.java:277)

              at org.richfaces.renderkit.TreeRendererBase.encodeSelectionStateInput(TreeRendererBase.java:

      131)

              at org.richfaces.renderkit.html.TreeRenderer.encodeEnd(TreeRenderer.java:145)

              at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:883)

              at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1659)

              at org.richfaces.renderkit.RendererBase.renderChildren(RendererBase.java:278)

              at org.richfaces.renderkit.html.PanelRenderer.encodeEnd(PanelRenderer.java:181)

              at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:883)

              at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.ja

      va:312)

              at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)

              at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)

              at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:853)

              at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1652)

              at org.richfaces.renderkit.RendererBase.renderChildren(RendererBase.java:278)

              at org.richfaces.renderkit.html.PanelRenderer.encodeEnd(PanelRenderer.java:181)

              at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:883)

              at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.ja

      va:312)

              at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)

              at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)

              at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:853)

              at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1652)

              at javax.faces.render.Renderer.encodeChildren(Renderer.java:168)

              at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:853)

              at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1652)

              at org.richfaces.renderkit.RendererBase.renderChildren(RendererBase.java:278)

              at org.richfaces.renderkit.html.CollapsiblePanelRenderer.encodeContent(CollapsiblePanelRende

      rer.java:215)

              at org.richfaces.renderkit.html.CollapsiblePanelRenderer.encodeContentChild(CollapsiblePanel

      Renderer.java:161)

              at org.richfaces.renderkit.html.CollapsiblePanelRenderer.doEncodeChildren(CollapsiblePanelRe

      nderer.java:155)

              at org.richfaces.renderkit.RendererBase.encodeChildren(RendererBase.java:157)

              at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:853)

              at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.ja

      va:304)

              at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)

              at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)

              at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:853)

              at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1652)

              at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1655)

              at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandling

      Strategy.java:399)

              at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)

              at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:273)

              at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)

              at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)

              at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)

              at javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)

              at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)

              at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:367)

              at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)

              at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)

              at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:712)

              at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)

              at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:268)

              at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:126)

              at org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:791)

              at org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:758)

              at org.apache.jsp.index_jsp._jspService(org.apache.jsp.index_jsp:51)

              at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:80)

              at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)

              at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:373)

              at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:464)

              at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:358)

              at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)

              at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)

              at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:367)

              at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)

              at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)

              at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:712)

              at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)

              at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:268)

              at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:126)

              at org.mortbay.jetty.servlet.DefaultServlet.doGet(DefaultServlet.java:431)

              at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)

              at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)

              at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)

              at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:367)

              at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)

              at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)

              at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:712)

              at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)

              at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:2

      11)

              at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)

              at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139)

              at org.mortbay.jetty.Server.handle(Server.java:285)

              at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:502)

              at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:821)

              at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:513)

              at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:208)

              at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:378)

              at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:226)

              at org.mortbay.thread.BoundedThreadPool$PoolThread.run(BoundedThreadPool.java:442)

       

      It looks for the row key that is set in the selection listener. If that row key is no longer present, it gives this exception. The code is (as mentioned in the rich:tree example)-

       

      public void selectionChanged(TreeSelectionChangeEvent selectionChangeEvent) {

              // considering only single selection

              List<Object> selection = new ArrayList<Object>(selectionChangeEvent.getNewSelection());

              Object currentSelectionKey = selection.get(0);

              UITree tree = (UITree) selectionChangeEvent.getSource();

       

              Object storedKey = tree.getRowKey();

              tree.setRowKey(currentSelectionKey);

              currentSelection = (TreeNode) tree.getRowData();

              tree.setRowKey(storedKey);

          }

       

      So, we set the row key in this selection listener and if I delete the same node, that row key no longer exists. I understand the issue but I am not sure about the right way to delete the selected node and how to refresh the model or clear the selected row key.

       

      Any suggestion will be of great help.

       

      Thanks,

      Anuj