KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > registry > StateUpdater


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.netbeans.core.registry;
21
22 import org.netbeans.api.convertor.Convertors;
23 import org.openide.modules.ModuleInfo;
24 import org.openide.util.Lookup;
25 import org.openide.util.LookupEvent;
26 import org.openide.util.LookupListener;
27 import org.openide.util.WeakListeners;
28
29 import java.beans.PropertyChangeEvent JavaDoc;
30 import java.beans.PropertyChangeListener JavaDoc;
31 import java.util.*;
32
33 /**
34  * This class listens on installed modules and installed convertors
35  * and whenever there is a change it notifies all registered root contexts.
36  * Root contexts should in turn notify all their existing instances of ContextImpl
37  * that installed modules/convertors has changed and that some bindings might
38  * need to be enabled or disable. The root contexts are weakly referenced so
39  * that they can be GCed.
40  *
41  * There is only one instance of this class.
42  *
43  */

44 final class StateUpdater {
45
46     private Lookup.Result query;
47     private ArrayList enabledModules;
48     
49     private static StateUpdater DEFAULT;
50     
51     private WeakHashMap list = new WeakHashMap();
52     private final ModuleInfoLkpListener lookupListener = new ModuleInfoLkpListener();
53     private final ConvertorsPropertyChangeListener propertyChangeListener = new ConvertorsPropertyChangeListener();
54
55     private StateUpdater() {
56         init();
57     }
58     
59     static synchronized StateUpdater getDefault() {
60         if (DEFAULT == null) {
61             DEFAULT= new StateUpdater();
62         }
63         return DEFAULT;
64     }
65     
66     private void init() {
67         query = Lookup.getDefault().lookup(new Lookup.Template(ModuleInfo.class));
68         enabledModules = getModules();
69         // Weak listeners might not be needed, because StateUpdater is static:
70
query.addLookupListener((LookupListener)WeakListeners.create(LookupListener.class, lookupListener, query));
71         Convertors convertors = Convertors.getDefault();
72         convertors.addPropertyChangeListener(WeakListeners.propertyChange(propertyChangeListener, convertors));
73     }
74     
75     private ArrayList getModules() {
76         Iterator it = query.allInstances().iterator();
77         ArrayList m = new ArrayList();
78         while (it.hasNext()) {
79             ModuleInfo mi = (ModuleInfo)it.next();
80             if (mi.isEnabled()) {
81                 m.add(cutOffVersion(mi.getCodeName()));
82             }
83         }
84         return m;
85     }
86
87     boolean isModuleEnabled(String JavaDoc moduleCodeName) {
88         return enabledModules.contains(cutOffVersion(moduleCodeName));
89     }
90     
91     // some .setting files are using incorect version number
92
// or are not using module version number at all.
93
// cut off version and ignore it
94
static String JavaDoc cutOffVersion(String JavaDoc moduleCodeName) {
95         int index = moduleCodeName.lastIndexOf('/');
96         if (index != -1) {
97             moduleCodeName = moduleCodeName.substring(0, index);
98         }
99         return moduleCodeName;
100     }
101     
102     /** Root context will be weakly referenced so that it can be GCed. */
103     void registerRootContext(ContextImpl root) {
104         list.put(root, null);
105     }
106
107     private class ModuleInfoLkpListener implements LookupListener {
108         public void resultChanged(LookupEvent ev) {
109             // Called whenever the list of modules is changed.
110
// CLARIFY: will module reload be fired as module
111
// disable and enable? If not this will not work.
112
Set roots = list.keySet();
113             if (roots.size() == 0) {
114                 return;
115             }
116             ArrayList modules = getModules();
117             ArrayList added = new ArrayList(modules);
118             added.removeAll(enabledModules);
119             ArrayList removed = new ArrayList(enabledModules);
120             removed.removeAll(modules);
121             enabledModules = modules;
122         
123             Iterator all = roots.iterator();
124             while (all.hasNext()) {
125                 ContextImpl root = (ContextImpl)all.next();
126                 root.getContextCache().modulesChanged(added, removed);
127             }
128         }
129     }
130
131     private class ConvertorsPropertyChangeListener implements PropertyChangeListener JavaDoc {
132         public void propertyChange(PropertyChangeEvent JavaDoc evt) {
133             Set roots = list.keySet();
134             if (roots.size() == 0) {
135                 return;
136             }
137             if (evt.getPropertyName().equals(Convertors.CONVERTOR_DESCRIPTORS)) {
138                 Set oldConvertors = (Set)evt.getOldValue();
139                 Set newConvertors = (Set)evt.getNewValue();
140                 // current impl of convertors calculates diff and sends
141
// correct old and new value
142
assert oldConvertors != null && newConvertors != null;
143
144                 ArrayList added = new ArrayList(newConvertors);
145                 added.removeAll(oldConvertors);
146                 ArrayList removed = new ArrayList(oldConvertors);
147                 removed.removeAll(newConvertors);
148             
149                 Iterator all = roots.iterator();
150                 while (all.hasNext()) {
151                     ContextImpl root = (ContextImpl)all.next();
152                     root.getContextCache().modulesChanged(added, removed);
153                 }
154             }
155         }
156     }
157     
158 /*
159     public void unregisterRootContext(ContextImpl root) {
160         list.remove(root);
161     }
162 */

163     
164     
165 }
166     
167
Popular Tags