Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 10th, 2012  |  syntax: None  |  size: 2.71 KB  |  hits: 7  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Implementing TreeModel
  2. public class PMEntry implements TreeModel{
  3.  
  4. private String title;
  5. private List<PMEntry> pmEntryCollection;
  6. private String pmId;
  7. private String href;
  8. private PMEntry root;
  9. private ModuleType type;
  10.  
  11. public PMEntry (PMEntry root){
  12.  
  13.   this.root = root;
  14. }
  15.  
  16. @Override
  17. public Object getRoot() {        
  18.  
  19.    return ((PMEntry)this.root);
  20. }
  21.  
  22. @Override
  23. public Object getChild(Object o, int i) {
  24.  
  25.  
  26.     if(getPmEntryCollection().isEmpty()){
  27.  
  28.         return null;
  29.  
  30.     }else {
  31.  
  32.       return (PMEntry) getPmEntryCollection().get(i);
  33.  
  34.     }
  35. }
  36.  
  37. @Override
  38. public int getChildCount(Object o) {
  39.  
  40.    if(getPmEntryCollection().isEmpty()){
  41.  
  42.         return 0;
  43.  
  44.     }else {
  45.  
  46.       return getPmEntryCollection().size();
  47.  
  48.     }
  49. }
  50.  
  51. @Override
  52. public boolean isLeaf(Object o) {
  53.     PMEntry pmentry = (PMEntry)o;
  54.     return (pmentry.getType() == ModuleType.DM) ? true : false;
  55.  }
  56.  
  57. @Override
  58. public void valueForPathChanged(TreePath tp, Object o) {
  59.    //todo
  60. }
  61.  
  62. @Override
  63. public int getIndexOfChild(Object parent, Object child) {
  64.  
  65.     if (!(parent instanceof PMEntry)){
  66.  
  67.         System.out.println("Returning -1");
  68.         return -1;
  69.     }          
  70.  
  71.     PMEntry pParent = (PMEntry) parent;
  72.  
  73.     List<PMEntry> children = pParent.getPmEntryCollection();
  74.  
  75.     if (children == null) {
  76.         System.out.println("children = null, Returning -1");
  77.         return -1;
  78.  
  79.     }
  80.  
  81.     for (int i = 0; i < children.size(); i++) {
  82.  
  83.         System.out.println("Child:" + child);
  84.  
  85.         if (children.get(i) == child) {
  86.  
  87.             return i;
  88.         }
  89.  
  90.     }
  91.  
  92.     return -1;        
  93. }
  94.  
  95. @Override
  96. public void addTreeModelListener(TreeModelListener tl) {
  97.    //todo
  98. }
  99.  
  100. @Override
  101. public void removeTreeModelListener(TreeModelListener tl) {
  102.     //todo
  103. }
  104.  
  105. @Override
  106. public String toString(){
  107.  
  108.     return this.getTitle();
  109. }
  110. public enum ModuleType {
  111.  
  112.     PM,
  113.     DM
  114.  
  115. }
  116.  
  117. // getters and setters here....
  118.        
  119. PMEntry tm = new PMEntry(null);
  120. tm.setTitle("Root");
  121.  
  122. PMEntry pmRoot = new PMEntry((PMEntry)(tm));
  123. pmRoot.setTitle("Project");
  124.  
  125. PMEntry pm1 = new PMEntry(pmRoot);
  126. pm1.setType(PMEntry.ModuleType.DM);
  127. pm1.setTitle("Publication Module");
  128.  
  129. PMEntry pm2 = new PMEntry(pmRoot);
  130. pm2.setType(PMEntry.ModuleType.PM);
  131. pm2.setTitle("Chapter");    
  132.  
  133. List<PMEntry> pmCollection = new ArrayList<PMEntry>();      
  134. List<PMEntry> pmCollection1 = new ArrayList<PMEntry>();
  135.  
  136. PMEntry pm3 = new PMEntry(null);
  137. pm3.setType(PMEntry.ModuleType.DM);
  138. pm3.setTitle("Data Module");
  139.  
  140. PMEntry pm4 = new PMEntry(null);
  141. pm4.setType(PMEntry.ModuleType.DM);
  142. pm4.setTitle("Data Module");
  143.  
  144. pmCollection1.add(pm3);
  145. pmCollection1.add(pm4);
  146.  
  147. pm2.setPmEntryCollection(pmCollection1);
  148.  
  149. pmCollection.add(pm1);
  150. pmCollection.add(pm2);                    
  151.  
  152. pmRoot.setPmEntryCollection(pmCollection);
  153.  
  154. this.jTree1.setModel(pmRoot);