001: package org.columba.core.context;
002:
003: import java.util.Collection;
004: import java.util.Hashtable;
005: import java.util.Map;
006:
007: import javax.swing.SwingUtilities;
008: import javax.swing.event.EventListenerList;
009:
010: import org.columba.api.command.ICommandReference;
011: import org.columba.api.command.IWorkerStatusController;
012: import org.columba.api.gui.frame.IFrameMediator;
013: import org.columba.core.command.Command;
014: import org.columba.core.command.CommandProcessor;
015: import org.columba.core.command.DefaultCommandReference;
016: import org.columba.core.context.api.IContextProvider;
017: import org.columba.core.context.api.IContextResultEvent;
018: import org.columba.core.context.api.IContextResultListener;
019: import org.columba.core.context.api.IContextSearchManager;
020:
021: public class ContextSearchManager implements IContextSearchManager {
022:
023: private static final int RESULT_COUNT = 10;
024:
025: private final Map<String, IContextProvider> map = new Hashtable<String, IContextProvider>();
026:
027: IFrameMediator frameMediator;
028:
029: protected EventListenerList listenerList = new EventListenerList();
030:
031: public ContextSearchManager(final IFrameMediator theFrameMediator) {
032: this .frameMediator = theFrameMediator;
033: }
034:
035: public void register(final IContextProvider provider) {
036: map.put(provider.getTechnicalName(), provider);
037: }
038:
039: public void unregister(final IContextProvider provider) {
040: map.remove(provider.getTechnicalName());
041: }
042:
043: public IContextProvider getProvider(final String technicalName) {
044: return map.get(technicalName);
045: }
046:
047: public Collection<IContextProvider> getAllProviders() {
048: return map.values();
049: }
050:
051: public void addResultListener(final IContextResultListener listener) {
052: listenerList.add(IContextResultListener.class, listener);
053: }
054:
055: public void removeResultListener(
056: final IContextResultListener listener) {
057: listenerList.remove(IContextResultListener.class, listener);
058: }
059:
060: public void search() {
061: final SearchCommand command = new SearchCommand(
062: new DefaultCommandReference());
063: CommandProcessor.getInstance().addOp(command);
064: }
065:
066: void fireFinished() {
067: final IContextResultEvent e = new ContextResultEvent(this );
068: // Guaranteed to return a non-null array
069: final Object[] listeners = listenerList.getListenerList();
070:
071: // Process the listeners last to first, notifying
072: // those that are interested in this event
073: for (int i = listeners.length - 2; i >= 0; i -= 2) {
074: if (listeners[i] == IContextResultListener.class) {
075: ((IContextResultListener) listeners[i + 1]).finished(e);
076: }
077: }
078:
079: }
080:
081: void fireResultArrived(final String providerName) {
082: final IContextResultEvent e = new ContextResultEvent(this ,
083: providerName);
084: // Guaranteed to return a non-null array
085: final Object[] listeners = listenerList.getListenerList();
086:
087: // Process the listeners last to first, notifying
088: // those that are interested in this event
089: for (int i = listeners.length - 2; i >= 0; i -= 2) {
090: if (listeners[i] == IContextResultListener.class) {
091: ((IContextResultListener) listeners[i + 1])
092: .resultArrived(e);
093: }
094: }
095:
096: }
097:
098: class SearchCommand extends Command {
099:
100: public SearchCommand(final ICommandReference reference) {
101: super (reference);
102: }
103:
104: @Override
105: public void execute(final IWorkerStatusController worker)
106: throws Exception {
107: for (final IContextProvider p : getAllProviders()) {
108: p.clear();
109: p.search(frameMediator.getSemanticContext(), 0,
110: ContextSearchManager.RESULT_COUNT);
111:
112: // notify all listeners that have a new search result
113: // ensure this is called in the EDT
114: final Runnable run = new Runnable() {
115: public void run() {
116: fireResultArrived(p.getTechnicalName());
117: }
118: };
119: SwingUtilities.invokeLater(run);
120:
121: }
122:
123: // notify all listeners that search is finished
124:
125: Runnable run = new Runnable() {
126: public void run() {
127: fireFinished();
128: }
129: };
130: SwingUtilities.invokeLater(run);
131: }
132:
133: @Override
134: public void updateGUI() throws Exception {
135: }
136:
137: }
138:
139: }
|