001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.xslt.project.spi;
020:
021: import java.util.HashMap;
022: import java.util.HashSet;
023: import java.util.LinkedHashSet;
024: import java.util.Set;
025: import java.util.concurrent.locks.Lock;
026: import java.util.concurrent.locks.ReentrantReadWriteLock;
027: import org.netbeans.api.project.Project;
028: import org.netbeans.modules.xslt.tmap.util.Util;
029: import org.openide.filesystems.FileAttributeEvent;
030: import org.openide.filesystems.FileChangeListener;
031: import org.openide.filesystems.FileEvent;
032: import org.openide.filesystems.FileObject;
033: import org.openide.filesystems.FileRenameEvent;
034:
035: /**
036: *
037: * @author Vitaly Bychkov
038: * @version 1.0
039: */
040: public class ProjectsFilesChangeHandler {
041: private Lock writeLock = new ReentrantReadWriteLock().writeLock();
042: private Project myCurrentProject;
043: private ProjectsFileChangeListener myFileChangeListener;
044: private static HashMap<String, Object> SUPPORTED_EXTS = new HashMap<String, Object>();
045: static {
046: SUPPORTED_EXTS.put("wsdl", Boolean.TRUE);
047: SUPPORTED_EXTS.put("xsd", Boolean.TRUE);
048: }
049: private ProjectsFilesChangesSupport myChangesSupport = new ProjectsFilesChangesSupport();
050:
051: private LinkedHashSet<FileObject> supportedFos = new LinkedHashSet<FileObject>();
052: private Set<FileObject> subscribedFos = new HashSet<FileObject>();
053:
054: public ProjectsFilesChangeHandler(Project currentProject) {
055: if (currentProject == null) {
056: throw new IllegalStateException(
057: "context project shouldn't be null");
058: }
059:
060: myCurrentProject = currentProject;
061: myFileChangeListener = new ProjectsFileChangeListener();
062: }
063:
064: public void subscribes() {
065: subscribes(myCurrentProject);
066: }
067:
068: public void subscribes(Project project) {
069: if (project == null) {
070: return;
071: }
072:
073: FileObject[] rootSources = Util.getProjectSources(project);
074: if (rootSources == null) {
075: return;
076: }
077:
078: for (FileObject fo : rootSources) {
079: subscribes(fo);
080: }
081: }
082:
083: private void subscribes(FileObject folder) {
084: if (folder == null) {
085: return;
086: }
087:
088: folder.addFileChangeListener(myFileChangeListener);
089: add2cache(folder);
090:
091: FileObject[] fos = null;
092: if (folder.isFolder()) {
093: fos = folder.getChildren();
094: }
095: if (fos != null) {
096: for (FileObject fo : fos) {
097: subscribes(fo);
098: }
099: }
100: }
101:
102: public void addProjectsFilesChangeListener(
103: ProjectsFilesChangeListener l) {
104: myChangesSupport.addPropertyChangeListener(l);
105: }
106:
107: public void removeProjectsFilesChangeListener(
108: ProjectsFilesChangeListener l) {
109: myChangesSupport.removePropertyChangeListener(l);
110: }
111:
112: private void add2cache(FileObject fo) {
113: assert fo != null;
114: writeLock.lock();
115: try {
116: subscribedFos.add(fo);
117: if (isSupportedFo(fo)) {
118: boolean isAddedSupported = supportedFos.add(fo);
119: if (isAddedSupported) {
120: // fire number of supported fos have been changed
121: }
122: }
123: } finally {
124: writeLock.unlock();
125: }
126: }
127:
128: private void removeFromCache(FileObject fo) {
129: assert fo != null;
130: writeLock.lock();
131: try {
132: subscribedFos.remove(fo);
133: boolean isRemovedSupported = supportedFos.remove(fo);
134: if (isRemovedSupported) {
135: // fire number of supported fos have been changed
136: }
137: } finally {
138: writeLock.unlock();
139: }
140: }
141:
142: private void clearCache() {
143: subscribedFos.clear();
144: supportedFos.clear();
145: }
146:
147: public void unsubscribes() {
148: for (FileObject fo : subscribedFos) {
149: fo.removeFileChangeListener(myFileChangeListener);
150: }
151: clearCache();
152: myChangesSupport.removeAllPropertyChangeListener();
153: }
154:
155: private static boolean isSupportedFo(FileObject fo) {
156: if (fo == null || fo.isFolder() || fo.isVirtual()) {
157: return false;
158: }
159: String ext = fo.getExt();
160: if (ext != null && SUPPORTED_EXTS.get(ext) != null) {
161: return true;
162: }
163:
164: return false;
165: }
166:
167: private class ProjectsFileChangeListener implements
168: FileChangeListener {
169:
170: public void fileFolderCreated(FileEvent fe) {
171: FileObject fo = fe.getFile();
172: subscribes(fo);
173: // throw new UnsupportedOperationException("Not supported yet.");
174: }
175:
176: public void fileDataCreated(FileEvent fe) {
177: FileObject fo = fe.getFile();
178: add2cache(fo);
179: if (isSupportedFo(fo)) {
180: myChangesSupport.fireFileAdded(fo);
181: }
182: // throw new UnsupportedOperationException("Not supported yet.");
183: }
184:
185: public void fileChanged(FileEvent fe) {
186: // throw new UnsupportedOperationException("Not supported yet.");
187: }
188:
189: public void fileDeleted(FileEvent fe) {
190: FileObject fo = fe.getFile();
191: // System.out.println("file deleted: "+fo);
192: if (isSupportedFo(fo)) {
193: fo.removeFileChangeListener(this );
194: myChangesSupport.fireFileDeleted(fo);
195: removeFromCache(fo);
196: }
197: // throw new UnsupportedOperationException("Not supported yet.");
198: }
199:
200: public void fileRenamed(FileRenameEvent fe) {
201: FileObject fo = fe.getFile();
202: if (isSupportedFo(fo)) {
203: myChangesSupport.fireFileRenamed(fe);
204: }
205: // throw new UnsupportedOperationException("Not supported yet.");
206: }
207:
208: public void fileAttributeChanged(FileAttributeEvent fe) {
209: // throw new UnsupportedOperationException("Not supported yet.");
210: }
211: }
212:
213: }
|