KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > nodes > AntProjectChildren


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.apache.tools.ant.module.nodes;
21
22 import java.io.IOException JavaDoc;
23 import java.text.Collator JavaDoc;
24 import java.util.Collection JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Comparator JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Set JavaDoc;
29 import java.util.SortedSet JavaDoc;
30 import java.util.TreeSet JavaDoc;
31 import javax.swing.event.ChangeEvent JavaDoc;
32 import javax.swing.event.ChangeListener JavaDoc;
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 JavaDoc, Comparator JavaDoc<TargetLister.Target> {
41     
42     private static Collator JavaDoc SORTER = Collator.getInstance();
43     
44     private final AntProjectCookie cookie;
45     private SortedSet JavaDoc<TargetLister.Target> allTargets;
46     
47     public AntProjectChildren (AntProjectCookie cookie) {
48         super ();
49         this.cookie = cookie;
50     }
51     
52     @Override JavaDoc
53     protected void addNotify () {
54         super.addNotify ();
55         refreshKeys(true);
56         cookie.addChangeListener (this);
57     }
58
59     @Override JavaDoc
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 JavaDoc<TargetLister.Target> _allTargets = TargetLister.getTargets(cookie);
72             Collection JavaDoc<TargetLister.Target> keys;
73             synchronized (this) {
74                 if (allTargets == null && !createKeys) {
75                     // Aynch refresh after removeNotify; ignore. (#44428)
76
return;
77                 }
78                 allTargets = new TreeSet JavaDoc<TargetLister.Target>(this);
79                 allTargets.addAll(_allTargets);
80                 Iterator JavaDoc<TargetLister.Target> it = allTargets.iterator();
81                 while (it.hasNext()) {
82                     TargetLister.Target t = it.next();
83                     if (t.isOverridden()) {
84                         // Don't include these.
85
it.remove();
86                     }
87                 }
88                 keys = allTargets;
89             }
90             if (keys != null) { // #65235
91
setKeys(keys);
92             }
93         } catch (IOException JavaDoc e) {
94             // XXX should mark the project node as being somehow in error
95
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 JavaDoc 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             // #44491: was not displaying overridden targets.
114
return System.identityHashCode(t1) - System.identityHashCode(t2);
115         }
116     }
117     
118 }
119
Popular Tags