3939import gwt .material .design .client .constants .ProgressType ;
4040import gwt .material .design .client .ui .MaterialChip ;
4141import gwt .material .design .client .ui .MaterialLabel ;
42+ import gwt .material .design .client .ui .MaterialToast ;
43+ import gwt .material .design .client .ui .html .Label ;
4244import gwt .material .design .client .ui .html .ListItem ;
4345import gwt .material .design .client .ui .html .UnorderedList ;
4446
4547import java .util .*;
4648import java .util .Map .Entry ;
4749
4850//@formatter:off
51+
4952/**
5053 * <p>
5154 * Use GWT Autocomplete to search for matches from local or remote data sources.
9396 * List<UserSuggestion> list = new ArrayList<>();
9497 *
9598 * /{@literal *}
96- * {@literal *} Finds the contacts that meets the criteria. Note that since the
99+ * {@literal *} Finds the contacts that meets the criteria. Note that since the
97100 * {@literal *} requestSuggestions method is asynchronous, you can fetch the
98101 * {@literal *} results from the server instead of using a local contacts List.
99102 * {@literal *}/
144147 * for (Suggestion value : values) {
145148 * if (value instanceof UserSuggestion){
146149 * UserSuggestion us = (UserSuggestion) value;
147- * User user = us.getUser();
150+ * User user = us.getUser();
148151 * users.add(user);
149152 * }
150153 * }
@@ -169,6 +172,7 @@ public class MaterialAutoComplete extends MaterialWidget implements HasError, Ha
169172 }
170173
171174 private Map <Suggestion , Widget > suggestionMap = new LinkedHashMap <>();
175+ private Label label = new Label ();
172176
173177 private List <ListItem > itemsHighlighted = new ArrayList <>();
174178 private FlowPanel panel = new FlowPanel ();
@@ -179,7 +183,7 @@ public class MaterialAutoComplete extends MaterialWidget implements HasError, Ha
179183 private int limit = 0 ;
180184 private MaterialLabel lblError = new MaterialLabel ();
181185 private final ProgressMixin <MaterialAutoComplete > progressMixin = new ProgressMixin <>(this );
182-
186+
183187 private String selectedChipStyle = "blue white-text" ;
184188 private boolean directInputAllowed = true ;
185189 private MaterialChipProvider chipProvider = new DefaultMaterialChipProvider ();
@@ -196,7 +200,7 @@ public class MaterialAutoComplete extends MaterialWidget implements HasError, Ha
196200 * sources.
197201 */
198202 public MaterialAutoComplete () {
199- super (Document .get ().createDivElement ());
203+ super (Document .get ().createDivElement (), "autocomplete" , "input-field" );
200204 add (panel );
201205 }
202206
@@ -231,6 +235,7 @@ protected void generateAutoComplete(SuggestOracle suggestions) {
231235 itemBox .getElement ().setId (autocompleteId );
232236
233237 item .add (box );
238+ item .add (label );
234239 list .add (item );
235240
236241 list .addDomHandler (new ClickHandler () {
@@ -240,6 +245,15 @@ public void onClick(ClickEvent event) {
240245 }
241246 }, ClickEvent .getType ());
242247
248+ itemBox .addBlurHandler (new BlurHandler () {
249+ @ Override
250+ public void onBlur (BlurEvent event ) {
251+ if (getValue ().size () > 0 ) {
252+ label .addStyleName ("active" );
253+ }
254+ }
255+ });
256+
243257 itemBox .addKeyDownHandler (new KeyDownHandler () {
244258 public void onKeyDown (KeyDownEvent event ) {
245259 boolean itemsChanged = false ;
@@ -266,15 +280,15 @@ public void onKeyDown(KeyDownEvent event) {
266280
267281 ListItem li = (ListItem ) list .getWidget (list .getWidgetCount () - 2 );
268282 MaterialChip p = (MaterialChip ) li .getWidget (0 );
269-
283+
270284 boolean removable = true ;
271-
285+
272286 Set <Entry <Suggestion , Widget >> entrySet = suggestionMap .entrySet ();
273287 for (Entry <Suggestion , Widget > entry : entrySet ) {
274288 if (p .equals (entry .getValue ())) {
275289 if (chipProvider .isChipRemovable (entry .getKey ())){
276290 suggestionMap .remove (entry .getKey ());
277- itemsChanged = true ;
291+ itemsChanged = true ;
278292 }
279293 else {
280294 removable = false ;
@@ -284,7 +298,7 @@ public void onKeyDown(KeyDownEvent event) {
284298 }
285299
286300 if (removable ){
287- list .remove (li );
301+ list .remove (li );
288302 }
289303 }
290304 }
@@ -294,25 +308,25 @@ public void onKeyDown(KeyDownEvent event) {
294308 if (itemBox .getValue ().trim ().isEmpty ()) {
295309 for (ListItem li : itemsHighlighted ) {
296310 MaterialChip p = (MaterialChip ) li .getWidget (0 );
297-
311+
298312 boolean removable = true ;
299-
313+
300314 Set <Entry <Suggestion , Widget >> entrySet = suggestionMap .entrySet ();
301315 for (Entry <Suggestion , Widget > entry : entrySet ) {
302316 if (p .equals (entry .getValue ())) {
303317 if (chipProvider .isChipRemovable (entry .getKey ())){
304318 suggestionMap .remove (entry .getKey ());
305- itemsChanged = true ;
319+ itemsChanged = true ;
306320 }
307321 else {
308322 removable = false ;
309323 }
310324 break ;
311325 }
312326 }
313-
327+
314328 if (removable ){
315- li .removeFromParent ();
329+ li .removeFromParent ();
316330 }
317331 }
318332 itemsHighlighted .clear ();
@@ -393,8 +407,8 @@ public void onClick(ClickEvent clickEvent) {
393407 }
394408 }
395409 }
396- });
397-
410+ });
411+
398412 if (chip .getIcon () != null ){
399413 chip .getIcon ().addClickHandler (new ClickHandler () {
400414 public void onClick (ClickEvent clickEvent ) {
@@ -406,7 +420,7 @@ public void onClick(ClickEvent clickEvent) {
406420 box .showSuggestionList ();
407421 }
408422 }
409- });
423+ });
410424 }
411425
412426 suggestionMap .put (suggestion , chip );
@@ -524,12 +538,12 @@ public void setLimit(int limit) {
524538
525539 @ Override
526540 public String getPlaceholder () {
527- return itemBox . getElement (). getAttribute ( "placeholder" );
541+ return label . getText ( );
528542 }
529543
530544 @ Override
531545 public void setPlaceholder (String placeholder ) {
532- itemBox . getElement (). setAttribute ( "placeholder" , placeholder );
546+ label . setText ( placeholder );
533547 }
534548
535549 @ Override
@@ -541,7 +555,7 @@ public void setError(String error) {
541555 public void setSuccess (String success ) {
542556 errorMixin .setSuccess (success );
543557 }
544-
558+
545559 @ Override
546560 public void setHelperText (String helperText ) {
547561 errorMixin .setHelperText (helperText );
@@ -585,20 +599,20 @@ public void setDirectInputAllowed(boolean directInputAllowed) {
585599 public boolean isDirectInputAllowed () {
586600 return directInputAllowed ;
587601 }
588-
602+
589603 /**
590604 * Sets the style class applied to chips when they are selected.
591605 * <p>
592606 * Defaults to "blue white-text".
593607 * </p>
594- *
608+ *
595609 * @param selectedChipStyle
596610 * The class or classes to be applied to selected chips
597611 */
598612 public void setSelectedChipStyle (String selectedChipStyle ) {
599613 this .selectedChipStyle = selectedChipStyle ;
600614 }
601-
615+
602616 /**
603617 * Returns the style class applied to chips when they are selected.
604618 * <p>
@@ -677,28 +691,28 @@ public static interface MaterialChipProvider {
677691 * suggestion should be ignored.
678692 */
679693 MaterialChip getChip (Suggestion suggestion );
680-
694+
681695 /**
682696 * Returns whether the chip defined by the suggestion should be selected when the user clicks on it.
683- *
697+ *
684698 * <p>
685699 * Selecion of chips is used to batch remove suggestions, for example.
686700 * </p>
687- *
701+ *
688702 * @param suggestion
689703 * the selected {@link Suggestion}
690- *
691- * @see MaterialAutoComplete#setSelectedChipStyle(String)
704+ *
705+ * @see MaterialAutoComplete#setSelectedChipStyle(String)
692706 */
693707 boolean isChipSelectable (Suggestion suggestion );
694-
708+
695709 /**
696710 * Returns whether the chip defined by the suggestion should be removed from the autocomplete when clicked on its icon.
697- *
711+ *
698712 * <p>
699713 * Override this method returning <code>false</code> to implement your own logic when the user clicks on the chip icon.
700714 * </p>
701- *
715+ *
702716 * @param suggestion
703717 * the selected {@link Suggestion}
704718 */
@@ -708,10 +722,10 @@ public static interface MaterialChipProvider {
708722 /**
709723 * Default implementation of the {@link MaterialChipProvider} interface,
710724 * used by the {@link gwt.material.design.addins.client.autocomplete.MaterialAutoComplete}.
711- *
725+ *
712726 * <p>
713727 * By default all chips are selectable and removable. The default {@link IconType} used by the chips provided is the {@link IconType#CLOSE}.
714- * </p>
728+ * </p>
715729 *
716730 * @see gwt.material.design.addins.client.autocomplete.MaterialAutoComplete#setChipProvider(MaterialChipProvider)
717731 */
@@ -733,15 +747,15 @@ public MaterialChip getChip(Suggestion suggestion) {
733747 }
734748 chip .setText (textChip );
735749 chip .setIconType (IconType .CLOSE );
736-
750+
737751 return chip ;
738752 }
739-
753+
740754 @ Override
741755 public boolean isChipRemovable (Suggestion suggestion ) {
742756 return true ;
743757 }
744-
758+
745759 @ Override
746760 public boolean isChipSelectable (Suggestion suggestion ) {
747761 return true ;
@@ -789,4 +803,10 @@ public void setValue(List<? extends Suggestion> value, boolean fireEvents) {
789803 ValueChangeEvent .fire (this , getValue ());
790804 }
791805 }
806+
807+ @ Override
808+ public void setEnabled (boolean enabled ) {
809+ super .setEnabled (enabled );
810+ itemBox .setEnabled (enabled );
811+ }
792812}
0 commit comments