1 package org.oddjob.monitor.model; 2 3 import java.util.Enumeration ; 4 import java.util.HashMap ; 5 import java.util.Map ; 6 import java.util.Vector ; 7 8 import javax.swing.tree.TreeNode ; 9 10 import org.oddjob.Iconic; 11 import org.oddjob.Structural; 12 import org.oddjob.images.IconEvent; 13 import org.oddjob.images.IconHelper; 14 import org.oddjob.images.IconListener; 15 import org.oddjob.images.IconTip; 16 import org.oddjob.images.NoSuchIconException; 17 import org.oddjob.scheduling.OddjobScheduler; 18 import org.oddjob.structural.StructuralEvent; 19 import org.oddjob.structural.StructuralListener; 20 21 27 28 29 public class JobTreeNode 30 implements TreeNode , StructuralListener { 31 32 33 private final Vector nodeList = new Vector (); 34 35 36 final private JobTreeNode parent; 37 38 39 final private JobTreeModel model; 40 41 42 private final OurIconListener iconListener = new OurIconListener(); 43 private volatile IconTip iconTip = IconHelper.nullIcon; 44 45 46 final private Object component; 47 48 final private ExplorerContext explorerContext; 49 50 56 public JobTreeNode(ExplorerModel explorerModel, JobTreeModel model) { 57 this.parent = null; 58 this.model = model; 59 this.component = explorerModel.getRoot(); 60 this.explorerContext = new ExplorerContext(explorerModel); 61 } 62 63 69 public JobTreeNode(JobTreeNode parent, Object node) { 70 if (parent == null) { 71 throw new NullPointerException ("Parent must not be null!"); 72 } 73 this.parent = parent; 74 this.model = parent.model; 75 this.component = node; 76 77 this.explorerContext = new ExplorerContext(node, parent.getExplorerContext()); 78 } 79 80 81 85 public void build() { 86 if (component instanceof Structural) { 87 ((Structural)component).addStructuralListener(this); 88 } 89 } 90 91 public void visible() { 92 iconListener.listen(); 93 } 94 95 public void inVisible() { 96 iconListener.dont(); 97 } 98 99 public void setIcon(IconTip iconTip) { 100 synchronized (this) { 101 this.iconTip = iconTip; 102 } 103 model.fireTreeNodesChanged(this); 104 } 105 106 public Object getComponent() { 107 return component; 108 } 109 110 112 public Enumeration children() { 113 return nodeList.elements(); 114 } 115 116 public boolean getAllowsChildren() { 117 return true; 118 } 119 120 public TreeNode getChildAt(int index) { 121 return (JobTreeNode)nodeList.elementAt(index); 122 } 123 124 public int getChildCount() { 125 return nodeList.size(); 126 } 127 128 public boolean isLeaf() { 129 return nodeList.size() == 0 ? true : false; 130 } 131 132 public int getIndex(TreeNode child) { 133 134 return nodeList.indexOf(child); 135 } 136 137 public TreeNode getParent() { 138 139 return parent; 140 } 141 142 public String toString() { 143 return component.toString(); 144 } 145 146 synchronized public IconTip getIcon() { 147 return iconTip; 148 } 149 150 public JobTreeNode[] getChildren() { 151 synchronized (nodeList) { 152 return (JobTreeNode[]) nodeList.toArray(new JobTreeNode[0]); 153 } 154 } 155 156 160 public void childAdded(StructuralEvent e) { 161 162 int index = e.getIndex(); 163 JobTreeNode child = new JobTreeNode(this, e.getChild()); 164 165 synchronized (nodeList) { 166 nodeList.insertElementAt(child, index); 167 } 168 169 model.fireTreeNodesInserted(this, child, index); 170 child.build(); 171 } 172 173 177 public void childRemoved(StructuralEvent e) { 178 179 int index = e.getIndex(); 180 JobTreeNode child = null; 181 synchronized (nodeList) { 182 child = (JobTreeNode)nodeList.elementAt(index); 183 nodeList.removeElementAt(index); 184 } 185 child.destroy(); 186 187 model.fireTreeNodesRemoved(this, child, index); 188 } 189 190 public void destroy() { 191 while (nodeList.size() > 0) { 192 int index= nodeList.size() - 1; 193 JobTreeNode child = (JobTreeNode)nodeList.remove(index); 194 child.destroy(); 195 model.fireTreeNodesRemoved(this, child, index); 196 } 197 198 if (component instanceof Structural) { 199 ((Structural)component).removeStructuralListener(this); 200 } 201 if (component instanceof OddjobScheduler) { 202 explorerContext.getSchedulerTraker().removeSchedule( 203 (OddjobScheduler) component); 204 } 205 } 206 207 class OurIconListener implements IconListener { 208 private final Map icons = new HashMap (); 209 210 void listen() { 211 if (component instanceof Iconic) { 212 ((Iconic)component).addIconListener(this); 213 } 214 } 215 216 public void iconEvent(IconEvent event) { 217 String iconId = event.getIconId(); 218 IconTip it = (IconTip) icons.get(iconId); 219 if (it == null) { 220 try { 221 it = ((Iconic)component).iconForId(iconId); 222 } catch (NoSuchIconException e) { 223 e.printStackTrace(); 224 it = IconHelper.nullIcon; 225 } 226 icons.put(iconId, it); 227 } 228 setIcon(it); 229 } 230 231 public void dont() { 232 if (component instanceof Iconic) { 233 ((Iconic)component).removeIconListener(this); 234 } 235 } 236 } 237 238 241 public ExplorerContext getExplorerContext() { 242 return explorerContext; 243 } 244 } 245 | Popular Tags |