Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » Observing CellEditor.value
Observing CellEditor.value [message #542552] Fri, 25 June 2010 06:18 Go to next message
SlowStrider Mising name is currently offline SlowStrider Mising name
Messages: 115
Registered: July 2009
Senior Member
Hello, I am making cell editor support for data binding with a Boolean through a CheckBoxCellEditor.

I tried this:
package nl.hm.ilja.ui.databinding.viewers;

import nl.hm.ilja.ui.databinding.CellEditorValueProperty;

import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.viewers.ObservableValueEditingSupport;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.ViewerCell;

/**
 * Editing support that uses JFace Data Binding to control the editing
 * lifecycle. The standard EditingSupport get/setValue(...) lifecycle is not
 * used.
 */
public class CheckBoxObservableValueEditingSupport extends
		ObservableValueEditingSupport {
	private CheckboxCellEditor cellEditor;
	private final String propertyName;

	public CheckBoxObservableValueEditingSupport(ColumnViewer viewer,
			CheckboxCellEditor cellEditor, DataBindingContext dbc,
			String propertyName) {
		super(viewer, dbc);
		this.cellEditor = cellEditor;
		this.propertyName = propertyName;
	}

	@Override
	protected IObservableValue doCreateCellEditorObservable(
			CellEditor cellEditor) {
		return ????????????????;
	}

	@Override
	protected IObservableValue doCreateElementObservable(Object element,
			ViewerCell cell) {
		System.out.println("doCreateElementObservable" + element);
		return BeansObservables.observeValue(element, propertyName);
	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		return cellEditor;
	}
}



But how can I get an IObservable (or an IValueProperty) for observing the "value" property in CellEditor and specifically the CheckBoxCellEditor?

I tried by extending WidgetValueProperty but I get a classcastexception because a CellEditor is not a Widget. The weird thing is that the superclass CellEditor does seem to use org.eclipse.jface.util.IPropertyChangeListener. But this is incompatible with Java's own property change listener system so BeansObservables.observe(cellEditor, "value") does not work.

Also, the CheckBoxCellEditor seems to override the setvalue method without using this property change support.
Re: Observing CellEditor.value [message #542571 is a reply to message #542552] Fri, 25 June 2010 07:09 Go to previous message
SlowStrider Mising name is currently offline SlowStrider Mising name
Messages: 115
Registered: July 2009
Senior Member
For the time being I solved my problem with a simpler editor support which does not handle model updates while editing, but I think this is not important for the CheckBoxCellEditor. Warning: I use internal Eclipse API....

It can be used as:
	private EditingSupport createBooleanEditingSupport(ColumnViewer viewer,
			String propertyName) {
		// TODO: I prefer an editor based on ObservableValueEditingSupport
		CheckboxCellEditor cellEditor = new CheckboxCellEditor(
				(Composite) viewer.getControl());
		return new SimplePropertyEditingSupport(viewer, cellEditor,
				propertyName);
	}


package nl.hm.ilja.ui.databinding.viewers;

import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.internal.databinding.beans.BeanPropertyHelper;
import org.eclipse.jface.databinding.viewers.ObservableValueEditingSupport;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.EditingSupport;

/**
 * Support for editing model properties. This is comparable to the binding
 * achieved with {@link ObservableValueEditingSupport} but without conversion
 * and without listening to model updates during editing. I mainly created this
 * because I did not know how to get an {@link IObservableValue} for the value
 * in a {@link CheckboxCellEditor}.
 * 
 * <p>
 * Note that Ralf Ebert also created a similar thing:
 * de.ralfebert.rcputils.properties.PropertyEditingSupport, but he uses some
 * extra helper interfaces for conversion.
 * 
 * @author Henno Vermeulen
 * 
 */
public class SimplePropertyEditingSupport extends EditingSupport {

	private final CellEditor cellEditor;
	private final String propertyName;

	public SimplePropertyEditingSupport(ColumnViewer viewer,
			CellEditor cellEditor, String propertyName) {
		super(viewer);
		this.cellEditor = cellEditor;
		this.propertyName = propertyName;
	}

	@Override
	protected boolean canEdit(Object element) {
		return true;
	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		return cellEditor;
	}

	@Override
	protected Object getValue(Object element) {
		return BeanPropertyHelper.readProperty(element, BeanPropertyHelper
				.getPropertyDescriptor(element.getClass(), propertyName));
	}

	@Override
	protected void setValue(Object element, Object value) {
		BeanPropertyHelper
				.writeProperty(element,
						BeanPropertyHelper.getPropertyDescriptor(element
								.getClass(), propertyName), value);
	}

}
Previous Topic:using other celleditors than TextCellEditor with ObservableValueEditingSupport
Next Topic:Disable hiding celleditor controls
Goto Forum:
  


Current Time: Sat Aug 30 14:25:23 EDT 2014

Powered by FUDForum. Page generated in 0.02558 seconds