Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » UpdateListStrategy validation
UpdateListStrategy validation [message #24245] Sun, 26 July 2009 09:05 Go to next message
Matt Biggs is currently offline Matt BiggsFriend
Messages: 71
Registered: July 2009
Member
It appears that UpdateListStrategy doesn't have similar flexibility for
supporting validation such as UpdateValueStrategy. Is there a suggested
way of achieving this or is this planned for the future?

cheers

Matt
Re: UpdateListStrategy validation [message #557402 is a reply to message #24245] Tue, 07 September 2010 10:07 Go to previous messageGo to next message
Henno Vermeulen is currently offline Henno VermeulenFriend
Messages: 126
Registered: July 2009
Senior Member
Oops double post due to connection problems.

[Updated on: Tue, 07 September 2010 10:08]

Report message to a moderator

Re: UpdateListStrategy validation [message #557403 is a reply to message #24245] Tue, 07 September 2010 10:07 Go to previous messageGo to next message
Henno Vermeulen is currently offline Henno VermeulenFriend
Messages: 126
Registered: July 2009
Senior Member
I'm interested in this as well. I currently add decorators that show validation errors and warnings to my widgets and would like to do the same for a ListViewer.
Re: UpdateListStrategy validation [message #629758 is a reply to message #557403] Wed, 29 September 2010 13:34 Go to previous message
Henno Vermeulen is currently offline Henno VermeulenFriend
Messages: 126
Registered: July 2009
Senior Member
I had a lot of trouble with UpdateListStrategy and DataBindingContext.bindList not working as expected (see this post).

I finally solved my problem by using a normal value binding and wrapping the multi selection observable list into a WritableValue... This has the added benefit that you can use everything you use in the usual value bindings.

Disclaimer: not sure if this code is bug (or resource-leak) free.

package nl.hm.ilja.ui.databinding;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.list.IListChangeListener;
import org.eclipse.core.databinding.observable.list.IObservableList;
import org.eclipse.core.databinding.observable.list.ListChangeEvent;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.observable.value.WritableValue;

/**
 * An {@link IObservableValue} which wraps {@link IObservableList} and
 * represents a {@link List}-typed value that is synchronized with the contents
 * of the wrapped observable list.
 * 
 * <p>
 * Useful for implementing a list data binding as a value binding. This is a
 * workaround for <a
 * href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=326507">bugs</a> in
 * {@link DataBindingContext#bindList(IObservableList, IObservableList)}.
 * 
 * @author Henno Vermeulen
 */
public class ObservableListValue extends WritableValue {

	private final IObservableList list;
	private boolean inSynchronizeValueToList;
	private IListChangeListener listChangeListener;

	/**
	 * Private constructor, use {@link #newInstance(IObservableList)} instead.
	 */
	private ObservableListValue(final IObservableList list) {
		this.list = list;
		listChangeListener = new IListChangeListener() {
			@Override
			public void handleListChange(ListChangeEvent event) {
				if (!inSynchronizeValueToList) {
					synchronizeObservableListToValue();
				}
			}
		};
	}

	/**
	 * @return a new {@link ObservableListValue} that is synchronized with
	 *         <code>listToWrap</code>.
	 */
	public static ObservableListValue newInstance(
			final IObservableList listToWrap) {
		ObservableListValue instance = new ObservableListValue(listToWrap);
		instance.synchronizeObservableListToValue();
		listToWrap.addListChangeListener(instance.listChangeListener);
		return instance;
	}

	private void synchronizeObservableListToValue() {
		// Simply setting the list itself does not fire a change event the
		// second time!
		super.doSetValue(new ArrayList(list));
	}

	private void synchronizeValueToObservableList() {
		inSynchronizeValueToList = true;
		// TODO optimization this throws two list change events
		list.clear();
		Collection value = (Collection) getValue();
		if (value != null) {
			list.addAll(value);
		}
		inSynchronizeValueToList = false;
	}

	@Override
	public void doSetValue(Object value) {
		super.doSetValue(value);
		synchronizeValueToObservableList();
	}

	@Override
	public synchronized void dispose() {
		list.removeListChangeListener(listChangeListener);
		super.dispose();
	}
}
Previous Topic:TextViewer and change TextPresentation
Next Topic:[databinding] Customize WizardPageSupport
Goto Forum:
  


Current Time: Thu Apr 18 01:37:08 GMT 2024

Powered by FUDForum. Page generated in 0.01675 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top