Form.commit() firing ValueChange on fields

Hi,

Some fields of my Form depend on the value of others. I have 2 ComboBoxes, one for the Country and another one for the City. So every time the user selects a Country, the ComboBox for the Cities must be Updated. So I handle this in a ValueChangeListener on the Countries ComboBox.


		final ValueChangeListener listener = new ValueChangeListener() {
			@Override
			public void valueChange(ValueChangeEvent event) {
				
				// here we would query the cities for the selected country
				Country selectedCountry = (Country) event.getProperty()
						.getValue();
				List<City> cities = new ArrayList<City>();
				if (selectedCountry.getCode().equals("US")) {
					cities.add(new City("NY", "New York"));
					cities.add(new City("TX", "Texas City"));
				} else if (selectedCountry.getCode().equals("UK")) {
					cities.add(new City("LD", "London"));
				}

				// and then refresh the cities combo
				ComboBox cityComboBox = (ComboBox) form.getField("city");
				cityComboBox.removeAllItems();
				for (City city : cities) {
					cityComboBox.addItem(city);
				}
			}
		};
		countryComboBox.addListener(listener);

The problem is that whenever I commit the form this handler gets called and clears the value of the City and my bean associated with the form remains with a null City. Is there any better Aproach for this problem?

Just for the record, I solved this problem creating a new subclass of ComboBox, where ValueChange is only fired if the field is not being commited.

Here is the code:

public class SimpleComboBox extends ComboBox {

	private boolean commiting;

	public SimpleComboBox(String caption) {
		super(caption);
	}

	@Override
	public void commit() throws SourceException, InvalidValueException {
		try {
			setCommiting(true);
			super.commit();
		} finally {
			setCommiting(false);
		}
	}

	@Override
	protected void fireValueChange(boolean repaintIsNotNeeded) {
		if (!isCommiting()) {
			super.fireValueChange(repaintIsNotNeeded);
		}
	}

	public boolean isCommiting() {
		return commiting;
	}

	public void setCommiting(boolean commiting) {
		this.commiting = commiting;
	}

}

I think it makes sense. ValueChange should not fire when commiting the form, at least if you are using setImmediate(true) on the form. Any thoughts?

I used to be an atheist, but I’m not anymore.
It seems like God actually exists AND he uses this marvelous framework! :smiley:

Just registered to thank you for this code fragment, I spent the whole weekend looking for a workaround to the resetting Selects and ComboBoxes. Really, thanks a lot.

Cheers,

Ignacio Dramis