1 19 20 package org.apache.tools.ant.module.nodes; 21 22 import java.io.IOException ; 23 import java.text.Collator ; 24 import java.util.Collection ; 25 import java.util.Collections ; 26 import java.util.Comparator ; 27 import java.util.Iterator ; 28 import java.util.Set ; 29 import java.util.SortedSet ; 30 import java.util.TreeSet ; 31 import javax.swing.event.ChangeEvent ; 32 import javax.swing.event.ChangeListener ; 33 import org.apache.tools.ant.module.AntModule; 34 import org.apache.tools.ant.module.api.AntProjectCookie; 35 import org.apache.tools.ant.module.api.support.TargetLister; 36 import org.openide.ErrorManager; 37 import org.openide.nodes.Children; 38 import org.openide.nodes.Node; 39 40 final class AntProjectChildren extends Children.Keys<TargetLister.Target> implements ChangeListener , Comparator <TargetLister.Target> { 41 42 private static Collator SORTER = Collator.getInstance(); 43 44 private final AntProjectCookie cookie; 45 private SortedSet <TargetLister.Target> allTargets; 46 47 public AntProjectChildren (AntProjectCookie cookie) { 48 super (); 49 this.cookie = cookie; 50 } 51 52 @Override 53 protected void addNotify () { 54 super.addNotify (); 55 refreshKeys(true); 56 cookie.addChangeListener (this); 57 } 58 59 @Override 60 protected void removeNotify () { 61 super.removeNotify (); 62 setKeys(Collections.<TargetLister.Target>emptySet()); 63 synchronized (this) { 64 allTargets = null; 65 } 66 cookie.removeChangeListener (this); 67 } 68 69 private void refreshKeys(boolean createKeys) { 70 try { 71 Set <TargetLister.Target> _allTargets = TargetLister.getTargets(cookie); 72 Collection <TargetLister.Target> keys; 73 synchronized (this) { 74 if (allTargets == null && !createKeys) { 75 return; 77 } 78 allTargets = new TreeSet <TargetLister.Target>(this); 79 allTargets.addAll(_allTargets); 80 Iterator <TargetLister.Target> it = allTargets.iterator(); 81 while (it.hasNext()) { 82 TargetLister.Target t = it.next(); 83 if (t.isOverridden()) { 84 it.remove(); 86 } 87 } 88 keys = allTargets; 89 } 90 if (keys != null) { setKeys(keys); 92 } 93 } catch (IOException e) { 94 AntModule.err.notify(ErrorManager.INFORMATIONAL, e); 96 setKeys(Collections.<TargetLister.Target>emptySet()); 97 } 98 } 99 100 protected Node[] createNodes(TargetLister.Target key) { 101 return new Node[] {new AntTargetNode(cookie, key)}; 102 } 103 104 public void stateChanged (ChangeEvent ev) { 105 refreshKeys(false); 106 } 107 108 public int compare(TargetLister.Target t1, TargetLister.Target t2) { 109 int x = SORTER.compare(t1.getName(), t2.getName()); 110 if (x != 0 || t1 == t2) { 111 return x; 112 } else { 113 return System.identityHashCode(t1) - System.identityHashCode(t2); 115 } 116 } 117 118 } 119 | Popular Tags |