selectionService.addPostSelectionListener( new ISelectionListener() {And you think: oh well, shouldn't this be a little easier to do?
public void selectionChanged( IWorkbenchPart part, ISelection selection ) {
if( selection instanceof IStructuredSelection && !selection.isEmpty() ) {
IStructuredSelection strusel = (IStructuredSelection)selection;
Object element = strusel.getFirstElement();
if( element instanceof MyCoolType ) {
MyCoolType coolThing = (MyCoolType)element;
// ... and now we finally can do something with the cool thing
}
}
}
} );
You are right to ask that question. Here's one way to do it a little more elegantly:
public void selectionChanged( IWorkbenchPart part, ISelection selection ) {And this would be the implementation of the
MyCoolType coolThing = new ElementFrom( selection ).as( MyCoolType.class );
// ... now do something with coolThing already
}
ElementFrom
utility:Have fun :-)
/** Extract the first element (if any) from a JFace selection, in a type-safe
* manner.
*/
public class ElementFrom {
private final ISelection selection;
/** constructs a new extraction operation.
*
* @param selection a JFace selection object from which to extract the
* selected element. Can benull
, in which case this extraction
* operation yieldsnull
.
*/
public ElementFrom( ISelection selection ) {
this.selection = selection;
}
/** retrieves the extracted element typed as specified.
*
* @param cls the class representing the expected type of the extracted element.
* Must not benull
.
* @paramthe expected type of the extracted element.
* @return the element under the expected type, ornull
*/
publicT as( Class cls ) {
return extractElement( cls );
}
privateT extractElement( Class cls ) {
T result = null;
if( selectionIsGood() ) {
IStructuredSelection strusel = (IStructuredSelection)selection;
Object element = strusel.getFirstElement();
if( elementTypeIsGood( element, cls ) ) {
result = cls.cast( element );
}
}
return result;
}
privateboolean elementTypeIsGood( Object element, Class cls ) {
return cls.isAssignableFrom( element.getClass() );
}
private boolean selectionIsGood() {
return selection instanceof IStructuredSelection && !selection.isEmpty();
}
}
Strange things are going on in this universe, btw: I haven't got the faintest idea where these </t> things at the end of the post come from. They're not in my post. Hmmm...
ReplyDeleteLove this idea, thanks Leif for sharing!
ReplyDeleteyep, a nice way to get selections in strict type manner. Sorry to mention that, but the idea's pretty common in jface land: I've used and found it several times already: have a look at the rcp company utilities that are around for quite some time already:
ReplyDeletehttp://code.google.com/p/rcp-company-utilities/source/browse/trunk/com.rcpcompany.utils.selection/src/com/rcpcompany/utils/selection/SelectionUtils.java
Greets
André
André, thanks for the pointer :-)
ReplyDeleteDidn't know of this particular lib, but I expect that many have similar utilities, it really is a common pattern.
completely agree! IMHO this stuff (and further common tasks) should make it into the official code
ReplyDeletecls.isAssignableFrom( element.getClass() )
ReplyDeleteshould be written as
cls.isInstance( element )