Selection monitoring mechanism in Eclipse plug-in development(Selection provider-listener)
The monitoring mechanism is a common technique used in Eclipse plug-in development or RCP application development, such as clicking on an element in Tableviewer or treeviewer, which requires some processing for the current selection.
There are two general implementations of selection provider and listener in a view, or in a different view;
1 , selection provider to increase their own monitoring to handle
The former directly let selection provider realize monitoring and processing business can.
The implementation code is as follows:
Viewer . Addselectionchangedlistener (newIselectionchangedlistener () {
public voidselectionchanged (selectionchangedevente ) {
if(e. getselection () =null) {
istructuredselection is = (istructuredselection)e .getselection ();
txtinfo. SetText (is. Getfirstelement (). toString ());
}
}
});
2. Workbenchpage Registration mechanism
Selection provider and listener are not in one view, but are in workbenchpage; First define selection provider, then define listener to handle selection event. Finally, the association between the two is registered through Workbenchpage.
ü Set selection provider in view
Getsite (). Setselectionprovider (Viewer);
Üsecondviewpart implements the Iselectionlistener interface and implements
Secondviewpartextends Viewpart implements Iselectionlistener
Publicvoid selectionchanged (iworkbenchpart part, iselection e) {
if (e!= null) {
Istructuredselectionis = (istructuredselection) e;
if (is.getfirstelement () = null)
Txtinfo.settext (String) is.getfirstelement ());
}
}
Register the association between the two in Üsecondviewpart
Secondviewpart Createpartcontrol (Composite Parent) method registers the association; Getsite (). GetPage (). Addselectionlistener (this);
Conclusion:
In the conventional RCP development, the task of handling selection monitoring can be accomplished easily by the two methods described above. If selection's provider and listener are all in one view and only need to handle selectionchangedevent simply, we can choose Method 1. If the selection provider and listener are scattered across two or more views, we must do so through Method 2. In addition, other more complex selection Provider-listener can also be implemented using RCP's iadaptable.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Opt-in monitoring mechanism in Eclipse plug-in development (Selection Provider-listener)