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 java.lang.ref.WeakReference;
044: import java.util.Collection;
045: import java.util.Collections;
046: import java.util.HashMap;
047:
048: import org.netbeans.modules.vmd.api.inspector.InspectorFolder;
049: import org.netbeans.modules.vmd.api.model.TypeID;
050: import org.openide.util.Mutex;
051:
052: /**
053: *
054: * @author Karol Harezlak
055: */
056: final class FolderRegistry {
057:
058: private static final HashMap<String, WeakReference<FolderRegistry>> registries = new HashMap<String, WeakReference<FolderRegistry>>();
059:
060: private Mutex mutex = new Mutex();
061: private HashMap<TypeID, InspectorFolder> folders = new HashMap<TypeID, InspectorFolder>();
062: private GlobalFolderRegistry globalFolderRegistry;
063:
064: static FolderRegistry getRegistry(String projectType,
065: String projectID) {
066: synchronized (registries) {
067: WeakReference<FolderRegistry> ref = registries
068: .get(projectID);
069: FolderRegistry registry = ref != null ? ref.get() : null;
070: if (registry == null) {
071: registry = new FolderRegistry(projectType);
072: registries.put(projectID,
073: new WeakReference<FolderRegistry>(registry));
074: }
075: return registry;
076: }
077: }
078:
079: private FolderRegistry(String projectType) {
080: globalFolderRegistry = GlobalFolderRegistry
081: .getGlobalFolderRegistry(projectType);
082: reload();
083: }
084:
085: private boolean isAccess() {
086: return mutex.isReadAccess() || mutex.isWriteAccess();
087: }
088:
089: void readAccess(final Runnable runnable) {
090: globalFolderRegistry.readAccess(new Runnable() {
091: public void run() {
092: mutex.readAccess(runnable);
093: }
094: });
095: }
096:
097: private void writeAccess(final Runnable runnable) {
098: globalFolderRegistry.readAccess(new Runnable() {
099: public void run() {
100: mutex.writeAccess(runnable);
101: }
102: });
103: }
104:
105: private void reload() {
106: writeAccess(new Runnable() {
107: public void run() {
108: reloadCore();
109: }
110: });
111: }
112:
113: private void reloadCore() {
114: HashMap<TypeID, InspectorFolder> tempFolders = new HashMap<TypeID, InspectorFolder>();
115: Collection<InspectorFolder> _folders = globalFolderRegistry
116: .getInspectorFolder();
117:
118: for (InspectorFolder folder : _folders)
119: tempFolders.put(folder.getTypeID(), folder);
120:
121: this .folders = tempFolders;
122: }
123:
124: Collection<InspectorFolder> getFolders() {
125: assert isAccess();
126: return Collections.unmodifiableCollection(folders.values());
127: }
128:
129: void addListener(Listener listener) {
130: globalFolderRegistry.addListener(listener);
131: }
132:
133: void removeListener(Listener listener) {
134: globalFolderRegistry.removeListener(listener);
135: }
136:
137: interface Listener {
138: void notifyRegistryContentChange();
139: }
140: }
|