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.util.LinkedList;
23 import java.util.List;
24
25 import org.xnap.event.ListEvent;
26 import org.xnap.event.ListListener;
27 import org.xnap.event.ListSupport;
28 import org.xnap.event.StatusListener;
29 import org.xnap.util.*;
30 import org.apache.log4j.Logger;
31
32 /***
33 * Controls a search object. Groups search results.
34 */
35 public class SearchController implements SearchHandler
36 {
37
38
39
40
41
42 private static Logger logger = Logger.getLogger(SearchController.class);
43
44 private List data = new LinkedList();
45 private Search search;
46 private SearchFilter filter;
47 private ListListener llResults;
48 private StatusListener slStatus;
49 private ListSupport lsResults = new ListSupport(this);
50
51
52
53 public SearchController(Search search, ListListener llResults,
54 StatusListener slStatus)
55 {
56 this.search = search;
57 this.llResults = llResults;
58 this.slStatus = slStatus;
59 }
60
61
62
63 /***
64 * <code>listener</code> is notified when a result is received,
65 * regardless of any filters.
66 */
67 public void addListListener(ListListener listener)
68 {
69 lsResults.addListListener(listener);
70 }
71
72 public SearchFilter getFilter()
73 {
74 return filter;
75 }
76
77 public synchronized int getResultCount()
78 {
79 return data.size();
80 }
81
82 /***
83 * Returns the search object that is controlled by this controller.
84 */
85 public Search getSearch()
86 {
87 return search;
88 }
89
90 public String getStatus()
91 {
92 return search.getStatus();
93 }
94
95 public boolean isDone()
96 {
97 return search.isDone();
98 }
99
100 /***
101 * @see #addListListener(ListListener)
102 */
103 public void removeListListener(ListListener listener)
104 {
105 lsResults.removeListListener(listener);
106 }
107
108 public synchronized void resultReceived(SearchResult result)
109 {
110 data.add(result);
111 lsResults.fireItemAdded(result);
112
113 if (filter == null || filter.matches(result)) {
114 llResults.itemAdded
115 (new ListEvent(this, ListEvent.ITEM_ADDED, result));
116 }
117 }
118
119 public synchronized void setFilter(SearchFilter fiter)
120 {
121
122 llResults.itemRemoved(null);
123 }
124
125 public synchronized void stateChanged(Search search)
126 {
127 slStatus.setStatus(getStatus());
128 }
129
130 public void start()
131 {
132 search.start(this);
133 }
134
135 /***
136 * Cancels the controlled search.
137 */
138 public void stop()
139 {
140 try {
141 search.stop();
142 }
143 catch (IllegalOperationException e) {
144 logger.debug("unexpected state", e);
145 }
146 }
147
148 }