Java tutorial
/******************************************************************************* * Copyright (c) 2014, 2015 Scott Clarke (scott@dawg6.com). * * This file is part of Dawg6's Common GWT Libary. * * Dawg6's Common GWT Libary is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Dawg6's Common GWT Libary is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. *******************************************************************************/ package com.dawg6.gwt.client.widgets; import java.util.List; import com.dawg6.common.data.objects.PersistentObject; import com.dawg6.gwt.common.util.ObservableListInterface.ListChangedHandler; import com.google.gwt.event.dom.client.ChangeHandler; import com.google.gwt.event.logical.shared.SelectionEvent; import com.google.gwt.event.logical.shared.SelectionHandler; import com.google.gwt.user.client.ui.Composite; import com.google.gwt.user.client.ui.MultiWordSuggestOracle; import com.google.gwt.user.client.ui.SuggestBox; import com.google.gwt.user.client.ui.SuggestOracle.Suggestion; import com.google.gwt.user.client.ui.VerticalPanel; public class SearchableComboBox<T extends PersistentObject> extends Composite { protected SuggestBox textField; protected MultiWordSuggestOracle oracle; protected ComboBox<T> comboBox; public SearchableComboBox(ComboBox<T> comboBox) { VerticalPanel panel = new VerticalPanel(); initWidget(panel); this.comboBox = comboBox; this.oracle = new MultiWordSuggestOracle(); this.textField = new SuggestBox(oracle); panel.add(textField); panel.add(comboBox); comboBox.setWidth("100%"); comboBox.addObserver(new ListChangedHandler<T>() { @Override public void listChanged(List<T> list) { setSuggestions(list); } }); textField.addSelectionHandler(new SelectionHandler<Suggestion>() { @Override public void onSelection(SelectionEvent<Suggestion> event) { suggestionSelected(event); } }); } protected void setSuggestions(List<T> list) { oracle.clear(); for (T t : list) { String display = comboBox.getDisplayText(t); oracle.add(display); } } protected void suggestionSelected(SelectionEvent<Suggestion> event) { String value = event.getSelectedItem().getReplacementString(); comboBox.setValue(value); } public void refresh() { comboBox.refresh(); } public T getValue() { return comboBox.getValue(); } public void setValue(Long id) { comboBox.setValue(id); } public void setValue(T value) { comboBox.setValue(value); } public void setVisibleItemCount(int i) { comboBox.setVisibleItemCount(i); if (i > 1) comboBox.setHeight("100%"); } public void addChangeHandler(ChangeHandler changeHandler) { comboBox.addChangeHandler(changeHandler); } public int getSelectedIndex() { return comboBox.getSelectedIndex(); } public void removeItem(int i) { comboBox.removeItem(i); } public void insertItem(T value, int i) { comboBox.insertItem(value, i); } public void setSelectedIndex(int i) { comboBox.setSelectedIndex(i); } public int getItemCount() { return comboBox.getItemCount(); } public T getItem(int i) { return comboBox.getItem(i); } public void addItem(T item) { comboBox.addItem(item); } public void clear() { comboBox.clear(); } public void addAll(List<T> items) { comboBox.addAll(items); } }