2

I am trying to write a class that implements the TreeModel class. I was hoping someone could guide me in the right direction. Below is my class. The problem is when I bind it to a jTree Component, the second level keeps being added over and over. So I suspect my reference to the parent object is wrong:

public class PMEntry implements TreeModel{

private String title;
private List<PMEntry> pmEntryCollection;
private String pmId;
private String href;
private PMEntry root;
private ModuleType type;

public PMEntry (PMEntry root){

  this.root = root;
}

@Override
public Object getRoot() {        

   return ((PMEntry)this.root);
}

@Override
public Object getChild(Object o, int i) {


    if(getPmEntryCollection().isEmpty()){

        return null;

    }else {

      return (PMEntry) getPmEntryCollection().get(i);

    }
}

@Override
public int getChildCount(Object o) {

   if(getPmEntryCollection().isEmpty()){

        return 0;

    }else {

      return getPmEntryCollection().size();

    }
}

@Override
public boolean isLeaf(Object o) {
    PMEntry pmentry = (PMEntry)o;
    return (pmentry.getType() == ModuleType.DM) ? true : false;
 }

@Override
public void valueForPathChanged(TreePath tp, Object o) {
   //todo
}

@Override
public int getIndexOfChild(Object parent, Object child) {

    if (!(parent instanceof PMEntry)){

        System.out.println("Returning -1");
        return -1;
    }           

    PMEntry pParent = (PMEntry) parent;

    List<PMEntry> children = pParent.getPmEntryCollection();

    if (children == null) {
        System.out.println("children = null, Returning -1");
        return -1;

    }

    for (int i = 0; i < children.size(); i++) {

        System.out.println("Child:" + child);

        if (children.get(i) == child) {

            return i;
        }

    }

    return -1;        
}

@Override
public void addTreeModelListener(TreeModelListener tl) {
   //todo
}

@Override
public void removeTreeModelListener(TreeModelListener tl) {
    //todo
}

@Override
public String toString(){

    return this.getTitle();
}
public enum ModuleType {

    PM,
    DM

}

// getters and setters here....

And here is a snippet of how I am binding the data

PMEntry tm = new PMEntry(null);
tm.setTitle("Root");

PMEntry pmRoot = new PMEntry((PMEntry)(tm));
pmRoot.setTitle("Project");

PMEntry pm1 = new PMEntry(pmRoot);
pm1.setType(PMEntry.ModuleType.DM);
pm1.setTitle("Publication Module");

PMEntry pm2 = new PMEntry(pmRoot);
pm2.setType(PMEntry.ModuleType.PM);
pm2.setTitle("Chapter");     

List<PMEntry> pmCollection = new ArrayList<PMEntry>();      
List<PMEntry> pmCollection1 = new ArrayList<PMEntry>();

PMEntry pm3 = new PMEntry(null);
pm3.setType(PMEntry.ModuleType.DM);
pm3.setTitle("Data Module");

PMEntry pm4 = new PMEntry(null);
pm4.setType(PMEntry.ModuleType.DM);
pm4.setTitle("Data Module");

pmCollection1.add(pm3);
pmCollection1.add(pm4); 

pm2.setPmEntryCollection(pmCollection1);

pmCollection.add(pm1);
pmCollection.add(pm2);                     

pmRoot.setPmEntryCollection(pmCollection);

this.jTree1.setModel(pmRoot);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
PhillyNJ
  • 3,859
  • 4
  • 38
  • 64

2 Answers2

4

I'd wonder why you think you need to implement TreeModel. Have you looked into DefaultTreeModel? What new behavior over and above that class do you plan to implement?

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • Hi, I want to preserver my object. The example I posted is a dumb down version of a larger object, that want to be able to create, bind it and edit it. – PhillyNJ Jul 22 '12 at 00:44
  • Not the answer you should be thinking about: How is your object different from DefaultTreeModel? You can create, edit, and bind it. – duffymo Jul 22 '12 at 00:48
  • Hi, I dont just want to display a Node or a leaf node, I want to represent a TreeModel of my objects and its reference to over objects. – PhillyNJ Jul 22 '12 at 00:56
  • @Phil: that comment still doesn't explain why you wouldn't want to use the DefaultTreeModel class. – Hovercraft Full Of Eels Jul 22 '12 at 00:59
  • 1
    ok, I give :) - So I will create a class that extends DefaultTreeModel and give it a try. Thx – PhillyNJ Jul 22 '12 at 01:22
  • 1
    You're *still* missing the point. Just use DefaultTreeModel; don't extend it. I don't see what new behavior you need to add. If you need to persist, then write a new class that accepts a DefaultTreeModel as a parameter and persists it. – duffymo Jul 22 '12 at 01:24
  • @duffymo, you are correct. I don't need to extend DefaultTreeModel. I believe to accomplish what I want to do is simply extend the DefaultMutableTreeNode class and use it on the DefaultTreeModel. I was able to create and test this and it works as I need it to. Thanks for the help. – PhillyNJ Jul 22 '12 at 02:01
  • 1
    Excellent; sounds like a better solution. Good luck. – duffymo Jul 22 '12 at 02:03
2

I have to agree with @duffymo & @HFOE: don't reject DefaultTreeModel prematurely. There's an example here that illustrates a TreeCellEditor for editing the name of a userObject.

If you really do need to implement TreeModel, FileTreeModel, discussed here, is a fairly accessible example.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045