PDA

View Full Version : ComboBox, LazyRender and hidden from view



VadimV1144
5 Aug 2010, 10:12 AM
Hi, I switched over to GXT 2.2.0 from 2.1.1 and I am having an issue with ComboBoxes that are hidden from view.
I have set a LazyRender option to false on several ComboBoxes, whose values I need to be available regardless if they have been rendered or not.
However, in 2.2.0, I started getting null pointer exceptions in onLastFocusChanged() method of ListViewSelectionModel.java, line 270. On that line the code is trying to access the store that has been associated with the ComoBox. Unfortunately, the store is only binded to view in the initList() method of ComboBox.java which is called only when the ComboBox is rendered.
In my situation, I have the ComboBox on the tab that is hidden and it has not been rendered yet, but it is available for manipulation - (for example setting the data before it is displayed to the user), but if I try to preload the list by setting the LazyRender to false, I get the null pointer exception.

Any thoughts how to fix this?

sven
5 Aug 2010, 1:29 PM
This should already be fixed in SVN and will be part of the next 2.2 pre release.

VadimV1144
7 Aug 2010, 8:28 AM
Hi Sven, I build the GXT version from svn (releases/2.2 folder), and this issue still seems to be there. Is that the correct package?

sven
7 Aug 2010, 9:12 AM
Yes it is. Can you pelase post a fully working testcase that implements EntryPoint and demonstrates your problem?

VadimV1144
7 Aug 2010, 9:59 AM
Here is a little example, it will throw null pointer exception right away when the LazyLoad is set to false.


@Override
public void onModuleLoad() {
RootPanel.get().add(getTest());
}

enum XCities {

MOSCOW("Moscow"),
NEW_YORK("New York"),
LONDON("London");

private String defaultName;

XCities(final String defaultName) {
this.defaultName = defaultName;
}

public String getDefaultName() {
return defaultName;
}

public void setDefaultName(String defaultName) {
this.defaultName = defaultName;
}

public String toString(){
return defaultName;
}
}


public Widget getTest() {


final ListStore<EnumModel> store = new ListStore<EnumModel>();
store.add(EnumModel.convertValuesToEnumModels(XCities.values(), false));

final ComboBox<EnumModel> combo = new ComboBox<EnumModel>();
combo.setFieldLabel("Test Combo");
combo.setName("testCombo");
combo.setDisplayField(EnumModel.ENUM_DISPLAY_NAME_ID);
combo.setValueField(EnumModel.ENUM_DISPLAY_NAME_ID);
combo.setForceSelection(true);
combo.setTypeAhead(true);
combo.setAllowBlank(false);
combo.setStore(store);
combo.setTriggerAction(ComboBox.TriggerAction.ALL);
combo.clearInvalid();

combo.setLazyRender(false);

LayoutContainer lc = new LayoutContainer(new FormLayout());

lc.add(combo, new FormData("95%"));
return lc;
}

class XEnumModel extends BaseModel {

public String ENUM_ID = "enum";
public String ENUM_DISPLAY_NAME_ID = "displayName";

public XEnumModel() {
}

public <T> XEnumModel(T enumValue, String displayName) {
set(ENUM_ID, enumValue);
if(displayName == null)
set(ENUM_DISPLAY_NAME_ID, enumValue != null?enumValue.toString():"-------------------");
else
set(ENUM_DISPLAY_NAME_ID, displayName);
}

public <T> XEnumModel(T enumValue) {
set(ENUM_ID, enumValue);
set(ENUM_DISPLAY_NAME_ID, enumValue != null?enumValue.toString():"-------------------");
}

@SuppressWarnings("unchecked")
public <T>T getEnumValue(){
return (T)get(ENUM_ID);
}

public String getDisplayName(){
return get(ENUM_DISPLAY_NAME_ID);
}

public String getENUM_ID() {
return ENUM_ID;
}

public String getENUM_DISPLAY_NAME_ID() {
return ENUM_DISPLAY_NAME_ID;
}

/**
* Creates the list of EnumModels based on the given array of Enum values. The EnumModels
* list is used to se the values in the store.
*
* @param values - array of enum values
* @param sort - true to sort the values alphabetically before creating the EnumModel list
* @param <T> - enum type
* @return list of EnumModel corresponding to a given enum values
*/
public <T> List<XEnumModel> convertValuesToEnumModels(T[] values, boolean sort){
List<XEnumModel> list = new ArrayList<XEnumModel>();
if(values == null)
return list;

for(T enumValue: values){
list.add(new XEnumModel(enumValue));
}

class EnumValueComparator implements Comparator<XEnumModel> {
public int compare(XEnumModel st1, XEnumModel st2){
return st1.getDisplayName().compareTo(st2.getDisplayName());
}
}
if(sort)
Collections.sort(list, new EnumValueComparator());

return list;
}
}

VadimV1144
7 Aug 2010, 9:09 PM
My apologies, there is a little bug in the above example.
Here is a fully functioning version:



public class GenericTestsEntryPoint implements EntryPoint {
@Override
public void onModuleLoad() {
RootPanel.get().add(getTest());
}

public Widget getTest() {

final ListStore<XEnumModel> store = new ListStore<XEnumModel>();
store.add(XEnumModel.convertValuesToEnumModels(XCities.values(), false));

final ComboBox<XEnumModel> combo = new ComboBox<XEnumModel>();
combo.setFieldLabel("Test Combo");
combo.setName("testCombo");
combo.setDisplayField(XEnumModel.ENUM_DISPLAY_NAME_ID);
combo.setValueField(XEnumModel.ENUM_DISPLAY_NAME_ID);
combo.setForceSelection(true);
combo.setTypeAhead(true);
combo.setAllowBlank(false);
combo.setStore(store);
combo.setTriggerAction(ComboBox.TriggerAction.ALL);
combo.clearInvalid();

combo.setLazyRender(false);

LayoutContainer lc = new LayoutContainer(new FormLayout());
lc.setBorders(true);
lc.setSize(400,200);
lc.add(combo, new FormData("95%"));
return lc;
}
} //end class GenericTestsEntryPoint

enum XCities {

MOSCOW("Moscow"),
NEW_YORK("New York"),
LONDON("London");

private String defaultName;

XCities(final String defaultName) {
this.defaultName = defaultName;
}

public String getDefaultName() {
return defaultName;
}

public void setDefaultName(String defaultName) {
this.defaultName = defaultName;
}

public String toString(){
return defaultName;
}
}

class XEnumModel extends BaseModel {

public static String ENUM_ID = "enum";
public static String ENUM_DISPLAY_NAME_ID = "displayName";

public XEnumModel() {
}

public <T> XEnumModel(T enumValue, String displayName) {
set(ENUM_ID, enumValue);
if(displayName == null)
set(ENUM_DISPLAY_NAME_ID, enumValue != null?enumValue.toString():"-------------------");
else
set(ENUM_DISPLAY_NAME_ID, displayName);
}

public <T> XEnumModel(T enumValue) {
set(ENUM_ID, enumValue);
set(ENUM_DISPLAY_NAME_ID, enumValue != null?enumValue.toString():"-------------------");
}

@SuppressWarnings("unchecked")
public <T>T getEnumValue(){
return (T)get(ENUM_ID);
}

public String getDisplayName(){
return get(ENUM_DISPLAY_NAME_ID);
}

public String getENUM_ID() {
return ENUM_ID;
}

public String getENUM_DISPLAY_NAME_ID() {
return ENUM_DISPLAY_NAME_ID;
}

/**
* Creates the list of EnumModels based on the given array of Enum values. The EnumModels
* list is used to se the values in the store.
*
* @param values - array of enum values
* @param sort - true to sort the values alphabetically before creating the EnumModel list
* @param <T> - enum type
* @return list of EnumModel corresponding to a given enum values
*/
public static <T> List<XEnumModel> convertValuesToEnumModels(T[] values, boolean sort){
List<XEnumModel> list = new ArrayList<XEnumModel>();
if(values == null)
return list;

for(T enumValue: values){
list.add(new XEnumModel(enumValue));
}

class EnumValueComparator implements Comparator<XEnumModel> {
public int compare(XEnumModel st1, XEnumModel st2){
return st1.getDisplayName().compareTo(st2.getDisplayName());
}
}
if(sort)
Collections.sort(list, new EnumValueComparator());

return list;
}
}

sven
9 Aug 2010, 5:27 AM
Fixed in SVN as of revision #2170

xperetz
21 Sep 2010, 12:17 AM
Can any please post the fix for this problems
and a short explanation for why is it happening
thanks
avi