1 19 20 package org.openide.filesystems; 21 22 import java.lang.ref.Reference ; 23 import java.lang.ref.WeakReference ; 24 import java.util.Enumeration ; 25 import org.openide.util.NbCollections; 26 import org.openide.util.RequestProcessor; 27 28 32 final class RefreshRequest extends Object implements Runnable { 33 34 private static final int REFRESH_COUNT = 30; 35 private static RequestProcessor REFRESHER = new RequestProcessor("FS refresher"); 37 38 private Reference <AbstractFileSystem> system; 39 40 41 private Enumeration <AbstractFolder> en; 42 43 44 private int refreshTime; 45 46 47 private RequestProcessor.Task task; 48 49 53 public RefreshRequest(AbstractFileSystem fs, int ms) { 54 system = new WeakReference <AbstractFileSystem>(fs); 55 refreshTime = ms; 56 task = REFRESHER.post(this, ms, Thread.MIN_PRIORITY); 57 } 58 59 61 public int getRefreshTime() { 62 return refreshTime; 63 } 64 65 67 public synchronized void stop() { 68 refreshTime = 0; 69 70 if (task == null) { 71 try { 74 wait(); 75 } catch (InterruptedException ex) { 76 } 77 } 78 } 79 80 82 public void run() { 83 int ms; 85 RequestProcessor.Task t; 86 87 synchronized (this) { 88 ms = refreshTime; 91 92 if (ms <= 0) { 93 return; 95 } 96 97 t = task; 98 99 task = null; 102 } 103 104 try { 105 doLoop(ms); 106 } finally { 107 synchronized (this) { 108 task = t; 111 112 notifyAll(); 113 } 114 115 if ((system != null) && (system.get() != null)) { 117 t.schedule(ms); 118 } else { 119 refreshTime = 0; 120 } 121 } 122 } 123 124 private void doLoop(int ms) { 125 AbstractFileSystem system = this.system.get(); 126 127 if (system == null) { 128 return; 130 } 131 132 if ((en == null) || !en.hasMoreElements()) { 133 en = NbCollections.checkedEnumerationByFilter(existingFolders(system), AbstractFolder.class, true); 135 } 136 137 for (int i = 0; (i < REFRESH_COUNT) && en.hasMoreElements(); i++) { 138 AbstractFolder fo = en.nextElement(); 139 140 if ((fo != null) && (!fo.isFolder() || fo.isInitialized())) { 141 fo.refresh(); 142 } 143 144 if (refreshTime <= 0) { 145 return; 148 } 149 } 150 151 if (!en.hasMoreElements()) { 153 en = null; 154 } 155 } 156 157 159 private static Enumeration <? extends FileObject> existingFolders(AbstractFileSystem fs) { 160 return fs.existingFileObjects(fs.getAbstractRoot()); 161 } 162 163 166 public String toString() { 167 AbstractFileSystem fs = system.get(); 168 169 return "RefreshRequest for " + ((fs == null) ? "gone FS" : fs); } 172 } 173 | Popular Tags |