1 Reply Latest reply on Jan 27, 2009 5:22 PM by judy guglielmin

    Navigation Tree refreshing issue

    Govind Mishra Newbie
      package org.web.action;

      import javax.faces.context.FacesContext;
      import javax.faces.event.ActionEvent;
      import javax.swing.tree.DefaultMutableTreeNode;
      import javax.swing.tree.DefaultTreeModel;

      import org.navigation.NavigationBean;
      import org.web.navigation.TreeNodeListener;
      import org.web.service.Service;
      import org.service.ServiceException;

      import org.jboss.seam.ScopeType;
      import org.jboss.seam.annotations.Begin;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Scope;
      import org.jboss.seam.annotations.Unwrap;
      import org.jboss.seam.faces.FacesMessages;

      import com.icesoft.faces.async.render.RenderManager;
      import com.icesoft.faces.async.render.Renderable;
      import com.icesoft.faces.webapp.xmlhttp.RenderingException;
      /*
      * For Tree
      */
      @Scope(ScopeType.CONVERSATION)
      @Name("tree")
      public class Tree implements Serializable,TreeNodeListener,Renderable {

           //private IntervalRenderer ir;
           
           private PersistentFacesState state = PersistentFacesState.getInstance();
           
           private static final long serialVersionUID = 1L;
           
           @In
           private RenderManager renderManager;
           
           @In
           protected FacesMessages facesMessages;
           
           public static final String TREE_ROOT_NAME = "trees";

           private DefaultTreeModel tree;
           
           int i=0;
           
           @In("#{service}")
           private Service service;
           
           @In("#{navigationBean}")
           private NavigationBean navigationBean;
           
           @In("#{identity.token.user}")
           private String loggedInUser;
           
           private String sessionId;

           @Unwrap
           @Begin(join=true)
           public DefaultTreeModel getTree() {
                
                state = PersistentFacesState.getInstance();
                
                if (tree == null) {
                     tree = new DefaultTreeModel(getTreeRoot());
                     List<Test> tests = null;
                     try
                     {
                          tests = service.getTests();
                          Collections.sort(tests,Test.getComparator("urName", true));
                     } catch(ServiceException e){
                          facesMessages.add(StatusMessage.Severity.ERROR," Service Error: {0}", e.getMessage());
                          tests = new ArrayList<Test>();
                          tests.add(new Realm(new User(loggedInUser,null,null)));// Let's add the user's realm to the list.
                     }
                     if (tests != null) {
                          for (Test test : tests) {
                               DefaultMutableTreeNode branchNode = new DefaultMutableTreeNode();
                               UserObject branchObject = new UserObject(branchNode,test,navigationBean);
                               //Let's set selected test to loggedInUser                         
                               if(loggedInUser.equalsIgnoreCase(test.getUser().getUrName())){
                                    navigationBean.setSelectedUserObject(branchObject);
                               }
                               branchObject.setLeaf(true);
                               branchNode.setUserObject(branchObject);
                               ((DefaultMutableTreeNode) tree.getRoot()).add(branchNode);
                          }
                          //register for navigation event.          
                          navigationBean.addNavigationListener(this);
                }
                
           
                return tree;
           }

           public PersistentFacesState getState() {
                return state;
                }
           
           private  DefaultMutableTreeNode getTreeRoot() {
                Test root = new Test();
                root.setUser(new User());
                root.getUser.setUrName(TREE_ROOT_NAME);
                DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode();
                UserObject rootObject = new UserObject(rootTreeNode,root,navigationBean);
                rootObject.setExpanded(true);
                rootTreeNode.setUserObject(rootObject);
                return rootTreeNode;
           }
           
           
            public void renderingException( RenderingException re) {
                 facesMessages.add("Interval Render Stopped");
           //      cleanup();
               }

      /*       private void cleanup() {
                   if (ir != null) {
                       ir.remove(this);
                       if (ir.isEmpty() ) {
                            facesMessages.add("Interval Render Stopped");
                           ir.requestStop();
                       }
                   }
               }
      */
                
           /**
            * Reset's the selected variable of a UserObject.
            * @param node
            */
           private void resetTreeNodeSelection(DefaultMutableTreeNode node){
                UserObject root = (UserObject)node.getUserObject();
                root.getTest().setSelected(false);
                Enumeration enumer = node.children();
                while (enumer.hasMoreElements()) {
                     DefaultMutableTreeNode childNode = (DefaultMutableTreeNode) enumer.nextElement();
                     resetTreeNodeSelection(childNode);
                }
           }
           
           /**
            * Reset the selected flag of all tree nodes.
            */
           public void handleTestSelected(Test selectedTest) {          
                resetTreeNodeSelection((DefaultMutableTreeNode) tree.getRoot());
                
           }

           /**
            * @return the renderManager
            */
           public RenderManager getRenderManager() {
                return renderManager;
           }

           /**
            * @param renderManager the renderManager to set
            */
           @In
           public void setRenderManager(RenderManager renderManager) {
                state = PersistentFacesState.getInstance();
           //     state.addViewListener(this );
      /*       FacesContext fc = FacesContext.getCurrentInstance();
              synchronous = (String) fc.getExternalContext().getInitParameterMap().
                      get( "com.icesoft.faces.synchronousUpdate" );
      is "+synchronous);
      */      
              i++;
                sessionId = FacesContext.getCurrentInstance().getExternalContext().getSession(false).toString();
                System.out.println("session Id is "+sessionId);
                if (i<2)
                renderManager.getOnDemandRenderer(sessionId).requestRender();
          }

           }
      }





      by clicking on Tree Node, Popup menu displays from where we can add or remove Tree node. its working fine but after adding or removing Tree node navigation tree should refresh. So please help me or suggest me what I can do for Tree refreshing.

      If u need any clarification please write me on mishragovindi@gmail.com

      Advance Thanks,
      Govind