1 19 20 package org.netbeans.swing.outline; 21 22 import java.util.ArrayList ; 23 import java.util.Enumeration ; 24 import java.util.HashMap ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import javax.swing.event.TreeExpansionEvent ; 31 import javax.swing.event.TreeExpansionListener ; 32 import javax.swing.event.TreeWillExpandListener ; 33 import javax.swing.tree.AbstractLayoutCache ; 34 import javax.swing.tree.ExpandVetoException ; 35 import javax.swing.tree.TreePath ; 36 37 60 public final class TreePathSupport { 61 private OutlineModel mdl; 62 private Map expandedPaths = new HashMap (); 63 private List eListeners = new ArrayList (); 64 private List weListeners = new ArrayList (); 65 private AbstractLayoutCache layout; 66 67 68 public TreePathSupport(OutlineModel mdl, AbstractLayoutCache layout) { 69 this.mdl = mdl; 70 this.layout = layout; 71 } 72 73 76 public void clear() { 77 expandedPaths.clear(); 78 } 79 80 84 public void expandPath (TreePath path) { 85 if (Boolean.TRUE.equals(expandedPaths.get(path))) { 86 return; 88 } 89 TreeExpansionEvent e = new TreeExpansionEvent (this, path); 90 try { 91 fireTreeWillExpand(e, true); 92 expandedPaths.put(path, Boolean.TRUE); 93 layout.setExpandedState(path, true); 94 fireTreeExpansion(e, true); 95 } catch (ExpandVetoException eve) { 96 fireTreeExpansionVetoed (e, eve); 97 } 98 } 99 100 104 public void collapsePath (TreePath path) { 105 if (Boolean.FALSE.equals(expandedPaths.get(path))) { 106 return; 108 } 109 TreeExpansionEvent e = new TreeExpansionEvent (this, path); 110 try { 111 fireTreeWillExpand(e, false); 112 expandedPaths.put(path, Boolean.FALSE); 113 layout.setExpandedState(path, false); 114 fireTreeExpansion(e, false); 115 } catch (ExpandVetoException eve) { 116 fireTreeExpansionVetoed (e, eve); 117 } 118 } 119 120 122 public void removePath (TreePath path) { 123 expandedPaths.remove(path); 124 } 125 126 private void fireTreeExpansion (TreeExpansionEvent e, boolean expanded) { 127 int size = eListeners.size(); 128 129 TreeExpansionListener [] listeners = new TreeExpansionListener [size]; 130 synchronized (this) { 131 listeners = (TreeExpansionListener []) eListeners.toArray(listeners); 132 } 133 for (int i=0; i < listeners.length; i++) { 134 if (expanded) { 135 listeners[i].treeExpanded(e); 136 } else { 137 listeners[i].treeCollapsed(e); 138 } 139 } 140 } 141 142 private void fireTreeWillExpand (TreeExpansionEvent e, boolean expanded) throws ExpandVetoException { 143 int size = eListeners.size(); 144 145 TreeWillExpandListener [] listeners = new TreeWillExpandListener [size]; 146 synchronized (this) { 147 listeners = (TreeWillExpandListener []) weListeners.toArray(listeners); 148 } 149 for (int i=0; i < listeners.length; i++) { 150 if (expanded) { 151 listeners[i].treeWillExpand(e); 152 } else { 153 listeners[i].treeWillCollapse(e); 154 } 155 } 156 } 157 158 private void fireTreeExpansionVetoed (TreeExpansionEvent e, ExpandVetoException ex) { 159 int size = eListeners.size(); 160 161 TreeWillExpandListener [] listeners = new TreeWillExpandListener [size]; 162 synchronized (this) { 163 listeners = (TreeWillExpandListener []) weListeners.toArray(listeners); 164 } 165 for (int i=0; i < listeners.length; i++) { 166 if (listeners[i] instanceof ExtTreeWillExpandListener) { 167 ((ExtTreeWillExpandListener) listeners[i]).treeExpansionVetoed(e, 168 ex); 169 } 170 } 171 } 172 173 174 public boolean hasBeenExpanded(TreePath path) { 175 return (path != null && expandedPaths.get(path) != null); 176 } 177 178 185 public boolean isExpanded(TreePath path) { 186 if(path == null) 187 return false; 188 189 Object value = expandedPaths.get(path); 191 192 if(value == null || !((Boolean )value).booleanValue()) 193 return false; 194 195 TreePath parentPath = path.getParentPath(); 197 198 if(parentPath != null) 199 return isExpanded(parentPath); 200 return true; 201 } 202 203 protected void removeDescendantToggledPaths(Enumeration toRemove) { 204 if(toRemove != null) { 205 while(toRemove.hasMoreElements()) { 206 TreePath [] descendants = getDescendantToggledPaths( 207 (TreePath ) toRemove.nextElement()); 208 for (int i=0; i < descendants.length; i++) { 209 expandedPaths.remove(descendants[i]); 210 } 211 } 212 } 213 } 214 215 protected TreePath [] getDescendantToggledPaths(TreePath parent) { 216 if(parent == null) 217 return null; 218 219 ArrayList descendants = new ArrayList (); 220 Iterator nodes = expandedPaths.keySet().iterator(); 221 TreePath path; 222 while (nodes.hasNext()) { 223 path = (TreePath ) nodes.next(); 224 if (parent.isDescendant(path)) { 225 descendants.add(path); 226 } 227 } 228 TreePath [] result = new TreePath [descendants.size()]; 229 return (TreePath []) descendants.toArray(result); 230 } 231 232 public boolean isVisible(TreePath path) { 233 if(path != null) { 234 TreePath parentPath = path.getParentPath(); 235 236 if(parentPath != null) { 237 return isExpanded(parentPath); 238 } 239 return true; 241 } 242 return false; 243 } 244 245 public TreePath [] getExpandedDescendants(TreePath parent) { 246 TreePath [] result = new TreePath [0]; 247 if(isExpanded(parent)) { 248 TreePath path; 249 Object value; 250 List results = null; 251 252 if (!expandedPaths.isEmpty()) { 253 254 Iterator i = expandedPaths.keySet().iterator(); 255 256 while(i.hasNext()) { 257 path = (TreePath ) i.next(); 258 value = expandedPaths.get(path); 259 260 if(path != parent && value != null && 264 ((Boolean )value).booleanValue() && 265 parent.isDescendant(path) && isVisible(path)) { 266 if (results == null) { 267 results = new ArrayList (); 268 } 269 results.add (path); 270 } 271 } 272 if (results != null) { 273 result = (TreePath []) results.toArray(result); 274 } 275 } 276 } 277 return result; 278 } 279 280 283 public synchronized void addTreeExpansionListener (TreeExpansionListener l) { 284 eListeners.add(l); 285 } 286 287 public synchronized void removeTreeExpansionListener (TreeExpansionListener l) { 288 eListeners.remove(l); 289 } 290 291 public synchronized void addTreeWillExpandListener (TreeExpansionListener l) { 292 weListeners.add(l); 293 } 294 295 public synchronized void removeTreeWillExpandListener (TreeExpansionListener l) { 296 weListeners.remove(l); 297 } 298 } 299 | Popular Tags |