12 Replies Latest reply on Sep 10, 2010 1:33 PM by tomj

    Populating Tree from Java Remote Object

    arkcto
      Hi,

      I was wondering if there is a specific "type" of Java object(s) I need to create to pass a Java object as the data provider for a Tree.
      My example is as follows : I have a Building, inside a building I have a room, inside a room I have a cubicle, inside a cubicle I have a desk.

      Obviously each node can have many subnodes (i.e. One room can have many cubicles).

      I tried representing this hierarchy in Java.
      I have the following structure :

      Building.java
      public class Building {
      String name;
      Room room;
      public Building(String name, Room room) {
      this.name = name;
      this.room = room;
      }

      Room.java
      public class Room {
      String name;
      Cubicle cubcle;
      public Room(String name, Cubicle cubicle) {
      this.name = name;
      this.cubicle = cubicle;
      }

      etc etc.

      I also created ActionScripts for these correpsonding files.

      package rpc.tree {
      [Bindable]
      [RemoteClass(alias="rpc.tree.Building")]
      public class Building {
      public var name:String;

      public var room:Room;

      }
      }

      etc etc


      I have a Service class that has a method that returns a Building.

      <mx:RemoteObject id="myTree" destination="MyTreeService">
      <mx:method name="getMeATree" result="now(event.result as Building)"/>
      </mx:RemoteObject>

      However when I try to use the Buidling structure as a data structure it doesn't work.

      <mx:Tree dataProvider="{aBuilding}" height="100" width="100%">
      </mx:Tree>

      Someone suggested that one way to represent Trees is as a Collection of Collections.
      Is there anyway I can use a Java representation of a tree as a data provider without me having to manipulate it in mxml (or Action script).

      I was able to pass a simple ArrayCollection (Java ArrayList) as a data provider for datagrid. Is there an equivalent for a tree ?

      Thanks.
        • 1. Re: Populating Tree from Java Remote Object
          T._Ruggles
          In order for this to work out of the gate with a tree your objects would need to be nested in ArrayCollection properties named 'children'. For example:

          Building contains ArrayList children and you add 0:n rooms via myBuilding.children.add(new Room("Bob's Cube", "B123"));
          • 2. Populating Tree from Java Remote Object
            arkcto Level 1
            Hi Tom

            Thanks for the response. Well, I did the classes and action script files as you suggested. However, I get the same result - the tree just shows 'objectMyTree' (I think it's just giving an instance name of the class).

            My modified classes are as follows :
            Building.java
            public class Building {
            String name;
            List children;
            public Building(String name, List children) {
            this.name = name;
            this.children = children;
            }

            Room.java
            public class Room {
            String name;
            List children;
            public Room(String name, List children) {
            this.name = name;
            this.children = children;
            }

            I changed the service class that returns the Tree structure appropriately.

            My action script files are :

            Building.as
            package rpc.tree {
            [Bindable]
            [RemoteClass(alias="rpc.tree.Building")]
            public class Building {
            public var name:String;
            public var children:List;
            }
            }

            Building.as
            package rpc.tree {
            [Bindable]
            [RemoteClass(alias="rpc.tree.Room")]
            public class Room {
            public var name:String;
            public var children:List;
            }
            }


            The mxml file is :

            private function treePlease(event: Object):void {
            aTree = Building(event);
            }
            <mx:RemoteObject id="myTree" destination="MyTreeService">
            <mx:method name="getMeATree" result="treePlease(event.result as Object)"/>
            </mx:RemoteObject>

            <mx:Button label="Get me Tree" click="myTree.getMeATree()" />

            What is the recommended (or most common way) to populate a tree from a Java Remote object ?

            I am just starting my application, so I'd rather learn the "right" way. If the way I'm approaching it in my sample isn't the appropriate way, please let me know.

            Thanks.
            • 3. Re: Populating Tree from Java Remote Object
              T._Ruggles Level 1
              Do you have labelField="name" in your tree control? If yes, please include your RO method and tree control in your reply.

              Thanks.
              • 4. Re: Populating Tree from Java Remote Object
                arkcto Level 1
                Hi Tom,

                My Tree control is

                <mx:Tree dataProvider="{aTree}" height="100" width="100%" labelField="@name">
                </mx:Tree>

                Also I can't import 'java.util.List' in my action script class. But I have used List in my .as files.

                // import java.util.*; - This doesn't work.
                package rpc.tree {
                [Bindable]
                [RemoteClass(alias="rpc.tree.Building")]
                public class Building {
                public var name:String;
                public var children:List
                }
                }



                My Detination class.

                public static Building getMeATree() {
                Cubicle cubicle = new Cubicle("Cubicle A");
                List cubicles = new ArrayList();
                cubicles.add(cubicle);

                Room room = new Room("Room A", cubicles);
                List roomr = new ArrayList();
                rooms.add(room);

                Building building = new Building("sample building",rooms);
                return building;
                }

                Thanks Tom.
                • 5. Re: Populating Tree from Java Remote Object
                  T._Ruggles Level 1
                  A couple things.

                  -The labelField should be just "name" when you're using a Java List. You just need the @ when it's XML to signify that it's an attribute.
                  - The .as class should type children as an ArrayCollection. That's the AS mapping for a Java List.
                  • 6. Re: Populating Tree from Java Remote Object
                    arkcto Level 1
                    Thanks Tom.

                    I am getting somewhere now. However it still doesn't work fully yet. Instead of just one 'object:MyTree' it's giving me a 2-level tree.
                    i.e. objectMyTree.
                    ----------------objectMyTree
                    --------------------------------------objectMyTree.

                    So it's getting the data, I believe, but it's not showing it ?

                    My mxml looks like this :


                    <?xml version="1.0" encoding="utf-8"?>
                    <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml">

                    <mx:Script>
                    <![CDATA[
                    import mx.collections.ArrayCollection;
                    import rpc.tree.*;

                    [Bindable]
                    public var aTree:Building;

                    private function now(event: Object):void {
                    aTree = Building(event);
                    }

                    ]]>
                    </mx:Script>


                    <mx:RemoteObject id="myTree" destination="MyTreeService">
                    <mx:method name="getMeATree" result="now(event.result as Object)"/>
                    </mx:RemoteObject>

                    <mx:Panel title="Tree Stuff">
                    <mx:Tree dataProvider="{aTree}" height="100" width="100%" labelField="name">
                    </mx:Tree>

                    <mx:Button label="Get me NOw" click="myTree.getMeATree()" />
                    </mx:Panel>

                    </mx:Application>

                    • 7. Re: Populating Tree from Java Remote Object
                      T._Ruggles Level 1
                      You need to make the properties on your server side classes public or give them public getters. Only public properties will be serialized and sent to the client. When I do this I get a proper tree using your code.
                      • 8. Re: Populating Tree from Java Remote Object
                        arkcto Level 1
                        H I Tom,

                        I have public attributes AND have public getters/setters, and still doesnt' work.

                        More importantly, Tom, is my approach to building a Tree from a Java object correct ? What if I've more than just the 'Name' field that I want displayed ? Nodes such as building and room might only have 'Name' as the labelField, but nodes such as Cubicle also have fields such as Width, Height that need to be displayed.

                        The way I am doing it now (labelField='Name') precludes that from happening. Is that right ?

                        What I am actually trying to figure out is - what's the recommended way to build a tree from a Java remote object ?
                        Are there ways other than what I'm trying to do, that are more effective ? If so, where is the documentation for it ?

                        You guys are doing a great job on the board, but if I had access to the proper documentation, I wouldn't have to be on these boards that often.

                        Thanks.
                        • 9. Populating Tree from Java Remote Object
                          T._Ruggles Level 1
                          Hi,

                          Unfortunately there is no doc or article that I am aware of that describes a best practice from one end to the other. However, I got the following info from one of the developers involved in the Tree control. If you use the workflow described then it really doesn't matter where the data comes from.

                          "What the customer needs to do is write a custom dataDescriptor that tells the Tree certain things, based on the structure of his Java RO result. The dataDescriptor is like the contract between the Tree and the bound data – it tells the Tree which nodes to consider branches, which property off a branch node represents the children for that node, etc.

                          "Have him start with looking at ITreeDataDescriptor and its implementing class DefaultDataDescriptor. The methods he most likely needs to override are getChildren(), hasChildren(), and isBranch(). What his custom implementations will do is based on the type of node that the Tree comes across (building node, room node, cubicle node, etc) his code will return the correct collection of children or whether there are children, etc.

                          "The documentation should have examples of custom descriptors and how to write them."

                          As for your example with just labelField I have versions here that are working. I'd be glad to zip up what I've got and email it.
                          • 10. Re: Populating Tree from Java Remote Object
                            arkcto Level 1
                            Thanks Tom,

                            Yes, If you could please email the zip file to arkcto@yahoo.com, I'd very much appreciate it.

                            Thanks for your help again.
                            • 11. Re: Populating Tree from Java Remote Object
                              ajk74

                              Hi,

                              Appreciate if you could  also send me the working zip code for "Populating Tree from Java Remote Object" as i am aslo struggling for the same issue.

                              My mail id is "ajayk74@hotmail.com"

                              Thanks in advance.

                              Aj

                              • 12. Re: Populating Tree from Java Remote Object
                                tomj Adobe Employee

                                AJ,

                                 

                                This thread is 4+ years old and that Tom doesn't work at Adobe any more.

                                Sorry.