[Arakhnę-Dev] [427] * The embodied file chooser is displaying the filenames with a case-insensitive sorting algorithm .

[ Thread Index | Date Index | More arakhne.org/dev Archives ]


Revision: 427
Author:   galland
Date:     2013-04-27 14:56:49 +0200 (Sat, 27 Apr 2013)
Log Message:
-----------
* The embodied file chooser is displaying the filenames with a case-insensitive sorting algorithm.

Modified Paths:
--------------
    trunk/ui/ui-android/src/main/java/org/arakhne/afc/ui/android/filechooser/AsyncFileLoader.java

Modified: trunk/ui/ui-android/src/main/java/org/arakhne/afc/ui/android/filechooser/AsyncFileLoader.java
===================================================================
--- trunk/ui/ui-android/src/main/java/org/arakhne/afc/ui/android/filechooser/AsyncFileLoader.java	2013-04-27 12:54:34 UTC (rev 426)
+++ trunk/ui/ui-android/src/main/java/org/arakhne/afc/ui/android/filechooser/AsyncFileLoader.java	2013-04-27 12:56:49 UTC (rev 427)
@@ -23,8 +23,11 @@
 import java.io.File;
 import java.io.FileFilter;
 import java.util.ArrayList;
+import java.util.Comparator;
 import java.util.List;
 
+import org.arakhne.afc.util.ListUtil;
+
 import android.content.Context;
 import android.os.FileObserver;
 import android.support.v4.content.AsyncTaskLoader;
@@ -47,7 +50,7 @@
 	
 	private FileObserver fileObserver;
 
-	private List<File> bufferedList = null;
+	private List<File> discoveredFiles = null;
 
 	/**
 	 * @param context is the execution context of the task.
@@ -59,6 +62,16 @@
 		this.path = path;
 		this.filter = fileFilter;
 	}
+	
+	/** Replies if the given file is hidden in the file chooser.
+	 * 
+	 * @param file
+	 * @return <code>true</code> if hidden; <code>false</code> otherwise.
+	 */
+	@SuppressWarnings("static-method")
+	protected boolean isHiddenFile(File file) {
+		return file.isHidden() || file.getName().equalsIgnoreCase("lost.dir"); //$NON-NLS-1$
+	}
 
 	/**
 	 * {@inheritDoc}
@@ -74,23 +87,17 @@
 		}
 		List<File> list = new ArrayList<File>(array.length);
 		// Dichotomic insertion
-		int f, l, c;
-		File cf;
+		Comparator<File> comparator = new Comparator<File>() {
+			@Override
+			public int compare(File lhs, File rhs) {
+				String ln = lhs.getName();
+				String rn = rhs.getName();
+				return ln.compareToIgnoreCase(rn);
+			}
+		};
 		for(File file : array) {
-			if (!file.isHidden() && !file.getName().equalsIgnoreCase("lost.dir")) { //$NON-NLS-1$
-				f = 0;
-				l = list.size()-1;
-				while (f<=l) {
-					c = (f+l)/2;
-					cf = array[c];
-					if (file.compareTo(cf)<=0) { 
-						l = c-1;
-					}
-					else {
-						f = c+1;
-					}
-				}
-				list.add(f,file);
+			if (!isHiddenFile(file)) {
+				ListUtil.add(list, comparator, file, false, true);
 			}
 		}
 		return list;
@@ -102,7 +109,7 @@
 	@Override
 	protected void onStartLoading() {
 		// A list is already available. Publish it.
-		if (this.bufferedList != null) deliverResult(this.bufferedList);
+		if (this.discoveredFiles != null) deliverResult(this.discoveredFiles);
 
 		if (this.fileObserver == null) {
 			this.fileObserver = new FileObserver(this.path.getAbsolutePath(), 
@@ -118,7 +125,7 @@
 		}
 		this.fileObserver.startWatching();
 
-		if (takeContentChanged() || this.bufferedList==null)
+		if (takeContentChanged() || this.discoveredFiles==null)
 			forceLoad();
 	}
 
@@ -131,11 +138,11 @@
 			return;
 		}
 
-		List<File> old = this.bufferedList;
-		this.bufferedList = data;
+		List<File> old = this.discoveredFiles;
+		this.discoveredFiles = data;
 
 		if (isStarted())
-			super.deliverResult(this.bufferedList);
+			super.deliverResult(this.discoveredFiles);
 
 		if (old!=null && old!=data)
 			unwatch();
@@ -156,9 +163,9 @@
 	protected void onReset() {
 		onStopLoading();
 
-		if (this.bufferedList!=null) {
+		if (this.discoveredFiles!=null) {
 			unwatch();
-			this.bufferedList = null;
+			this.discoveredFiles = null;
 		}
 	}
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/