Lines 7-108
Link Here
|
7 |
* http://www.sun.com/ |
7 |
* http://www.sun.com/ |
8 |
* |
8 |
* |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
9 |
* The Original Code is NetBeans. The Initial Developer of the Original |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2001 Sun |
10 |
* Code is Sun Microsystems, Inc. Portions Copyright 1997-2002 Sun |
11 |
* Microsystems, Inc. All Rights Reserved. |
11 |
* Microsystems, Inc. All Rights Reserved. |
12 |
*/ |
12 |
*/ |
13 |
package org.netbeans.modules.xml.core.lib; |
13 |
package org.netbeans.modules.xml.core.lib; |
14 |
|
14 |
|
15 |
import java.util.Collection; |
15 |
import java.util.*; |
16 |
import java.util.ArrayList; |
|
|
17 |
|
16 |
|
18 |
import org.openide.util.Lookup; |
17 |
import org.openide.util.*; |
19 |
import org.openide.util.LookupListener; |
|
|
20 |
import org.openide.util.LookupEvent; |
21 |
|
18 |
|
22 |
/** |
19 |
/** |
23 |
* |
20 |
* |
24 |
* @author Libor Kramolis |
21 |
* @author Libor Kramolis, Jesse Glick |
25 |
* @version 0.1 |
22 |
* @version 0.2 |
26 |
*/ |
23 |
*/ |
27 |
public abstract class LookupManager { |
24 |
public abstract class LookupManager { |
28 |
/** */ |
|
|
29 |
private final Class lookupClass; |
30 |
/** */ |
31 |
private Lookup.Result lookupResult; |
32 |
/** */ |
33 |
private Collection lastResult; |
34 |
|
35 |
// |
36 |
// init |
37 |
// |
38 |
|
39 |
/** Create new LookupManager. */ |
40 |
public LookupManager (Class clazz) { |
41 |
lookupClass = clazz; |
42 |
} |
43 |
|
25 |
|
44 |
/** |
26 |
private static final Map handles = new WeakHashMap(); // Map<Class,Handle> |
45 |
*/ |
27 |
|
46 |
private Lookup.Result getLookupResult () { |
28 |
private static final class Handle implements LookupListener { |
47 |
if ( lookupResult == null ) { |
29 |
|
48 |
lookupResult = (Lookup.getDefault()).lookup (new Lookup.Template (lookupClass)); |
30 |
private final Class clazz; |
49 |
lookupResult.addLookupListener (new LookupListenerImpl()); |
31 |
private Lookup.Result lookupResult = null; |
|
|
32 |
private Collection lastResult = null; |
33 |
private final Set lms = new WeakSet(300); // Set<LookupManager> |
34 |
|
35 |
private Handle(Class clazz) { |
36 |
this.clazz = clazz; |
50 |
} |
37 |
} |
51 |
return lookupResult; |
|
|
52 |
} |
53 |
|
38 |
|
54 |
|
39 |
public void register(LookupManager lm) { |
55 |
// |
40 |
synchronized (lms) { |
56 |
// itself |
41 |
lms.add(lm); |
57 |
// |
42 |
} |
|
|
43 |
} |
58 |
|
44 |
|
59 |
/** |
45 |
private Lookup.Result getLookupResult () { |
60 |
*/ |
46 |
if ( lookupResult == null ) { |
61 |
protected final Collection getResult () { |
47 |
lookupResult = (Lookup.getDefault()).lookup (new Lookup.Template (clazz)); |
62 |
lastResult = getLookupResult().allInstances(); |
48 |
lookupResult.addLookupListener (this); |
|
|
49 |
} |
50 |
return lookupResult; |
51 |
} |
63 |
|
52 |
|
64 |
return lastResult; |
53 |
public void resultChanged (LookupEvent evt) { |
65 |
} |
54 |
Collection currentResult = getLookupResult().allInstances(); |
66 |
|
55 |
|
67 |
/** |
56 |
Collection removed = new HashSet(lastResult); |
68 |
*/ |
57 |
removed.removeAll(currentResult); |
69 |
private void updateResult () { |
58 |
Collection added = new HashSet(currentResult); |
70 |
Collection currentResult = getLookupResult().allInstances(); |
59 |
added.removeAll(lastResult); |
71 |
Collection lastResultCopy = new ArrayList (lastResult); |
60 |
if (!removed.isEmpty() || !added.isEmpty()) { |
72 |
Collection currentResultCopy = new ArrayList (currentResult); |
61 |
synchronized (lms) { |
73 |
|
62 |
Iterator it = lms.iterator(); |
74 |
if ( lastResultCopy.removeAll (currentResult) ) { |
63 |
while (it.hasNext()) { |
75 |
removedFromResult (lastResultCopy); |
64 |
LookupManager lm = (LookupManager)it.next(); |
|
|
65 |
if (!removed.isEmpty()) lm.removedFromResult(removed); |
66 |
if (!added.isEmpty()) lm.addedToResult(added); |
67 |
} |
68 |
} |
69 |
} |
70 |
|
71 |
lastResult = currentResult; |
76 |
} |
72 |
} |
77 |
if ( currentResultCopy.removeAll (lastResult) ) { |
73 |
|
78 |
addedToResult (currentResultCopy); |
74 |
public Collection getInstances() { |
|
|
75 |
if (lastResult == null) { |
76 |
lastResult = getLookupResult().allInstances(); |
77 |
} |
78 |
return lastResult; |
79 |
} |
79 |
} |
80 |
|
80 |
|
81 |
lastResult = currentResult; |
|
|
82 |
} |
81 |
} |
|
|
82 |
|
83 |
private Handle handle = null; |
83 |
|
84 |
|
84 |
/** |
85 |
// |
85 |
*/ |
86 |
// init |
86 |
protected abstract void removedFromResult (Collection removed); |
87 |
// |
|
|
88 |
|
89 |
/** Create new LookupManager. Call register() when ready. */ |
90 |
public LookupManager () {} |
87 |
|
91 |
|
88 |
/** |
92 |
/** To be called when it is fully initialized and ready to receive events. |
|
|
93 |
* Subclasses may wish to call addedToResult(getResults()) immediately. |
89 |
*/ |
94 |
*/ |
90 |
protected abstract void addedToResult (Collection added); |
95 |
protected final void register(Class clazz) { |
|
|
96 |
if (handle != null) throw new IllegalStateException(); |
97 |
synchronized (handles) { |
98 |
handle = (Handle)handles.get(clazz); |
99 |
if (handle == null) { |
100 |
handles.put(clazz, handle = new Handle(clazz)); |
101 |
} |
102 |
} |
103 |
handle.register(this); |
104 |
} |
91 |
|
105 |
|
|
|
106 |
protected final Collection getResult() { |
107 |
return handle.getInstances(); |
108 |
} |
92 |
|
109 |
|
93 |
// |
110 |
// |
94 |
// class LookupListenerImpl |
111 |
// itself |
95 |
// |
112 |
// |
96 |
|
113 |
|
97 |
/** |
114 |
/** |
98 |
*/ |
115 |
*/ |
99 |
private class LookupListenerImpl implements LookupListener { |
116 |
protected abstract void removedFromResult (Collection removed); |
100 |
/** |
|
|
101 |
*/ |
102 |
public void resultChanged (LookupEvent evt) { |
103 |
LookupManager.this.updateResult(); |
104 |
} |
105 |
|
117 |
|
106 |
} // end: class LookupListenerImpl |
118 |
/** |
|
|
119 |
*/ |
120 |
protected abstract void addedToResult (Collection added); |
107 |
|
121 |
|
108 |
} |
122 |
} |