|
Re: [databinding] ComboViewer with empty/null entry [message #547192 is a reply to message #546587] |
Thu, 15 July 2010 21:39 |
budili Missing name 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 #549877 is a reply to message #549860] |
Wed, 28 July 2010 10:23 |
Henno Vermeulen 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
|
|
|
Powered by
FUDForum. Page generated in 0.03935 seconds