Customizing a SuggestBox to simulate a Selection Box.

0

This class attempts to combine the features of a SuggestBox and a ListBox .

/** *PUBLIC SOFTWARE * *This source code has been placed in the public domain. You can use, modify, and distribute *the source code and executable programs based on the source code. * *However, note the following: * *DISCLAIMER OF WARRANTY * * This source code is provided "as is" and without warranties as to performance * or merchantability. The author and/or distributors of this source code may * have made statements about this source code. Any such statements do not constitute * warranties and shall not be relied on by the user in deciding whether to use * this source code.This source code is provided without any express or implied * warranties whatsoever. Because of the diversity of conditions and hardware * under which this source code may be used, no warranty of fitness for a * particular purpose is offered. The user is advised to test the source code * thoroughly before relying on it. The user must assume the entire risk of * using the source code. * */ import com.google.gwt.event.dom.client.ChangeEvent; import com.google.gwt.event.dom.client.ChangeHandler; import com.google.gwt.event.dom.client.KeyUpEvent; import com.google.gwt.event.dom.client.KeyUpHandler; import com.google.gwt.event.logical.shared.SelectionEvent; import com.google.gwt.event.logical.shared.SelectionHandler; import com.google.gwt.event.shared.HandlerRegistration; import com.google.gwt.user.client.ui.MultiWordSuggestOracle; import com.google.gwt.user.client.ui.SuggestBox; import com.google.gwt.user.client.ui.SuggestOracle; import com.google.gwt.user.client.ui.SuggestOracle.Suggestion; import com.google.gwt.user.client.ui.TextBox; import com.google.gwt.user.client.ui.TextBoxBase; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Extension of suggestBox simulating a traditional single-select list . * @author amal * @version 1.0 */ public final class MyGwtSuggestBox extends SuggestBox { private String value; private HashMap<String, String> itemValues; private HashMap<String, String> valueItems; private MultiWordSuggestOracle oracle; public MyGwtSuggestBox(String value, HashMap<String, String> itemValues, HashMap<String, String> valueItems, MultiWordSuggestOracle oracle) { this.value = value; this.itemValues = itemValues; this.valueItems = valueItems; this.oracle = oracle; } private ChangeHandler changeHandler = new ChangeHandler() { /** * check if the current value is in set of possible values , else rollback */ public void onChange(ChangeEvent event) { TextBox x = (TextBox) event.getSource(); String text = x.getText().trim(); if (value == null) { value = ""; } if (itemValues.keySet().contains(text)) { value = text; } else { x.setText(value); } } }; private KeyUpHandler keyUpHandler = new KeyUpHandler() { public void onKeyUp(KeyUpEvent event) { MyGwtSuggestBox x = (MyGwtSuggestBox) event.getSource(); if (x.getText().trim().equals("") && event.isDownArrow() && !((DefaultSuggestionDisplay) x. getSuggestionDisplay()).isSuggestionListShowing()) { x.showSuggestionList(); } } }; private SelectionHandler<SuggestOracle.Suggestion> selectionHandler = new SelectionHandler<SuggestOracle.Suggestion>() { public void onSelection(SelectionEvent<Suggestion> event) { SuggestBox x = (SuggestBox) event.getSource(); String text = x.getText().trim(); if (value == null) { value = ""; } if (itemValues.keySet().contains(text)) { value = text; } else { x.setText(value); } } }; private HandlerRegistration regChange = null; private HandlerRegistration regFocus = null; ; private HandlerRegistration regKeyPress = null; private HandlerRegistration regSelection = null; public MyGwtSuggestBox() { this(false); } public MyGwtSuggestBox(boolean enforceInputValidation) { super(); itemValues = new HashMap<String, String>(); valueItems = new HashMap<String, String>(); oracle = (MultiWordSuggestOracle) super.getSuggestOracle(); regKeyPress = super.addKeyUpHandler(keyUpHandler); if (!enforceInputValidation) { regChange = super.getTextBox().addChangeHandler(changeHandler); regSelection = super.addSelectionHandler(selectionHandler); } } public void removeInputValidation() { if (null != regChange) { regChange.removeHandler(); regChange = null; } if (null != regFocus) { regFocus.removeHandler(); regFocus = null; } } public MyGwtSuggestBox(SuggestOracle oracle, TextBoxBase box, SuggestionDisplay suggestDisplay) { throw new UnsupportedOperationException("Not Supported.Yet."); } public MyGwtSuggestBox(SuggestOracle oracle, TextBoxBase box) { throw new UnsupportedOperationException("Not Supported.Yet."); } public MyGwtSuggestBox(SuggestOracle oracle) { throw new UnsupportedOperationException("Not Supported.Yet."); } public void removeAllItems() { itemValues = new HashMap<String, String>(); valueItems = new HashMap<String, String>(); oracle.clear(); } public void addItems(List<String> items) { for (String item : items) { addItem(item); } } public void addItems(String[] items) { for (String item : items) { addItem(item); } } public void addItems(Map<String, String> keyItems) { for (String key : keyItems.keySet()) { addItem(keyItems.get(key), key); } } public void addItem(String item) { if (itemValues.containsKey(item)) { throw new RuntimeException("Duplicate item in list : " + item); } if (valueItems.containsKey(item)) { throw new RuntimeException("Duplicate value in list : " + item); } itemValues.put(item, item); valueItems.put(item, item); oracle.add(item); oracle.setDefaultSuggestionsFromText(itemValues.keySet()); } public void addItem(String item, String key) { if (itemValues.containsKey(item)) { throw new RuntimeException("Duplicate item in list : " + item); } if (valueItems.containsKey(key)) { throw new RuntimeException("Duplicate value in list : " + key); } itemValues.put(item, key); valueItems.put(key, item); oracle.add(item); oracle.setDefaultSuggestionsFromText(itemValues.keySet()); } public String getSelectedValue() { return itemValues.get(super.getText()); } public void setSelectedValue(String key) { super.setText(valueItems.get(key)); } public void clear() { super.setText(""); } }

Read more »

0 comments:

Post a Comment

© Zone817. Powered by Blogger.