1 package org.oddjob.designer.model; 2 3 import java.awt.Component ; 4 import java.util.Enumeration ; 5 import java.util.Vector ; 6 7 import javax.swing.tree.TreeNode ; 8 9 import org.oddjob.Structural; 10 import org.oddjob.designer.view.FormFactory; 11 import org.oddjob.structural.StructuralEvent; 12 import org.oddjob.structural.StructuralListener; 13 14 20 21 22 public class DesignTreeNode 23 implements TreeNode , StructuralListener { 24 25 26 private final Vector nodeList = new Vector (); 27 28 29 final private DesignTreeNode parent; 30 31 32 final private DesignTreeModel model; 33 34 35 private final DesignComponent designComponent; 36 37 private Component detailView; 38 39 46 public DesignTreeNode(DesignTreeModel model, 47 DesignTreeNode parent, DesignComponent node) { 48 49 this.model = model; 50 this.parent = parent; 51 this.designComponent = node; 52 } 53 54 public void build() { 55 if (designComponent instanceof Structural) { 56 ((Structural)designComponent).addStructuralListener(this); 57 } 58 } 59 60 public DesignComponent getDesignComponent() { 61 return designComponent; 62 } 63 64 66 public Enumeration children() { 67 return nodeList.elements(); 68 } 69 70 public boolean getAllowsChildren() { 71 return true; 72 } 73 74 public TreeNode getChildAt(int index) { 75 return (DesignTreeNode)nodeList.elementAt(index); 76 } 77 78 public int getChildCount() { 79 return nodeList.size(); 80 } 81 82 public boolean isLeaf() { 83 return nodeList.size() == 0 ? true : false; 84 } 85 86 public int getIndex(TreeNode child) { 87 88 return nodeList.indexOf(child); 89 } 90 91 public TreeNode getParent() { 92 93 return parent; 94 } 95 96 public String toString() { 97 98 return designComponent.name(); 99 } 100 101 public void childAdded(StructuralEvent e) { 102 103 int index = e.getIndex(); 104 Object o = e.getChild(); 105 if (!(o instanceof DesignComponent)) { 106 throw new IllegalStateException ("[" + o + "] is not a DesignComponent at in [" + designComponent + "]" ); 107 } 108 DesignComponent childComponent = (DesignComponent) o; 109 DesignTreeNode child = new DesignTreeNode(model, this, childComponent); 110 111 synchronized (nodeList) { 112 nodeList.insertElementAt(child, index); 113 } 114 115 model.fireTreeNodesInserted(this, child, index); 116 child.build(); 117 } 118 119 public void childRemoved(StructuralEvent e) { 120 121 int index = e.getIndex(); 122 DesignTreeNode child = null; 123 synchronized (nodeList) { 124 child = (DesignTreeNode)nodeList.elementAt(index); 125 nodeList.removeElementAt(index); 126 } 127 child.destroy(); 128 129 model.fireTreeNodesRemoved(this, child, index); 130 } 131 132 public void destroy() { 133 while (nodeList.size() > 0) { 134 int index= nodeList.size() - 1; 135 DesignTreeNode child = (DesignTreeNode)nodeList.remove(index); 136 child.destroy(); 137 model.fireTreeNodesRemoved(this, child, index); 138 } 139 140 if (designComponent instanceof Structural) { 141 ((Structural)designComponent).removeStructuralListener(this); 142 } 143 } 144 145 148 public Component getDetailView() { 149 if (detailView == null) { 150 detailView = FormFactory.createForm(designComponent.form()); 151 } 152 return detailView; 153 } 154 } 155 | Popular Tags |