Add menus and toolbar to view of Eclipse plug-in

Source: Internet
Author: User

Each view in eclipse has its own menu and toolbar. View deals with these items through its own iviewsite object. To be exact, it is managed through the iactionbars object of this iviewsite object, and the actionbars object is responsible for menus, toolbar, and status bar.

A typical view (inherited from org. Eclipse. UI. Part. viewpart)CodeThe structure will look like this. As an example, suppose we have three functional items: open, remove, And reload. Our view is a simple table named tableviewer, which displays a list of some entries, you can select multiple options:

Tableviewer tvresult; openaction; removeaction; reloadaction; Public Void Createpartcontrol (composite parent ){

// Create View Interface


// Create menu

Createactions ();

Createmenu ();

Createcontextmenu ();

Createmedilbar ();

Hookglobalactions ();

}

Createactions () is a necessary iaction object, which can be used in menus and toolbar; createmenu () the function is to put the newly created iaction object into the view-related menumanager. As mentioned above, menumanager can use getviewsite (). getactionbars (). the getmenumanager () method is used to obtain the context menu, while the createmedilbar () method places the same object in the toolbar. The method to obtain the toolbar is similar to that of the menu. The createcontextmenu () method is used to create the context menu triggered by right-clicking, the method is to create a new menumanager, then create a menu object, and then associate the menu object with the control; hookglobalactions () the function of iaction is to associate the iaction object with the system menu (instead of the View menu) to achieve different responses of the same menu item to different views. The specific code is as follows:

Package net. SF. Solo. actions;


Import java. Io. ioexception;

Import java. util. iterator;

Import net. SF. Solo. model. iinstance;

Import org. Eclipse. jface. action. Action;

Import org. Eclipse. jface. Viewers. iselectionchangedlistener;

Import org. Eclipse. jface. Viewers. istructuredselection;

Import org. Eclipse. jface. Viewers. selectionchangedevent;


Public class openaction extends action implements iselectionchangedlistener {


Istructuredselection selection;


Public openaction (){

Setenabled ( False );

}



Public Void Run (){

For (Iterator ITER = Selection. iterator (); ITER. hasnext ();){

Iinstance INS = (Iinstance) ITER. Next ();

Try {

// Todo only in Windows can do this.

Runtime.getruntime(cmd.exe C ( " CMD/E: On/C start "   + INS. getreferenceurl ());

} Catch (Ioexception e ){

E. printstacktrace ();

}

}

}


Public Void Selectionchanged (selectionchangedevent event ){

Selection = (Istructuredselection) event. getselection ();

Setenabled (selection. Size () >   0 );

}


Public String gettext (){

Return   " & Open in browser " ;

}

}

The above is an example of iaction, which is used to open the user's selected entries in the browser at the time of triggering. This class also implements iselectionchangeaction, so that you can change yourself to unavailable when you have not selected any items. Of course, you need to add it as a listener to the listener list of a list object, as shown in the following code:

Private Void Createactions (){

Openaction =   New Openaction ();

Removeaction =   New Removeaction (tvresult );

Reloadaction =   New Reloadaction (tvresult );

Tvresult. addselectionchangedlistener (openaction );

Tvresult. addselectionchangedlistener (removeaction );

Tvresult. addselectionchangedlistener (reloadaction );

}

Note that the last three sentences are added to the listener list. Some iactions need to change the behavior of the Monitored object (such as a tableviewer), so you must pass the object as a parameter to it. The following code adds an iaction object to the menu:

Private Void Createmenu (){

Imenumanager Mgr = Getviewsite (). getactionbars (). getmenumanager ();

Mgr. Add (openaction );

Mgr. Add (removeaction );

Mgr. Add (reloadaction );

}

The code for adding an iaction object to a toolbar is almost identical, but the first sentence is different:

Private Void Createmenu (){

Itoolbarmanager Mgr = Getviewsite (). getactionbars (). gettoolbarmanager ();

Mgr. Add (openaction );

Mgr. Add (removeaction );

Mgr. Add (reloadaction );

}

The context menu seems a little troublesome, because the view does not have anything like "popupmenumanager", so we can only create it manually:

Private Void Createcontextmenu (){

Menumanager Mgr =   New Menumanager ();

Mgr. setremoveallwhenshown ( True );

Mgr. addmenulistener ( New Imenulistener (){

Public Void Menuabouttoshow (imenumanager manager ){

Fillcontextmenu (manager );

}

});

Menu = Mgr. createcontextmenu (tvresult. getcontrol ());

Tvresult. getcontrol (). setmenu (menu );

Getsite (). registercontextmenu (MGR, tvresult );

}

This menumanager and we get the same class (but not the same instance) through getmenumanager () in createmenu (), setremoveallwhenshown (true) the function is to clear the previously displayed menu items. When the menu event is triggered, refill (fillcontextmenu), so if you do not set removeallwhenshow to true, right-click each point and you will see more than doubled menu items. Menu is a SWT control (menumanager and toolbarmanager are all in jface, and jface packages SWT a layer). You can use menumanager to create a menu object, then we can use the setmenu method of the table to associate the table control with the menu control.

). Then, where will the newly added menu items appear in the context menu? at the top or bottom, or ......? Therefore, you need to specify the following when fillcontextmenu:

Protected Void Fillcontextmenu (imenumanager manager ){

Manager. Add (openaction );

Manager. Add (removeaction );

Manager. Add (reloadaction );

Manager. Add ( New Groupmarker (iworkbenchactionconstants. mb_additions ));


}

There is nothing special about the first three sentences. The last sentence is to specify the "add point" we mentioned above. In this way, you can store the subsequent menus wherever you want.

Finally, eclipse Workbench provides some common system menu items, as shown below:

Public static final string [] global_actions = {

Undo,

Redo,

Cut,

Copy,

Paste,

Print,

Delete,

Find,

Select_all,

Bookmark

};

When your focus is on different views or editors, the same system menu item has different effects. For example, in the text editor, delete is used to delete the selected text. In your view, delete is used to delete the table entries selected by the user, which is exactly the removeaction function. So you need to put your iaction object and System menu together:

Private Void Hookglobalactions (){ Iactionbars bars = getviewsite (). getactionbars ();

Bars. setglobalactionhandler (iworkbenchactionconstants. Delete, removeaction );

}

Note: You need to choose a system menu item with similar semantics to hook up, otherwise it will cause user problems. For example, you have to implement copy as openaction. When you select the Copy command in the System menu, you thought that the selected entries will be copied to the clipboard, But you opened these entries for others, funny.

Well, the menu is basically like this. It can be seen that the eclipse workbench does provide us with a lot of convenience, especially if you can flexibly use plugin for definition, not only can save a lot of code, it also keeps us informed of the system. Therefore, the popularity of RCP is not unreasonable.

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.