Eclipse Development Plugin (ii)

Source: Internet
Author: User
Tags event listener

Implementing event snooping between views

The interaction between components in two views is a common problem when developing plug-ins. When you click an item in the View 1 list, the text box for view 2 displays the corresponding character.

Active Type: D

The active approach is to get a reference to the object on view 2 in the code block of Figure 1. The object in view 1 is then actively passed to view 2.

Modify View1.java and View2.java

Eclipse uses Plugin.xml to load extension points (such as view extension points) in plug-ins and plug-ins, so the view 2 object can be obtained by ID identification in View1.java.

View1.java

1  Public classView1extendsViewpart {2     PrivateList List;//write a list as an instance variable of a class to widen its accessible scope3     //Note that the List is not java.util, but Org.eclipse.swt.widgets.List .4      Public voidCreatepartcontrol (Composite parent) {5Iworkbenchhelpsystem help =platformui.getworkbench (). Gethelpsystem ();6Help.sethelp (Parent, "Cn.com.kxh.myplugin.buttonHelpId");7Composite Topcomp =NewComposite (parent, SWT.) NONE);8Topcomp.setlayout (Newfilllayout ());9List =NewList (Topcomp, SWT. BORDER);TenList.add ("China")); OneList.add ("The United States"); AList.add ("France"); -         //List Selection Event listener -List.addselectionlistener (NewSelectionadapter () { the              Public voidwidgetselected (selectionevent e) { -                 //View2 object obtained by Iworkbenchpage -Iworkbenchpage WBP =getviewsite (). GetPage (); -                 //It is important to iworkbenchpage objects in the plug-in, and here is a common way to get this object. +                 //Activator.getdefault (). Getworkbench (). Getactiveworkbenchwindow (). GetActivePage (); -Iviewpart view2 = Wbp.findview ("Cn.com.kxh.myplugin.View2"); +                 //The parameter of this place is the ID identifier of "View 2" in Plugin.xml. This shows that the status of Plugin.xml files in plugins is extremely important. A                 //Displays the currently selected list item in a text box atText Text =((VIEW2) view2). GetText (); -Text.settext (list.getselection () [0]); -             } -         }); -     } - @Override in      Public voidSetFocus () {} -}

View2.java (Change the View2.java text box object to an instance variable of the class and write the appropriate setter and getter method)

1  Public classView2extendsViewpart { 2private  text text;      3      Public voidCreatepartcontrol (Composite parent) {4Composite Topcomp =NewComposite (PARENT,SWT. NONE);5Topcomp.setlayout (Newfilllayout ());6Text =NewText (TOPCOMP,SWT. BORDER);7Text.settext ("I am the text box");8     }9      Public voidSetFocus () {}Ten     One public    text GetText () {return  text;   the public void  setText (text text) { this. Text = text; +  }                              -}

Sampleperspective.java (and I didn't make any changes to my last blog post)

1  Public classSampleperspectiveImplementsIperspectivefactory {2     //parameter ipagelayout is the layout manager for the Perspective view3      Public voidcreateinitiallayout (ipagelayout layout) {4         //get the edit space identifier for this perspective5String Editorarea =Layout.geteditorarea ();6         //Create a space on the left side of the perspective and put "View 1" in it. 7         //"Left" is the identification of this space, and Ipagelayout.left indicates that the space in the perspective layout is positioned on the right;8         //0.2f means this space occupies the width of the perspective figure 20%; Editorarea refers to the editing space using perspective9Ifolderlayout left = Layout.createfolder ("Left", Ipagelayout.left, 0.2f, Editorarea);TenLeft.addview ("Cn.com.kxh.myplugin.View1");//parameter is ID identification for "View 1" in Plugin.xml One         //Add "View 2" to the bottom of the perspective AIfolderlayout bottom = Layout.createfolder ("Bottom", Ipagelayout.bottom, 0.8f, Editorarea); -Bottom.addview (View2.class. GetName ());//since we take the ID of the view as the same as the class name, we can also use this notation -         //adds a previously defined actionset (Main menu, toolbar button) to this perspective. It's going to be in Plugin.xml . the         //visible= "False" in the action setting of the item to see the effect, then open the other perspective, the action setting -         //The main Menu, toolbar buttons will not appear on the interface, only the open this perspective will appear.  -Layout.addactionset ("Myplugin.actionset");//The ID of the parameter is actionset in Plugin.xml -     } +}

Summarize:

(1) in the plug-in Iworkbenchpage object is more important, here gives a general method to obtain this object, but he is the Iworkbenchpage object to obtain the current activity.

Activator.getdefault (). Getworkbench (). Getactiveworkbenchwindow (). GetActivePage ();

(2) The parameter in Iworkbenchpage.findview ("Cn.com.kxh.myplugin.View2") is identified by the ID set in Plugin.xml in "View 2".

Thus, the position of the Plugin.xml file in the plugin is important. Iworkbenchpage handles the Findview method. The Findeditor method is also used to get the editor object. Like "Cn.com.kxh.myplugin.View2 "This identifier is often used in system development, and it's a good idea to set up a class to centrally place these string constants." Then the system is used only with its constant name, otherwise the identifier string scattered in the code, later change will be very troublesome. The sample code for a constant class is as follows:

 Public Final class stringconstants{    publicfinalstatic String view1= " Cn.com.kxh.myplugin.View1 ";      Public Final Static String View2=view2. class . GetName ();}

It is possible to invoke the name of the class directly at the time of use.

Attach the result of the above code.

Eclipse Development Plugin (ii)

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.