KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > faces > search > component > UISearchBar


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.faces.search.component;
6
7 import java.util.*;
8 import org.exoplatform.faces.core.component.*;
9 import org.exoplatform.faces.core.component.model.*;
10 import org.exoplatform.faces.core.event.ExoActionEvent;
11 import org.exoplatform.faces.core.event.ExoActionListener;
12 /**
13  * @author Tuan Nguyen (tuan08@users.sourceforge.net)
14  * @since Aug 27, 2004
15  * @version $Id: UISearchBar.java,v 1.2 2004/10/30 02:29:51 tuan08 Exp $
16  */

17 public class UISearchBar extends UISimpleForm {
18   static final public String JavaDoc SEARCH_ACTION = "search" ;
19   static final public String JavaDoc ADVANCED_SEARCH_ACTION = "advancedSearch" ;
20   
21   private UIStringInput termInput_ ;
22   private UISelectBox searchFieldInput_;
23   private List searchOptions_ ;
24   
25   public UISearchBar() {
26     super("quickSearchForm", "post", null) ;
27     setId("UISearchBar") ;
28     setClazz("UISearchBar");
29     
30     termInput_ = new UIStringInput("term", "") ;
31     searchFieldInput_= new UISelectBox("searchField", "", null );
32     add(new Row().
33           add(new ListComponentCell().
34               add(this, termInput_ ).
35             add(this, searchFieldInput_ ).
36               add(new FormButton("#{UISearchBar.button.search}", SEARCH_ACTION))).
37         add(new ListComponentCell().
38             add(new FormButton("#{UISearchBar.button.advanced-search}", ADVANCED_SEARCH_ACTION)).
39             addAlign("right"))) ;
40     
41     addActionListener(AdvancedSearchActionListener.class, ADVANCED_SEARCH_ACTION) ;
42     addActionListener(SearchActionListener.class, SEARCH_ACTION) ;
43   }
44   
45   public boolean isRendered() { return true ; }
46   
47   public void setSearchOptions(List options) {
48     searchOptions_ = options ;
49     searchFieldInput_.setOptions(options) ;
50   }
51   
52   static public class SearchActionListener extends ExoActionListener {
53     public void execute(ExoActionEvent event) throws Exception JavaDoc {
54       UISearchBar uiForm = (UISearchBar) event.getSource();
55       String JavaDoc term = uiForm.termInput_.getValue() ;
56       if(term == null || term.length() == 0) return ;
57       String JavaDoc select = uiForm.searchFieldInput_.getValue() ;
58       List fields = new ArrayList() ;
59       if("".equals(select)) {
60         for(int i = 0; i < uiForm.searchOptions_.size(); i++) {
61           SelectItem item = (SelectItem) uiForm.searchOptions_.get(i) ;
62           if(item.value_.length() > 0) {
63             fields.add(item.value_) ;
64           }
65         }
66       } else {
67         fields.add(select) ;
68       }
69       UISearcher uiSearcher = (UISearcher) uiForm.getParent() ;
70       uiSearcher.quickSearch(term, fields) ;
71     }
72   }
73   
74   static public class AdvancedSearchActionListener extends ExoActionListener {
75     public void execute(ExoActionEvent event) throws Exception JavaDoc {
76       UISearchBar uiSearch = (UISearchBar) event.getSource();
77       UISearcher uiSearcher = (UISearcher) uiSearch.getParent() ;
78       uiSearcher.showAdvancedSearch() ;
79     }
80   }
81 }
Popular Tags