1 package org.oddjob.monitor.model; 2 3 import java.util.Enumeration ; 4 import java.util.LinkedList ; 5 import java.util.Vector ; 6 7 import javax.swing.SwingUtilities ; 8 import javax.swing.event.TreeModelEvent ; 9 import javax.swing.event.TreeModelListener ; 10 import javax.swing.tree.TreeModel ; 11 import javax.swing.tree.TreeNode ; 12 import javax.swing.tree.TreePath ; 13 14 15 20 public class JobTreeModel implements TreeModel { 21 22 23 private final Vector tmListeners = new Vector (); 24 25 26 private JobTreeNode root; 27 28 33 public void setRootTreeNode(JobTreeNode node) { 34 this.root = node; 35 } 36 37 41 public void addTreeModelListener(TreeModelListener tml) { 42 synchronized (tmListeners) { 43 tmListeners.add(tml); 44 } 45 } 46 47 51 public Object getChild(Object parent, int index) { 52 return ((JobTreeNode)parent).getChildAt(index); 53 } 54 55 59 public boolean isLeaf(Object node) { 60 return ((JobTreeNode)node).isLeaf(); 61 } 62 63 67 public int getChildCount(Object parent) { 68 return ((JobTreeNode)parent).getChildCount(); 69 } 70 71 75 public int getIndexOfChild(Object parent, Object child) { 76 return ((JobTreeNode)parent).getIndex((JobTreeNode)child); 77 } 78 79 83 public Object getRoot() { 84 return root; 85 } 86 87 public void removeTreeModelListener(TreeModelListener tml) { 88 tmListeners.remove(tml); 89 } 90 91 95 public void valueForPathChanged(TreePath path, Object newValue) { 96 throw new UnsupportedOperationException ("Don't need valueForPathChanged!"); 97 } 98 99 public void fireTreeNodesChanged(TreeNode changed) { 100 101 LinkedList list = new LinkedList (); 102 for (TreeNode i = changed; i != null; i = i.getParent()) { 103 list.addFirst(i); 104 } 105 106 final TreeModelEvent event = new TreeModelEvent (changed, list.toArray()); 107 108 synchronized (tmListeners) { 109 for (Enumeration e = tmListeners.elements(); 110 e.hasMoreElements(); ) { 111 final TreeModelListener tml = (TreeModelListener )e.nextElement(); 112 Runnable r = new Runnable () { 113 public void run() { 114 tml.treeNodesChanged(event); 115 } 116 }; 117 if (SwingUtilities.isEventDispatchThread()) { 118 r.run(); 119 } else { 120 SwingUtilities.invokeLater(r); 121 } 122 } 123 } 124 } 125 126 public void fireTreeNodesInserted(TreeNode changed, JobTreeNode child, int index) { 127 128 LinkedList list = new LinkedList (); 129 130 for (TreeNode i = changed; i != null; i = i.getParent()) { 131 list.addFirst(i); 132 } 133 134 int childIndecies[] = {index}; 135 Object children [] = {child}; 136 137 final TreeModelEvent event = new TreeModelEvent (changed, list.toArray(), 138 childIndecies, children); 139 140 synchronized (tmListeners) { 141 for (Enumeration e = tmListeners.elements(); 142 e.hasMoreElements(); ) { 143 final TreeModelListener tml = (TreeModelListener )e.nextElement(); 144 Runnable r = new Runnable () { 145 public void run() { 146 tml.treeNodesInserted(event); 147 } 148 }; 149 if (SwingUtilities.isEventDispatchThread()) { 150 r.run(); 151 } else { 152 SwingUtilities.invokeLater(r); 153 } 154 } 155 } 156 } 157 158 public void fireTreeNodesRemoved(TreeNode changed, JobTreeNode child, int index) { 159 160 LinkedList list = new LinkedList (); 161 162 for (TreeNode i = changed; i != null; i = i.getParent()) { 163 164 list.addFirst(i); 165 } 166 167 int childIndecies[] = {index}; 168 Object children [] = {child}; 169 170 final TreeModelEvent event = new TreeModelEvent (changed, list.toArray(), 171 childIndecies, children); 172 173 synchronized (tmListeners) { 174 for (Enumeration e = tmListeners.elements(); 175 e.hasMoreElements(); ) { 176 final TreeModelListener tml = (TreeModelListener )e.nextElement(); 177 Runnable r = new Runnable () { 178 public void run() { 179 tml.treeNodesRemoved(event); 180 } 181 }; 182 if (SwingUtilities.isEventDispatchThread()) { 183 r.run(); 184 } else { 185 SwingUtilities.invokeLater(r); 186 } 187 } 188 } 189 } 190 } 191 | Popular Tags |