I had the same problem and there is not much online resources about how to implement MasterDetails pattern in eclipse. I came here, but unfortunately nobody has answered yet, so I had to investigate it on my own. I mainly tried to analyse the ExtensionsPage
class of eclipse editor of plugin.xml
file. I am quite fresh to Eclipse and Java in general, so do not take my answer as an oracle, but since there are no other answers, it may have some use for you.
Using debugger you will notice that the method initialize()
is called when you register a page, so it does not depend on your current selection.
My MasterDetailsBloc class is something like this:
public class MyMasterDetailsBlock extends MasterDetailsBlock{
private SectionPart fMasterSectionPart;
private Section fMasterSection;
private TreeViewer fTreeViewer;
[...]
/**
* Create contents of the master details block.
* @param managedForm
* @param parent
*/
@Override
protected void createMasterPart(IManagedForm managedForm, Composite parent) {
fMasterSection = managedForm.getToolkit().createSection(parent,
ExpandableComposite.EXPANDED | ExpandableComposite.TITLE_BAR);
fMasterSectionPart = new SectionPart(fMasterSection, 0);
Composite composite = new Composite(fMasterSection, SWT.RIGHT);
fTreeViewer = new TreeViewer(composite, SWT.FILL);
fTreeViewer.setContentProvider(new MyContentProvider());
fTreeViewer.setLabelProvider(new MyLabelProvider());
fTreeViewer.setInput(fMyInput);
fTreeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
@Override
public void selectionChanged(SelectionChangedEvent event) {
detailsPart.selectionChanged(fMasterSectionPart,
event.getSelection());
}
);
[...]
}
/**
* Register the pages.
* @param part
*/
@Override
protected void registerPages(DetailsPart part) {
part.registerPage(MyClass.class, new MyClassDetailsPage());
}
[...]
}
The trick seems to be that you need to create a SectionPart
that wraps your master Section
widget (in my code these are fMasterSectionPart
and fMasterSection
respectively, initialized in the function createMasterPart()
). Then you need to add a ISelectionChangedListener
object to your viewer, where you call detailsPart.selectionChanged()
and voila. Registering pages goes intuitively, but I noticed that you have to register each class separately, i.e. registering base class will not work for derived classes. This is however just first observation and I haven't investigated the mechanism deeply yet.
It may happen that this is ugly and dirty solution and "you just shouldn't do it this way". Nevertheless this is the only way I could make it work, and there is no material on web about how to do it right. I hope I could help.