PDA

View Full Version : ComboBox<Bean> .getvalue() method return null if not modified



DaRkViDe
9 Dec 2010, 9:39 AM
Hi,
I've a problem with combobox containig Beans object!
All works fine except if i set a default value with .setValue(BEANOBJECT) loaded from db and the combos shows the correct value but when i call .getValue() without modify the selected value the combo returns null!
If i select another value all works correctly!
I used this method with combobox of content type <String> and when i call .getValue() all works fine!
Instead with beans the combos returns null if not modified...


I tryed with .getOriginalValue() method and this works but if i select a different vaule from the combo the returned value is the old wrong...

I thought about using a listener to set a boolean value if the combo where modified or not, and call the getValue() or .getOriginalValue() method but i hope that exist a better solution...

Thanks

Below some code...



RpcProxy<PagingLoadResult<EnteModel>> proxyEnte = new RpcProxy<PagingLoadResult<EnteModel>>() {

@Override
protected void load(Object loadConfig,
AsyncCallback<PagingLoadResult<EnteModel>> callback) {

bibliotecheService.getEntiPaginatiFilteredPerCombos(
(ModelData) loadConfig, callback);
}

};

ModelReader enteReader = new ModelReader();

final PagingLoader<PagingLoadResult<VoceUnicaModel>> enteLoader = new BasePagingLoader<PagingLoadResult<VoceUnicaModel>>(
proxyEnte, enteReader);
enteLoader.load();
final ListStore<EnteModel> listStoreEnte = new ListStore<EnteModel>(
enteLoader);

final ComboBox<EnteModel> denominazioneEnteField = new ComboBox<EnteModel>();

denominazioneEnteField.setWidth(400);
denominazioneEnteField.setDisplayField("denominazione");

denominazioneEnteField.setStore(listStoreEnte);
denominazioneEnteField.setFireChangeEventOnSetValue(true);
denominazioneEnteField.setEmptyText("Scegli una denominazione...");
denominazioneEnteField.setForceSelection(false);
denominazioneEnteField.setLazyRender(false);
denominazioneEnteField.setTriggerAction(TriggerAction.ALL);
denominazioneEnteField.setEditable(true);
denominazioneEnteField.setPageSize(10);
denominazioneEnteField.setTypeAhead(true);
denominazioneEnteField.setMinChars(1);

//HERE SET A DEFAULT VALUE
denominazioneEnteField.setValue(biblioteca.getEnte());

Button updateEnteAppartenenza = new Button("Crea l'ente di appartenenza");

updateEnteAppartenenza.addSelectionListener(new SelectionListener<ButtonEvent>() {

@Override
public void componentSelected(ButtonEvent ce) {


final Listener<MessageBoxEvent> l = new Listener<MessageBoxEvent>() {
public void handleEvent(MessageBoxEvent ce) {
Button btn = ce.getButtonClicked();
if (btn.getText().equalsIgnoreCase("Si")) {
HashMap<String, Object> params = new HashMap<String, Object>();

//THIS METHOD RETURN A NULL VALUe: .getValue(BEAN) params.put("denominazione",denominazioneEnteField.getValue().getDenominazione());
params.put("enteTipologiaAmministrativa",tipologiaAmministrativaField.getValue());
params.put("stato",statoAppartenenzaField.getValue());

bibliotecheService.setEnte(biblioteca.getIdBiblio(),
params, new AsyncCallback<Void>() {

@Override
public void onFailure(Throwable caught) {
MessageBox m = new MessageBox();
m.setType(MessageBoxType.ALERT);
m.setTitle("ERRORE UPDATE");
m.setMessage("Errore durante l'aggiornamento!");
m.setIcon(MessageBox.ERROR);
m.show();

}

@Override
public void onSuccess(Void result) {

MessageBox
.info("ESITO UPDATE",
"Aggiornamento effettuato con successo!",
null)
.show();
fireReleoadbiblioDataEvent();
}
});

}

}
};
MessageBox
.confirm(
"UPDATE",
"La modifica verrĂ  applicata al database! Continuare?",
l).show();
}
});

sven
9 Dec 2010, 9:51 AM
All works fine except if i set a default value with .setValue(BEANOBJECT) loaded from db and the combos shows the correct value but when i call .getValue() without modify the selected value the combo returns null!
Is the model you set currently in the store of the combo? If you have paging, it could happen that is not in there.

DaRkViDe
9 Dec 2010, 10:00 AM
Is the model you set currently in the store of the combo? If you have paging, it could happen that is not in there.

in the drop down panel, combo can show only 10 items at time...all record in db are more then 2 hundred, certainly the defalut value set is not in the store...or is very difficult...


I thought about using a listener to set a boolean value if the combo where modified or not, and call the getValue() or .getOriginalValue() method but i hope that exist a better solution...
is good-for-nothing iterate the store if in 99% it doesn't contain the default value cause of the pagination..
In this case the above can be a solution?

sven
9 Dec 2010, 10:06 AM
private class MyCombo<M extends ModelData> extends ComboBox<M> {
@Override
public M getValue() {
if (value != null) {
Object val = value.get(getDisplayField());
if (getRawValue().equals(val != null ? val.toString() : null)) {
return value;
}

}
return super.getValue();
}
}

Try this custom class. Should work for this usecase too

DaRkViDe
9 Dec 2010, 10:08 AM
ok thanks ;)

sven
9 Dec 2010, 10:09 AM
Please report back here if it works or not.

DaRkViDe
10 Dec 2010, 1:34 AM
It works, thanks! I'm very grateful for your support sven!
Thanks again.. :)