1 package autotest.common.table;
2 
3 import com.google.gwt.event.dom.client.ClickEvent;
4 import com.google.gwt.event.dom.client.ClickHandler;
5 import com.google.gwt.event.dom.client.KeyCodes;
6 import com.google.gwt.event.dom.client.KeyUpEvent;
7 import com.google.gwt.event.dom.client.KeyUpHandler;
8 import com.google.gwt.json.client.JSONString;
9 import com.google.gwt.json.client.JSONValue;
10 import com.google.gwt.user.client.ui.Button;
11 import com.google.gwt.user.client.ui.FlowPanel;
12 import com.google.gwt.user.client.ui.Panel;
13 import com.google.gwt.user.client.ui.TextBox;
14 import com.google.gwt.user.client.ui.Widget;
15 
16 public class SearchFilter extends FieldFilter {
17     private TextBox searchBox = new TextBox();
18     private Button searchButton;
19     private Panel container = new FlowPanel();
20 
21     private String activeSearch = "";
22 
SearchFilter(String fieldName, final boolean isIncremental)23     public SearchFilter(String fieldName, final boolean isIncremental) {
24         this(fieldName, "Search", isIncremental);
25     }
26 
SearchFilter(String fieldName, String buttonName, final boolean isIncremental)27     public SearchFilter(String fieldName, String buttonName,
28                         final boolean isIncremental) {
29         super(fieldName);
30         setExactMatch(false);
31         container.add(searchBox);
32         searchBox.setStylePrimaryName("filter-box");
33 
34         if (!isIncremental) {
35             searchButton = new Button(buttonName);
36             container.add(searchButton);
37             searchButton.addClickHandler(new ClickHandler() {
38                 public void onClick(ClickEvent event) {
39                     notifyListeners();
40                 }
41             });
42         }
43 
44         searchBox.addKeyUpHandler (new KeyUpHandler() {
45             public void onKeyUp(KeyUpEvent event) {
46                 if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER || isIncremental) {
47                     notifyListeners();
48                 }
49             }
50         });
51     }
52 
53     @Override
notifyListeners()54     protected void notifyListeners() {
55         activeSearch = searchBox.getText();
56         super.notifyListeners();
57     }
58 
59     @Override
getMatchValue()60     public JSONValue getMatchValue() {
61         return new JSONString(activeSearch);
62     }
63 
64     @Override
isActive()65     public boolean isActive() {
66         return !activeSearch.equals("");
67     }
68 
69     @Override
getWidget()70     public Widget getWidget() {
71         return container;
72     }
73 }
74