Home » Modeling » EMF » Editing columns in "Tree with Columns" view
| Editing columns in "Tree with Columns" view [message #912369] |
Thu, 13 September 2012 08:51  |
Eclipse User |
|
|
|
Hello,
I am trying to use the "Tree with Columns" view for editing my data.
My first implementation looks like the following:
final Tree tree = treeViewerWithColumns.getTree();
final TreeEditor editor = new TreeEditor(tree);
tree.setLayoutData(new FillLayout());
tree.setHeaderVisible(true);
tree.setLinesVisible(true);
tree.addMouseListener(new MouseAdapter() {
@Override
public void mouseDoubleClick(MouseEvent e) {
// Clean up any previous editor control
Control oldEditor = editor.getEditor();
if (oldEditor != null)
oldEditor.dispose();
if (tree.getSelectionCount() != 1) {
return;
}
// Identify the selected row
TreeItem[] selection = tree.getSelection();
TreeItem item = selection[0];
if (item == null)
return;
final SequenceContentImpl s = (SequenceContentImpl) item.getData();
// The control that will be the editor must be a
// child of the Table
Text newEditor = new Text(tree, SWT.BORDER);
newEditor.setText(s.getComment());
newEditor.addModifyListener(new ModifyListener() {
public void modifyText(ModifyEvent me) {
Text text = (Text) editor.getEditor();
s.setComment(text.getText());
}
});
newEditor.selectAll();
newEditor.setFocus();
editor.setEditor(newEditor, item, COLUMN_INDEX_COMMENT);
}
});
TreeColumn objectColumn = new TreeColumn(tree, SWT.NONE);
objectColumn.setText(getString("_UI_ObjectColumn_label"));
objectColumn.setResizable(true);
objectColumn.setWidth(250);
TreeColumn commentColumn = new TreeColumn(tree, SWT.NONE);
commentColumn.setText("Comment");
commentColumn.setResizable(true);
commentColumn.setWidth(200);
treeViewerWithColumns.setContentProvider(new AdapterFactoryContentProvider(adapterFactory));
treeViewerWithColumns.setLabelProvider(new SequenceTreeColumnLabelProvider(adapterFactory));
treeViewerWithColumns.setColumnProperties(new String[] { "a", "b" });
treeViewerWithColumns.setInput(s);
It works so far, but the file is never marked as dirty. I have tried it with firePropertyChange(PROP_DIRTY); , but no result.
How can I mark the file as dirty?
Furthermore the input field is never shown directly, so it is impossible to see that the editor is active.
I think there could be a easier way, but I don't know which. Thanks for any advices.
|
|
|
| Re: Editing columns in "Tree with Columns" view [message #912380 is a reply to message #912369] |
Thu, 13 September 2012 09:02   |
Eclipse User |
|
|
|
You must use commands to make changes to the model, so you shouldn't
just do s.setComment you should use SetCommand.create to create the
command and then execute it on the command stack. Have a look at
org.eclipse.emf.edit.provider.ItemPropertyDescriptor.setPropertyValue(Object,
Object)
On 13/09/2012 2:51 PM, Missing name Mising name wrote:
> Hello,
>
> I am trying to use the "Tree with Columns" view for editing my data.
> My first implementation looks like the following:
>
> final Tree tree = treeViewerWithColumns.getTree();
> final TreeEditor editor = new TreeEditor(tree);
>
> tree.setLayoutData(new FillLayout());
> tree.setHeaderVisible(true);
> tree.setLinesVisible(true);
> tree.addMouseListener(new MouseAdapter() {
> @Override
> public void mouseDoubleClick(MouseEvent e) {
> // Clean up any previous editor control
> Control oldEditor = editor.getEditor();
> if (oldEditor != null)
> oldEditor.dispose();
>
> if (tree.getSelectionCount() != 1) {
> return;
> }
>
> // Identify the selected row
> TreeItem[] selection = tree.getSelection();
> TreeItem item = selection[0];
> if (item == null)
> return;
>
> final SequenceContentImpl s = (SequenceContentImpl)
> item.getData();
>
> // The control that will be the editor must be a
> // child of the Table
> Text newEditor = new Text(tree, SWT.BORDER);
> newEditor.setText(s.getComment());
> newEditor.addModifyListener(new ModifyListener() {
> public void modifyText(ModifyEvent me) {
> Text text = (Text) editor.getEditor();
> s.setComment(text.getText());
> }
> });
> newEditor.selectAll();
> newEditor.setFocus();
> editor.setEditor(newEditor, item, COLUMN_INDEX_COMMENT);
> }
> });
>
> TreeColumn objectColumn = new TreeColumn(tree, SWT.NONE);
> objectColumn.setText(getString("_UI_ObjectColumn_label"));
> objectColumn.setResizable(true);
> objectColumn.setWidth(250);
>
> TreeColumn commentColumn = new TreeColumn(tree, SWT.NONE);
> commentColumn.setText("Comment");
> commentColumn.setResizable(true);
> commentColumn.setWidth(200);
>
> treeViewerWithColumns.setContentProvider(new
> AdapterFactoryContentProvider(adapterFactory));
> treeViewerWithColumns.setLabelProvider(new
> SequenceTreeColumnLabelProvider(adapterFactory));
> treeViewerWithColumns.setColumnProperties(new String[] { "a", "b" });
> treeViewerWithColumns.setInput(s);
>
> It works so far, but the file is never marked as dirty. I have tried
> it with firePropertyChange(PROP_DIRTY);, but no result.
> How can I mark the file as dirty?
>
> Furthermore the input field is never shown directly, so it is
> impossible to see that the editor is active.
>
> I think there could be a easier way, but I don't know which. Thanks
> for any advices.
|
|
| | | | |
| Re: Editing columns in "Tree with Columns" view [message #912835 is a reply to message #912477] |
Fri, 14 September 2012 05:14   |
Eclipse User |
|
|
|
>> Thanks for your advice.
>> Is there no possibility to activate the editor function in the
>> generated editor?
> I'm not sure what you're asking now. You asked about why the editor (I
> assume the generated editor) doesn't get dirty, and I explained that
> it's because you're not using a command. The editor determines
> dirtiness from the state of the command stack. Is this question
> different but related somehow?
The first thing I got working.
I was searching for a simple possibility to activate the editor functionality in that view.
My solution for the moment:
TreeViewerFocusCellManager focusCellManager = new TreeViewerFocusCellManager(treeViewerWithColumns,
new FocusCellOwnerDrawHighlighter(treeViewerWithColumns));
ColumnViewerEditorActivationStrategy actStrategy = new ColumnViewerEditorActivationStrategy(
treeViewerWithColumns) {
@Override
protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
return super.isEditorActivationEvent(event)
|| (event.eventType == ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode == SWT.CR);
}
};
TreeViewerEditor.create(treeViewerWithColumns, focusCellManager, actStrategy,
ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR | ColumnViewerEditor.TABBING_VERTICAL
| ColumnViewerEditor.KEYBOARD_ACTIVATION);
TreeViewerColumn viewerColumn = new TreeViewerColumn(treeViewerWithColumns, commentColumn);
viewerColumn.setLabelProvider(new DelegatingSequenceColumnLabelProvider(slp, COLUMN_INDEX_COMMENT));
viewerColumn.setEditingSupport(new EditingSupport(treeViewerWithColumns) {
@Override
protected void setValue(Object element, Object value) {
if (element instanceof CommandImpl && !value.equals(((CommandImpl) element).getComment())) {
Command save = SetCommand.create(getEditingDomain(), element,
SequencePackage.Literals.SEQUENCE_CONTENT__COMMENT, value);
getEditingDomain().getCommandStack().execute(save);
}
}
@Override
protected Object getValue(Object element) {
return ((SequenceContentImpl) element).getComment();
}
@Override
protected CellEditor getCellEditor(Object element) {
return new TextCellEditor((Composite) treeViewerWithColumns.getControl());
}
@Override
protected boolean canEdit(Object element) {
return true;
}
});
Perhaps there is a more recommend way.
|
|
|
| Re: Editing columns in "Tree with Columns" view [message #912871 is a reply to message #912835] |
Fri, 14 September 2012 06:04   |
Eclipse User |
|
|
|
The rest is more of a JFace question. I don't have so much experience
with that. It looks quite complicated. I have a feeling it does
something very similar to the utilities described here:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=220843#c3
These are part of JFace now. They help delegate editing to EMF's
property descriptors so you don't have to write much of anything...
I see again you're casting to CommandImpl? Why? Why not use the
Command interface.
On 14/09/2012 11:14 AM, Missing name Mising name wrote:
>>> Thanks for your advice.
>>> Is there no possibility to activate the editor function in the
>>> generated editor?
>> I'm not sure what you're asking now. You asked about why the editor
>> (I assume the generated editor) doesn't get dirty, and I explained
>> that it's because you're not using a command. The editor determines
>> dirtiness from the state of the command stack. Is this question
>> different but related somehow?
>
> The first thing I got working.
> I was searching for a simple possibility to activate the editor
> functionality in that view.
>
> My solution for the moment:
> TreeViewerFocusCellManager focusCellManager = new
> TreeViewerFocusCellManager(treeViewerWithColumns,
> new FocusCellOwnerDrawHighlighter(treeViewerWithColumns));
> ColumnViewerEditorActivationStrategy actStrategy = new
> ColumnViewerEditorActivationStrategy(
> treeViewerWithColumns) {
> @Override
> protected boolean
> isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
> return super.isEditorActivationEvent(event)
> || (event.eventType ==
> ColumnViewerEditorActivationEvent.KEY_PRESSED && event.keyCode ==
> SWT.CR);
> }
> };
>
> TreeViewerEditor.create(treeViewerWithColumns, focusCellManager,
> actStrategy,
> ColumnViewerEditor.TABBING_MOVE_TO_ROW_NEIGHBOR |
> ColumnViewerEditor.TABBING_VERTICAL
> | ColumnViewerEditor.KEYBOARD_ACTIVATION);
>
> TreeViewerColumn viewerColumn = new
> TreeViewerColumn(treeViewerWithColumns, commentColumn);
> viewerColumn.setLabelProvider(new
> DelegatingSequenceColumnLabelProvider(slp, COLUMN_INDEX_COMMENT));
> viewerColumn.setEditingSupport(new
> EditingSupport(treeViewerWithColumns) {
> @Override
> protected void setValue(Object element, Object value) {
> if (element instanceof CommandImpl &&
> !value.equals(((CommandImpl) element).getComment())) {
> Command save = SetCommand.create(getEditingDomain(), element,
> SequencePackage.Literals.SEQUENCE_CONTENT__COMMENT, value);
> getEditingDomain().getCommandStack().execute(save);
> }
> }
>
> @Override
> protected Object getValue(Object element) {
> return ((SequenceContentImpl) element).getComment();
> }
>
> @Override
> protected CellEditor getCellEditor(Object element) {
> return new TextCellEditor((Composite)
> treeViewerWithColumns.getControl());
> }
>
> @Override
> protected boolean canEdit(Object element) {
> return true;
> }
> });
>
> Perhaps there is a more recommend way.
|
|
| | | |
Goto Forum:
Current Time: Thu Nov 13 10:40:19 EST 2025
Powered by FUDForum. Page generated in 0.05981 seconds
|