1 package org.columba.core.context; 2 3 import java.util.Collection ; 4 import java.util.Hashtable ; 5 import java.util.Map ; 6 7 import javax.swing.SwingUtilities ; 8 import javax.swing.event.EventListenerList ; 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 <String , IContextProvider> map = new Hashtable <String , IContextProvider>(); 26 27 IFrameMediator frameMediator; 28 29 protected EventListenerList listenerList = new EventListenerList (); 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 technicalName) { 44 return map.get(technicalName); 45 } 46 47 public Collection <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 final Object [] listeners = listenerList.getListenerList(); 69 70 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 providerName) { 81 final IContextResultEvent e = new ContextResultEvent(this, providerName); 82 final Object [] listeners = listenerList.getListenerList(); 84 85 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 102 public void execute(final IWorkerStatusController worker) 103 throws Exception { 104 for (final IContextProvider p : getAllProviders()) { 105 p.clear(); 106 p.search(frameMediator.getSemanticContext(), 0, 107 ContextSearchManager.RESULT_COUNT); 108 109 final Runnable run = new Runnable () { 112 public void run() { 113 fireResultArrived(p.getTechnicalName()); 114 } 115 }; 116 SwingUtilities.invokeLater(run); 117 118 } 119 120 122 Runnable run = new Runnable () { 123 public void run() { 124 fireFinished(); 125 } 126 }; 127 SwingUtilities.invokeLater(run); 128 } 129 130 @Override 131 public void updateGUI() throws Exception { 132 } 133 134 } 135 136 } 137 | Popular Tags |