KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > context > ContextSearchManager


1 package org.columba.core.context;
2
3 import java.util.Collection JavaDoc;
4 import java.util.Hashtable JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import javax.swing.SwingUtilities JavaDoc;
8 import javax.swing.event.EventListenerList JavaDoc;
9
10 import org.columba.api.command.ICommandReference;
11 import org.columba.api.command.IWorkerStatusController;
12 import org.columba.api.gui.frame.IFrameMediator;
13 import org.columba.core.command.Command;
14 import org.columba.core.command.CommandProcessor;
15 import org.columba.core.command.DefaultCommandReference;
16 import org.columba.core.context.api.IContextProvider;
17 import org.columba.core.context.api.IContextResultEvent;
18 import org.columba.core.context.api.IContextResultListener;
19 import org.columba.core.context.api.IContextSearchManager;
20
21 public class ContextSearchManager implements IContextSearchManager {
22
23     private static final int RESULT_COUNT = 10;
24
25     private final Map JavaDoc<String JavaDoc, IContextProvider> map = new Hashtable JavaDoc<String JavaDoc, IContextProvider>();
26
27     IFrameMediator frameMediator;
28
29     protected EventListenerList JavaDoc listenerList = new EventListenerList JavaDoc();
30
31     public ContextSearchManager(final IFrameMediator theFrameMediator) {
32         this.frameMediator = theFrameMediator;
33     }
34
35     public void register(final IContextProvider provider) {
36         map.put(provider.getTechnicalName(), provider);
37     }
38
39     public void unregister(final IContextProvider provider) {
40         map.remove(provider.getTechnicalName());
41     }
42
43     public IContextProvider getProvider(final String JavaDoc technicalName) {
44         return map.get(technicalName);
45     }
46
47     public Collection JavaDoc<IContextProvider> getAllProviders() {
48         return map.values();
49     }
50
51     public void addResultListener(final IContextResultListener listener) {
52         listenerList.add(IContextResultListener.class, listener);
53     }
54
55     public void removeResultListener(final IContextResultListener listener) {
56         listenerList.remove(IContextResultListener.class, listener);
57     }
58
59     public void search() {
60         final SearchCommand command = new SearchCommand(
61                 new DefaultCommandReference());
62         CommandProcessor.getInstance().addOp(command);
63     }
64
65     void fireFinished() {
66         final IContextResultEvent e = new ContextResultEvent(this);
67         // Guaranteed to return a non-null array
68
final Object JavaDoc[] listeners = listenerList.getListenerList();
69
70         // Process the listeners last to first, notifying
71
// those that are interested in this event
72
for (int i = listeners.length - 2; i >= 0; i -= 2) {
73             if (listeners[i] == IContextResultListener.class) {
74                 ((IContextResultListener) listeners[i + 1]).finished(e);
75             }
76         }
77
78     }
79
80     void fireResultArrived(final String JavaDoc providerName) {
81         final IContextResultEvent e = new ContextResultEvent(this, providerName);
82         // Guaranteed to return a non-null array
83
final Object JavaDoc[] listeners = listenerList.getListenerList();
84
85         // Process the listeners last to first, notifying
86
// those that are interested in this event
87
for (int i = listeners.length - 2; i >= 0; i -= 2) {
88             if (listeners[i] == IContextResultListener.class) {
89                 ((IContextResultListener) listeners[i + 1]).resultArrived(e);
90             }
91         }
92
93     }
94
95     class SearchCommand extends Command {
96
97         public SearchCommand(final ICommandReference reference) {
98             super(reference);
99         }
100
101         @Override JavaDoc
102         public void execute(final IWorkerStatusController worker)
103                 throws Exception JavaDoc {
104             for (final IContextProvider p : getAllProviders()) {
105                 p.clear();
106                 p.search(frameMediator.getSemanticContext(), 0,
107                         ContextSearchManager.RESULT_COUNT);
108
109                 // notify all listeners that have a new search result
110
// ensure this is called in the EDT
111
final Runnable JavaDoc run = new Runnable JavaDoc() {
112                     public void run() {
113                         fireResultArrived(p.getTechnicalName());
114                     }
115                 };
116                 SwingUtilities.invokeLater(run);
117
118             }
119
120             // notify all listeners that search is finished
121

122             Runnable JavaDoc run = new Runnable JavaDoc() {
123                 public void run() {
124                     fireFinished();
125                 }
126             };
127             SwingUtilities.invokeLater(run);
128         }
129
130         @Override JavaDoc
131         public void updateGUI() throws Exception JavaDoc {
132         }
133
134     }
135
136 }
137
Popular Tags