KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > core > lib > LookupManager


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 package org.netbeans.modules.xml.core.lib;
20
21 import java.util.*;
22
23 import org.openide.util.*;
24
25 /**
26  *
27  * @author Libor Kramolis, Jesse Glick
28  * @version 0.2
29  */

30 public abstract class LookupManager {
31     /** */
32     private static final Map handles = new WeakHashMap(); // Map<Class,Handle>
33

34     /** */
35     private Handle handle = null;
36
37
38     //
39
// init
40
//
41

42     /**
43      * Create new LookupManager. Call register() when ready.
44      */

45     public LookupManager () {
46     }
47
48
49     /** To be called when it is fully initialized and ready to receive events.
50      * Subclasses may wish to call addedToResult (getResults()) immediately.
51      */

52     protected final void register (Class JavaDoc clazz) {
53         if ( handle != null ) {
54             throw new IllegalStateException JavaDoc();
55         }
56         synchronized (handles) {
57             handle = (Handle)handles.get (clazz);
58             if ( handle == null ) {
59                 handles.put (clazz, handle = new Handle (clazz));
60             }
61         }
62         handle.register (this);
63     }
64
65
66     //
67
// itself
68
//
69

70     /**
71      */

72     protected final Collection getResult() {
73         return handle.getInstances();
74     }
75
76
77     /**
78      */

79     protected abstract void removedFromResult (Collection removed);
80
81     /**
82      */

83     protected abstract void addedToResult (Collection added);
84
85
86     //
87
// class Handle
88
//
89

90     /**
91      *
92      */

93     private static final class Handle implements LookupListener {
94
95         private final Class JavaDoc clazz;
96         private Lookup.Result lookupResult = null;
97         private Collection lastResult = null;
98         private final Set lms = new WeakSet(300); // Set<LookupManager>
99

100         //
101
// init
102
//
103

104         /**
105          */

106         private Handle (Class JavaDoc clazz) {
107             this.clazz = clazz;
108         }
109
110         /**
111          */

112         public void register (LookupManager lm) {
113             synchronized (lms) {
114                 lms.add (lm);
115             }
116         }
117
118         
119         //
120
// itself
121
//
122

123         /**
124          */

125         private Lookup.Result getLookupResult () {
126             if ( lookupResult == null ) {
127                 lookupResult = (Lookup.getDefault()).lookup (new Lookup.Template (clazz));
128                 lookupResult.addLookupListener (this);
129             }
130             return lookupResult;
131         }
132
133         /**
134          */

135         public void resultChanged (LookupEvent evt) {
136             Collection currentResult = getLookupResult().allInstances();
137
138             Collection removed = new HashSet (lastResult);
139             removed.removeAll (currentResult);
140             Collection added = new HashSet (currentResult);
141             added.removeAll (lastResult);
142
143             if ( ( removed.isEmpty() == false ) ||
144                  ( added.isEmpty() == false ) ) {
145                 synchronized (lms) {
146                     Iterator it = lms.iterator();
147                     while (it.hasNext()) {
148                         LookupManager lm = (LookupManager)it.next();
149                         if ( removed.isEmpty() == false ) {
150                             lm.removedFromResult(removed);
151                         }
152                         if ( added.isEmpty() == false ) {
153                             lm.addedToResult(added);
154                         }
155                     }
156                 }
157             }
158             
159             lastResult = currentResult;
160         }
161
162         /**
163          */

164         public Collection getInstances() {
165             //!!! can we use caching? I'm affraid we cannot because
166
// lookup callbakcs are asynchronous so we can miss some
167
// registrations (it may be crucuial for cookies)
168
if (lastResult == null) {
169                 lastResult = getLookupResult().allInstances();
170             }
171             return lastResult;
172         }
173
174     } // end: class Handle
175

176 }
177
Popular Tags