001: package org.columba.core.gui.search;
002:
003: import java.awt.BorderLayout;
004: import java.awt.Color;
005: import java.awt.event.ActionEvent;
006: import java.awt.event.ActionListener;
007: import java.util.Enumeration;
008: import java.util.Hashtable;
009: import java.util.Iterator;
010: import java.util.List;
011: import java.util.Map;
012: import java.util.Vector;
013: import java.util.logging.Logger;
014:
015: import javax.swing.BorderFactory;
016: import javax.swing.JButton;
017: import javax.swing.JComponent;
018: import javax.swing.JPanel;
019: import javax.swing.JScrollPane;
020: import javax.swing.UIManager;
021:
022: import org.columba.api.gui.frame.IDock;
023: import org.columba.api.gui.frame.IFrameMediator;
024: import org.columba.api.plugin.IExtension;
025: import org.columba.api.plugin.IExtensionHandler;
026: import org.columba.api.plugin.IExtensionHandlerKeys;
027: import org.columba.api.plugin.PluginException;
028: import org.columba.api.plugin.PluginHandlerNotFoundException;
029: import org.columba.core.context.ContextSearchManager;
030: import org.columba.core.context.api.IContextProvider;
031: import org.columba.core.context.api.IContextSearchManager;
032: import org.columba.core.gui.context.ContextDebugProvider;
033: import org.columba.core.gui.context.ContextResultBox;
034: import org.columba.core.gui.frame.api.IComponentBox;
035: import org.columba.core.gui.search.api.IResultPanel;
036: import org.columba.core.gui.search.api.ISearchPanel;
037: import org.columba.core.logging.Logging;
038: import org.columba.core.plugin.PluginManager;
039: import org.columba.core.search.SearchManager;
040: import org.columba.core.search.api.ISearchCriteria;
041: import org.columba.core.search.api.ISearchManager;
042: import org.columba.core.search.api.ISearchProvider;
043: import org.columba.core.search.api.ISearchRequest;
044: import org.jdesktop.swingx.VerticalLayout;
045:
046: public class SearchPanel extends JPanel implements ISearchPanel {
047:
048: private static final int RESULT_COUNT = 20;
049:
050: private static final Logger LOG = Logger
051: .getLogger("org.columba.core.search.gui.SearchPanel");
052:
053: private IFrameMediator frameMediator;
054:
055: // private SearchResultView searchResultView;
056:
057: //private StackedBox box;
058: private JPanel defaultBox;
059: private JPanel searchBox;
060:
061: private Hashtable<String, SearchResultBox> searchMap = new Hashtable<String, SearchResultBox>();
062:
063: private ISearchManager searchManager;
064:
065: private Map<String, ContextResultBox> contextMap = new Hashtable<String, ContextResultBox>();
066:
067: private IContextSearchManager contextSearchManager;
068:
069: private JPanel topPanel;
070:
071: public SearchPanel(IFrameMediator frameMediator) {
072: super ();
073:
074: this .frameMediator = frameMediator;
075:
076: this .searchManager = new SearchManager();
077: initSearchProvider();
078:
079: contextSearchManager = new ContextSearchManager(frameMediator);
080: initContextProvider();
081:
082: setBackground(UIManager.getColor("TextField.background"));
083:
084: //box = new StackedBox();
085: defaultBox = new JPanel();
086: defaultBox.setLayout(new VerticalLayout());
087: defaultBox.setBackground(UIManager
088: .getColor("TextField.background"));
089:
090: searchBox = new JPanel();
091: searchBox.setLayout(new VerticalLayout());
092: searchBox.setBackground(UIManager
093: .getColor("TextField.background"));
094:
095: //box.setBackground(new Color(248, 248, 248));
096: // JScrollPane pane = new JScrollPane(box);
097: // pane.setBorder(null);
098:
099: setLayout(new BorderLayout());
100: add(defaultBox, BorderLayout.CENTER);
101:
102: topPanel = new JPanel();
103: //topPanel.setBackground(UIManager.getColor("TextField.background"));
104: topPanel.setLayout(new BorderLayout());
105: topPanel.setBorder(BorderFactory.createEmptyBorder(2, 2, 2, 2));
106:
107: JButton closeButton = new JButton("Close");
108: closeButton.setMnemonic('c');
109: closeButton.addActionListener(new ActionListener() {
110: public void actionPerformed(ActionEvent event) {
111: //createDefaultStackedBox();
112: SearchPanel.this .removeAll();
113: SearchPanel.this .add(defaultBox, BorderLayout.CENTER);
114: validate();
115: repaint();
116: }
117: });
118: topPanel.add(closeButton, BorderLayout.EAST);
119:
120: add(topPanel, BorderLayout.NORTH);
121:
122: createDefaultStackedBox();
123: }
124:
125: private void initContextProvider() {
126:
127: // if in debug mode, register context debugger view
128: if (Logging.DEBUG)
129: contextSearchManager.register(new ContextDebugProvider());
130:
131: try {
132:
133: IExtensionHandler handler = PluginManager
134: .getInstance()
135: .getExtensionHandler(
136: IExtensionHandlerKeys.ORG_COLUMBA_CORE_CONTEXT_PROVIDER);
137:
138: String[] ids = handler.getPluginIdList();
139: for (int i = 0; i < ids.length; i++) {
140: try {
141: IExtension extension = handler.getExtension(ids[i]);
142:
143: IContextProvider provider = (IContextProvider) extension
144: .instanciateExtension(null);
145: contextSearchManager.register(provider);
146: } catch (PluginException e) {
147: LOG.severe("Error while loading plugin: "
148: + e.getMessage());
149: if (Logging.DEBUG)
150: e.printStackTrace();
151: }
152: }
153:
154: } catch (PluginHandlerNotFoundException e) {
155: LOG.severe("Error while loading plugin: " + e.getMessage());
156: if (Logging.DEBUG)
157: e.printStackTrace();
158: }
159: }
160:
161: private void initSearchProvider() {
162: List<ISearchProvider> list = new Vector<ISearchProvider>();
163: try {
164:
165: IExtensionHandler handler = PluginManager
166: .getInstance()
167: .getExtensionHandler(
168: IExtensionHandlerKeys.ORG_COLUMBA_CORE_SEARCH);
169:
170: String[] ids = handler.getPluginIdList();
171: for (int i = 0; i < ids.length; i++) {
172: try {
173: IExtension extension = handler.getExtension(ids[i]);
174:
175: ISearchProvider provider = (ISearchProvider) extension
176: .instanciateExtension(null);
177: searchManager.registerProvider(provider);
178: } catch (PluginException e) {
179: LOG.severe("Error while loading plugin: "
180: + e.getMessage());
181: if (Logging.DEBUG)
182: e.printStackTrace();
183: }
184: }
185:
186: } catch (PluginHandlerNotFoundException e) {
187: LOG.severe("Error while loading plugin: " + e.getMessage());
188: if (Logging.DEBUG)
189: e.printStackTrace();
190: }
191:
192: }
193:
194: // search individual provider and individual criteria
195: public void searchInCriteria(String searchTerm,
196: String providerName, String criteriaName,
197: boolean searchInside) {
198:
199: showSearchDockingView();
200:
201: ISearchManager manager = searchManager;
202:
203: // start a new search -> clear all previous search results
204: manager.clearSearch(searchTerm);
205: manager.reset();
206:
207: createSearchStackedBox(searchTerm, providerName, criteriaName);
208:
209: // TODO @author fdietz: no paging used currently
210: // show only first 5 results
211: manager.executeSearch(searchTerm, providerName, criteriaName,
212: searchInside, 0, SearchPanel.RESULT_COUNT);
213: }
214:
215: // search individual provider
216: public void searchInProvider(String searchTerm,
217: String providerName, boolean searchInside) {
218:
219: showSearchDockingView();
220:
221: ISearchManager manager = searchManager;
222:
223: // start a new search -> clear all previous search results
224: manager.clearSearch(searchTerm);
225: manager.reset();
226:
227: createSearchStackedBox(searchTerm, providerName, null);
228:
229: // TODO @author fdietz: no paging used currently
230: // show only first 5 results
231: manager.executeSearch(searchTerm, providerName, searchInside,
232: 0, SearchPanel.RESULT_COUNT);
233: }
234:
235: // search across all providers
236: public void searchAll(String searchTerm, boolean searchInside) {
237:
238: showSearchDockingView();
239:
240: ISearchManager manager = searchManager;
241:
242: // start a new search -> clear all previous search results
243: manager.clearSearch(searchTerm);
244: manager.reset();
245:
246: createSearchStackedBox(searchTerm, null, null);
247:
248: // TODO @author fdietz: no paging used currently
249: // show only first 5 results
250: manager.executeSearch(searchTerm, searchInside, 0,
251: SearchPanel.RESULT_COUNT);
252: }
253:
254: // search across a few specific search criteria at once
255: public void searchComplex(List<ISearchRequest> list,
256: boolean matchAll, boolean searchInside) {
257: showSearchDockingView();
258:
259: // start a new search -> clear all previous search results
260: searchManager.reset();
261:
262: removeAll();
263: //remove(topPanel);
264: add(topPanel, BorderLayout.NORTH);
265: add(searchBox, BorderLayout.CENTER);
266:
267: searchBox.removeAll();
268:
269: // first, create bucket for each provider
270: Map<String, Vector<ISearchRequest>> map = new Hashtable<String, Vector<ISearchRequest>>();
271: Iterator<ISearchRequest> it = list.iterator();
272: while (it.hasNext()) {
273: ISearchRequest r = it.next();
274: String providerName = r.getProvider();
275:
276: if (map.containsKey(providerName)) {
277: Vector<ISearchRequest> v = map.get(providerName);
278: v.add(r);
279: } else {
280: Vector<ISearchRequest> v = new Vector<ISearchRequest>();
281: v.add(r);
282: map.put(providerName, v);
283: }
284: }
285:
286: // now search through all buckets
287: Iterator<String> it2 = map.keySet().iterator();
288: while (it2.hasNext()) {
289: final String providerName = it2.next();
290: ISearchProvider p = searchManager.getProvider(providerName);
291: createComplexResultPanel(p);
292: }
293: validate();
294: repaint();
295:
296: // TODO @author fdietz: no paging used currently
297: // show only first 5 results
298: searchManager.executeSearch(list, matchAll, searchInside, 0,
299: SearchPanel.RESULT_COUNT);
300: }
301:
302: private void createDefaultStackedBox() {
303:
304: removeAll();
305: //remove(topPanel);
306:
307: add(defaultBox, BorderLayout.CENTER);
308:
309: //defaultBox.removeAll();
310:
311: try {
312:
313: IExtensionHandler handler = PluginManager
314: .getInstance()
315: .getExtensionHandler(
316: IExtensionHandlerKeys.ORG_COLUMBA_CORE_COMPONENT_BOX);
317:
318: Enumeration<IExtension> e = handler
319: .getExtensionEnumeration();
320:
321: while (e.hasMoreElements()) {
322: try {
323: IExtension extension = e.nextElement();
324:
325: IComponentBox compBox = (IComponentBox) extension
326: .instanciateExtension(null);
327:
328: defaultBox.add(new ComponentBoxContainer(compBox));
329: } catch (PluginException ex) {
330: LOG.severe("Error while loading plugin: "
331: + ex.getMessage());
332: if (Logging.DEBUG)
333: ex.printStackTrace();
334: }
335: }
336:
337: } catch (PluginHandlerNotFoundException e) {
338: LOG.severe("Error while loading plugin: " + e.getMessage());
339: if (Logging.DEBUG)
340: e.printStackTrace();
341: }
342:
343: Iterator<IContextProvider> it = contextSearchManager
344: .getAllProviders().iterator();
345: while (it.hasNext()) {
346: IContextProvider p = it.next();
347:
348: // clear previous search results
349: p.clear();
350: ContextResultBox resultBox = new ContextResultBox(
351: frameMediator, p, contextSearchManager);
352: defaultBox.add(resultBox);
353:
354: contextMap.put(p.getName(), resultBox);
355: }
356:
357: validate();
358: repaint();
359: }
360:
361: // create new stacked box
362: private void createSearchStackedBox(String searchTerm,
363: String providerName, String criteriaName) {
364: if (searchTerm == null)
365: throw new IllegalArgumentException("searchTerm == null");
366:
367: //remove(topPanel);
368: removeAll();
369: add(topPanel, BorderLayout.NORTH);
370: add(searchBox, BorderLayout.CENTER);
371:
372: searchBox.removeAll();
373:
374: // search across all providers
375: boolean providerAll = (providerName == null) ? true : false;
376: // search all criteria in specific provider only
377: boolean providerSearch = (providerName != null) ? true : false;
378: // search in specific criteria
379: boolean criteriaSearch = (criteriaName != null && providerName != null) ? true
380: : false;
381:
382: ISearchManager manager = searchManager;
383:
384: if (criteriaSearch) {
385: // query with only a single criteria
386:
387: ISearchProvider p = manager.getProvider(providerName);
388:
389: ISearchCriteria c = p.getCriteria(criteriaName, searchTerm);
390:
391: createResultPanel(p, c);
392:
393: } else if (providerSearch) {
394:
395: // query only a single provider
396:
397: ISearchProvider p = manager.getProvider(providerName);
398:
399: Iterator<ISearchCriteria> it2 = p
400: .getAllCriteria(searchTerm).iterator();
401: while (it2.hasNext()) {
402: ISearchCriteria c = it2.next();
403: createResultPanel(p, c);
404: }
405:
406: } else if (providerAll) {
407: // query all criteria of all providers
408:
409: Iterator<ISearchProvider> it = manager.getAllProviders();
410: while (it.hasNext()) {
411: ISearchProvider p = it.next();
412: if (p == null)
413: continue;
414:
415: Iterator<ISearchCriteria> it2 = p.getAllCriteria(
416: searchTerm).iterator();
417: while (it2.hasNext()) {
418: ISearchCriteria c = it2.next();
419: createResultPanel(p, c);
420: }
421: }
422: }
423:
424: // repaint box
425: validate();
426: repaint();
427: }
428:
429: private void createComplexResultPanel(ISearchProvider p) {
430:
431: IResultPanel resultPanel = p.getComplexResultPanel();
432:
433: // add result panel as listener for new search results
434: searchManager.addResultListener(resultPanel);
435:
436: // create visual container for result panel
437: SearchResultBox resultBox = new SearchResultBox(frameMediator,
438: p, null, resultPanel);
439: resultBox.installListener(searchManager);
440:
441: // add to search panel
442: searchBox.add(resultBox);
443: }
444:
445: private void createResultPanel(ISearchProvider p, ISearchCriteria c) {
446:
447: IResultPanel resultPanel = p.getResultPanel(c
448: .getTechnicalName());
449:
450: // add result panel as listener for new search results
451: searchManager.addResultListener(resultPanel);
452:
453: // create visual container for result panel
454: SearchResultBox resultBox = new SearchResultBox(frameMediator,
455: p, c, resultPanel);
456: resultBox.installListener(searchManager);
457:
458: // add to search panel
459: searchBox.add(resultBox);
460: }
461:
462: // show search docking view
463: private void showSearchDockingView() {
464: if (frameMediator instanceof IDock) {
465: // show docking view
466: ((IDock) frameMediator)
467: .showDockable(IDock.DOCKING_VIEW_SEARCH);
468: }
469:
470: }
471:
472: /**
473: * @see org.columba.core.gui.search.api.ISearchPanel#getView()
474: */
475: public JComponent getView() {
476: return this ;
477: }
478:
479: public ISearchManager getSearchManager() {
480: return searchManager;
481: }
482:
483: public IContextSearchManager getContextSearchManager() {
484: return contextSearchManager;
485: }
486:
487: public void searchInContext() {
488: // init result view
489: //createContextStackedBox();
490:
491: // execute background search
492: contextSearchManager.search();
493: }
494:
495: // private void createContextStackedBox() {
496: //
497: // searchMap.clear();
498: //
499: // remove(topPanel);
500: // add(topPanel, BorderLayout.NORTH);
501: //
502: // box.removeAll();
503: //
504: // Iterator<IContextProvider> it = contextSearchManager.getAllProviders()
505: // .iterator();
506: // while (it.hasNext()) {
507: // IContextProvider p = it.next();
508: //
509: // // clear previous search results
510: // p.clear();
511: // ContextResultBox resultBox = new ContextResultBox(frameMediator, p,
512: // contextSearchManager);
513: // box.addBox(resultBox);
514: //
515: // contextMap.put(p.getName(), resultBox);
516: // }
517: //
518: // // repaint box
519: // validate();
520: // repaint();
521: // }
522:
523: }
|