tags:

views:

265

answers:

2

Hello,

So i need to create a tree with tree items for my gwt project. i am using the composite pattern to store all the information i need to be placed within a tree.

A User has a root Folder that extends Hierarchy, this root Folder then has a list of Hierarchy objects, that can be FileLocations or Folders. Trouble i am having is building my tree based on this pattern. this data is all stored using hibernate in a mysql database

How would i be able to implement this as a tree in gwt.

Also the tree item that i create would have to reference back to the object so i can rename or move it.

@Entity
@Table(name ="HIERARCHY")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE) @DiscriminatorColumn(
name = "HIERARCHY_TYPE", discriminatorType = DiscriminatorType.STRING)      
public abstract class  Hierarchy implements Serializable {

 @Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "hierarchy_id", updatable = false, nullable = false)
private int hId;



@Entity
@DiscriminatorValue("F")
public class Folder extends Hierarchy  {

@OneToMany(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinTable(name = "FOLDER_JOIN_FILELOCATION", joinColumns = { 
    @JoinColumn(name = "folder_id") }, inverseJoinColumns = { 
    @JoinColumn(name = "file_information_id") })
private List<Hierarchy> children = new ArrayList<Hierarchy>() ;
@Column(name = "folder_name")
private String folderName;

@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
@JoinTable(name="FOLDER_JOIN_FOLDER",
    joinColumns = @JoinColumn(name="parent_folder_id"),
    inverseJoinColumns = @JoinColumn(name="folder_ID")
 ) 
private Hierarchy parent;




@Entity
@DiscriminatorValue("FI")
public class FileInformation extends Hierarchy  {


@Column (name = "location")
private String location;

@ManyToOne( cascade = {CascadeType.PERSIST, CascadeType.MERGE} )
 @JoinTable(name="FILEINFORMATION_JOIN_FOLDER",
    joinColumns = @JoinColumn(name="filelocation_id"),
    inverseJoinColumns = @JoinColumn(name="folder_ID")
)  
private Hierarchy parent;

now so i want to build the tree based on this structure. if my logic is wrong could you point me in the right direction

Tree tree = new Tree();

Now for every Folder or fileInformation within a Folder i want to create a tree item? that tree item must also reference back to the object itself

A: 

This can all be done pretty easily with GWT-RPC requests to your server and the Tree and TreeItem classes in the client.

On startup (or when you want to display the tree for the first time), make a call to your service, and in the callback add those items to the tree using tree.addItem(). A lot of how easy this is will depend on how your database is structured and how you pass the information to the client, but that should get you started.

Another option is to use GWT-incubator's FastTree class, which is good for showing large trees, and for only loading data when a folder is opened, for example.

Jason Hall
thank you for the replyyou said this is will depend on how your database is structuredUsers have a root Folder, this then has many sub folders, and sub files by which sub folders can have sub files(only holding a location to the file). i am using the composite pattern to implement this. when a User logs in the data is passed client side, and then i want to build the tree based on that. i will display my classes above. my trouble is building the tree.
molleman
i edited my post above
molleman
You will need to write a service that returns the file tree for a given user, in a tree structure. In your client code, call this service (asynchronously) and with the result, add items to your Tree, and to TreeItems for deeper files. Here's a good example of adding items to the tree: http://examples.roughian.com/index.htm#Widgets~Tree
Jason Hall
A: 

You have to traverse your Folder / Hierarchy and find the parent node, then add this parent to the one TreeItem t, then find the parents children, and add them to t, now for each children found, do the same.

AntonioP