{"id":690,"date":"2013-02-01T18:47:54","date_gmt":"2013-02-01T13:17:54","guid":{"rendered":"http:\/\/ramkulkarni.com\/blog\/?p=690"},"modified":"2013-02-01T18:47:54","modified_gmt":"2013-02-01T13:17:54","slug":"in-place-editing-in-eclipse-treeviewer","status":"publish","type":"post","link":"http:\/\/ramkulkarni.com\/blog\/in-place-editing-in-eclipse-treeviewer\/","title":{"rendered":"In-place Editing in Eclipse TreeViewer"},"content":{"rendered":"<p>In an Eclipse RCP application I was working on recently, I had to implement a TreeViewer with in-place editing feature. It was not easy to find all the information required to implement this, so I thought I would explain it here.<\/p>\n<p>First let&#8217;s take a simple example, where\u00a0hierarchical data is displayed in \u00a0a tree.<br \/>\n<a href=\"https:\/\/i0.wp.com\/138.197.85.232\/blog\/wp-content\/uploads\/2013\/02\/tree_viewer1.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"693\" data-permalink=\"http:\/\/ramkulkarni.com\/blog\/in-place-editing-in-eclipse-treeviewer\/tree_viewer1\/\" data-orig-file=\"https:\/\/i0.wp.com\/ramkulkarni.com\/blog\/wp-content\/uploads\/2013\/02\/tree_viewer1.png?fit=228%2C234\" data-orig-size=\"228,234\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"tree_viewer1\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/ramkulkarni.com\/blog\/wp-content\/uploads\/2013\/02\/tree_viewer1.png?fit=228%2C234\" data-large-file=\"https:\/\/i0.wp.com\/ramkulkarni.com\/blog\/wp-content\/uploads\/2013\/02\/tree_viewer1.png?fit=228%2C234\" class=\"size-full wp-image-693 alignnone\" alt=\"tree_viewer1\" src=\"https:\/\/i0.wp.com\/138.197.85.232\/blog\/wp-content\/uploads\/2013\/02\/tree_viewer1.png?resize=228%2C234\" width=\"228\" height=\"234\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>When any item in the tree is double clicked, I want to edit the value in-place.<\/p>\n<p>Here is the code to create this tree, without editing support. I will first create a data model using a Map. To simplify the example, I am assuming only one level of\u00a0hierarchy.<\/p>\n<p><!--more--><\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\"><span style=\"color: #595979;\">\/\/Class to represent each node of the tree<\/span>\n<span style=\"color: #200080; font-weight: bold;\">class<\/span> TreeItemData\n<span style=\"color: #406080;\">{<\/span>\n    <span style=\"color: #6679aa; font-weight: bold;\">String<\/span> value<span style=\"color: #406080;\">;<\/span>\n    <span style=\"color: #200080; font-weight: bold;\">public<\/span> TreeItemData <span style=\"color: #308080;\">(<\/span><span style=\"color: #6679aa; font-weight: bold;\">String<\/span> value<span style=\"color: #308080;\">)<\/span>\n    <span style=\"color: #406080;\">{<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">this<\/span><span style=\"color: #308080;\">.<\/span>value <span style=\"color: #308080;\">=<\/span> value<span style=\"color: #406080;\">;<\/span>\n    <span style=\"color: #406080;\">}<\/span>\n\n    <span style=\"color: #200080; font-weight: bold;\">public<\/span> <span style=\"color: #6679aa; font-weight: bold;\">String<\/span> toString<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span>\n    <span style=\"color: #406080;\">{<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">return<\/span> value<span style=\"color: #406080;\">;<\/span>\n    <span style=\"color: #406080;\">}<\/span>\n<span style=\"color: #406080;\">}<\/span>\n\nMap&lt;TreeItemData,TreeItemData[]&gt; treeModelData = new HashMap&lt;&gt;()<span style=\"color: #308080;\">;<\/span>\n\ntreeModelData.put(new TreeItemData(\"parent1\"), new TreeItemData[]<span style=\"color: #406080;\">{<\/span>\n    <span style=\"color: #200080; font-weight: bold;\">new<\/span> TreeItemData<span style=\"color: #308080;\">(<\/span><span style=\"color: #1060b6;\">\"child1\"<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">,<\/span>\n    <span style=\"color: #200080; font-weight: bold;\">new<\/span> TreeItemData<span style=\"color: #308080;\">(<\/span><span style=\"color: #1060b6;\">\"child2\"<\/span><span style=\"color: #308080;\">)<\/span>\n    <span style=\"color: #406080;\">}<\/span>)<span style=\"color: #308080;\">;<\/span><\/pre>\n<p>Then create an instanceof TreeViewer and its content provider.<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\">TreeViewer treeViewer = new TreeViewer(shell)<span style=\"color: #308080;\">;<\/span>\n\ntreeViewer.setContentProvider(new ITreeContentProvider() <span style=\"color: #406080;\">{<\/span>\n\n    <span style=\"color: #308080;\">@<\/span>Override\n    <span style=\"color: #200080; font-weight: bold;\">public<\/span> <span style=\"color: #7779bb;\">void<\/span> inputChanged<span style=\"color: #308080;\">(<\/span>Viewer viewer<span style=\"color: #308080;\">,<\/span> <span style=\"color: #6679aa; font-weight: bold;\">Object<\/span> oldInput<span style=\"color: #308080;\">,<\/span> <span style=\"color: #6679aa; font-weight: bold;\">Object<\/span> newInput<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n    <span style=\"color: #406080;\">}<\/span>\n\n    <span style=\"color: #308080;\">@<\/span>Override\n    <span style=\"color: #200080; font-weight: bold;\">public<\/span> <span style=\"color: #7779bb;\">void<\/span> dispose<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n    <span style=\"color: #406080;\">}<\/span>\n\n    <span style=\"color: #308080;\">@<\/span>Override\n    <span style=\"color: #200080; font-weight: bold;\">public<\/span> <span style=\"color: #7779bb;\">boolean<\/span> hasChildren<span style=\"color: #308080;\">(<\/span><span style=\"color: #6679aa; font-weight: bold;\">Object<\/span> element<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">if<\/span> <span style=\"color: #308080;\">(<\/span>treeModelData<span style=\"color: #308080;\">.<\/span>containsKey<span style=\"color: #308080;\">(<\/span>element<span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">)<\/span>\n            <span style=\"color: #200080; font-weight: bold;\">return<\/span> <span style=\"color: #200080; font-weight: bold;\">true<\/span><span style=\"color: #406080;\">;<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">return<\/span> <span style=\"color: #200080; font-weight: bold;\">false<\/span><span style=\"color: #406080;\">;<\/span>\n    <span style=\"color: #406080;\">}<\/span>\n\n    <span style=\"color: #308080;\">@<\/span>Override\n    <span style=\"color: #200080; font-weight: bold;\">public<\/span> <span style=\"color: #6679aa; font-weight: bold;\">Object<\/span> getParent<span style=\"color: #308080;\">(<\/span><span style=\"color: #6679aa; font-weight: bold;\">Object<\/span> element<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">return<\/span> <span style=\"color: #200080; font-weight: bold;\">null<\/span><span style=\"color: #406080;\">;<\/span>\n    <span style=\"color: #406080;\">}<\/span>\n\n    <span style=\"color: #308080;\">@<\/span>Override\n    <span style=\"color: #200080; font-weight: bold;\">public<\/span> <span style=\"color: #6679aa; font-weight: bold;\">Object<\/span><span style=\"color: #308080;\">[<\/span><span style=\"color: #308080;\">]<\/span> getElements<span style=\"color: #308080;\">(<\/span><span style=\"color: #6679aa; font-weight: bold;\">Object<\/span> inputElement<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">if<\/span> <span style=\"color: #308080;\">(<\/span>inputElement<span style=\"color: #308080;\">.<\/span>equals<span style=\"color: #308080;\">(<\/span><span style=\"color: #1060b6;\">\"initial_input\"<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">)<\/span>\n            <span style=\"color: #200080; font-weight: bold;\">return<\/span> treeModelData<span style=\"color: #308080;\">.<\/span>keySet<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">.<\/span>toArray<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">return<\/span> <span style=\"color: #200080; font-weight: bold;\">null<\/span><span style=\"color: #406080;\">;<\/span>\n    <span style=\"color: #406080;\">}<\/span>\n\n    <span style=\"color: #308080;\">@<\/span>Override\n    <span style=\"color: #200080; font-weight: bold;\">public<\/span> <span style=\"color: #6679aa; font-weight: bold;\">Object<\/span><span style=\"color: #308080;\">[<\/span><span style=\"color: #308080;\">]<\/span> getChildren<span style=\"color: #308080;\">(<\/span><span style=\"color: #6679aa; font-weight: bold;\">Object<\/span> parentElement<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n        parentElement<span style=\"color: #308080;\">.<\/span>toString<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n        <span style=\"color: #6679aa; font-weight: bold;\">Object<\/span> value <span style=\"color: #308080;\">=<\/span> treeModelData<span style=\"color: #308080;\">.<\/span>get<span style=\"color: #308080;\">(<\/span>parentElement<span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">if<\/span> <span style=\"color: #308080;\">(<\/span>value <span style=\"color: #308080;\">!<\/span><span style=\"color: #308080;\">=<\/span> <span style=\"color: #200080; font-weight: bold;\">null<\/span><span style=\"color: #308080;\">)<\/span>\n            <span style=\"color: #200080; font-weight: bold;\">return<\/span> <span style=\"color: #308080;\">(<\/span>TreeItemData<span style=\"color: #308080;\">[<\/span><span style=\"color: #308080;\">]<\/span><span style=\"color: #308080;\">)<\/span>value<span style=\"color: #406080;\">;<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">return<\/span> <span style=\"color: #200080; font-weight: bold;\">null<\/span><span style=\"color: #406080;\">;<\/span>\n    <span style=\"color: #406080;\">}<\/span>\n<span style=\"color: #406080;\">}<\/span>)<span style=\"color: #308080;\">;<\/span>\n\n<span style=\"color: #595979;\">\/\/set initial input<\/span>\ntreeViewer.setInput(\"initial_input\")<span style=\"color: #308080;\">;<\/span><\/pre>\n<p>The above code will create a <a href=\"http:\/\/help.eclipse.org\/juno\/topic\/org.eclipse.platform.doc.isv\/reference\/api\/org\/eclipse\/jface\/viewers\/TableViewer.html\" target=\"_blank\">TreeViewer<\/a> with tree nodes, but with no editing support.<\/p>\n<p>To trigger editing in Tree cells on double click, we need to create TreeViewerEditor.<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\">TreeViewerEditor.create(treeViewer, new ColumnViewerEditorActivationStrategy(treeViewer)<span style=\"color: #406080;\">{<\/span>\n    <span style=\"color: #200080; font-weight: bold;\">protected<\/span> <span style=\"color: #7779bb;\">boolean<\/span> isEditorActivationEvent<span style=\"color: #308080;\">(<\/span>ColumnViewerEditorActivationEvent event<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">return<\/span> event<span style=\"color: #308080;\">.<\/span>eventType <span style=\"color: #308080;\">=<\/span><span style=\"color: #308080;\">=<\/span> ColumnViewerEditorActivationEvent<span style=\"color: #308080;\">.<\/span>MOUSE_DOUBLE_CLICK_SELECTION<span style=\"color: #406080;\">;<\/span>\n    <span style=\"color: #406080;\">}<\/span><\/pre>\n<p>There are two ways to add editing support for cells in the tree. 1)Using <a href=\"http:\/\/help.eclipse.org\/juno\/topic\/org.eclipse.platform.doc.isv\/reference\/api\/org\/eclipse\/jface\/viewers\/ColumnViewer.html#setCellEditors(org.eclipse.jface.viewers.CellEditor[])\" target=\"_blank\">setCellEditors<\/a> method of TreeViewer, and 2)Using <a href=\"http:\/\/help.eclipse.org\/juno\/topic\/org.eclipse.platform.doc.isv\/reference\/api\/org\/eclipse\/jface\/viewers\/ViewerColumn.html#setEditingSupport(org.eclipse.jface.viewers.EditingSupport)\" target=\"_blank\">ViewerColumn.setEditingSupport<\/a> API. Eclipse documentation says that the second API is available since Eclipse 3.3 and it is more flexible.<\/p>\n<p>First I will show how to use <a href=\"http:\/\/help.eclipse.org\/juno\/topic\/org.eclipse.platform.doc.isv\/reference\/api\/org\/eclipse\/jface\/viewers\/ColumnViewer.html#setCellEditors(org.eclipse.jface.viewers.CellEditor[])\" target=\"_blank\">setCellEditors<\/a> API<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\"><span style=\"color: #200080; font-weight: bold;\">private<\/span> void enableEditing1(TreeViewer treeViewer)\n<span style=\"color: #406080;\">{<\/span>\n    <span style=\"color: #595979;\">\/\/You have to create identifier for tree columns<\/span>\n    treeViewer<span style=\"color: #308080;\">.<\/span>setColumnProperties<span style=\"color: #308080;\">(<\/span><span style=\"color: #200080; font-weight: bold;\">new<\/span> <span style=\"color: #6679aa; font-weight: bold;\">String<\/span><span style=\"color: #308080;\">[<\/span><span style=\"color: #308080;\">]<\/span><span style=\"color: #406080;\">{<\/span><span style=\"color: #1060b6;\">\"col1\"<\/span><span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n    treeViewer<span style=\"color: #308080;\">.<\/span>setLabelProvider<span style=\"color: #308080;\">(<\/span><span style=\"color: #200080; font-weight: bold;\">new<\/span> TreeLabelProvider<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n    <span style=\"color: #595979;\">\/\/Create text editor<\/span>\n    treeViewer<span style=\"color: #308080;\">.<\/span>setCellEditors<span style=\"color: #308080;\">(<\/span><span style=\"color: #200080; font-weight: bold;\">new<\/span> CellEditor<span style=\"color: #308080;\">[<\/span><span style=\"color: #308080;\">]<\/span><span style=\"color: #406080;\">{<\/span><span style=\"color: #200080; font-weight: bold;\">new<\/span> TextCellEditor<span style=\"color: #308080;\">(<\/span>treeViewer<span style=\"color: #308080;\">.<\/span>getTree<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n    treeViewer<span style=\"color: #308080;\">.<\/span>setCellModifier<span style=\"color: #308080;\">(<\/span><span style=\"color: #200080; font-weight: bold;\">new<\/span> ICellModifier<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n\n        <span style=\"color: #308080;\">@<\/span>Override\n        <span style=\"color: #200080; font-weight: bold;\">public<\/span> <span style=\"color: #7779bb;\">void<\/span> modify<span style=\"color: #308080;\">(<\/span><span style=\"color: #6679aa; font-weight: bold;\">Object<\/span> element<span style=\"color: #308080;\">,<\/span> <span style=\"color: #6679aa; font-weight: bold;\">String<\/span> property<span style=\"color: #308080;\">,<\/span> <span style=\"color: #6679aa; font-weight: bold;\">Object<\/span> value<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n            <span style=\"color: #200080; font-weight: bold;\">if<\/span> <span style=\"color: #308080;\">(<\/span>element <span style=\"color: #200080; font-weight: bold;\">instanceof<\/span> TreeItem<span style=\"color: #308080;\">)<\/span>\n            <span style=\"color: #406080;\">{<\/span>\n                <span style=\"color: #595979;\">\/\/update element and tree model<\/span>\n                TreeItem treeItem <span style=\"color: #308080;\">=<\/span> <span style=\"color: #308080;\">(<\/span>TreeItem<span style=\"color: #308080;\">)<\/span>element<span style=\"color: #406080;\">;<\/span>\n                TreeItemData data <span style=\"color: #308080;\">=<\/span> <span style=\"color: #308080;\">(<\/span>TreeItemData<span style=\"color: #308080;\">)<\/span>treeItem<span style=\"color: #308080;\">.<\/span>getData<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n                data<span style=\"color: #308080;\">.<\/span>value <span style=\"color: #308080;\">=<\/span> value<span style=\"color: #308080;\">.<\/span>toString<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n                treeItem<span style=\"color: #308080;\">.<\/span>setText<span style=\"color: #308080;\">(<\/span>value<span style=\"color: #308080;\">.<\/span>toString<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n            <span style=\"color: #406080;\">}<\/span>\n        <span style=\"color: #406080;\">}<\/span>\n\n        <span style=\"color: #308080;\">@<\/span>Override\n        <span style=\"color: #200080; font-weight: bold;\">public<\/span> <span style=\"color: #6679aa; font-weight: bold;\">Object<\/span> getValue<span style=\"color: #308080;\">(<\/span><span style=\"color: #6679aa; font-weight: bold;\">Object<\/span> element<span style=\"color: #308080;\">,<\/span> <span style=\"color: #6679aa; font-weight: bold;\">String<\/span> property<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n            <span style=\"color: #200080; font-weight: bold;\">return<\/span> element<span style=\"color: #308080;\">.<\/span>toString<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n        <span style=\"color: #406080;\">}<\/span>\n\n        <span style=\"color: #308080;\">@<\/span>Override\n        <span style=\"color: #200080; font-weight: bold;\">public<\/span> <span style=\"color: #7779bb;\">boolean<\/span> canModify<span style=\"color: #308080;\">(<\/span><span style=\"color: #6679aa; font-weight: bold;\">Object<\/span> element<span style=\"color: #308080;\">,<\/span> <span style=\"color: #6679aa; font-weight: bold;\">String<\/span> property<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n            <span style=\"color: #200080; font-weight: bold;\">return<\/span> <span style=\"color: #200080; font-weight: bold;\">true<\/span><span style=\"color: #406080;\">;<\/span>\n        <span style=\"color: #406080;\">}<\/span>\n    <span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n<span style=\"color: #406080;\">}<\/span><\/pre>\n<pre style=\"color: #000020; background: #f6f8ff;\"><span style=\"color: #200080; font-weight: bold;\">class<\/span> TreeLabelProvider <span style=\"color: #200080; font-weight: bold;\">extends<\/span> ColumnLabelProvider\n<span style=\"color: #406080;\">{<\/span>\n    <span style=\"color: #200080; font-weight: bold;\">public<\/span> <span style=\"color: #6679aa; font-weight: bold;\">String<\/span> getText<span style=\"color: #308080;\">(<\/span><span style=\"color: #6679aa; font-weight: bold;\">Object<\/span> element<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">return<\/span> element<span style=\"color: #308080;\">.<\/span>toString<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n    <span style=\"color: #406080;\">}<\/span>\n<span style=\"color: #406080;\">}<\/span><\/pre>\n<p>Alternatively, here is the code to add editing support using\u00a0<a href=\"http:\/\/help.eclipse.org\/juno\/topic\/org.eclipse.platform.doc.isv\/reference\/api\/org\/eclipse\/jface\/viewers\/ViewerColumn.html#setEditingSupport(org.eclipse.jface.viewers.EditingSupport)\" target=\"_blank\">ViewerColumn.setEditingSupport<\/a> API.<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\"><span style=\"color: #200080; font-weight: bold;\">private<\/span> void enableEditing2 (TreeViewer treeViewer)\n<span style=\"color: #406080;\">{<\/span>\n    <span style=\"color: #200080; font-weight: bold;\">final<\/span> TreeViewerColumn column <span style=\"color: #308080;\">=<\/span> <span style=\"color: #200080; font-weight: bold;\">new<\/span> TreeViewerColumn<span style=\"color: #308080;\">(<\/span>treeViewer<span style=\"color: #308080;\">,<\/span> SWT<span style=\"color: #308080;\">.<\/span>NONE<span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n    column<span style=\"color: #308080;\">.<\/span>getColumn<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">.<\/span>setWidth<span style=\"color: #308080;\">(<\/span><span style=\"color: #008c00;\">150<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n    column<span style=\"color: #308080;\">.<\/span>setLabelProvider<span style=\"color: #308080;\">(<\/span><span style=\"color: #200080; font-weight: bold;\">new<\/span> TreeLabelProvider<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n    <span style=\"color: #200080; font-weight: bold;\">final<\/span> TreeViewer finalTreeViewer <span style=\"color: #308080;\">=<\/span> treeViewer<span style=\"color: #406080;\">;<\/span>\n    <span style=\"color: #200080; font-weight: bold;\">final<\/span> TextCellEditor cellEditor <span style=\"color: #308080;\">=<\/span> <span style=\"color: #200080; font-weight: bold;\">new<\/span> TextCellEditor<span style=\"color: #308080;\">(<\/span>treeViewer<span style=\"color: #308080;\">.<\/span>getTree<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n\n    column<span style=\"color: #308080;\">.<\/span>setEditingSupport<span style=\"color: #308080;\">(<\/span><span style=\"color: #200080; font-weight: bold;\">new<\/span> EditingSupport<span style=\"color: #308080;\">(<\/span>treeViewer<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n\n        <span style=\"color: #308080;\">@<\/span>Override\n        <span style=\"color: #200080; font-weight: bold;\">protected<\/span> <span style=\"color: #7779bb;\">void<\/span> setValue<span style=\"color: #308080;\">(<\/span><span style=\"color: #6679aa; font-weight: bold;\">Object<\/span> element<span style=\"color: #308080;\">,<\/span> <span style=\"color: #6679aa; font-weight: bold;\">Object<\/span> value<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n            <span style=\"color: #200080; font-weight: bold;\">if<\/span> <span style=\"color: #308080;\">(<\/span>element <span style=\"color: #200080; font-weight: bold;\">instanceof<\/span> TreeItemData<span style=\"color: #308080;\">)<\/span>\n            <span style=\"color: #406080;\">{<\/span>\n                TreeItemData data <span style=\"color: #308080;\">=<\/span> <span style=\"color: #308080;\">(<\/span>TreeItemData<span style=\"color: #308080;\">)<\/span>element<span style=\"color: #406080;\">;<\/span>\n                data<span style=\"color: #308080;\">.<\/span>value <span style=\"color: #308080;\">=<\/span> value<span style=\"color: #308080;\">.<\/span>toString<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n            <span style=\"color: #406080;\">}<\/span>\n            finalTreeViewer<span style=\"color: #308080;\">.<\/span>update<span style=\"color: #308080;\">(<\/span>element<span style=\"color: #308080;\">,<\/span> <span style=\"color: #200080; font-weight: bold;\">null<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n        <span style=\"color: #406080;\">}<\/span>\n\n        <span style=\"color: #308080;\">@<\/span>Override\n        <span style=\"color: #200080; font-weight: bold;\">protected<\/span> <span style=\"color: #6679aa; font-weight: bold;\">Object<\/span> getValue<span style=\"color: #308080;\">(<\/span><span style=\"color: #6679aa; font-weight: bold;\">Object<\/span> element<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n            <span style=\"color: #200080; font-weight: bold;\">return<\/span> element<span style=\"color: #308080;\">.<\/span>toString<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n        <span style=\"color: #406080;\">}<\/span>\n\n        <span style=\"color: #308080;\">@<\/span>Override\n        <span style=\"color: #200080; font-weight: bold;\">protected<\/span> TextCellEditor getCellEditor<span style=\"color: #308080;\">(<\/span><span style=\"color: #6679aa; font-weight: bold;\">Object<\/span> element<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n            <span style=\"color: #200080; font-weight: bold;\">return<\/span> cellEditor<span style=\"color: #406080;\">;<\/span>\n        <span style=\"color: #406080;\">}<\/span>\n\n        <span style=\"color: #308080;\">@<\/span>Override\n        <span style=\"color: #200080; font-weight: bold;\">protected<\/span> <span style=\"color: #7779bb;\">boolean<\/span> canEdit<span style=\"color: #308080;\">(<\/span><span style=\"color: #6679aa; font-weight: bold;\">Object<\/span> element<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n            <span style=\"color: #200080; font-weight: bold;\">return<\/span> <span style=\"color: #200080; font-weight: bold;\">true<\/span><span style=\"color: #406080;\">;<\/span>\n        <span style=\"color: #406080;\">}<\/span>\n    <span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n\n    <span style=\"color: #595979;\">\/\/To set width of the column to tree width<\/span>\n    treeViewer<span style=\"color: #308080;\">.<\/span>getControl<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">.<\/span>addControlListener<span style=\"color: #308080;\">(<\/span><span style=\"color: #200080; font-weight: bold;\">new<\/span> ControlListener<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n\n        <span style=\"color: #308080;\">@<\/span>Override\n        <span style=\"color: #200080; font-weight: bold;\">public<\/span> <span style=\"color: #7779bb;\">void<\/span> controlResized<span style=\"color: #308080;\">(<\/span>ControlEvent e<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n            column<span style=\"color: #308080;\">.<\/span>getColumn<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">.<\/span>setWidth<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">(<\/span>Tree<span style=\"color: #308080;\">)<\/span>e<span style=\"color: #308080;\">.<\/span>getSource<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">.<\/span>getBounds<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">.<\/span>width<span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n        <span style=\"color: #406080;\">}<\/span>\n\n        <span style=\"color: #308080;\">@<\/span>Override\n        <span style=\"color: #200080; font-weight: bold;\">public<\/span> <span style=\"color: #7779bb;\">void<\/span> controlMoved<span style=\"color: #308080;\">(<\/span>ControlEvent e<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n        <span style=\"color: #406080;\">}<\/span>\n    <span style=\"color: #406080;\">}<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n<span style=\"color: #406080;\">}<\/span><\/pre>\n<p>Note that you have to specify the width of the table cell column, else it would be set to zero. If you want to set column width equal to the width of the tree then you can do that in the <a href=\"http:\/\/help.eclipse.org\/juno\/topic\/org.eclipse.platform.doc.isv\/reference\/api\/org\/eclipse\/swt\/events\/ControlListener.html\" target=\"_blank\">ControlListener<\/a>, as shown in the above code.<\/p>\n<p>Both above methods will let you edit values in TreeViewer by double clicking on tree items.<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/138.197.85.232\/blog\/wp-content\/uploads\/2013\/02\/tree_viewer2.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"694\" data-permalink=\"http:\/\/ramkulkarni.com\/blog\/in-place-editing-in-eclipse-treeviewer\/tree_viewer2\/\" data-orig-file=\"https:\/\/i0.wp.com\/ramkulkarni.com\/blog\/wp-content\/uploads\/2013\/02\/tree_viewer2.png?fit=241%2C179\" data-orig-size=\"241,179\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"tree_viewer2\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/ramkulkarni.com\/blog\/wp-content\/uploads\/2013\/02\/tree_viewer2.png?fit=241%2C179\" data-large-file=\"https:\/\/i0.wp.com\/ramkulkarni.com\/blog\/wp-content\/uploads\/2013\/02\/tree_viewer2.png?fit=241%2C179\" class=\"size-full wp-image-694 alignnone\" alt=\"tree_viewer2\" src=\"https:\/\/i0.wp.com\/138.197.85.232\/blog\/wp-content\/uploads\/2013\/02\/tree_viewer2.png?resize=241%2C179\" width=\"241\" height=\"179\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>The default text box created by TextCellEditor is without borders. You can customize this text box by extending TexCellEditor. The following class adds border to the text box and sets it height to 10 pixels more than the font height.<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\"><span style=\"color: #200080; font-weight: bold;\">class<\/span> MyTextCellEditor <span style=\"color: #200080; font-weight: bold;\">extends<\/span> TextCellEditor\n<span style=\"color: #406080;\">{<\/span>\n    <span style=\"color: #7779bb;\">int<\/span> minHeight <span style=\"color: #308080;\">=<\/span> <span style=\"color: #008c00;\">0<\/span><span style=\"color: #406080;\">;<\/span>\n\n    <span style=\"color: #200080; font-weight: bold;\">public<\/span> MyTextCellEditor<span style=\"color: #308080;\">(<\/span>Tree tree<span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">super<\/span><span style=\"color: #308080;\">(<\/span>tree<span style=\"color: #308080;\">,<\/span> SWT<span style=\"color: #308080;\">.<\/span>BORDER<span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n        Text txt <span style=\"color: #308080;\">=<\/span> <span style=\"color: #308080;\">(<\/span>Text<span style=\"color: #308080;\">)<\/span>getControl<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n\n        <span style=\"color: #6679aa; font-weight: bold;\">Font<\/span> fnt <span style=\"color: #308080;\">=<\/span> txt<span style=\"color: #308080;\">.<\/span>getFont<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n        FontData<span style=\"color: #308080;\">[<\/span><span style=\"color: #308080;\">]<\/span> fontData <span style=\"color: #308080;\">=<\/span> fnt<span style=\"color: #308080;\">.<\/span>getFontData<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">if<\/span> <span style=\"color: #308080;\">(<\/span>fontData <span style=\"color: #308080;\">!<\/span><span style=\"color: #308080;\">=<\/span> <span style=\"color: #200080; font-weight: bold;\">null<\/span> <span style=\"color: #308080;\">&amp;<\/span><span style=\"color: #308080;\">&amp;<\/span> fontData<span style=\"color: #308080;\">.<\/span>length <span style=\"color: #308080;\">&gt;<\/span> <span style=\"color: #008c00;\">0<\/span><span style=\"color: #308080;\">)<\/span>\n            minHeight <span style=\"color: #308080;\">=<\/span> fontData<span style=\"color: #308080;\">[<\/span><span style=\"color: #008c00;\">0<\/span><span style=\"color: #308080;\">]<\/span><span style=\"color: #308080;\">.<\/span>getHeight<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span> <span style=\"color: #308080;\">+<\/span> <span style=\"color: #008c00;\">10<\/span><span style=\"color: #406080;\">;<\/span>\n    <span style=\"color: #406080;\">}<\/span>\n\n    <span style=\"color: #200080; font-weight: bold;\">public<\/span> LayoutData getLayoutData<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span> <span style=\"color: #406080;\">{<\/span>\n        LayoutData data <span style=\"color: #308080;\">=<\/span> <span style=\"color: #200080; font-weight: bold;\">super<\/span><span style=\"color: #308080;\">.<\/span>getLayoutData<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">;<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">if<\/span> <span style=\"color: #308080;\">(<\/span>minHeight <span style=\"color: #308080;\">&gt;<\/span> <span style=\"color: #008c00;\">0<\/span><span style=\"color: #308080;\">)<\/span>\n            data<span style=\"color: #308080;\">.<\/span>minimumHeight <span style=\"color: #308080;\">=<\/span> minHeight<span style=\"color: #406080;\">;<\/span>\n        <span style=\"color: #200080; font-weight: bold;\">return<\/span> data<span style=\"color: #406080;\">;<\/span>\n    <span style=\"color: #406080;\">}<\/span>\n<span style=\"color: #406080;\">}<\/span><\/pre>\n<p>Now set instance of this class as cell editor in the function\u00a0enableEditing1 &#8211;<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\">treeViewer.setCellEditors(new CellEditor[]<span style=\"color: #406080;\">{<\/span><span style=\"color: #200080; font-weight: bold;\">new<\/span> MyTextCellEditor<span style=\"color: #308080;\">(<\/span>treeViewer<span style=\"color: #308080;\">.<\/span>getTree<span style=\"color: #308080;\">(<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #308080;\">)<\/span><span style=\"color: #406080;\">}<\/span>)<span style=\"color: #308080;\">;<\/span><\/pre>\n<p>And in the function enableEditing2 \u00a0&#8211;<\/p>\n<pre style=\"color: #000020; background: #f6f8ff;\"><span style=\"color: #200080; font-weight: bold;\">final<\/span> TextCellEditor cellEditor = new MyTextCellEditor(treeViewer.getTree())<span style=\"color: #308080;\">;<\/span><\/pre>\n<p><a href=\"https:\/\/i0.wp.com\/138.197.85.232\/blog\/wp-content\/uploads\/2013\/02\/tree_viewer3.png\"><img loading=\"lazy\" decoding=\"async\" data-attachment-id=\"695\" data-permalink=\"http:\/\/ramkulkarni.com\/blog\/in-place-editing-in-eclipse-treeviewer\/tree_viewer3\/\" data-orig-file=\"https:\/\/i0.wp.com\/ramkulkarni.com\/blog\/wp-content\/uploads\/2013\/02\/tree_viewer3.png?fit=215%2C208\" data-orig-size=\"215,208\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"tree_viewer3\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/ramkulkarni.com\/blog\/wp-content\/uploads\/2013\/02\/tree_viewer3.png?fit=215%2C208\" data-large-file=\"https:\/\/i0.wp.com\/ramkulkarni.com\/blog\/wp-content\/uploads\/2013\/02\/tree_viewer3.png?fit=215%2C208\" class=\"alignleft size-full wp-image-695\" alt=\"tree_viewer3\" src=\"https:\/\/i0.wp.com\/138.197.85.232\/blog\/wp-content\/uploads\/2013\/02\/tree_viewer3.png?resize=215%2C208\" width=\"215\" height=\"208\" data-recalc-dims=\"1\" \/><\/a>With the above changes, the cell editor is created with border, as shown in the image on the left.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>You can find the complete source code of this example <a href=\"http:\/\/ramkulkarni.com\/temp\/2013-02-01\/TreeViewerExample.java\" target=\"_blank\">here<\/a>.<\/p>\n<p>-Ram Kulkarni<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In an Eclipse RCP application I was working on recently, I had to implement a TreeViewer with in-place editing feature. It was not easy to find all the information required to implement this, so I thought I would explain it here. First let&#8217;s take a simple example, where\u00a0hierarchical data is displayed in \u00a0a tree. When &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/ramkulkarni.com\/blog\/in-place-editing-in-eclipse-treeviewer\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;In-place Editing in Eclipse TreeViewer&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"In-place Cell Editing in Eclipse TreeViewer http:\/\/wp.me\/p2g9O8-b8","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[38,1],"tags":[64,25,65,56],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2g9O8-b8","jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/690"}],"collection":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/comments?post=690"}],"version-history":[{"count":0,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/posts\/690\/revisions"}],"wp:attachment":[{"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/media?parent=690"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/categories?post=690"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/ramkulkarni.com\/blog\/wp-json\/wp\/v2\/tags?post=690"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}