Monday, August 31, 2009

Disabling nodes in CheckboxTreeViewer

Working on a project at work I had to use CheckboxTreeViewer, one of the requirements was that some nodes in the tree are disabled. I thought that this would be easy enough because surely CheckboxTreeViewer has some method that I can call to disable some checkboxes. It turns out it does not.

The first thing I obviously did was googled for possible solution. Unfortunately the solutions were not what I needed, so I decided to try the problem myself. Luckily there are two methods that are of help in CheckboxTreeViewer: setGrayed and setChecked, setGrayed nicely grays out checkbox so the user knows that this checkbox is disabled.

Here is the code in pastebin, since it does not render well in my template.

The disable mechanism revolves around value in model's isEnabled field. My model is roughly based on example from this article, but any model will do as long as there is variable that sets a particular model element not enabled. Screen shot on the left shows how this code works in my application.

Eclipse 3.5 has interface ICheckStateProvider that pretty much does what I wanted to do here. However, I am currently at Eclipse 3.4, so will have to wait for this once I move to 3.5.

5 comments:

  1. Hey Aleksandr,

    The link to the code is pastebin does not work. Do you have an alternate link ?

    Thanks.

    ReplyDelete
  2. I updated the post with good link

    ReplyDelete
  3. fyi - setGrayed() on Mac doesn't give a disabled look like Windows does. On Mac its a dash inside the checkbox [-] with blue background.

    This solution could also lead to confusing the user. Since setGrayed() is used to indicate some child nodes are checked but not all. So how is the user suppose to distinguish between disabled and some children checked.

    ReplyDelete
  4. @colt

    This was done for Linux and Windows operating systems only. I don't have access to Mac and at my current job we don't support Mac OS. Feel free to change the code to make it work for Mac if you wish :)

    ReplyDelete
  5. hey hello guys..im stuck with th problem ...pls help
    i have to implement a radio tree...
    wre only the first level nodes will be radio .ie i can select only 1 node.then on i must be able to select any num of nodes..kindly help.

    ReplyDelete

Blogger Syntax Highliter