Fix bug 11465
authorEd Swartz <ed.swartz@nokia.com>
Mon, 12 Jul 2010 10:30:21 -0500
changeset 1606 32971633ac9f
parent 1594 92ca7b538bd2
child 1607 36d484a2e680
Fix bug 11465
debuggercdi/com.nokia.cdt.debug.launch/src/com/nokia/cdt/internal/debug/launch/ui/FileTransferTab.java
debuggercdi/com.nokia.cdt.debug.launch/src/com/nokia/cdt/internal/debug/launch/ui/FilesBlock.java
--- a/debuggercdi/com.nokia.cdt.debug.launch/src/com/nokia/cdt/internal/debug/launch/ui/FileTransferTab.java	Wed Jul 07 15:29:48 2010 -0500
+++ b/debuggercdi/com.nokia.cdt.debug.launch/src/com/nokia/cdt/internal/debug/launch/ui/FileTransferTab.java	Mon Jul 12 10:30:21 2010 -0500
@@ -35,6 +35,7 @@
 import org.eclipse.swt.widgets.Control;
 import org.eclipse.ui.PlatformUI;
 
+import java.io.File;
 import java.util.*;
 
 public class FileTransferTab extends CLaunchConfigurationTab {
@@ -127,11 +128,11 @@
 	 */
 	public void performApply(ILaunchConfigurationWorkingCopy configuration) {
 		FileToTransfer[] files = fFilesBlock.getFiles();
-		String filesString = ""; //$NON-NLS-1$
+		StringBuilder filesString = new StringBuilder();
 		for (int i=0; i<files.length; i++) {
-			filesString += files[i].getHostPath() + "," + files[i].getTargetPath() + "," + (files[i].getEnabled() ? "1" : "0") + ","; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
+			filesString.append(files[i].getHostPath()).append(",").append(files[i].getTargetPath()).append(",").append(files[i].getEnabled() ? "1" : "0").append(","); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
 		}
-		configuration.setAttribute( PreferenceConstants.J_PN_FilesToTransfer, filesString);
+		configuration.setAttribute( PreferenceConstants.J_PN_FilesToTransfer, filesString.toString());
 	}
 
 	/* (non-Javadoc)
@@ -176,9 +177,12 @@
 			// be building before launch which would build the missing
 			// files before launching the debugger.
 			for (FileToTransfer file : fFilesBlock.getFiles()) {
-				if (file.getEnabled() && !new Path(file.getHostPath()).toFile().exists()) {
-					setMessage(Messages.getString("FileTransferTab.HostFilesDontExist")); //$NON-NLS-1$
-					break;
+				if (file.getEnabled()) {
+					File hostFile = new Path(file.getHostPath()).toFile();
+					if (!fFilesBlock.fileExists(hostFile)) {
+						setMessage(Messages.getString("FileTransferTab.HostFilesDontExist")); //$NON-NLS-1$
+						break;
+					}
 				}
 			}
 		}		
--- a/debuggercdi/com.nokia.cdt.debug.launch/src/com/nokia/cdt/internal/debug/launch/ui/FilesBlock.java	Wed Jul 07 15:29:48 2010 -0500
+++ b/debuggercdi/com.nokia.cdt.debug.launch/src/com/nokia/cdt/internal/debug/launch/ui/FilesBlock.java	Mon Jul 12 10:30:21 2010 -0500
@@ -19,7 +19,9 @@
 import java.io.File;
 import java.util.ArrayList;
 import java.util.Iterator;
+import java.util.LinkedHashMap;
 import java.util.List;
+import java.util.Map;
 
 import org.eclipse.core.runtime.Path;
 import org.eclipse.jface.dialogs.IDialogSettings;
@@ -61,6 +63,7 @@
 import org.eclipse.swt.widgets.TableColumn;
 
 import com.nokia.cdt.internal.debug.launch.LaunchPlugin;
+import com.nokia.cpp.internal.api.utils.core.Pair;
 
 /**
  * A composite that displays files in a table. Files can be 
@@ -101,6 +104,12 @@
 	// index of column used for sorting
 	private int fSortColumn = 1;
 	
+	// cache of file info: we expect this to remain relatively static while
+	// any UI using FilesBlock is up.
+	private static Map<File, Pair<Long, Boolean>> fFileInfoCache = new LinkedHashMap<File, Pair<Long,Boolean>>();
+	private static long fFileInfoCacheFlushTime = 0;
+	private static final int CACHE_CHECK_QUANTUM = 60 * 1000;
+
 	/** 
 	 * Content provider to show a list of files to be transferred
 	 */ 
@@ -149,7 +158,7 @@
 				// add warning icon for any host files that don't exist
 				FileToTransfer file = (FileToTransfer)element;
 				File hostFile = new Path(file.getHostPath()).toFile();
-				if (!hostFile.exists()) {
+				if (!fileExists(hostFile)) {
 					return LaunchPlugin.getImageDescriptor("icons/Launch/etool16/warning_obj.gif").createImage();
 				}
 			}
@@ -160,6 +169,7 @@
 	
 	FilesBlock(FileTransferTab launchTab) {
 		fLaunchTab = launchTab;
+		fFileInfoCacheFlushTime = System.currentTimeMillis();
 	}
 
 	/**
@@ -541,7 +551,7 @@
 	private void removeFiles() {
 		IStructuredSelection selection= (IStructuredSelection)fFileList.getSelection();
 		FileToTransfer[] files = new FileToTransfer[selection.size()];
-		Iterator iter = selection.iterator();
+		Iterator<?> iter = selection.iterator();
 		int i = 0;
 		while (iter.hasNext()) {
 			files[i] = (FileToTransfer)iter.next();
@@ -633,4 +643,32 @@
 	Viewer getViewer() {
 		return fFileList;
 	}
+	
+	boolean fileExists(File file) {
+		Pair<Long, Boolean> entry = fFileInfoCache.get(file);
+		
+		// recheck the status occasionally, either after time passes or
+		// whenever the UI is recreated (as in constructor)
+		if (System.currentTimeMillis() >= fFileInfoCacheFlushTime) {
+			// remove entries not checked at all in a while
+			long deadLine = System.currentTimeMillis() - CACHE_CHECK_QUANTUM;
+			Iterator<Pair<Long, Boolean>> iter = fFileInfoCache.values().iterator();
+			while (iter.hasNext()) {
+				Pair<Long, Boolean> ientry = iter.next();
+				if (ientry.first < deadLine) {
+					iter.remove();
+				}
+			}
+			fFileInfoCacheFlushTime = System.currentTimeMillis() + CACHE_CHECK_QUANTUM;
+		}
+		
+		if (entry == null) {
+			//System.out.println("Checking " + file);
+			entry = new Pair<Long, Boolean>(System.currentTimeMillis(), file.exists());
+			fFileInfoCache.put(file, entry);
+		} else {
+			//System.out.println("Not checking " + file);
+		}
+		return entry.second;
+	}
 }