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