-
12 Jan 2012 6:55 AM #1
SelectionChangedEvent.getSource() - ClassCastException with TreeSelectionModel
SelectionChangedEvent.getSource() - ClassCastException with TreeSelectionModel
Ok, this appears to be very strange.
I've been playing around with Tree. Mostly working. I have an odd thing happening with the SelectionChangedEvent class and onSelectionChanged method.
My code basically uses the BasicTree from the Demo as a template.
The Tree is a Tree<TreeItem, String> with TreeItem being my own class.
So, my EntryPoint class implements SelectionChangedHandler<TreeItem>, and the onSelectionChanged method signature thus looks as follows:
PHP Code:public void onSelectionChanged(SelectionChangedEvent<TreeItem> event)
Here's where it gets weird.
Inside that method, I can NOT do the following:
Eclipse will red-underline it and complain that I have "Incompatible conditional operand types Component and TreeSelectionModel<?>"PHP Code:if(event.getSource() instanceof TreeSelectionModel<?>)
If I skip the parameterized <?>, I get basically the same error.
I will also get the same "Incompatible conditional operand..." complaint in the if statement below when I try to do something like this:
So I tried to be circuitous. I instead tried this:PHP Code:TreeSelectionModel selectionModel = myTree.getSelectionModel();
if(event.getSource() == selectionModel) {
// do stuff
}
and figured I would do my instanceof check after the assignment. This will compile, but the line:PHP Code:if(event.getSource() instanceof Object) {
Object source = event.getSource();
}
will throw ClassCastException! It complains that TreeSelectionModel cannot be cast to ComponentPHP Code:if(event.getSource() instanceof Object)
The complaint is actually with line 57 in SelectionChangedEvent.java. That line is trying to downcast an Object (returned by GwtEvent.getSource()) as a Component, and TreeSelectionModel is not in the Component hierarchy.
My workaround is to have a handle to the Tree available, and use the onSelectionChanged method simply to note when the selection has changed. I have to ignore the SelectionChangedEvent instance that's passed in, because I can't do much with it.
However, it looks like SelectionChangedEvent.getSource() should be returning an Object rather than a Component... (unless I'm missing something)
Thank you for reporting this bug. We will make it our priority to review this report.


Reply With Quote