Tableviewer with databinding and inline edit support for boolean [message #328226] |
Mon, 19 May 2008 13:32 ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: lukas.myslivec.com
Hello,
my tableviewer is bound to a collection (like in the jface databinding
snippet).
i want to add in line edit support. it works great for text ( i have done
it like the example in Snippet013TableViewerEditing ).
now i want to do the same with booleans. when the user clicks the cell it
should change from true to false (an the other why).
i tryed it the following way:
public class InlineCheckboxEditingSupport extends
ObservableValueEditingSupport
{
private CellEditor cellEditor;
private String propertyName;
/**
* @param viewer
* @param dbc
*/
public InlineCheckboxEditingSupport(ColumnViewer viewer,
DataBindingContext dbc, String propertyName)
{
super(viewer, dbc);
cellEditor = new CheckboxCellEditor((Composite) viewer.getControl());
this.propertyName = propertyName;
}
protected CellEditor getCellEditor(Object element)
{
return cellEditor;
}
protected IObservableValue doCreateCellEditorObservable(CellEditor
cellEditor)
{
return SWTObservables.observeSelection(cellEditor.getControl());
}
protected IObservableValue doCreateElementObservable(Object element,
ViewerCell cell)
{
return BeansObservables.observeValue(element, propertyName);
}
}
But this fails because checkboxcelleditor doesnt provide a control.
any suggestions?
reagards
lukas
|
|
|
Re: Tableviewer with databinding and inline edit support for boolean [message #328235 is a reply to message #328226] |
Mon, 19 May 2008 17:07 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: eclipse-news.rizzoweb.com
lukas wrote:
> Hello,
>
> my tableviewer is bound to a collection (like in the jface databinding
> snippet).
>
> i want to add in line edit support. it works great for text ( i have
> done it like the example in Snippet013TableViewerEditing ).
>
> now i want to do the same with booleans. when the user clicks the cell
> it should change from true to false (an the other why).
>
> i tryed it the following way:
>
> public class InlineCheckboxEditingSupport extends
> ObservableValueEditingSupport
> {
> private CellEditor cellEditor;
> private String propertyName;
>
> /**
> * @param viewer
> * @param dbc
> */
> public InlineCheckboxEditingSupport(ColumnViewer viewer,
> DataBindingContext dbc, String propertyName)
> {
> super(viewer, dbc);
> cellEditor = new CheckboxCellEditor((Composite) viewer.getControl());
> this.propertyName = propertyName;
> }
>
> protected CellEditor getCellEditor(Object element)
> {
> return cellEditor;
> }
>
> protected IObservableValue doCreateCellEditorObservable(CellEditor
> cellEditor)
> {
> return SWTObservables.observeSelection(cellEditor.getControl());
> }
>
> protected IObservableValue doCreateElementObservable(Object element,
> ViewerCell cell)
> {
> return BeansObservables.observeValue(element, propertyName);
> }
> }
>
> But this fails because checkboxcelleditor doesnt provide a control.
You have to set up a label provider for the cell that returns a checkbox
image depending on the boolean state of the model. It is ugly, but that
is how it has to work in 3.3 - maybe 3.4 will address this...
Check out this little trick that one of the SWT/JFace committers posted:
http://tom-eclipse-dev.blogspot.com/2007/01/tableviewers-and -nativelooking.html
Hope this helps,
Eric
|
|
|
|
Re: Tableviewer with databinding and inline edit support for boolean [message #328250 is a reply to message #328241] |
Tue, 20 May 2008 08:25 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
Originally posted by: lukas.myslivec.com
Hello,
thank you very much (booth of you).
the BooleanCellEditor works greate. i only had to modify the if statement
in deactivate method otherwise the cell gets empty if i am changing the
checkbox to the origin value (like from true to false to true).
regards
lukas
public class BooleanCellEditor extends CellEditor
{
private Button button;
private ViewerRow row;
private int index;
private String restoredText;
private Image restoredImage;
private Boolean oldValue;
/**
* @param parent
*/
public BooleanCellEditor(Composite parent)
{
super(parent);
}
/**
* @param parent
* @param style
*/
public BooleanCellEditor(Composite parent, int style)
{
super(parent, style);
}
public LayoutData getLayoutData()
{
LayoutData data = super.getLayoutData();
data.horizontalAlignment = SWT.CENTER;
data.grabHorizontal = false;
return data;
}
protected Control createControl(Composite parent)
{
Font font = parent.getFont();
Color bg = parent.getBackground();
button = new Button(parent, getStyle() | SWT.CHECK);
button.setFont(font);
button.setBackground(bg);
button.addKeyListener(new KeyAdapter()
{
/*
* (non-Javadoc)
*
* @see
org.eclipse.swt.events.KeyAdapter#keyReleased(org.eclipse.sw t.events.KeyEvent)
*/
public void keyReleased(KeyEvent e)
{
if (e.character == SWT.ESC)
{
fireCancelEditor();
}
}
});
return button;
}
protected Object doGetValue()
{
return new Boolean(button.getSelection());
}
protected void doSetValue(Object value)
{
boolean selection = Boolean.TRUE.equals(value);
button.setSelection(selection);
}
protected void doSetFocus()
{
if (button != null)
{
button.setFocus();
}
}
protected void deactivate(ColumnViewerEditorDeactivationEvent event)
{
super.deactivate(event);
if (event.eventType ==
ColumnViewerEditorDeactivationEvent.EDITOR_CANCELED
|| oldValue == button.getSelection())
{
row.setImage(index, restoredImage);
row.setText(index, restoredText);
}
row = null;
restoredImage = null;
restoredText = null;
}
public void activate(ColumnViewerEditorActivationEvent activationEvent)
{
ViewerCell cell = (ViewerCell) activationEvent.getSource();
index = cell.getColumnIndex();
row = (ViewerRow) cell.getViewerRow().clone();
restoredImage = row.getImage(index);
restoredText = row.getText(index);
row.setImage(index, null);
row.setText(index, ""); //$NON-NLS-1$
super.activate(activationEvent);
oldValue = button.getSelection();
}
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.viewers.CellEditor#getDoubleClickTimeout()
*/
protected int getDoubleClickTimeout()
{
return 0;
}
}
|
|
|
Re: Tableviewer with databinding and inline edit support for boolean [message #328251 is a reply to message #328250] |
Tue, 20 May 2008 08:53 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) |
Thomas Schindl![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=tom.schindl%40bestsolution.at) Messages: 6651 Registered: July 2009 |
Senior Member |
|
|
Hi Lukas,
Could you file a bug against this snippet under Platform/UI and CC me.
I'll then take care that the bug is fixed.
Tom
lukas schrieb:
> Hello,
>
> thank you very much (booth of you).
>
> the BooleanCellEditor works greate. i only had to modify the if
> statement in deactivate method otherwise the cell gets empty if i am
> changing the checkbox to the origin value (like from true to false to
> true).
>
> regards
> lukas
>
> public class BooleanCellEditor extends CellEditor
> {
> private Button button;
> private ViewerRow row;
> private int index;
> private String restoredText;
> private Image restoredImage;
> private Boolean oldValue;
>
> /**
> * @param parent
> */
> public BooleanCellEditor(Composite parent)
> {
> super(parent);
> }
>
> /**
> * @param parent
> * @param style
> */
> public BooleanCellEditor(Composite parent, int style)
> {
> super(parent, style);
> }
>
> public LayoutData getLayoutData()
> {
> LayoutData data = super.getLayoutData();
> data.horizontalAlignment = SWT.CENTER;
> data.grabHorizontal = false;
> return data;
> }
>
> protected Control createControl(Composite parent)
> {
> Font font = parent.getFont();
> Color bg = parent.getBackground();
> button = new Button(parent, getStyle() | SWT.CHECK);
> button.setFont(font);
> button.setBackground(bg);
> button.addKeyListener(new KeyAdapter()
> {
> /*
> * (non-Javadoc)
> * * @see
> org.eclipse.swt.events.KeyAdapter#keyReleased(org.eclipse.sw t.events.KeyEvent)
>
> */
> public void keyReleased(KeyEvent e)
> {
> if (e.character == SWT.ESC)
> {
> fireCancelEditor();
> }
> }
> });
> return button;
> }
>
> protected Object doGetValue()
> {
> return new Boolean(button.getSelection());
> }
>
> protected void doSetValue(Object value)
> {
> boolean selection = Boolean.TRUE.equals(value);
> button.setSelection(selection);
> }
>
> protected void doSetFocus()
> {
> if (button != null)
> {
> button.setFocus();
> }
> }
>
> protected void deactivate(ColumnViewerEditorDeactivationEvent event)
> {
> super.deactivate(event);
> if (event.eventType ==
> ColumnViewerEditorDeactivationEvent.EDITOR_CANCELED
> || oldValue == button.getSelection()) {
> row.setImage(index, restoredImage);
> row.setText(index, restoredText);
> }
> row = null;
> restoredImage = null;
> restoredText = null;
> }
>
> public void activate(ColumnViewerEditorActivationEvent activationEvent)
> {
> ViewerCell cell = (ViewerCell) activationEvent.getSource();
> index = cell.getColumnIndex();
> row = (ViewerRow) cell.getViewerRow().clone();
> restoredImage = row.getImage(index);
> restoredText = row.getText(index);
> row.setImage(index, null);
> row.setText(index, ""); //$NON-NLS-1$
> super.activate(activationEvent);
> oldValue = button.getSelection();
> }
>
> /*
> * (non-Javadoc)
> * * @see org.eclipse.jface.viewers.CellEditor#getDoubleClickTimeout()
> */
> protected int getDoubleClickTimeout()
> {
> return 0;
> }
> }
>
--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------
|
|
|
Powered by
FUDForum. Page generated in 0.03523 seconds