ISelectionListener listener = new ISelectionListener() {
public void selectionChanged(IWorkbenchPart part, ISelection sel) {
if (!(sel instanceof IStructuredSelection))
return;
IStructuredSelection ss = (IStructuredSelection) sel;
Object o = ss.getFirstElement();
if (o instanceof Book)
viewer.setInput(ss.size()==1 ? o : null);
}
};
ISelectionListener listener = new ISelectionListener() { public void selectionChanged(IWorkbenchPart part, ISelection sel) { if (!(sel instanceof IStructuredSelection)) return; IStructuredSelection ss = (IStructuredSelection) sel; Object o = ss.getFirstElement(); if (o instanceof Book) viewer.setInput(ss.size()==1 ? o : null); } };
But the observer pattern is not limited to single user interface components. For example you could have a part A in your application which displays the current temperature.
Another part B displays a green light if the temperature is above 20 degree celsius. To react to changes in the temperature part B registers itself as a listener to Part A.
IWorkbench workbench = PlatformUI.getWorkbench();
IWorkbenchPart view =(IWorkbenchPart)workbench.getActiveWorkbenchWindow().getActivePage().findViewReference("UnserProjekt.view4").getView(false);
((ViewPart4)view).update(selectedFile);