-
-
Save pives/337159 to your computer and use it in GitHub Desktop.
add HasChoices<K,V> interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.Map; | |
import com.google.gwt.event.dom.client.ChangeEvent; | |
import com.google.gwt.event.dom.client.ChangeHandler; | |
import com.google.gwt.event.logical.shared.ValueChangeEvent; | |
import com.google.gwt.event.logical.shared.ValueChangeHandler; | |
import com.google.gwt.event.shared.HandlerRegistration; | |
import com.google.gwt.user.client.ui.HasValue; | |
import com.google.gwt.user.client.ui.ListBox; | |
/** | |
* A ListBox implementation supporting the HasValue interface. | |
* | |
* @author Lukasz Plotnicki <lukasz.plotnicki@med.uni-heidelberg.de> | |
* @author Philip Ives <pives@philadev.com> added ability to insert values for setValue(); | |
* April 19, 2010 added the ability to have Choices. | |
* | |
*/ | |
package com.nemt.mmsweb.client.ui.controls; | |
/** | |
* @param <T> | |
* | |
*/ | |
public class ValueListBox<K,V> extends ListBox implements HasValue<V> , HasChoices<K,V>{ | |
/** | |
* Flag indicating if the handler have been already initialized or not | |
*/ | |
private boolean valueChangeHandlerInitialized; | |
/** | |
* Flag to either add new values or don't set the selectedIndex at all | |
* | |
*/ | |
private boolean addNewValuesToList; | |
/** | |
* Default constructor.New values won't be added to listbox | |
*/ | |
private HashMap<K,V> data; | |
public ValueListBox( ){ | |
super(); | |
this.addNewValuesToList=false; | |
} | |
/** | |
* @param addNewValuesToList | |
*/ | |
public ValueListBox(Boolean addNewValuesToList) { | |
super(); | |
this.addNewValuesToList=addNewValuesToList; | |
} | |
/** | |
* | |
* @see com.google.gwt.user.client.ui.HasValue#getValue() | |
*/ | |
@Override | |
public String getValue() { | |
return getItemText(getSelectedIndex()); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object) | |
*/ | |
@Override | |
public void setValue(String value) { | |
int i; | |
for (i = 0; i < getItemCount(); i++) { | |
if (getItemText(i).equals(value)) { | |
setSelectedIndex(i); | |
break; | |
} | |
} | |
if(i>= getItemCount()) { | |
if (addNewValuesToList) { | |
addItem(value); | |
setSelectedIndex(getItemCount()); | |
} | |
} | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object, | |
* boolean) | |
*/ | |
@Override | |
public void setValue(String value, boolean arg1) { | |
setValue(value); | |
if (arg1) | |
ValueChangeEvent.fire(this, value); | |
} | |
@Override | |
public HandlerRegistration addValueChangeHandler( | |
ValueChangeHandler<String> handler) { | |
// Initialization code | |
if (!valueChangeHandlerInitialized) { | |
valueChangeHandlerInitialized = true; | |
addChangeHandler(new ChangeHandler() { | |
public void onChange(ChangeEvent event) { | |
ValueChangeEvent.fire(ValueListBox.this, getValue()); | |
} | |
}); | |
} | |
return addHandler(handler, ValueChangeEvent.getType()); | |
} | |
@Override | |
public Map<K, V> getChoices() { | |
return data; | |
} | |
@SuppressWarnings("unchecked") | |
@Override | |
public void setChoices(final Map<K, V> map) { | |
data = new HashMap<K,V>( map); | |
this.clear(); | |
for (Iterator iterator = data.keySet().iterator(); iterator.hasNext();) { | |
K key = (K) iterator.next(); | |
this.addItem((String) data.get(key),(String)key); | |
} | |
} | |
@Override | |
public void addChoice(K key, V value) { | |
data.put(key, value); | |
this.addItem((String) value, (String)key); | |
} | |
} |
I'm new to GWT and I've come across a need for this code. When using it, I'm getting the "no default (zero args) constructor. The page http://code.google.com/intl/sv-SE/webtoolkit/doc/latest/DevGuideUiBinder.html#Using_a_widget addresses how to fix this issue. I'm not sure where to use the @uiconstructor annotation that the above site talks about. Can you show me an example of using this?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello thanks for this interesting code.
You could change the use of getItemText (int) into getValue(int) to make it also work for value+ display item.
Jean-Claude