1 11 package org.eclipse.core.internal.refresh; 12 13 import java.util.*; 14 import org.eclipse.core.internal.utils.Messages; 15 import org.eclipse.core.internal.utils.Policy; 16 import org.eclipse.core.resources.*; 17 import org.eclipse.core.runtime.*; 18 import org.eclipse.osgi.util.NLS; 19 20 27 public class RefreshJob extends WorkspaceJob { 28 private static final long UPDATE_DELAY = 200; 29 35 private final List fRequests; 36 37 public RefreshJob() { 38 super(Messages.refresh_jobName); 39 fRequests = new ArrayList(1); 40 } 41 42 47 private synchronized void addRequest(IResource resource) { 48 IPath toAdd = resource.getFullPath(); 49 for (Iterator it = fRequests.iterator(); it.hasNext();) { 50 IPath request = ((IResource) it.next()).getFullPath(); 51 if (toAdd.isPrefixOf(request)) 53 it.remove(); 54 else if (request.isPrefixOf(toAdd)) 56 return; 57 } 58 fRequests.add(resource); 60 } 61 62 private synchronized void addRequests(List list) { 63 fRequests.addAll(0, list); 65 } 66 67 70 public boolean belongsTo(Object family) { 71 return family == ResourcesPlugin.FAMILY_AUTO_REFRESH; 72 } 73 74 78 private List collectChildrenToDepth(IResource resource, ArrayList children, int depth) { 79 if (resource.getType() == IResource.FILE) 80 return children; 81 IResource[] members; 82 try { 83 members = ((IContainer) resource).members(); 84 } catch (CoreException e) { 85 return children; 87 } 88 for (int i = 0; i < members.length; i++) { 89 if (members[i].getType() == IResource.FILE) 90 continue; 91 if (depth <= 1) 92 children.add(members[i]); 93 else 94 collectChildrenToDepth(members[i], children, depth - 1); 95 } 96 return children; 97 } 98 99 102 private synchronized IResource nextRequest() { 103 int len = fRequests.size(); 105 if (len == 0) 106 return null; 107 return (IResource) fRequests.remove(len - 1); 108 } 109 110 113 public void refresh(IResource resource) { 114 if (resource == null) 115 return; 116 addRequest(resource); 117 schedule(UPDATE_DELAY); 118 } 119 120 123 public IStatus runInWorkspace(IProgressMonitor monitor) { 124 long start = System.currentTimeMillis(); 125 String msg = Messages.refresh_refreshErr; 126 MultiStatus errors = new MultiStatus(ResourcesPlugin.PI_RESOURCES, 1, msg, null); 127 long longestRefresh = 0; 128 try { 129 if (RefreshManager.DEBUG) 130 Policy.debug(RefreshManager.DEBUG_PREFIX + " starting refresh job"); int refreshCount = 0; 132 int depth = 2; 133 monitor.beginTask("", IProgressMonitor.UNKNOWN); IResource toRefresh; 135 while ((toRefresh = nextRequest()) != null) { 136 if (monitor.isCanceled()) 137 throw new OperationCanceledException(); 138 try { 139 refreshCount++; 140 long refreshTime = -System.currentTimeMillis(); 141 toRefresh.refreshLocal(1000 + depth, null); 142 refreshTime += System.currentTimeMillis(); 143 if (refreshTime > longestRefresh) 144 longestRefresh = refreshTime; 145 if (refreshCount % 100 == 0) 147 monitor.subTask(NLS.bind(Messages.refresh_task, Integer.toString(fRequests.size()))); 148 if (refreshCount % 1000 == 0) { 149 Thread.yield(); 151 if (longestRefresh > 2000 && depth > 1) { 153 depth = 1; 154 } 155 if (longestRefresh < 1000) { 156 depth *= 2; 157 } 158 longestRefresh = 0; 159 } 160 addRequests(collectChildrenToDepth(toRefresh, new ArrayList(), depth)); 161 } catch (CoreException e) { 162 errors.merge(new Status(IStatus.ERROR, ResourcesPlugin.PI_RESOURCES, 1, errors.getMessage(), e)); 163 } 164 } 165 } finally { 166 monitor.done(); 167 if (RefreshManager.DEBUG) 168 System.out.println(RefreshManager.DEBUG_PREFIX + " finished refresh job in: " + (System.currentTimeMillis() - start) + "ms"); } 170 if (!errors.isOK()) 171 return errors; 172 return Status.OK_STATUS; 173 } 174 175 178 public synchronized boolean shouldRun() { 179 return !fRequests.isEmpty(); 180 } 181 182 185 public void start() { 186 if (RefreshManager.DEBUG) 187 System.out.println(RefreshManager.DEBUG_PREFIX + " enabling auto-refresh"); } 189 190 193 public void stop() { 194 if (RefreshManager.DEBUG) 195 System.out.println(RefreshManager.DEBUG_PREFIX + " disabling auto-refresh"); cancel(); 197 } 198 } 199 | Popular Tags |