1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.search;
21
22 import java.io.File;
23
24 import org.xnap.XNap;
25 import org.xnap.io.Library;
26 import org.xnap.io.MetaInfoFile;
27
28 /***
29 * This class searches the local library
30 */
31 public class LibrarySearch extends AbstractSearch {
32
33
34
35
36
37 private SearchHandler handler;
38 private boolean done;
39
40
41
42 /***
43 * Constructs the search object.
44 *
45 * @param filter the search filter
46 */
47 public LibrarySearch(SearchFilter filter)
48 {
49 super(filter);
50 }
51
52
53
54 public String getStatus()
55 {
56 return (done)
57 ? XNap.tr("Library search done")
58 : XNap.tr("Searching library");
59 }
60
61 public boolean isDone()
62 {
63 return done;
64 }
65
66 public void start(SearchHandler handler)
67 {
68 this.handler = handler;
69
70 done = false;
71
72 Thread t = new Thread(new SearchRunner(), "LibrarySearch");
73 t.start();
74 }
75
76 public void stop()
77 {
78 done = true;
79 }
80
81
82
83 public class SearchRunner implements Runnable
84 {
85 public void run()
86 {
87 File[] files
88 = Library.getInstance().search(getFilter().getText());
89 if (files != null && files.length > 0) {
90 for (int i = 0; i < files.length && !done; i++) {
91 MetaInfoFileSearchResult result
92 = new MetaInfoFileSearchResult((MetaInfoFile)files[i]);
93 if (getFilter().matches(result)) {
94 handler.resultReceived(result);
95 }
96 }
97 }
98 done = true;
99 handler.stateChanged(LibrarySearch.this);
100 }
101 }
102
103 }
104