| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
package uk.ac.babraham.FastQC.Help; |
| 21 |
|
| 22 |
import java.io.File; |
| 23 |
import java.io.IOException; |
| 24 |
import java.util.Arrays; |
| 25 |
import java.util.Comparator; |
| 26 |
import java.util.Enumeration; |
| 27 |
import java.util.Vector; |
| 28 |
|
| 29 |
import javax.swing.tree.DefaultMutableTreeNode; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class HelpIndexRoot extends DefaultMutableTreeNode { |
| 36 |
|
| 37 |
|
| 38 |
public FileSorter fs = new FileSorter(); |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public HelpIndexRoot (File startingLocation) { |
| 46 |
super("Help Contents"); |
| 47 |
|
| 48 |
if (!startingLocation.exists() || !startingLocation.isDirectory()) { |
| 49 |
throw new IllegalArgumentException("Couldn't find help file directory at '"+startingLocation.getAbsolutePath()+"'"); |
| 50 |
} |
| 51 |
|
| 52 |
addSubfiles(startingLocation, this); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
private void addSubfiles (File directory, DefaultMutableTreeNode node) { |
| 62 |
File [] files = directory.listFiles(); |
| 63 |
|
| 64 |
Arrays.sort(files,fs); |
| 65 |
for (int f=0;f<files.length;f++) { |
| 66 |
if (files[f].isDirectory()) { |
| 67 |
HelpPage h = new HelpPage(files[f]); |
| 68 |
node.add(h); |
| 69 |
addSubfiles(files[f], h); |
| 70 |
} |
| 71 |
else if (files[f].getName().toLowerCase().endsWith(".html") || files[f].getName().toLowerCase().endsWith(".htm")){ |
| 72 |
HelpPage h = new HelpPage(files[f]); |
| 73 |
node.add(h); |
| 74 |
} |
| 75 |
|
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
@SuppressWarnings("unchecked") |
| 87 |
public HelpPage [] findPagesForTerm (String searchTerm) throws IOException { |
| 88 |
Vector<HelpPage>hits = new Vector<HelpPage>(); |
| 89 |
|
| 90 |
Enumeration kids = children(); |
| 91 |
while (kids.hasMoreElements()) { |
| 92 |
Object node = kids.nextElement(); |
| 93 |
if (node instanceof HelpPage) { |
| 94 |
((HelpPage)node).containsString(searchTerm, hits); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
return hits.toArray(new HelpPage[0]); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
private class FileSorter implements Comparator<File> { |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public int compare(File f1, File f2) { |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
int [] numbers1; |
| 117 |
int [] numbers2; |
| 118 |
|
| 119 |
|
| 120 |
try { |
| 121 |
numbers1 = getNumberArray(f1); |
| 122 |
numbers2 = getNumberArray(f2); |
| 123 |
} |
| 124 |
catch (NumberFormatException nfe) { |
| 125 |
return f1.getName().compareTo(f2.getName()); |
| 126 |
} |
| 127 |
|
| 128 |
int shortest = numbers1.length; |
| 129 |
if (numbers2.length < shortest) shortest = numbers2.length; |
| 130 |
|
| 131 |
for (int i=0;i<shortest;i++) { |
| 132 |
if (numbers1[i] != numbers2[i]) { |
| 133 |
return numbers1[i]-numbers2[i]; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
return numbers1.length - numbers2.length; |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
private int [] getNumberArray (File f) throws NumberFormatException { |
| 150 |
String [] numberStrings = f.getName().split(" ")[0].split("\\."); |
| 151 |
int [] ints = new int [numberStrings.length]; |
| 152 |
for (int i=0;i<numberStrings.length;i++) { |
| 153 |
ints[i] = Integer.parseInt(numberStrings[i]); |
| 154 |
} |
| 155 |
|
| 156 |
return ints; |
| 157 |
} |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
} |