Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Cell editing in TreeViewer in 3.3
Cell editing in TreeViewer in 3.3 [message #323912] Sat, 12 January 2008 17:08 Go to next message
Marcel is currently offline MarcelFriend
Messages: 11
Registered: July 2009
Junior Member
Hi folks,

Would love a hand from someone more familiar with cell editing in JFace.

I had a scheme where right-clicking on an item in a TreeViewer and
selecting "Rename" from the menu allows it to be edited in place. Moving
from 3.2 to 3.3 has broken this.

The canModify and getValue methods of my cell modifier are still called,
but modify is not called. On the screen, the user is not given the
chance to edit the cell - there is a flash, like a refresh, in which I
think the editor is activated and then deactivated (modify is not called
as the value hasn't been changed).

Yet, when I allow modification with a single mouse click - by adding the
event type to isEditorActivated - the whole thing works fine. The only
new code for 3.3 is the TreeViewerEditor.create call, the rest was
functioning in 3.2.

So I'm wondering - is there some flag to set or function call to make to
allow me to programmatically invoke the cell editor? Why does single
click editing work, but programmatic invocation result in the editor
simply opening and closing?

_Any_ help would be appreciated, I'm a little stuck.

Cheers

Marcel

Creating the viewer:

viewer = new TreeViewer(layout, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
viewer.setLabelProvider(new BrowsableTreeLabelProvider());
viewer.setContentProvider(new BrowsableTreeContentProvider());
viewer.setSorter(new BrowsableItemSorter());
viewer.setCellEditors(new CellEditor[] {new
TextCellEditor(viewer.getTree())});
viewer.setColumnProperties(new String[] { "name" });
viewer.setCellModifier(new BackupTreeCellModifier());

TreeViewerEditor.create(viewer, new
ColumnViewerEditorActivationStrategy(viewer) {
protected boolean
isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
return event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
}
}, ColumnViewerEditor.DEFAULT);


In widgetSelected:

else if (e.getSource() == renameItem) {
if (selection != null) {
BackupTreeCellModifier modifier =
(BackupTreeCellModifier) viewer.getCellModifier();
modifier.allowModify(true);
viewer.editElement(selection.getFirstElement(), 0);
modifier.allowModify(false);
}
}
Re: Cell editing in TreeViewer in 3.3 [message #323913 is a reply to message #323912] Sat, 12 January 2008 18:03 Go to previous messageGo to next message
Francis Upton IV is currently offline Francis Upton IVFriend
Messages: 472
Registered: July 2009
Location: Oakland, CA
Senior Member
Look at the RenameResourceAction, I think it does what you want.

Marcel wrote:
> Hi folks,
>
> Would love a hand from someone more familiar with cell editing in JFace.
>
> I had a scheme where right-clicking on an item in a TreeViewer and
> selecting "Rename" from the menu allows it to be edited in place. Moving
> from 3.2 to 3.3 has broken this.
>
> The canModify and getValue methods of my cell modifier are still called,
> but modify is not called. On the screen, the user is not given the
> chance to edit the cell - there is a flash, like a refresh, in which I
> think the editor is activated and then deactivated (modify is not called
> as the value hasn't been changed).
>
> Yet, when I allow modification with a single mouse click - by adding the
> event type to isEditorActivated - the whole thing works fine. The only
> new code for 3.3 is the TreeViewerEditor.create call, the rest was
> functioning in 3.2.
>
> So I'm wondering - is there some flag to set or function call to make to
> allow me to programmatically invoke the cell editor? Why does single
> click editing work, but programmatic invocation result in the editor
> simply opening and closing?
>
> _Any_ help would be appreciated, I'm a little stuck.
>
> Cheers
>
> Marcel
>
> Creating the viewer:
>
> viewer = new TreeViewer(layout, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
> viewer.setLabelProvider(new BrowsableTreeLabelProvider());
> viewer.setContentProvider(new BrowsableTreeContentProvider());
> viewer.setSorter(new BrowsableItemSorter());
> viewer.setCellEditors(new CellEditor[] {new
> TextCellEditor(viewer.getTree())});
> viewer.setColumnProperties(new String[] { "name" });
> viewer.setCellModifier(new BackupTreeCellModifier());
>
> TreeViewerEditor.create(viewer, new
> ColumnViewerEditorActivationStrategy(viewer) {
> protected boolean
> isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
> return event.eventType ==
> ColumnViewerEditorActivationEvent.PROGRAMMATIC;
> }
> }, ColumnViewerEditor.DEFAULT);
>
>
> In widgetSelected:
>
> else if (e.getSource() == renameItem) {
> if (selection != null) {
> BackupTreeCellModifier modifier =
> (BackupTreeCellModifier) viewer.getCellModifier();
> modifier.allowModify(true);
> viewer.editElement(selection.getFirstElement(), 0);
> modifier.allowModify(false);
> }
> }


Re: Cell editing in TreeViewer in 3.3 [message #323914 is a reply to message #323912] Sat, 12 January 2008 18:08 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Marcel schrieb:
> Hi folks,
>
> Would love a hand from someone more familiar with cell editing in JFace.
>
> I had a scheme where right-clicking on an item in a TreeViewer and
> selecting "Rename" from the menu allows it to be edited in place. Moving
> from 3.2 to 3.3 has broken this.
>

There was a regression introduced into 3.3 because an editor has been
activated on right click which is wrong.

This is fixed in 3.3.2.

Please provide a snippet so we can reproduce the problem.

Tom

--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------
Re: Cell editing in TreeViewer in 3.3 [message #323915 is a reply to message #323913] Sat, 12 January 2008 18:11 Go to previous messageGo to next message
Marcel is currently offline MarcelFriend
Messages: 11
Registered: July 2009
Junior Member
Thanks for the suggestion - I could certainly do the rename via a
dialog, standard or not, but I was hoping to stick with the edit in
place approach if possible.

Francis Upton wrote:
> Look at the RenameResourceAction, I think it does what you want.
>
> Marcel wrote:
>> Hi folks,
>>
>> Would love a hand from someone more familiar with cell editing in JFace.
>>
>> I had a scheme where right-clicking on an item in a TreeViewer and
>> selecting "Rename" from the menu allows it to be edited in place.
>> Moving from 3.2 to 3.3 has broken this.
>>
>> The canModify and getValue methods of my cell modifier are still
>> called, but modify is not called. On the screen, the user is not given
>> the chance to edit the cell - there is a flash, like a refresh, in
>> which I think the editor is activated and then deactivated (modify is
>> not called as the value hasn't been changed).
>>
>> Yet, when I allow modification with a single mouse click - by adding
>> the event type to isEditorActivated - the whole thing works fine. The
>> only new code for 3.3 is the TreeViewerEditor.create call, the rest
>> was functioning in 3.2.
>>
>> So I'm wondering - is there some flag to set or function call to make
>> to allow me to programmatically invoke the cell editor? Why does
>> single click editing work, but programmatic invocation result in the
>> editor simply opening and closing?
>>
>> _Any_ help would be appreciated, I'm a little stuck.
>>
>> Cheers
>>
>> Marcel
>>
>> Creating the viewer:
>>
>> viewer = new TreeViewer(layout, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
>> viewer.setLabelProvider(new BrowsableTreeLabelProvider());
>> viewer.setContentProvider(new BrowsableTreeContentProvider());
>> viewer.setSorter(new BrowsableItemSorter());
>> viewer.setCellEditors(new CellEditor[] {new
>> TextCellEditor(viewer.getTree())});
>> viewer.setColumnProperties(new String[] { "name" });
>> viewer.setCellModifier(new BackupTreeCellModifier());
>>
>> TreeViewerEditor.create(viewer, new
>> ColumnViewerEditorActivationStrategy(viewer) {
>> protected boolean
>> isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
>> return event.eventType ==
>> ColumnViewerEditorActivationEvent.PROGRAMMATIC;
>> }
>> }, ColumnViewerEditor.DEFAULT);
>>
>>
>> In widgetSelected:
>>
>> else if (e.getSource() == renameItem) {
>> if (selection != null) {
>> BackupTreeCellModifier modifier =
>> (BackupTreeCellModifier) viewer.getCellModifier();
>> modifier.allowModify(true);
>> viewer.editElement(selection.getFirstElement(), 0);
>> modifier.allowModify(false);
>> }
>> }
Re: Cell editing in TreeViewer in 3.3 [message #323919 is a reply to message #323915] Sat, 12 January 2008 20:24 Go to previous messageGo to next message
Francis Upton IV is currently offline Francis Upton IVFriend
Messages: 472
Registered: July 2009
Location: Oakland, CA
Senior Member
This action does both a dialog and inline. See the
queryNewResourceNameInline method.

Marcel wrote:
> Thanks for the suggestion - I could certainly do the rename via a
> dialog, standard or not, but I was hoping to stick with the edit in
> place approach if possible.
>
> Francis Upton wrote:
>> Look at the RenameResourceAction, I think it does what you want.
>>
>> Marcel wrote:
>>> Hi folks,
>>>
>>> Would love a hand from someone more familiar with cell editing in JFace.
>>>
>>> I had a scheme where right-clicking on an item in a TreeViewer and
>>> selecting "Rename" from the menu allows it to be edited in place.
>>> Moving from 3.2 to 3.3 has broken this.
>>>
>>> The canModify and getValue methods of my cell modifier are still
>>> called, but modify is not called. On the screen, the user is not
>>> given the chance to edit the cell - there is a flash, like a refresh,
>>> in which I think the editor is activated and then deactivated (modify
>>> is not called as the value hasn't been changed).
>>>
>>> Yet, when I allow modification with a single mouse click - by adding
>>> the event type to isEditorActivated - the whole thing works fine. The
>>> only new code for 3.3 is the TreeViewerEditor.create call, the rest
>>> was functioning in 3.2.
>>>
>>> So I'm wondering - is there some flag to set or function call to make
>>> to allow me to programmatically invoke the cell editor? Why does
>>> single click editing work, but programmatic invocation result in the
>>> editor simply opening and closing?
>>>
>>> _Any_ help would be appreciated, I'm a little stuck.
>>>
>>> Cheers
>>>
>>> Marcel
>>>
>>> Creating the viewer:
>>>
>>> viewer = new TreeViewer(layout, SWT.MULTI | SWT.H_SCROLL |
>>> SWT.V_SCROLL);
>>> viewer.setLabelProvider(new BrowsableTreeLabelProvider());
>>> viewer.setContentProvider(new BrowsableTreeContentProvider());
>>> viewer.setSorter(new BrowsableItemSorter());
>>> viewer.setCellEditors(new CellEditor[] {new
>>> TextCellEditor(viewer.getTree())});
>>> viewer.setColumnProperties(new String[] { "name" });
>>> viewer.setCellModifier(new BackupTreeCellModifier());
>>>
>>> TreeViewerEditor.create(viewer, new
>>> ColumnViewerEditorActivationStrategy(viewer) {
>>> protected boolean
>>> isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
>>> return event.eventType ==
>>> ColumnViewerEditorActivationEvent.PROGRAMMATIC;
>>> }
>>> }, ColumnViewerEditor.DEFAULT);
>>>
>>>
>>> In widgetSelected:
>>>
>>> else if (e.getSource() == renameItem) {
>>> if (selection != null) {
>>> BackupTreeCellModifier modifier =
>>> (BackupTreeCellModifier) viewer.getCellModifier();
>>> modifier.allowModify(true);
>>> viewer.editElement(selection.getFirstElement(), 0);
>>> modifier.allowModify(false);
>>> }
>>> }


Re: Cell editing in TreeViewer in 3.3 [message #323920 is a reply to message #323914] Sat, 12 January 2008 23:42 Go to previous messageGo to next message
Marcel is currently offline MarcelFriend
Messages: 11
Registered: July 2009
Junior Member
Tom Schindl wrote:
> Marcel schrieb:
>> Hi folks,
>>
>> Would love a hand from someone more familiar with cell editing in JFace.
>>
>> I had a scheme where right-clicking on an item in a TreeViewer and
>> selecting "Rename" from the menu allows it to be edited in place.
>> Moving from 3.2 to 3.3 has broken this.
>>
>
> There was a regression introduced into 3.3 because an editor has been
> activated on right click which is wrong.
>
> This is fixed in 3.3.2.
>
> Please provide a snippet so we can reproduce the problem.
>
> Tom
>

Thanks for the help all - I found the problem while putting together a
snippet. I was calling TreeViewer.refresh after the edit call in the
menu selection handler to display the changes after the edit. This was
refreshing the tree and closing the editor.

Marcel
Re: Cell editing in TreeViewer in 3.3 [message #323923 is a reply to message #323920] Sun, 13 January 2008 10:49 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Great, I often encounter this when I ask people to provide a snippet :-)

Tom

Marcel schrieb:
> Tom Schindl wrote:
>> Marcel schrieb:
>>> Hi folks,
>>>
>>> Would love a hand from someone more familiar with cell editing in JFace.
>>>
>>> I had a scheme where right-clicking on an item in a TreeViewer and
>>> selecting "Rename" from the menu allows it to be edited in place.
>>> Moving from 3.2 to 3.3 has broken this.
>>>
>>
>> There was a regression introduced into 3.3 because an editor has been
>> activated on right click which is wrong.
>>
>> This is fixed in 3.3.2.
>>
>> Please provide a snippet so we can reproduce the problem.
>>
>> Tom
>>
>
> Thanks for the help all - I found the problem while putting together a
> snippet. I was calling TreeViewer.refresh after the edit call in the
> menu selection handler to display the changes after the edit. This was
> refreshing the tree and closing the editor.
>
> Marcel


--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------
Re: Cell editing in TreeViewer in 3.3 [message #323924 is a reply to message #323923] Sun, 13 January 2008 10:51 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Tom Schindl schrieb:
> Great, I often encounter this when I ask people to provide a snippet :-)
>
> Tom

.... because it seems your snippet is fairly ready would like to
integrate it into our snippet collection?

http://wiki.eclipse.org/JFaceSnippets

Tom

--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------
Re: Cell editing in TreeViewer in 3.3 [message #323927 is a reply to message #323924] Sun, 13 January 2008 13:43 Go to previous messageGo to next message
Marcel is currently offline MarcelFriend
Messages: 11
Registered: July 2009
Junior Member
Tom Schindl wrote:
> Tom Schindl schrieb:
>> Great, I often encounter this when I ask people to provide a snippet :-)
>>
>> Tom
>
> ... because it seems your snippet is fairly ready would like to
> integrate it into our snippet collection?
>
> http://wiki.eclipse.org/JFaceSnippets
>
> Tom
>

If it's useful, please feel free to add it in. The base is from one or
two of your snippets. I've included it below but can send you the file
off list if the formatting gets mangled.

Marcel

import java.util.ArrayList;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnViewerEditor;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrate gy;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TreeItem;

/**
* A simple TreeViewer to demonstrate usage
*
* @author Tom Schindl <tom.schindl@bestsolution.at>
*
*/
public class TreeViewerSnippet implements SelectionListener {

private TreeViewer viewer;

private class MyContentProvider implements ITreeContentProvider {

/* (non-Javadoc)
* @see
org.eclipse.jface.viewers.IStructuredContentProvider#getElem ents(java.lang.Object)
*/
public Object[] getElements(Object inputElement) {
return ((MyModel)inputElement).child.toArray();
}

/* (non-Javadoc)
* @see org.eclipse.jface.viewers.IContentProvider#dispose()
*/
public void dispose() {

}

/* (non-Javadoc)
* @see
org.eclipse.jface.viewers.IContentProvider#inputChanged(org. eclipse.jface.viewers.Viewer,
java.lang.Object, java.lang.Object)
*/
public void inputChanged(Viewer viewer, Object oldInput, Object
newInput) {

}

/* (non-Javadoc)
* @see
org.eclipse.jface.viewers.ITreeContentProvider#getChildren(j ava.lang.Object)
*/
public Object[] getChildren(Object parentElement) {
return getElements(parentElement);
}

/* (non-Javadoc)
* @see
org.eclipse.jface.viewers.ITreeContentProvider#getParent(jav a.lang.Object)
*/
public Object getParent(Object element) {
if( element == null) {
return null;
}

return ((MyModel)element).parent;
}

/* (non-Javadoc)
* @see
org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(j ava.lang.Object)
*/
public boolean hasChildren(Object element) {
return ((MyModel)element).child.size() > 0;
}

}

public class MyModel {
public MyModel parent;
public ArrayList child = new ArrayList();
public int counter;

public MyModel(int counter, MyModel parent) {
this.parent = parent;
this.counter = counter;
}

public String toString() {
String rv = "Item ";
if( parent != null ) {
rv = parent.toString() + ".";
}

rv += counter;

return rv;
}
}

public TreeViewerSnippet(Shell shell) {
viewer = new TreeViewer(shell, SWT.BORDER);
viewer.setContentProvider(new MyContentProvider());
viewer.setCellEditors(new CellEditor[] {new
TextCellEditor(viewer.getTree())});
viewer.setColumnProperties(new String[] { "name" });
viewer.setCellModifier(new ICellModifier() {

/* (non-Javadoc)
* @see
org.eclipse.jface.viewers.ICellModifier#canModify(java.lang. Object,
java.lang.String)
*/
public boolean canModify(Object element, String property) {
return ((MyModel)element).counter % 2 == 0;
}

/* (non-Javadoc)
* @see
org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.O bject,
java.lang.String)
*/
public Object getValue(Object element, String property) {
return ((MyModel)element).counter + "";
}

/* (non-Javadoc)
* @see
org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Obj ect,
java.lang.String, java.lang.Object)
*/
public void modify(Object element, String property, Object value) {
TreeItem item = (TreeItem)element;
((MyModel)item.getData()).counter = Integer.parseInt(value.toString());
viewer.update(item.getData(), null);
}

});

TreeViewerEditor.create(viewer, new
ColumnViewerEditorActivationStrategy(viewer) {
protected boolean
isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
return event.eventType ==
ColumnViewerEditorActivationEvent.PROGRAMMATIC;
}
}, ColumnViewerEditor.DEFAULT);

Menu menu = new Menu(viewer.getControl());
MenuItem renameItem = new MenuItem(menu, SWT.PUSH);
renameItem.addSelectionListener(this);
renameItem.setText("Rename");
viewer.getTree().setMenu(menu);

viewer.setInput(createModel());
}

public void widgetDefaultSelected(SelectionEvent e) {
}

public void widgetSelected(SelectionEvent e) {
IStructuredSelection selection = (IStructuredSelection)
viewer.getSelection();
if (selection != null) {
viewer.editElement(selection.getFirstElement(), 0);
}
}

private MyModel createModel() {

MyModel root = new MyModel(0,null);
root.counter = 0;

MyModel tmp;
for( int i = 1; i < 10; i++ ) {
tmp = new MyModel(i, root);
root.child.add(tmp);
for( int j = 1; j < i; j++ ) {
tmp.child.add(new MyModel(j,tmp));
}
}

return root;
}

public static void main(String[] args) {
Display display = new Display ();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
new TreeViewerSnippet(shell);
shell.open ();

while (!shell.isDisposed ()) {
if (!display.readAndDispatch ()) display.sleep ();
}

display.dispose ();
}
}
Re: Cell editing in TreeViewer in 3.3 [message #323928 is a reply to message #323927] Sun, 13 January 2008 15:01 Go to previous messageGo to next message
Thomas Schindl is currently offline Thomas SchindlFriend
Messages: 6651
Registered: July 2009
Senior Member
Could you please file a bug against
https://bugs.eclipse.org/bugs/enter_bug.cgi?product=Platform &component=UI

And include the snippet?

Tom

Marcel schrieb:
> Tom Schindl wrote:
>> Tom Schindl schrieb:
>>> Great, I often encounter this when I ask people to provide a snippet :-)
>>>
>>> Tom
>>
>> ... because it seems your snippet is fairly ready would like to
>> integrate it into our snippet collection?
>>
>> http://wiki.eclipse.org/JFaceSnippets
>>
>> Tom
>>
>
> If it's useful, please feel free to add it in. The base is from one or
> two of your snippets. I've included it below but can send you the file
> off list if the formatting gets mangled.
>
> Marcel
>
> import java.util.ArrayList;
>
> import org.eclipse.jface.viewers.CellEditor;
> import org.eclipse.jface.viewers.ColumnViewerEditor;
> import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
> import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrate gy;
> import org.eclipse.jface.viewers.ICellModifier;
> import org.eclipse.jface.viewers.IStructuredSelection;
> import org.eclipse.jface.viewers.ITreeContentProvider;
> import org.eclipse.jface.viewers.LabelProvider;
> import org.eclipse.jface.viewers.TextCellEditor;
> import org.eclipse.jface.viewers.TreeViewer;
> import org.eclipse.jface.viewers.TreeViewerEditor;
> import org.eclipse.jface.viewers.Viewer;
> import org.eclipse.swt.SWT;
> import org.eclipse.swt.events.SelectionEvent;
> import org.eclipse.swt.events.SelectionListener;
> import org.eclipse.swt.layout.FillLayout;
> import org.eclipse.swt.widgets.Display;
> import org.eclipse.swt.widgets.Menu;
> import org.eclipse.swt.widgets.MenuItem;
> import org.eclipse.swt.widgets.Shell;
> import org.eclipse.swt.widgets.TreeItem;
>
> /**
> * A simple TreeViewer to demonstrate usage
> *
> * @author Tom Schindl <tom.schindl@bestsolution.at>
> *
> */
> public class TreeViewerSnippet implements SelectionListener {
>
> private TreeViewer viewer;
>
> private class MyContentProvider implements ITreeContentProvider {
>
> /* (non-Javadoc)
> * @see
> org.eclipse.jface.viewers.IStructuredContentProvider#getElem ents(java.lang.Object)
>
> */
> public Object[] getElements(Object inputElement) {
> return ((MyModel)inputElement).child.toArray();
> }
>
> /* (non-Javadoc)
> * @see org.eclipse.jface.viewers.IContentProvider#dispose()
> */
> public void dispose() {
>
> }
>
> /* (non-Javadoc)
> * @see
> org.eclipse.jface.viewers.IContentProvider#inputChanged(org. eclipse.jface.viewers.Viewer,
> java.lang.Object, java.lang.Object)
> */
> public void inputChanged(Viewer viewer, Object oldInput, Object
> newInput) {
>
> }
>
> /* (non-Javadoc)
> * @see
> org.eclipse.jface.viewers.ITreeContentProvider#getChildren(j ava.lang.Object)
>
> */
> public Object[] getChildren(Object parentElement) {
> return getElements(parentElement);
> }
>
> /* (non-Javadoc)
> * @see
> org.eclipse.jface.viewers.ITreeContentProvider#getParent(jav a.lang.Object)
> */
> public Object getParent(Object element) {
> if( element == null) {
> return null;
> }
>
> return ((MyModel)element).parent;
> }
>
> /* (non-Javadoc)
> * @see
> org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(j ava.lang.Object)
>
> */
> public boolean hasChildren(Object element) {
> return ((MyModel)element).child.size() > 0;
> }
>
> }
>
> public class MyModel {
> public MyModel parent;
> public ArrayList child = new ArrayList();
> public int counter;
>
> public MyModel(int counter, MyModel parent) {
> this.parent = parent;
> this.counter = counter;
> }
>
> public String toString() {
> String rv = "Item ";
> if( parent != null ) {
> rv = parent.toString() + ".";
> }
>
> rv += counter;
>
> return rv;
> }
> }
>
> public TreeViewerSnippet(Shell shell) {
> viewer = new TreeViewer(shell, SWT.BORDER);
> viewer.setContentProvider(new MyContentProvider());
> viewer.setCellEditors(new CellEditor[] {new
> TextCellEditor(viewer.getTree())});
> viewer.setColumnProperties(new String[] { "name" });
> viewer.setCellModifier(new ICellModifier() {
>
> /* (non-Javadoc)
> * @see
> org.eclipse.jface.viewers.ICellModifier#canModify(java.lang. Object,
> java.lang.String)
> */
> public boolean canModify(Object element, String property) {
> return ((MyModel)element).counter % 2 == 0;
> }
>
> /* (non-Javadoc)
> * @see
> org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.O bject,
> java.lang.String)
> */
> public Object getValue(Object element, String property) {
> return ((MyModel)element).counter + "";
> }
>
> /* (non-Javadoc)
> * @see
> org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Obj ect,
> java.lang.String, java.lang.Object)
> */
> public void modify(Object element, String property, Object
> value) {
> TreeItem item = (TreeItem)element;
> ((MyModel)item.getData()).counter =
> Integer.parseInt(value.toString());
> viewer.update(item.getData(), null);
> }
>
> });
>
> TreeViewerEditor.create(viewer, new
> ColumnViewerEditorActivationStrategy(viewer) {
> protected boolean
> isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
> return event.eventType ==
> ColumnViewerEditorActivationEvent.PROGRAMMATIC;
> }
> }, ColumnViewerEditor.DEFAULT);
>
> Menu menu = new Menu(viewer.getControl());
> MenuItem renameItem = new MenuItem(menu, SWT.PUSH);
> renameItem.addSelectionListener(this);
> renameItem.setText("Rename");
> viewer.getTree().setMenu(menu);
>
> viewer.setInput(createModel());
> }
>
> public void widgetDefaultSelected(SelectionEvent e) {
> }
>
> public void widgetSelected(SelectionEvent e) {
> IStructuredSelection selection = (IStructuredSelection)
> viewer.getSelection();
> if (selection != null) {
> viewer.editElement(selection.getFirstElement(), 0);
> }
> }
>
> private MyModel createModel() {
>
> MyModel root = new MyModel(0,null);
> root.counter = 0;
>
> MyModel tmp;
> for( int i = 1; i < 10; i++ ) {
> tmp = new MyModel(i, root);
> root.child.add(tmp);
> for( int j = 1; j < i; j++ ) {
> tmp.child.add(new MyModel(j,tmp));
> }
> }
>
> return root;
> }
>
> public static void main(String[] args) {
> Display display = new Display ();
> Shell shell = new Shell(display);
> shell.setLayout(new FillLayout());
> new TreeViewerSnippet(shell);
> shell.open ();
>
> while (!shell.isDisposed ()) {
> if (!display.readAndDispatch ()) display.sleep ();
> }
>
> display.dispose ();
> }
> }


--
B e s t S o l u t i o n . at
------------------------------------------------------------ --------
Tom Schindl JFace-Committer
------------------------------------------------------------ --------
Re: Cell editing in TreeViewer in 3.3 [message #323929 is a reply to message #323928] Sun, 13 January 2008 16:13 Go to previous message
Marcel is currently offline MarcelFriend
Messages: 11
Registered: July 2009
Junior Member
Done

Tom Schindl wrote:
> Could you please file a bug against
> https://bugs.eclipse.org/bugs/enter_bug.cgi?product=Platform &component=UI
>
> And include the snippet?
>
> Tom
>
> Marcel schrieb:
>> Tom Schindl wrote:
>>> Tom Schindl schrieb:
>>>> Great, I often encounter this when I ask people to provide a snippet
>>>> :-)
>>>>
>>>> Tom
>>>
>>> ... because it seems your snippet is fairly ready would like to
>>> integrate it into our snippet collection?
>>>
>>> http://wiki.eclipse.org/JFaceSnippets
>>>
>>> Tom
>>>
>>
>> If it's useful, please feel free to add it in. The base is from one or
>> two of your snippets. I've included it below but can send you the file
>> off list if the formatting gets mangled.
>>
>> Marcel
>>
>> import java.util.ArrayList;
>>
>> import org.eclipse.jface.viewers.CellEditor;
>> import org.eclipse.jface.viewers.ColumnViewerEditor;
>> import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
>> import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrate gy;
>> import org.eclipse.jface.viewers.ICellModifier;
>> import org.eclipse.jface.viewers.IStructuredSelection;
>> import org.eclipse.jface.viewers.ITreeContentProvider;
>> import org.eclipse.jface.viewers.LabelProvider;
>> import org.eclipse.jface.viewers.TextCellEditor;
>> import org.eclipse.jface.viewers.TreeViewer;
>> import org.eclipse.jface.viewers.TreeViewerEditor;
>> import org.eclipse.jface.viewers.Viewer;
>> import org.eclipse.swt.SWT;
>> import org.eclipse.swt.events.SelectionEvent;
>> import org.eclipse.swt.events.SelectionListener;
>> import org.eclipse.swt.layout.FillLayout;
>> import org.eclipse.swt.widgets.Display;
>> import org.eclipse.swt.widgets.Menu;
>> import org.eclipse.swt.widgets.MenuItem;
>> import org.eclipse.swt.widgets.Shell;
>> import org.eclipse.swt.widgets.TreeItem;
>>
>> /**
>> * A simple TreeViewer to demonstrate usage
>> *
>> * @author Tom Schindl <tom.schindl@bestsolution.at>
>> *
>> */
>> public class TreeViewerSnippet implements SelectionListener {
>> private TreeViewer viewer;
>> private class MyContentProvider implements ITreeContentProvider {
>> /* (non-Javadoc)
>> * @see
>> org.eclipse.jface.viewers.IStructuredContentProvider#getElem ents(java.lang.Object)
>>
>> */
>> public Object[] getElements(Object inputElement) {
>> return ((MyModel)inputElement).child.toArray();
>> }
>>
>> /* (non-Javadoc)
>> * @see org.eclipse.jface.viewers.IContentProvider#dispose()
>> */
>> public void dispose() {
>> }
>>
>> /* (non-Javadoc)
>> * @see
>> org.eclipse.jface.viewers.IContentProvider#inputChanged(org. eclipse.jface.viewers.Viewer,
>> java.lang.Object, java.lang.Object)
>> */
>> public void inputChanged(Viewer viewer, Object oldInput,
>> Object newInput) {
>> }
>>
>> /* (non-Javadoc)
>> * @see
>> org.eclipse.jface.viewers.ITreeContentProvider#getChildren(j ava.lang.Object)
>>
>> */
>> public Object[] getChildren(Object parentElement) {
>> return getElements(parentElement);
>> }
>>
>> /* (non-Javadoc)
>> * @see
>> org.eclipse.jface.viewers.ITreeContentProvider#getParent(jav a.lang.Object)
>>
>> */
>> public Object getParent(Object element) {
>> if( element == null) {
>> return null;
>> }
>> return ((MyModel)element).parent;
>> }
>>
>> /* (non-Javadoc)
>> * @see
>> org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(j ava.lang.Object)
>>
>> */
>> public boolean hasChildren(Object element) {
>> return ((MyModel)element).child.size() > 0;
>> }
>> }
>> public class MyModel {
>> public MyModel parent;
>> public ArrayList child = new ArrayList();
>> public int counter;
>> public MyModel(int counter, MyModel parent) {
>> this.parent = parent;
>> this.counter = counter;
>> }
>> public String toString() {
>> String rv = "Item ";
>> if( parent != null ) {
>> rv = parent.toString() + ".";
>> }
>> rv += counter;
>> return rv;
>> }
>> }
>> public TreeViewerSnippet(Shell shell) {
>> viewer = new TreeViewer(shell, SWT.BORDER);
>> viewer.setContentProvider(new MyContentProvider());
>> viewer.setCellEditors(new CellEditor[] {new
>> TextCellEditor(viewer.getTree())});
>> viewer.setColumnProperties(new String[] { "name" });
>> viewer.setCellModifier(new ICellModifier() {
>>
>> /* (non-Javadoc)
>> * @see
>> org.eclipse.jface.viewers.ICellModifier#canModify(java.lang. Object,
>> java.lang.String)
>> */
>> public boolean canModify(Object element, String property) {
>> return ((MyModel)element).counter % 2 == 0;
>> }
>>
>> /* (non-Javadoc)
>> * @see
>> org.eclipse.jface.viewers.ICellModifier#getValue(java.lang.O bject,
>> java.lang.String)
>> */
>> public Object getValue(Object element, String property) {
>> return ((MyModel)element).counter + "";
>> }
>>
>> /* (non-Javadoc)
>> * @see
>> org.eclipse.jface.viewers.ICellModifier#modify(java.lang.Obj ect,
>> java.lang.String, java.lang.Object)
>> */
>> public void modify(Object element, String property, Object
>> value) {
>> TreeItem item = (TreeItem)element;
>> ((MyModel)item.getData()).counter =
>> Integer.parseInt(value.toString());
>> viewer.update(item.getData(), null);
>> }
>> });
>>
>> TreeViewerEditor.create(viewer, new
>> ColumnViewerEditorActivationStrategy(viewer) {
>> protected boolean
>> isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
>> return event.eventType ==
>> ColumnViewerEditorActivationEvent.PROGRAMMATIC;
>> }
>> }, ColumnViewerEditor.DEFAULT);
>> Menu menu = new Menu(viewer.getControl());
>> MenuItem renameItem = new MenuItem(menu, SWT.PUSH);
>> renameItem.addSelectionListener(this);
>> renameItem.setText("Rename");
>> viewer.getTree().setMenu(menu);
>> viewer.setInput(createModel());
>> }
>> public void widgetDefaultSelected(SelectionEvent e) {
>> }
>>
>> public void widgetSelected(SelectionEvent e) {
>> IStructuredSelection selection = (IStructuredSelection)
>> viewer.getSelection();
>> if (selection != null) {
>> viewer.editElement(selection.getFirstElement(), 0);
>> }
>> }
>> private MyModel createModel() {
>> MyModel root = new MyModel(0,null);
>> root.counter = 0;
>> MyModel tmp;
>> for( int i = 1; i < 10; i++ ) {
>> tmp = new MyModel(i, root);
>> root.child.add(tmp);
>> for( int j = 1; j < i; j++ ) {
>> tmp.child.add(new MyModel(j,tmp));
>> }
>> }
>> return root;
>> }
>> public static void main(String[] args) {
>> Display display = new Display ();
>> Shell shell = new Shell(display);
>> shell.setLayout(new FillLayout());
>> new TreeViewerSnippet(shell);
>> shell.open ();
>> while (!shell.isDisposed ()) {
>> if (!display.readAndDispatch ()) display.sleep ();
>> }
>> display.dispose ();
>> }
>> }
>
>
Previous Topic:Editor & CommandStack
Next Topic:Text Editor problem.
Goto Forum:
  


Current Time: Sat Apr 27 23:30:49 GMT 2024

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

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

Back to the top