Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

A class for adding "standard" hierarchical selection behavior to your CheckboxTreeViewer

DZone's Guide to

A class for adding "standard" hierarchical selection behavior to your CheckboxTreeViewer

· Java Zone
Free Resource

Bitbucket is for the code that takes us to Mars, decodes the human genome, or drives your next car. What will your code do? Get started with Bitbucket today, it's free.

We've all seen this in numerous applications: checking a node in a checkbox tree widget selects all descendants and grays out all ancestors. Similarly, unchecking a node recursively executes the same logic in reverse. As a familiar example, the Resource Working Set dialog comes to mind (org.eclipse.ui.internal.dialogs.ResourceWorkingSetPage):



In fact, this behavior is so commonplace that if you've never used the JFace CheckboxTreeViewer class before, you might expect it to implement this behavior by default. However, this is not the case.

After reimplementing this behavior repeatedly in my applications, I finally made a simple helper class to make this process easier. To add this behavior to your CheckboxTreeViewer, simply add the following code, before setting the viewer input:

CheckboxTreeSelectionHelper.attach(checkboxTreeViewer,myContentProvider);

Make sure your content provider implements getParent(), otherwise ancestor updates won't work correctly.

I've attached the helper class along with a complete example application. Hopefully this will make it easier for you to implement this commonplace checkbox tree behavior in your applications.

A word of caution: this code is provided as-is and has not undergone extensive testing. Please post any bug fixes or enhancements here.

Helper class: CheckboxTreeSelectionHelper.java
Example app: CheckboxTreeSelectionHelperTestDialog.java

Bitbucket is the Git solution for professional teams who code with a purpose, not just as a hobby. Get started today, it's free.

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
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.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}