Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Tree does not show its updates in TreeGrid

    Hi,
    My TreeGrid displays well when its data is set at init time, the first time - but if I update the Tree content (tree.setData(TreeNode[])) later, the treegrid is not refreshed even if I do again a treegrid.setData(tree) or treegrid.redraw().

    I have a basic listgrid, when the user clicks on a row, a tree is built and the treegrid must be refreshed.

    I've tested with my own data and also the static employeeData exemple to make sure data is OK.

    How can I force the Tree / TreeGrid to be refreshed ? Firing a specific event ?

    This is the init code
    Code:
    goalTreeGrid = new TreeGrid();
    		goalTreeGrid.setCanSort(false);
    		goalTreeGrid.setCustomIconProperty("icon");
    		goalTreeGrid.setAnimateFolderTime(100);
    		goalTreeGrid.setAnimateFolders(true);
    		goalTreeGrid.setAnimateFolderSpeed(1000);
    		goalTreeGrid.setCanEdit(true);
    
    		goalTree = new Tree();
    		goalTree.setModelType(TreeModelType.PARENT);
    		goalTree.setRootValue("1");
    		goalTree.setIdField("nodeUID");
    		goalTree.setParentIdField("parentUID");
    		goalTree.setNameProperty("Nom");
    		goalTree.setShowRoot(true);
    		goalTree.setAutoOpenRoot(true);
    		goalTree.setRoot(fakeStartRoot); // dummy root to start with before the user selects anything
    		
    		goalTreeGrid.setWidth(500);
    		goalTreeGrid.setHeight100();
    		goalTreeGrid.setShowOpenIcons(true);
    		goalTreeGrid.setShowDropIcons(true);
    		goalTreeGrid.setShowConnectors(true);
    
    		TreeGridField nameTreeField = new TreeGridField("Name");
    		nameTreeField.setCanEdit(false);
    		TreeGridField valueTreeField = new TreeGridField("Valeur");
    		nameTreeField.setCanEdit(true);
    
    		goalTreeGrid.setFields(nameTreeField, valueTreeField);
    		goalTreeGrid.setData(goalTree);
    
    		goalTreeGrid.addDrawHandler(new DrawHandler() {
    			public void onDraw(DrawEvent event) {
    				goalTree.openAll();
    			}
    		});
    This is the method called when the user clicks on a row of the listgrid. Its prupose is to build a new Tree to display and edit

    Code:
    	private void feedGoalForm(Objetivo goal, ListGridRecord record) {
    
    		// Loop through the Goal (Objetivo) and build the tree up
    
    		int nodeUID = 0;
    		ArrayList<TreeNode> noeuds = new ArrayList<TreeNode>();
    
    		TreeNode goalNode = new GoalTreeNode(String.valueOf(++nodeUID), "0",
    				"Objectif", "goal");
    		noeuds.add(goalNode);
    		// Utility nodes
    		TreeNode question, facteur, metrique, methode, param;
    
    		Iterator<Pregunta> itp = goal.getPreguntas().iterator();
    
    		while (itp.hasNext()) {
    			Pregunta pregunta = itp.next();
    
    			question = new GoalTreeNode(String.valueOf(++nodeUID), goalNode
    					.getAttribute("nodeUID"), "Question", "question");
    			noeuds.add(question);
    
    			FactorAdaptado factorAdaptado = pregunta.getFactorAdaptado();
    
    			facteur = new GoalTreeNode(String.valueOf(++nodeUID), question
    					.getAttribute("nodeUID"), "Facteur", "factor");
    			noeuds.add(facteur);
    
    			Iterator<MetricaAdaptada> itma = factorAdaptado
    					.getMetricasAdaptadas().iterator();
    
    			while (itma.hasNext()) {
    
    				MetricaAdaptada metricaAdaptada = itma.next();
    
    				metrique = new GoalTreeNode(String.valueOf(++nodeUID), facteur
    						.getAttribute("nodeUID"), "Métrique", "metric");
    				noeuds.add(metrique);
    
    				Iterator<MetodoAdaptado> itmtha = metricaAdaptada
    						.getMetodosAdaptados().iterator();
    
    				while (itmtha.hasNext()) {
    
    					MetodoAdaptado metodoAdaptado = itmtha.next();
    
    					methode = new GoalTreeNode(String.valueOf(++nodeUID),
    							metrique.getAttribute("nodeUID"), "Méthode",
    							"method");
    					noeuds.add(methode);
    
    					Iterator<ValorParametro> itvp = metodoAdaptado.getValores()
    							.iterator();
    
    					while (itvp.hasNext()) {
    						ValorParametro valorParametro = itvp.next();
    
    						param = new GoalTreeNode(String.valueOf(++nodeUID),
    								methode.getAttribute("nodeUID"), "Param",
    								"param");
    						noeuds.add(param);
    
    					}
    
    				}
    			}
    
    		}
    
    		TreeNode[] nodes = new TreeNode[noeuds.size()];
    		goalTree.setData(noeuds.toArray(nodes));
    		goalTreeGrid.setData(goalTree);
    		goalTreeGrid.redraw();
    	
    		
    		// 
    		//goalTree.setData(employeeData2);
    		// SC.say(noeuds.toString());
    	}

    And this is the custom TreeNode class defnition

    Code:
    public static class GoalTreeNode extends TreeNode {
    
    		public GoalTreeNode(String nodeUID, String parentUID, String name,
    				String type) {
    			setAttribute("nodeUID", nodeUID);
    			setAttribute("parentUID", parentUID);
    
    			setAttribute("Nom", name);
    			setAttribute("Type", type);
    			setAttribute("Valeur", "vide");
    
    			if (type.equals("goal"))
    				setAttribute("icon", "chart_organisation.png");
    			else if (type.equals("question"))
    				setAttribute("icon", "question_node.gif");
    			else if (type.equals("factor"))
    				setAttribute("icon", "factor_node.gif");
    			else if (type.equals("method"))
    				setAttribute("icon", "method_node.gif");
    			else if (type.equals("metric"))
    				setAttribute("icon", "measure_node.gif");
    			else if (type.equals("param"))
    				setAttribute("icon", "parameter_node.gif");
    
    		}
    
    		public String toString() {
    
    			return getAttribute("Nom") + "  parentUID:"
    					+ getAttribute("parentUID") + " nodeUID:"
    					+ getAttribute("nodeUID") + "  type:"
    					+ getAttribute("Type") + "\n";
    		}
    
    	}



    *******************************

    I found the solution : strangely enough, one has to create a new instance of the Tree class, the setData(...) method is not enought.

    So to have the tree grid eventually updated I had to :
    1- do : new Tree(), resetting all fields (IdField, parentField)
    2- do : setData(TreeNode[])

    If I omit stage 1 and reuse an existing Tree instance, it won't work.
    Why ?


    (Using smartgwt1.2 + GWT 1.7)
    Last edited by semiosys; 13 Aug 2009, 01:10. Reason: found solution

    #2
    An existing Tree instance can be updated in place via setRoot(), add() and so forth, however, setData() in particular is a one-time method. We'll look at adding support for it to be callable more than once.

    Comment


      #3
      add(node,parentnode) versus remove(treenode) update behavior

      Thanks for your answer.

      I've a bit similar issue : given a Tree instance displayed in a TreeGrid, I've set context menu on node to allow editing (add, remove).

      When a node is removed (tree.remove(node)) and the treegrid refreshed (trigrid.redraw()) the tree layout is actually updated on screen.

      However when a node is added (tree.add(node, parentnode), and then the treegrid refreshed, the treelayout on the screen is not updated.

      How can I fix this ?

      Thank you!


      Code:
      MenuItem addQuestionItem = new MenuItem("Ajouter Question");  
      		addQuestionItem.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {  
      		             public void onClick(MenuItemClickEvent event) {  
      			            	String parentUID = currentlySelectedTreeNode.getAttribute("parentUID");
      			            	GoalTreeNode newQuestionNode = new GoalTreeNode(String.valueOf(++nodeUID), parentUID, "Question", "question");
      			            	GoalTreeNode parentNode = (GoalTreeNode) goalTree.findById(parentUID);
      			            	goalTree.add(newQuestionNode, parentNode);
      			            	//goalTree.reloadChildren(parentNode);  // no improvement with this
      			            	goalTreeGrid.redraw();
      			             }  
      			         });  
      		         
      		
      		goalMenu.setItems(addQuestionItem);
      		
      		
      		
      		final Menu questionMenu = new Menu();
      		questionMenu.setWidth(150); 
      		
      		MenuItem deleteQuestionItem = new MenuItem("Effacer Question");  
      		deleteQuestionItem.addClickHandler(new com.smartgwt.client.widgets.menu.events.ClickHandler() {  
      		             public void onClick(MenuItemClickEvent event) {  
      		            	goalTree.remove(currentlySelectedTreeNode);
      		            	goalTreeGrid.redraw();
      		             }  
      		         });  
      		
      		questionMenu.setItems(deleteQuestionItem);

      Comment


        #4
        Is the add() succeeding, eg, have you checked for errors in the developer console, checked for JavaScript errors, and checked that subsequent calls to eg getChildren() or getParent() work?

        Comment


          #5
          fixed

          Thanks for the suggestion.
          Yes : in the console a class cast exception is raised.

          Apparently, one has to use the actually class expected, not a subclass of it, otherwise the superclass is not automatically found/casted.
          This is not a Java behavior, this must come from the GWT/JS architecture, am I wrong ?

          Here is how I fixed it.

          Code:
          // GOOD:
          TreeNode newQuestionNode = new GoalTreeNode(String.valueOf(++nodeUID), parentUID, "Question", "question");
          
          // WRONG:
          //GoalTreeNode newQuestionNode = new GoalTreeNode(String.valueOf(++nodeUID), parentUID, "Question", "question");
          
          
          // GOOD: 
          TreeNode parentNode = goalTree.findById(parentUID);
          
          // WRONG: GoalTreeNodeparentNode = (GoalTreeNode) goalTree.findById(parentUID);
          
          
          
          goalTree.add(newQuestionNode, parentNode);

          Comment


            #6
            You should be able to set the data using a subclass and retrieve the subclass instance as well. The showcase example sets the side nav tree nodes to instances of ExampleTreeNode, and on lookup casts it back to ExampleTreeNode.

            Can you post a minimal standalone example illustrating the issue?

            Sanjiv

            Comment


              #7
              Originally posted by Isomorphic
              An existing Tree instance can be updated in place via setRoot(), add() and so forth, however, setData() in particular is a one-time method. We'll look at adding support for it to be callable more than once.
              The setRoot() and add(), addList() works not as expected. At least from my point of view.

              Code:
              TreeNode[] data = NavigationDataManager.getData(roles);
              TreeNode rootNode = NavigationData.getRootNode();
              
              tree.setRoot(rootNode);
              tree.addList(data, rootNode);
              The "data" nodes are ExplorerTreeNodes and i'm using the parentNodeID to create a hierarchical structure (just as in the ShowCase). If I'm using the above code, the nodes are added, but the hierarchical structure is lost, ie the elements are all shown as leaves on the same level of the tree.
              However if i'm using

              Code:
              Tree tree = new Tree();
              tree.setData(data);
              with the same date the hierarchy is ok.

              Any suggestions?

              Thanks,
              Michael

              Comment


                #8
                That method is doing exactly what it's documented to do. If you want to add nodes to different parents by id, use linkNodes().

                Comment


                  #9
                  Originally posted by Isomorphic
                  That method is doing exactly what it's documented to do. If you want to add nodes to different parents by id, use linkNodes().
                  Thanks, linkNodes() works!

                  From the documentation of addList() I coudnt see, that the "inner hierarchy" of the given nodes is flattened.

                  Comment


                    #10
                    I am facing the same problem... My treeGrid does not get updated with new Tree that is assigned to it using setData method.

                    1> I create the Tree data = new Tree() with initial data
                    2>I assign this to my treeGrid using TreeGrid.setData(data);
                    3> when after this i want to update my treeGrid with new updated data ,
                    I do Tree data = new Tree() again and set the data again in this.
                    4> then I call treeGrid.setData(data) again...


                    But this doesnt seem to work... any help on this?
                    Last edited by Rosh; 28 Dec 2010, 10:11.

                    Comment

                    Working...
                    X