Disable parts SWT-Table/Tree with SWT.CHECK


This is a hack, a hack, a hack posting.

I have read many many entries on the newsgroups asking a question like this:

How can I disable certain check boxes in an SWT-Tree/Table. Is this possible?

The standard answer to this was: “Sorry no this is not possible”. Today I faced the same problem (mine had to do with ViewerObservables#observeCheckElements()) where the user is not allowed to check the Top-Level-Nodes.

The tree looks like this:

+ Application 1
  + Privilege A
    + Privileg A1
  + Privilege B
    + Privileg B2
+ Application 1
  + Privileg C

The values bound are the ones in Privileg* so I have to lock the Application-checkboxes

The setup is something like this:

Databinding ctx = ....
IObservableSet mObs = ....

Tree tree = new Tree(parent,SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL);
CheckBoxTreeViewer v = new CheckBoxTreeViewer(tree);
IObservableSet uiOs = ViewerObservables.observeCheckedElements(v,IPrivileges.class);
ctx.bindSet(uiObs,mObs,null,null);

I nearly gave up but then I had the following idea.

final Tree tree = new Tree(parent,SWT.BORDER|SWT.V_SCROLL|SWT.H_SCROLL);
// Attach a listener directly after the creation
tree.addListener(SWT.Selection,new Listener() {
   public void handleEvent(Event event) {
       if( event.detail == SWT.CHECK ) {
           if( !(event.item.getData() instanceof IPrivileg) ) {
              event.detail = SWT.NONE;
              event.type   = SWT.None;
              event.doIt   = false;
              try {
                 tree.setRedraw(false);
                 TreeItem item = (TreeItem)event.item;
                 item.setChecked(! item.getChecked() );
              } finally {
                 tree.setRedraw(true);
              }
           }
       }
   }
});

CheckBoxTreeViewer v = new CheckBoxTreeViewer(tree);
// ....

This is a hack, I only tested it on Win32 (XP) and don’t know how cross platform it is so don’t kill me for posting this hack of hacks.

Advertisements
This entry was posted in 3.x. Bookmark the permalink.

7 Responses to Disable parts SWT-Table/Tree with SWT.CHECK

  1. Konstantin Komissarchik says:

    I’ve struggled with this problem as well. My solution was a little higher-level than yours (used check listener on the viewer). As far as I know, it works fine on only platforms. Unfortunately, not allowing check/uncheck is only half of the problem. You have to still give the user some sense that they are looking at an unmodifiable item or they get frustrated trying to check/uncheck. After a few iterations, the solution that I came up with is to use a lock overlay on the icon associated with the tree item and to display a tooltip explaining the situation if the user actually tries to check/uncheck. Before the tooltip, I tried a dialog box, but the dialog box was quite jarring. The tooltip conveys the message and is less in your face.

    If you want to see my solution in action, just take a look at WTP. Open Dynamic Web Project wizard and select “Modify” next to configurations combo.

  2. Tom says:

    That’s too late for me because then databinding already stepped in tried to update the model-observable and I’ll have an exception logged.

    With this solution the CheckTreeViewer doesn’t even get a check-changed event so so do all other who are attaching themselves on the control.

  3. Cyril Lakech says:

    Great idea !

    but, Why can't you use en image whith the labelprovider?
    (i know the image can change… depends of the platform & you can't use databinding but… you don't use hack ^^)

  4. Steve says:

    What is the setRedraw() doing? Get rid of it.

    Steve

  5. Anonymous says:

    In order to add the visual indication to this issue I changed the foreground of those “disabled” items…

  6. Laptop Parts says:

    Interesting stuff I must admit. But what I really want to know is how did you really compose all this coding?? Okay, once I get this down hopefully it will work for me because I tried a couple different ways and none of them seemed to work. No worries though, that’s why Google is here! haha I can literally find anything to everything on the web nowadays but enough of that.. But then thing is, that whenever I try inserting something like this it say, “The project type is not supported by this installation” How can I solve this problem?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s