Debian Codegraph @

uk/ac/babraham/FastQC/Help/HelpIndexRoot.java

raw symbols SCIP
1 /**
2 * Copyright 2009-15 Simon Andrews
3 *
4 * This file is part of SeqMonk.
5 *
6 * SeqMonk is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * SeqMonk is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with SeqMonk; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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 * The Class HelpIndexRoot is the root node of the tree of help files.
34 */
35 public class HelpIndexRoot extends DefaultMutableTreeNode {
36
37 /** The fs. */
38 public FileSorter fs = new FileSorter();
39
40 /**
41 * Instantiates a new help index root.
42 *
43 * @param startingLocation the starting location
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 * Adds the subfiles.
57 *
58 * @param directory the directory
59 * @param node the node
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 // Skip files which aren't html (eg images etc)
76 }
77 }
78
79 /**
80 * Find pages for term.
81 *
82 * @param searchTerm the search term
83 * @return the help page[]
84 * @throws IOException Signals that an I/O exception has occurred.
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 * The Class FileSorter.
104 */
105 private class FileSorter implements Comparator<File> {
106
107 /* (non-Javadoc)
108 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
109 */
110 public int compare(File f1, File f2) {
111
112 // The file names should be preceeded by a series of
113 // integers separated by dots (eg 1.2.1). We therefore
114 // split these out to compare the individual sections
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 // If we get here then the shortest number string wins
138 return numbers1.length - numbers2.length;
139
140 }
141
142 /**
143 * Gets the number array.
144 *
145 * @param f the f
146 * @return the number array
147 * @throws NumberFormatException the number format exception
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 }