Index: gli/trunk/src/org/greenstone/gatherer/collection/CollectionTreeNode.java
===================================================================
--- gli/trunk/src/org/greenstone/gatherer/collection/CollectionTreeNode.java	(revision 16579)
+++ gli/trunk/src/org/greenstone/gatherer/collection/CollectionTreeNode.java	(revision 16637)
@@ -33,4 +33,7 @@
 import org.greenstone.gatherer.file.FileNode;
 import org.greenstone.gatherer.util.JarTools;
+import java.util.Set;
+import java.util.Iterator;
+import java.nio.charset.Charset;
 
 
@@ -48,4 +51,6 @@
     private boolean is_srcreplaceable = false;
 
+    private String displayFileName = null;
+
     public CollectionTreeNode(File file)
     {
@@ -55,4 +60,72 @@
 	// To work with replace_srcdoc_with_html.pl
 	this.is_srcreplaceable = CollectionDesignManager.plugin_manager.isFileSrcReplaceable(file);
+    
+	displayFileName = calcDisplayString();
+    }
+
+    /** Similar to calcDisplayString, but this version only checks whether the filename
+     * String is UTF8. If so, it converts it into UTF8 and returns it. If the filename
+     * is not UTF8, it is returned unchanged.
+     */ 
+    protected String quickCalcDisplayString() {
+	String filename = super.toString();
+	try{
+	    String utf8filename = new String(filename.getBytes(), "UTF8");
+	    if(utf8filename.indexOf('\ufffd') == -1) {
+		return utf8filename;
+	    } else { // contains the character indicating that it's invalid utf8
+		// return the original string
+		return filename;
+	    }
+	} catch(java.io.UnsupportedEncodingException e) {
+	    return filename;
+	}
+    }
+
+    /** This method returns a string representation of the filenodes in the Collection
+     * Tree, that can then be displayed in the tree. 
+     * We'll initially assume that the filenames are utf8 encoded and so convert the
+     * filename into utf8 for proper presentation in the Collection tree pane.
+     * If the filenames are not utf8, then the conversion would have introduced funny
+     * characters. Therefore, when converting to utf8, if the converted filename 
+     * contains the special character '\ufffd', then we know the conversion did not work
+     * and we return the original string which may or may not be properly presented by
+     * default.
+     * See http://java.sun.com/j2se/1.4.2/docs/api/java/nio/charset/CharsetDecoder.html
+     * which says "How a decoding error is handled depends upon the action requested for 
+     * that type of error, which is described by an instance of the CodingErrorAction class.
+     * The possible error actions are to ignore the erroneous input, report the error to
+     * the invoker via the returned CoderResult object, or replace the erroneous input with
+     * the current value of the replacement string. The replacement has the initial value 
+     * "\uFFFD"; its value may be changed via the replaceWith method."
+     * The following made me think that String(byte[], String charsetName) constructor may
+     * use the replacement value \uFFFD.
+     * http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20512969.html
+     * mentions the following which made me think of this: 
+     * convertedStr = convertedStr.replace('\ufffd', ' ');
+     */
+    protected String calcDisplayString() {
+	String filename = super.toString();
+
+	try{
+	    String[] charsets = {"UTF-8", "ISO-8859-1", "US-ASCII", "UTF-16BE", "UTF-16LE", "UTF-16"};
+	    for(int i = 0; i < charsets.length; i++) {
+		String charset = charsets[i];
+		String convertedfilename = new String(filename.getBytes(), charset);
+		if(convertedfilename.indexOf('\ufffd') != -1) {
+		    // encountered the character that indicates when the conversion is invalid
+		    convertedfilename = null;
+		    continue; // try to use the next charset to encode the filename as
+		} 
+		else { // valid conversion
+		    System.err.println("Filename was encoded in: " + charset);
+		    return convertedfilename;
+		}
+	    }
+	} catch(java.io.UnsupportedEncodingException e) {
+	    return filename;
+	}
+
+	return filename; // attempted conversions all failed
     }
 
@@ -77,3 +150,16 @@
 	return is_srcreplaceable;
     }
+
+    /** This method returns a string representation of the filenodes in the Collection
+     * Tree, which is what will be displayed in the tree. It tries to convert it into
+     * some common encoding formats. Failing that, the unchanged filepath is returned.
+     * @see displayString
+     */ 
+    public String toString() 
+    {
+	if(displayFileName == null) {    
+	  displayFileName = calcDisplayString();
+	}
+	return displayFileName;
+    }
 }
