1 19 20 package org.netbeans.modules.project.ant; 21 22 import java.io.File ; 23 import java.util.Map ; 24 import java.util.WeakHashMap ; 25 import org.openide.filesystems.FileObject; 26 import java.lang.ref.WeakReference ; 27 import java.util.HashMap ; 28 29 import org.openide.filesystems.FileChangeListener; 30 import org.openide.filesystems.FileAttributeEvent; 31 import org.openide.filesystems.FileEvent; 32 import org.openide.filesystems.FileRenameEvent; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.util.Utilities; 35 36 38 47 public final class FileChangeSupport { 48 49 public static FileChangeSupport DEFAULT = new FileChangeSupport(); 50 51 private FileChangeSupport() {} 52 53 private final Map <FileChangeSupportListener,Map <File ,Holder>> holders = new WeakHashMap <FileChangeSupportListener,Map <File ,Holder>>(); 54 55 62 public void addListener(FileChangeSupportListener listener, File path) { 63 assert path.equals(FileUtil.normalizeFile(path)) : "Need to normalize " + path + " before passing to FCS!"; 64 Map <File ,Holder> f2H = holders.get(listener); 65 if (f2H == null) { 66 f2H = new HashMap <File ,Holder>(); 67 holders.put(listener, f2H); 68 } 69 if (f2H.containsKey(path)) { 70 throw new IllegalArgumentException ("Already listening to " + path); } 72 f2H.put(path, new Holder(listener, path)); 73 } 74 75 78 public void removeListener(FileChangeSupportListener listener, File path) { 79 assert path.equals(FileUtil.normalizeFile(path)) : "Need to normalize " + path + " before passing to FCS!"; 80 Map <File ,Holder> f2H = holders.get(listener); 81 if (f2H == null) { 82 throw new IllegalArgumentException ("Was not listening to " + path); } 84 if (!f2H.containsKey(path)) { 85 throw new IllegalArgumentException (listener + " was not listening to " + path + "; only to " + f2H.keySet()); } 87 f2H.remove(path); 88 } 89 90 private static final class Holder extends WeakReference <FileChangeSupportListener> implements FileChangeListener, Runnable { 91 92 private final File path; 93 private FileObject current; 94 private File currentF; 95 96 public Holder(FileChangeSupportListener listener, File path) { 97 super(listener, Utilities.activeReferenceQueue()); 98 assert path != null; 99 this.path = path; 100 locateCurrent(); 101 } 102 103 private void locateCurrent() { 104 FileObject oldCurrent = current; 105 currentF = path; 106 while (true) { 107 try { 108 current = FileUtil.toFileObject(currentF); 109 } catch (IllegalArgumentException x) { 110 currentF = FileUtil.normalizeFile(currentF); 112 current = FileUtil.toFileObject(currentF); 113 } 114 if (current != null) { 115 break; 116 } 117 currentF = currentF.getParentFile(); 118 if (currentF == null) { 119 return; 122 } 123 } 124 assert current != null; 126 if (current != oldCurrent) { 127 if (oldCurrent != null) { 128 oldCurrent.removeFileChangeListener(this); 129 } 130 current.addFileChangeListener(this); 131 current.getChildren(); } 133 } 134 135 private void someChange(FileObject modified) { 136 FileChangeSupportListener listener; 137 FileObject oldCurrent, nueCurrent; 138 File oldCurrentF, nueCurrentF; 139 synchronized (this) { 140 if (current == null) { 141 return; 142 } 143 listener = get(); 144 if (listener == null) { 145 return; 146 } 147 oldCurrent = current; 148 oldCurrentF = currentF; 149 locateCurrent(); 150 nueCurrent = current; 151 nueCurrentF = currentF; 152 } 153 if (modified != null && modified == nueCurrent) { 154 FileChangeSupportEvent event = new FileChangeSupportEvent(DEFAULT, FileChangeSupportEvent.EVENT_MODIFIED, path); 155 listener.fileModified(event); 156 } else { 157 boolean oldWasCorrect = path.equals(oldCurrentF); 158 boolean nueIsCorrect = path.equals(nueCurrentF); 159 if (oldWasCorrect && !nueIsCorrect) { 160 FileChangeSupportEvent event = new FileChangeSupportEvent(DEFAULT, FileChangeSupportEvent.EVENT_DELETED, path); 161 listener.fileDeleted(event); 162 } else if (nueIsCorrect && !oldWasCorrect) { 163 FileChangeSupportEvent event = new FileChangeSupportEvent(DEFAULT, FileChangeSupportEvent.EVENT_CREATED, path); 164 listener.fileCreated(event); 165 } 166 } 167 } 168 169 public void fileChanged(FileEvent fe) { 170 someChange(fe.getFile()); 171 } 172 173 public void fileDeleted(FileEvent fe) { 174 someChange(null); 175 } 176 177 public void fileDataCreated(FileEvent fe) { 178 someChange(null); 179 } 180 181 public void fileFolderCreated(FileEvent fe) { 182 someChange(null); 183 } 184 185 public void fileRenamed(FileRenameEvent fe) { 186 someChange(null); 187 } 188 189 public void fileAttributeChanged(FileAttributeEvent fe) { 190 } 192 193 public synchronized void run() { 194 if (current != null) { 195 current.removeFileChangeListener(this); 196 current = null; 197 } 198 } 199 200 } 201 202 } 203 | Popular Tags |