Opt-in monitoring mechanism in Eclipse plug-in development (Selection Provider-listener)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.