001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans.modules.vmd.inspector;
042:
043: import org.netbeans.modules.vmd.api.inspector.InspectorFolder;
044: import org.netbeans.modules.vmd.api.model.Debug;
045: import org.netbeans.modules.vmd.api.model.TypeID;
046: import org.openide.cookies.InstanceCookie;
047: import org.openide.filesystems.*;
048: import org.openide.loaders.DataFolder;
049: import org.openide.loaders.DataObject;
050: import org.openide.util.Mutex;
051:
052: import java.io.IOException;
053: import java.lang.ref.WeakReference;
054: import java.util.*;
055:
056: /**
057: *
058: *
059: * @author Karol Harezlak
060: * Based on Core Model GlobalDescriptorRegistry
061: */
062:
063: final class GlobalFolderRegistry {
064:
065: private static final String FILE_SYSTEM_FOLDER_NAME = "/inspectorfolders"; // NOI18N
066: private static final HashMap<String, WeakReference<GlobalFolderRegistry>> registries = new HashMap<String, WeakReference<GlobalFolderRegistry>>();
067:
068: static GlobalFolderRegistry getGlobalFolderRegistry(
069: String projectType) {
070: assert Debug.isFriend(FolderRegistry.class);
071:
072: synchronized (registries) {
073: WeakReference<GlobalFolderRegistry> ref = registries
074: .get(projectType);
075: GlobalFolderRegistry registry = ref != null ? ref.get()
076: : null;
077:
078: if (registry == null) {
079: registry = new GlobalFolderRegistry(projectType);
080: registries.put(projectType,
081: new WeakReference<GlobalFolderRegistry>(
082: registry));
083: }
084: return registry;
085: }
086: }
087:
088: private final DataFolder registryFolders;
089: private final Mutex mutex = new Mutex();
090: private HashMap<TypeID, InspectorFolder> descriptors = new HashMap<TypeID, InspectorFolder>();
091: private FolderRegistry.Listener listener;
092:
093: private GlobalFolderRegistry(String projectType) {
094: assert projectType != null && projectType.length() > 0;
095: FileObject resource = Repository.getDefault()
096: .getDefaultFileSystem().findResource(
097: projectType + FILE_SYSTEM_FOLDER_NAME);
098: if (resource != null) {
099: registryFolders = DataFolder.findFolder(resource); // NOI18N
100:
101: registryFolders.getPrimaryFile().addFileChangeListener(
102: new FileChangeListener() {
103: public void fileFolderCreated(
104: FileEvent fileEvent) {
105: }
106:
107: public void fileDataCreated(FileEvent fileEvent) {
108: reloadAll();
109: };
110:
111: public void fileChanged(FileEvent fileEvent) {
112: reloadAll();
113: }
114:
115: public void fileDeleted(FileEvent fileEvent) {
116: reloadAll();
117: }
118:
119: public void fileRenamed(
120: FileRenameEvent fileRenameEvent) {
121: reloadAll();
122: }
123:
124: public void fileAttributeChanged(
125: FileAttributeEvent fileAttributeEvent) {
126: reloadAll();
127: }
128: });
129: } else
130: registryFolders = null;
131:
132: reload();
133: }
134:
135: private void reloadAll() {
136: reload();
137: listener.notifyRegistryContentChange();
138: }
139:
140: void readAccess(Runnable runnable) {
141: assert Debug.isFriend(FolderRegistry.class);
142: mutex.readAccess(runnable);
143: }
144:
145: private void reload() {
146: mutex.writeAccess(new Runnable() {
147: public void run() {
148: reloadCore();
149: }
150: });
151: }
152:
153: private void reloadCore() {
154: HashMap<TypeID, InspectorFolder> tempDescriptors = new HashMap<TypeID, InspectorFolder>();
155:
156: if (registryFolders != null) {
157: Enumeration enumeration = registryFolders.children();
158:
159: while (enumeration.hasMoreElements()) {
160: DataObject dataObject = (DataObject) enumeration
161: .nextElement();
162: InspectorFolder descriptor = dao2descriptor(dataObject);
163:
164: if (descriptor == null) {
165: Debug.warning("No descriptor", dataObject
166: .getPrimaryFile().getNameExt()); // NOI18N
167: continue;
168: }
169: TypeID type = descriptor.getTypeID();
170: if (type == null) {
171: Debug.warning("Null type descriptor", descriptor); // NOI18N
172: continue;
173: }
174: final TypeID this Type = descriptor.getTypeID();
175: if (tempDescriptors.containsKey(this Type)) {
176: Debug.warning("Duplicate descriptor", this Type); // NOI18N
177: continue;
178: }
179: tempDescriptors.put(this Type, descriptor);
180: }
181: }
182:
183: for (InspectorFolder descriptor : tempDescriptors.values())
184: resolveDescriptor(descriptor);
185: descriptors = tempDescriptors;
186: }
187:
188: private static void resolveDescriptor(InspectorFolder descriptor) {
189: resolveDescriptor(new HashSet<TypeID>(), descriptor);
190: }
191:
192: private static void resolveDescriptor(
193: HashSet<TypeID> resolvingDescriptors,
194: InspectorFolder descriptor) {
195: assert Debug.isFriend(GlobalFolderRegistry.class)
196: || Debug.isFriend(FolderRegistry.class);
197:
198: TypeID this Type = descriptor.getTypeID();
199: if (this Type == null) {
200: Debug.warning("Null Type", descriptor); // NOI18N
201: return;
202: }
203:
204: resolvingDescriptors.add(this Type);
205: }
206:
207: private static InspectorFolder dao2descriptor(DataObject dataObject) {
208: InstanceCookie.Of instanceCookie = dataObject
209: .getCookie(InstanceCookie.Of.class);
210:
211: if (instanceCookie != null) {
212: try {
213: Object instance = instanceCookie.instanceCreate();
214: if (instance instanceof InspectorFolder)
215: return (InspectorFolder) instance;
216: } catch (IOException e) {
217: Debug.warning(e);
218: } catch (ClassNotFoundException e) {
219: Debug.warning(e);
220: }
221: Debug.warning("Instance is not InspectorFolder class"); // NOI18N
222: return null;
223: }
224:
225: return null;
226: }
227:
228: Collection<InspectorFolder> getInspectorFolder() {
229: assert Debug.isFriend(FolderRegistry.class);
230: return Collections.unmodifiableCollection(descriptors.values());
231: }
232:
233: void addListener(FolderRegistry.Listener listener) {
234: this .listener = listener;
235: }
236:
237: void removeListener(FolderRegistry.Listener listener) {
238: if (this .listener != listener)
239: Debug.warning("Listener to remove != registred listener"); //NOI18N
240: listener = null;
241: }
242:
243: }
|