Blog
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.