Over a million developers have joined DZone.

TreeViewer: Two Clicks to Edit

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

TreeViewer is the foundation behind most trees and tree tables found in Eclipse. It provides excellent extensibility and options for customization. One feature, in-place editing of tree nodes, is very useful -- however the default implementation activates editing on the first click. This can be frustrating and unintuitive to users who expect the first click to select a node, and the second click to edit. This tip shows a simple way to cause TreeViewer to require two clicks to edit.

Since Eclipse 3.3, ColumnViewerEditorActivationStrategy has been available as API to configure how a TreeViewer starts editing. Our strategy will require the tree's current selection to be the node being clicked in order for a click to be considered an editing event. We do this by extending ColumnViewerEditorActivationStrategy as follows:

 * an activation strategy that only allows editor activation if the item under the
 * event was already selected. This has the effect of requiring a click to first select
 * the item, then a second click to edit. 
class SecondClickColumnViewerEditorActivationStrategy extends
  ColumnViewerEditorActivationStrategy implements ISelectionChangedListener {

 private Object selectedElement;

 public SecondClickColumnViewerEditorActivationStrategy(ColumnViewer viewer) {
 protected boolean isEditorActivationEvent(
   ColumnViewerEditorActivationEvent event) {
  IStructuredSelection selection = (IStructuredSelection)getViewer().getSelection();
  return selection.size() == 1 && super.isEditorActivationEvent(event) && 
    selectedElement == selection.getFirstElement();

 public void selectionChanged(SelectionChangedEvent event) {
  IStructuredSelection ss = (IStructuredSelection) event.getSelection();
  if (ss.size() == 1) {
   selectedElement = ss.getFirstElement();
  selectedElement = null;


This code relies on the order of events when comparing the tree's current selection against it's previous current selection.

Now all we have to do is install the strategy on our TreeViewer:

viewer = new org.eclipse.jface.viewers.TreeViewer(parent, 
TreeViewerEditor.create(viewer,null,new SecondClickColumnViewerEditorActivationStrategy(viewer),

Voila! We're done: our TreeViewer now edits on the second click instead of the first.

From http://greensopinion.blogspot.com

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}