Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Tableviewer with databinding and inline edit support for boolean
Tableviewer with databinding and inline edit support for boolean [message #328226] Mon, 19 May 2008 13:32 Go to next message
Eclipse UserFriend
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 messageGo to next message
Eclipse UserFriend
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 #328241 is a reply to message #328226] Mon, 19 May 2008 18:03 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Well you are right this can't work. In 3.4 you have a chance to fix this
your own by using a real checkbox-editor.

Checkout the following examples from our Snippet collection [1]. If you
combine this with the solution I outlined on my blog you'll get as close
to Native-Checkboxes as possible.

Tom

[1] http://dev.eclipse.org/viewcvs/index.cgi/org.eclipse.jface.s nippets/Eclipse%20JFace%20Snippets/org/eclipse/jface/snippet s/viewers/BooleanCellEditor.java?view=markup


lukas schrieb:
> 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
>


--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------
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 messageGo to next message
Eclipse UserFriend
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
Thomas Schindl is currently offline Thomas SchindlFriend
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
------------------------------------------------------------ --------
Previous Topic:Eclipse version
Next Topic:Best strategy to bind handler to a control
Goto Forum:
  


Current Time: Sun Jul 07 06:19:49 GMT 2024

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

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

Back to the top