Skip to content

Commit 5223233

Browse files
committed
Added SearchFinish event on MaterialSearch
1 parent ca6f9fd commit 5223233

File tree

3 files changed

+110
-3
lines changed

3 files changed

+110
-3
lines changed

gwt-material/src/main/java/gwt/material/design/client/base/SearchObject.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class SearchObject implements Serializable {
3131

3232
private IconType icon;
3333
private String keyword;
34-
private String link;
34+
private String link = "";
3535
private Object o;
3636

3737
public SearchObject() {}
@@ -67,6 +67,17 @@ public SearchObject(Object o, String link, String keyword, IconType icon) {
6767
this.icon = icon;
6868
}
6969

70+
/**
71+
* Plain search only, you may need to add searchfinish callback instead of redirecting
72+
* to any links.
73+
* @param icon
74+
* @param keyword
75+
*/
76+
public SearchObject(IconType icon, String keyword) {
77+
this.icon = icon;
78+
this.keyword = keyword;
79+
}
80+
7081
public String getKeyword() {
7182
return keyword;
7283
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package gwt.material.design.client.events;
2+
3+
/*
4+
* #%L
5+
* GwtMaterial
6+
* %%
7+
* Copyright (C) 2015 GwtMaterialDesign
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
24+
import com.google.gwt.event.shared.EventHandler;
25+
import com.google.gwt.event.shared.GwtEvent;
26+
import com.google.gwt.event.shared.HasHandlers;
27+
import gwt.material.design.client.events.SearchFinishEvent.SearchFinishHandler;
28+
29+
public class SearchFinishEvent extends GwtEvent<SearchFinishHandler> {
30+
31+
public interface SearchFinishHandler extends EventHandler {
32+
void onSearchFinish(SearchFinishEvent event);
33+
}
34+
35+
public static final Type<SearchFinishHandler> TYPE = new Type<>();
36+
37+
public static void fire(HasHandlers source) {
38+
source.fireEvent(new SearchFinishEvent());
39+
}
40+
41+
@Override
42+
public Type<SearchFinishHandler> getAssociatedType() {
43+
return TYPE;
44+
}
45+
46+
@Override
47+
protected void dispatch(SearchFinishHandler handler) {
48+
handler.onSearchFinish(this);
49+
}
50+
}

gwt-material/src/main/java/gwt/material/design/client/ui/MaterialSearch.java

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import gwt.material.design.client.base.SearchObject;
3131
import gwt.material.design.client.constants.IconType;
3232
import gwt.material.design.client.constants.InputType;
33+
import gwt.material.design.client.events.SearchFinishEvent;
3334
import gwt.material.design.client.ui.html.Label;
3435

3536
import java.util.ArrayList;
@@ -74,10 +75,32 @@ public class MaterialSearch extends MaterialValueBox<String> implements HasClose
7475
private Label label = new Label();
7576
private MaterialIcon iconSearch = new MaterialIcon(IconType.SEARCH);
7677
private MaterialIcon iconClose = new MaterialIcon(IconType.CLOSE);
78+
79+
/**
80+
* The list of search objects added to MaterialSearchResult panel to
81+
* display the lists of result items
82+
*/
7783
private List<SearchObject> listSearches = new ArrayList<>();
84+
/**
85+
* Used to determine the selected searches while matching the keyword to result
86+
*/
87+
private List<SearchObject> tempSearches = new ArrayList<>();
88+
/**
89+
* Panel to display the result items
90+
*/
7891
private MaterialSearchResult searchResult;
92+
/**
93+
* Link selected to determine easily during the selection event (up / down key events)
94+
*/
7995
private MaterialLink selectedLink;
96+
/**
97+
* Gets the selected object after Search Finish event
98+
*/
8099
private SearchObject selectedObject;
100+
/**
101+
* -1 means that the selected index is not yet selected.
102+
* It will increment or decrement once triggere by key up / down events
103+
*/
81104
private int curSel = -1;
82105

83106
public MaterialSearch() {
@@ -106,7 +129,9 @@ public void onLoad() {
106129
@Override
107130
public void onKeyUp(KeyUpEvent event) {
108131
String keyword = getText().toLowerCase();
132+
// Clear the panel and temp objects
109133
searchResult.clear();
134+
tempSearches.clear();
110135

111136
// Populate the search result items
112137
for(final SearchObject obj : getListSearches()) {
@@ -119,20 +144,26 @@ public void onKeyUp(KeyUpEvent event) {
119144
link.addClickHandler(new ClickHandler() {
120145
@Override
121146
public void onClick(ClickEvent event) {
147+
setSelectedObject(obj);
122148
reset(obj.getKeyword());
123149
}
124150
});
125-
// If matches add to search result container
151+
// If matches add to search result container and object to temp searches
126152
if (obj.getKeyword().toLowerCase().contains(keyword)){
127153
searchResult.add(link);
154+
tempSearches.add(obj);
128155
}
129156
}
130157

131158
// Apply selected search
132159
if(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER){
133160
if(getCurSel()==-1){
161+
setSelectedObject(tempSearches.get(0));
134162
setSelectedLink((MaterialLink) searchResult.getWidget(0));
163+
}else{
164+
setSelectedObject(tempSearches.get(curSel));
135165
}
166+
136167
MaterialLink selLink = getSelectedLink();
137168
locateSearch(selLink.getHref());
138169
reset(selLink.getText());
@@ -164,6 +195,7 @@ public void onClick(ClickEvent event) {
164195

165196
// Resets the search result panel
166197
private void reset(String keyword){
198+
SearchFinishEvent.fire(MaterialSearch.this);
167199
curSel = -1;
168200
setText(keyword);
169201
searchResult.clear();
@@ -227,13 +259,27 @@ public void setCurSel(int curSel) {
227259
}
228260

229261
public SearchObject getSelectedObject() {
230-
listSearches.get(curSel);
231262
return selectedObject;
232263
}
233264

234265
public void setSelectedObject(SearchObject selectedObject) {
235266
this.selectedObject = selectedObject;
236267
}
268+
269+
/**
270+
* Gets the tempory search objects
271+
* @return
272+
*/
273+
public List<SearchObject> getTempSearches() {
274+
return tempSearches;
275+
}
276+
277+
/**
278+
* This handler will be triggered when search is finish
279+
*/
280+
public HandlerRegistration addSearchFinishHandler(SearchFinishEvent.SearchFinishHandler handler) {
281+
return addHandler(handler, SearchFinishEvent.TYPE);
282+
}
237283
}
238284

239285

0 commit comments

Comments
 (0)