Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » [databinding] ComboViewer with empty/null entry
[databinding] ComboViewer with empty/null entry [message #546587] Tue, 13 July 2010 13:25 Go to next message
Jan is currently offline JanFriend
Messages: 12
Registered: July 2009
Junior Member
Hi group,

I am new to this databinding stuff and it's nice but also frustrating.
I am using JFace data-binding to bind a field of my model to a
combo-box. I want to be able so set the field of my model to null.

So how do I select null in the UI?
How do I add an empty/null entry to the comboviewer that (automatically)
binds to null?

Thanks Jan
Re: [databinding] ComboViewer with empty/null entry [message #547192 is a reply to message #546587] Thu, 15 July 2010 21:39 Go to previous messageGo to next message
budili Missing name is currently offline budili Missing nameFriend
Messages: 64
Registered: May 2010
Member
One possibilty is to add an item to your combobox, which stands for null, but signalize on the ui an another text.

For example, you have the following items:
- Empty
- Value 1
- Value 2
- ETC

The first item (Empty) is the null item in the model, the others are real items, which you write to your model. For the first one, you can create an converter, which check if empty is selected, than write null to your model, else the real value.
Re: [databinding] ComboViewer with empty/null entry [message #549860 is a reply to message #547192] Wed, 28 July 2010 09:16 Go to previous messageGo to next message
Henno Vermeulen is currently offline Henno VermeulenFriend
Messages: 126
Registered: July 2009
Senior Member
I have exactly the same question. I tried this and it works, thank you!
It still feels a bit like a hack though...
Re: [databinding] ComboViewer with empty/null entry [message #549877 is a reply to message #549860] Wed, 28 July 2010 10:23 Go to previous message
Henno Vermeulen is currently offline Henno VermeulenFriend
Messages: 126
Registered: July 2009
Senior Member
For people who are interested, here is the code I came up with to enable a null element in a ComboViewer.

Usage: decorate your favorite contentprovider with a new NullElementStructuredContentProviderDecorator. For databinding to work you will need to set a NullElementIdentityConverter in the UpdateValueStrategy for both model to UI and UI to model.
If you need real conversion as well you have to adjust this converter class.

import org.eclipse.jface.viewers.Viewer;

/**
 * An element which can be used in JFace {@link Viewer}s to represent a
 * <code>null</code> value in the model.
 * 
 * @see NullElementIdentityConverter
 * @see NullElementStructuredContentProviderDecorator
 * 
 * @author Henno Vermeulen
 */
public final class NullElement {
	private static final NullElement instance = new NullElement();
	private final String name;

	private NullElement() {
		this("");
	}

	public NullElement(String name) {
		this.name = name;
	}

	public static NullElement getInstance() {
		return instance;
	}

	@Override
	public String toString() {
		return name;
	}

}


import org.eclipse.core.databinding.conversion.IConverter;

/**
 * A converter which simply returns the object itself, but converts
 * <code>null</code> into a {@link NullElement} and vice versa.
 * 
 * <p>
 * Possible refactoring: make this into a decorator for an existing converter.
 * 
 * @author Henno Vermeulen
 */
public final class NullElementIdentityConverter implements IConverter {

	@Override
	public Object convert(Object fromObject) {
		if (fromObject == null) {
			return NullElement.getInstance();
		}
		if (fromObject instanceof NullElement) {
			return null;
		}
		return fromObject;
	}

	@Override
	public Object getFromType() {
		return null;
	}

	@Override
	public Object getToType() {
		return null;
	}

}


import java.util.ArrayList;
import java.util.Arrays;

import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;

/**
 * A decorator for an <code>IStructuredContentProvider</code> which adds a
 * {@link NullElement} as the first element. This is useful because it does not
 * require the input to contain a <code>NullElement</code>.
 */
public class NullElementStructuredContentProviderDecorator implements
		IStructuredContentProvider {

	private final IStructuredContentProvider decorated;
	private final NullElement nullElement;

	public NullElementStructuredContentProviderDecorator(
			IStructuredContentProvider decorated) {
		this(decorated, NullElement.getInstance());
	}

	public NullElementStructuredContentProviderDecorator(
			IStructuredContentProvider decorated, NullElement nullElement) {
		this.decorated = decorated;
		this.nullElement = nullElement;
	}

	/**
	 * Returns the elements in the input, which must be either an array or a
	 * <code>Collection</code>.
	 */
	public Object[] getElements(Object inputElement) {
		Object[] elements = decorated.getElements(inputElement);
		ArrayList<Object> l = new ArrayList<Object>();
		l.add(nullElement);
		l.addAll(Arrays.asList(elements));
		return l.toArray();
	}

	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		decorated.inputChanged(viewer, oldInput, newInput);
	}

	public void dispose() {
		decorated.dispose();
	}
}

[Updated on: Wed, 28 July 2010 10:24]

Report message to a moderator

Previous Topic:RCP, slf4j and equinox.ds?
Next Topic:FocusListener vs IPartListener
Goto Forum:
  


Current Time: Fri Apr 26 06:31:12 GMT 2024

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

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

Back to the top