Skip to content

Commit 905a620

Browse files
committed
Material Search added advance functionality based on SearchObjects
1 parent 7d2050e commit 905a620

File tree

4 files changed

+358
-4
lines changed

4 files changed

+358
-4
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package gwt.material.design.client.base;
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+
import gwt.material.design.client.constants.IconType;
24+
25+
import java.io.Serializable;
26+
27+
/***
28+
* Its an object provided for search component, it uses a keyword and link to be redirected after search.
29+
*/
30+
public class SearchObject implements Serializable {
31+
32+
private IconType icon;
33+
private String keyword;
34+
private String link;
35+
private Object o;
36+
37+
public SearchObject() {}
38+
39+
/**
40+
* Provides a search result with icon
41+
* @param icon
42+
* @param keyword
43+
* @param link
44+
*/
45+
public SearchObject(IconType icon, String keyword, String link) {
46+
this.icon = icon;
47+
this.keyword = keyword;
48+
this.link = link;
49+
}
50+
51+
/**
52+
* Provides a search result with plain text
53+
* @param keyword
54+
* @param link
55+
* @param o
56+
*/
57+
public SearchObject(String keyword, String link, Object o) {
58+
this.keyword = keyword;
59+
this.link = link;
60+
this.o = o;
61+
}
62+
63+
public SearchObject(Object o, String link, String keyword, IconType icon) {
64+
this.o = o;
65+
this.link = link;
66+
this.keyword = keyword;
67+
this.icon = icon;
68+
}
69+
70+
public String getKeyword() {
71+
return keyword;
72+
}
73+
74+
public void setKeyword(String keyword) {
75+
this.keyword = keyword;
76+
}
77+
78+
public String getLink() {
79+
return link;
80+
}
81+
82+
public void setLink(String link) {
83+
this.link = link;
84+
}
85+
86+
public Object getO() {
87+
return o;
88+
}
89+
90+
public void setO(Object o) {
91+
this.o = o;
92+
}
93+
94+
public IconType getIcon() {
95+
return icon;
96+
}
97+
98+
public void setIcon(IconType icon) {
99+
this.icon = icon;
100+
}
101+
}

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

Lines changed: 179 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
* Licensed under the Apache License, Version 2.0 (the "License");
1010
* you may not use this file except in compliance with the License.
1111
* You may obtain a copy of the License at
12-
*
12+
*
1313
* http://www.apache.org/licenses/LICENSE-2.0
14-
*
14+
*
1515
* Unless required by applicable law or agreed to in writing, software
1616
* distributed under the License is distributed on an "AS IS" BASIS,
1717
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -20,11 +20,21 @@
2020
* #L%
2121
*/
2222

23+
import com.google.gwt.event.dom.client.*;
24+
import com.google.gwt.event.logical.shared.CloseEvent;
25+
import com.google.gwt.event.logical.shared.CloseHandler;
26+
import com.google.gwt.event.logical.shared.HasCloseHandlers;
27+
import com.google.gwt.event.shared.HandlerRegistration;
2328
import com.google.gwt.user.client.ui.TextBox;
29+
import gwt.material.design.client.base.HasActive;
30+
import gwt.material.design.client.base.SearchObject;
2431
import gwt.material.design.client.constants.IconType;
2532
import gwt.material.design.client.constants.InputType;
2633
import gwt.material.design.client.ui.html.Label;
2734

35+
import java.util.ArrayList;
36+
import java.util.List;
37+
2838
//@formatter:off
2939

3040
/**
@@ -37,19 +47,38 @@
3747
* <m:MaterialSearch placeholder="Sample"/>
3848
* }
3949
* </pre>
50+
*
51+
* <h3>Populating the search result objects</h3>
52+
* {@code
53+
*
54+
* List<SearchObject> objects = new ArrayList<>();
55+
*
56+
* private void onInitSearch(){
57+
* objects.add(new SearchObject(IconType.POLYMER, "Pushpin", "#!pushpin"));
58+
* objects.add(new SearchObject(IconType.POLYMER, "SideNavs", "#!sidenavs"));
59+
* objects.add(new SearchObject(IconType.POLYMER, "Scrollspy", "#!scrollspy"));
60+
* objects.add(new SearchObject(IconType.POLYMER, "Tabs", "#!tabs"));
61+
* txtSearch.setListSearches(objects);
62+
* }
63+
*
64+
* }
4065
* </p>
4166
*
4267
* @author kevzlou7979
4368
* @author Ben Dol
4469
* @see <a href="http://gwt-material-demo.herokuapp.com/#navigations">Material Search</a>
4570
*/
4671
//@formatter:on
47-
public class MaterialSearch extends MaterialValueBox<String>{
72+
public class MaterialSearch extends MaterialValueBox<String> implements HasCloseHandlers<String>, HasActive {
4873

4974
private Label label = new Label();
5075
private MaterialIcon iconSearch = new MaterialIcon(IconType.SEARCH);
5176
private MaterialIcon iconClose = new MaterialIcon(IconType.CLOSE);
52-
77+
private List<SearchObject> listSearches = new ArrayList<>();
78+
private MaterialSearchResult searchResult;
79+
private MaterialLink selectedLink;
80+
private SearchObject selectedObject;
81+
private int curSel = -1;
5382

5483
public MaterialSearch() {
5584
super(new TextBox());
@@ -58,6 +87,152 @@ public MaterialSearch() {
5887
label.getElement().setAttribute("for", "search");
5988
add(label);
6089
add(iconClose);
90+
iconClose.addClickHandler(new ClickHandler() {
91+
@Override
92+
public void onClick(ClickEvent event) {
93+
CloseEvent.fire(MaterialSearch.this, getText());
94+
}
95+
});
96+
}
97+
98+
@Override
99+
public void onLoad() {
100+
super.onLoad();
101+
// populate the lists of search result on search panel
102+
searchResult = new MaterialSearchResult();
103+
add(searchResult);
104+
// add keyup event to filter the searches
105+
addKeyUpHandler(new KeyUpHandler() {
106+
@Override
107+
public void onKeyUp(KeyUpEvent event) {
108+
String keyword = getText().toLowerCase();
109+
searchResult.clear();
110+
111+
// Populate the search result items
112+
for(final SearchObject obj : getListSearches()) {
113+
MaterialLink link = new MaterialLink();
114+
link.setIconColor("grey");
115+
link.setTextColor("black");
116+
link.setIconType(obj.getIcon());
117+
link.setHref(obj.getLink());
118+
link.setText(obj.getKeyword());
119+
link.addClickHandler(new ClickHandler() {
120+
@Override
121+
public void onClick(ClickEvent event) {
122+
reset(obj.getKeyword());
123+
}
124+
});
125+
// If matches add to search result container
126+
if (obj.getKeyword().toLowerCase().contains(keyword)){
127+
searchResult.add(link);
128+
}
129+
}
130+
131+
// Apply selected search
132+
if(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_ENTER){
133+
if(getCurSel()==-1){
134+
setSelectedLink((MaterialLink) searchResult.getWidget(0));
135+
}
136+
MaterialLink selLink = getSelectedLink();
137+
locateSearch(selLink.getHref());
138+
reset(selLink.getText());
139+
}
140+
141+
// Selection logic using key down event to navigate the search results
142+
int totalItems = searchResult.getWidgetCount();
143+
if(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_DOWN){
144+
if(curSel >= totalItems){
145+
setCurSel(getCurSel());
146+
applyHighlightedItem((MaterialLink) searchResult.getWidget(curSel - 1));
147+
}else{
148+
setCurSel(getCurSel() + 1);
149+
applyHighlightedItem((MaterialLink) searchResult.getWidget(curSel));
150+
}
151+
}
152+
153+
// Selection logic using key up event to navigate the search results
154+
if(event.getNativeEvent().getKeyCode() == KeyCodes.KEY_UP){
155+
if(curSel <= -1){
156+
setCurSel(-1);
157+
applyHighlightedItem((MaterialLink) searchResult.getWidget(curSel));
158+
}else {
159+
setCurSel(getCurSel() - 1);
160+
applyHighlightedItem((MaterialLink) searchResult.getWidget(curSel));
161+
}
162+
}
163+
}
164+
165+
// Resets the search result panel
166+
private void reset(String keyword){
167+
curSel = -1;
168+
setText(keyword);
169+
searchResult.clear();
170+
}
171+
});
172+
}
173+
174+
private void applyHighlightedItem(MaterialLink link){
175+
link.addStyleName("higlighted");
176+
setSelectedLink(link);
177+
}
178+
179+
private native void locateSearch(String location)/*-{
180+
$wnd.window.location.hash = location;
181+
}-*/;
182+
183+
@Override
184+
public HandlerRegistration addCloseHandler(CloseHandler<String> handler) {
185+
return addHandler(handler, CloseEvent.getType());
186+
}
187+
188+
@Override
189+
public void setActive(boolean active) {
190+
if(active){
191+
this.setTextColor("black");
192+
iconClose.setIconColor("black");
193+
iconSearch.setIconColor("black");
194+
}else{
195+
iconClose.setIconColor("white");
196+
iconSearch.setIconColor("white");
197+
}
198+
}
199+
200+
@Override
201+
public boolean isActive() {
202+
return false;
203+
}
204+
205+
public MaterialLink getSelectedLink() {
206+
return selectedLink;
207+
}
208+
209+
public void setSelectedLink(MaterialLink selectedLink) {
210+
this.selectedLink = selectedLink;
211+
}
212+
213+
public List<SearchObject> getListSearches() {
214+
return listSearches;
215+
}
216+
217+
public void setListSearches(List<SearchObject> listSearches) {
218+
this.listSearches = listSearches;
219+
}
220+
221+
public int getCurSel() {
222+
return curSel;
223+
}
224+
225+
public void setCurSel(int curSel) {
226+
this.curSel = curSel;
227+
}
228+
229+
public SearchObject getSelectedObject() {
230+
listSearches.get(curSel);
231+
return selectedObject;
232+
}
233+
234+
public void setSelectedObject(SearchObject selectedObject) {
235+
this.selectedObject = selectedObject;
61236
}
62237
}
63238

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package gwt.material.design.client.ui;
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+
import com.google.gwt.dom.client.Document;
24+
import gwt.material.design.client.base.MaterialWidget;
25+
26+
//@formatter:off
27+
28+
/**
29+
* Material Search Result is a panel used to display the list of suggested items during the
30+
* keyup events triggered on search component.
31+
*
32+
* @author kevzlou7979
33+
* @see <a href="http://gwt-material-demo.herokuapp.com/#navigations">Material Search</a>
34+
*/
35+
//@formatter:on
36+
public class MaterialSearchResult extends MaterialWidget {
37+
38+
public MaterialSearchResult() {
39+
super(Document.get().createDivElement());
40+
setStyleName("search-result");
41+
}
42+
43+
}

0 commit comments

Comments
 (0)