Debian Codegraph @

uk/ac/babraham/FastQC/Help/HelpPage.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.BufferedReader;
23 import java.io.File;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.util.Enumeration;
27 import java.util.Vector;
28
29 import javax.swing.tree.DefaultMutableTreeNode;
30
31 /**
32 * The Class HelpPage represents a single page of information in
33 * the help system
34 */
35 public class HelpPage extends DefaultMutableTreeNode {
36
37 /** The file. */
38 private File file;
39
40 /** The name. */
41 private String name;
42
43 /**
44 * Instantiates a new help page.
45 *
46 * @param file the file
47 */
48 public HelpPage (File file) {
49 this.file = file;
50 name = file.getName();
51 name = this.name.replaceFirst("\\.[hH][tT][mM][lL]?$", "");
52
53 String [] nameSections = name.split(" ");
54 if (nameSections.length > 1) {
55 // We have two sections so check if the first is just integers
56 // separated by dots. If it is then we can lose it.
57 String [] numbers = nameSections[0].split("\\.");
58 for (int n=0;n<numbers.length;n++) {
59 try {
60 Integer.parseInt(numbers[n]);
61 }
62 catch(NumberFormatException nfe) {
63 return; // We don't want to chop this off.
64 }
65 }
66
67 // If we get here then we want to chop the first part
68 // of the name off
69 StringBuffer sb = new StringBuffer(nameSections[1]);
70 for (int s=2;s<nameSections.length;s++) {
71 sb.append(" ");
72 sb.append(nameSections[s]);
73 }
74 name = sb.toString();
75
76 }
77
78 }
79
80 /**
81 * Contains string.
82 *
83 * @param searchTerm the search term
84 * @param hits the hits
85 * @throws IOException Signals that an I/O exception has occurred.
86 */
87 @SuppressWarnings("unchecked")
88 public void containsString (String searchTerm, Vector<HelpPage>hits) throws IOException {
89
90 // Since this will be part of a search thread then take a quick
91 // break in case we're trying to do anything else.
92 try {
93 Thread.sleep(10);
94 }
95 catch (InterruptedException e) {}
96
97 // We don't want to be trying to open directories
98 if (isLeaf()) {
99 BufferedReader br = new BufferedReader(new FileReader(file));
100 searchTerm = searchTerm.toLowerCase();
101 String line;
102 while ((line = br.readLine()) != null) {
103 // System.out.println("Read line "+line);
104 if (line.toLowerCase().indexOf(searchTerm)!=-1) {
105 hits.add(this);
106 break;
107 }
108 }
109 }
110
111 // Extend the search to our children
112 Enumeration kids = children();
113 while (kids.hasMoreElements()) {
114 Object node = kids.nextElement();
115 if (node instanceof HelpPage) {
116 ((HelpPage)node).containsString(searchTerm, hits);
117 }
118 }
119 }
120
121 /* (non-Javadoc)
122 * @see javax.swing.tree.DefaultMutableTreeNode#toString()
123 */
124 public String toString () {
125 return name;
126 }
127
128 /* (non-Javadoc)
129 * @see javax.swing.tree.DefaultMutableTreeNode#isLeaf()
130 */
131 public boolean isLeaf() {
132 if (file.isDirectory()) return false;
133 else return true;
134 }
135
136 /**
137 * Gets the file.
138 *
139 * @return the file
140 */
141 public File getFile () {
142 return file;
143 }
144
145 }