Introduction to multipageeditexceptionbarcontributor

Source: Internet
Author: User

Multipageeditexceptionbarcontributor is the editexceptionbarcontributor Implementation of multipageeditorpart.

Recently, we have reconstructed a GEF project and transformed the previous single-page editor (editorpart) into a multi-page editor (multipageeditorpart). We found a series
The retarget action (CUT, delete, paste, etc.) is invalid.
Debug has a problem. Since it was originally a single-page editor, editexceptionbarcontributor is an actionbarcontributor class inherited from GEF. Because it is multi-page, during the switching process,
Naturally, problems may occur.

The editexceptionbarcontributor of the Multi-page editor should implement multipageeditexceptionbarcontributor, which inherits from editexceptionbarcontributor.
The core is to implement the abstract method setactivepage (ieditorpart activeeditor ).

For the inheritance and usage, see the sample rules to see how the partial code is implemented:
Public class pdeformeditorcontributor extends multipageeditexceptionbarcontributor {

Protected pdeformeditor feditor;

Protected iformpage fpage;

Private saveaction fsaveaction;

Protected revertaction frevertaction;

Private clipboardaction fcutaction;

Private clipboardaction fcopyaction;

Private clipboardaction fpasteaction;

Private hashtable fglobalactions = new hashtable ();

Private isharedimages fsharedimages;

Class globalaction extends action implements iupdate {
Private string ID;

Public globalaction (string ID ){
This. ID = ID;
}

Public void run (){
Feditor. receivmglobalaction (ID );
Updateselectableactions (feditor. getselection ());
}

Public void Update (){
Getactionbars (). updateactionbars ();
}
}

Class clipboardaction extends globalaction {
Public clipboardaction (string ID ){
Super (ID );
Setenabled (false );
}

Public void selectionchanged (iselection selection ){
}

Public Boolean iseditable (){
If (feditor = NULL)
Return false;
Ibasemodel model = feditor. getaggresponmodel ();
Return Model instanceof ieditable? (Ieditable) model). iseditable (): false;
}
}

Class cutaction extends clipboardaction {
Public cutaction (){
Super (actionfactory. Cut. GETID ());
Settext (pdeuimessages. editexceptions_cut );
Setimagedescriptor (getincludimages (). getimagedescriptor (isharedimages. img_tool_cut ));
Setdisabledimagedescriptor (getincludimages (). getimagedescriptor (isharedimages. img_tool_cut_disabled ));
Setactiondefinitionid ("org. Eclipse. UI. Edit. Cut"); // $ NON-NLS-1 $
}

Public void selectionchanged (iselection selection ){
Setenabled (iseditable () & feditor. cancut (selection ));
}
}

Class copyaction extends clipboardaction {
Public copyaction (){
Super (actionfactory. Copy. GETID ());
Settext (pdeuimessages. editexceptions_copy );
Setimagedescriptor (getincludimages (). getimagedescriptor (isharedimages. img_tool_copy ));
Setdisabledimagedescriptor (getincludimages (). getimagedescriptor (isharedimages. img_tool_copy_disabled ));
Setactiondefinitionid ("org. Eclipse. UI. Edit. Copy"); // $ NON-NLS-1 $
}

Public void selectionchanged (iselection selection ){
Setenabled (feditor. cancopy (selection ));
}
}

Class pasteaction extends clipboardaction {
Public pasteaction (){
Super (actionfactory. paste. GETID ());
Settext (pdeuimessages. editexceptions_paste );
Setimagedescriptor (getincludimages (). getimagedescriptor (isharedimages. img_tool_paste ));
Setdisabledimagedescriptor (getincludimages (). getimagedescriptor (isharedimages. img_tool_paste_disabled ));
Setactiondefinitionid ("org. Eclipse. UI. Edit. Paste"); // $ NON-NLS-1 $
}

Public void selectionchanged (iselection selection ){
Setenabled (iseditable () & feditor. canpastefromclipboard ());
}
}

Class saveaction extends action implements iupdate {
Public saveaction (){
}

Public void run (){
If (feditor! = NULL)
Pdeplugin. getactivepage (). saveeditor (feditor, false );
}

Public void Update (){
Setenabled (feditor! = NULL? Feditor. isdirty (): false );
}
}

Class revertaction extends action implements iupdate {
Public revertaction (){
}

Public void run (){
If (feditor! = NULL)
Feditor. dorevert ();
}

Public void Update (){
Setenabled (feditor! = NULL? Feditor. isdirty (): false );
}
}

Public pdeformeditorcontributor (string menuname ){
}

Private void addglobalaction (string ID ){
Globalaction action = new globalaction (ID );
Addglobalaction (ID, action );
}

Private void addglobalaction (string ID, action Action ){
Fglobalactions. Put (ID, action );
Getactionbars (). setglobalactionhandler (ID, action); // associate retarget action
}

Public void addclipboardactions (imenumanager MNG ){
MNG. Add (fcutaction );
MNG. Add (fcopyaction );
MNG. Add (fpasteaction );
MNG. Add (new separator ());
MNG. Add (frevertaction );
}

Public void contextmenuabouttoshow (imenumanager MNG ){
Contextmenuabouttoshow (MNG, true );
}

Public void contextmenuabouttoshow (imenumanager MNG, Boolean addclipboard ){
If (feditor! = NULL)
Updateselectableactions (feditor. getselection ());
If (addclipboard)
Addclipboardactions (MNG );
MNG. Add (fsaveaction );
}
 
Public void contributetomenu (imenumanager mm ){
}

Public void contributetostatusline (istatuslinemanager SLM ){
}

Public void contributetotoolbar (itoolbarmanager TBM ){
}

Public void contributetocoolbar (icoolbarmanager CBM ){
}

Public pdeformeditor geteditor (){
Return feditor;
}

Public iaction getglobalaction (string ID ){
Return (iaction) fglobalactions. Get (ID );
}

Public iaction getsaveaction (){
Return fsaveaction;
}

Public iaction getrevertaction (){
Return frevertaction;
}

Public istatuslinemanager getstatuslinemanager (){
Return getactionbars (). getstatuslinemanager ();
}

Protected void makeactions (){
// Clipboard actions
Fcutaction = new cutaction ();
Fcopyaction = new copyaction ();
Fpasteaction = new pasteaction ();
Addglobalaction (actionfactory. Cut. GETID (), fcutaction); // set retarget Action Association (Hook)
Addglobalaction (actionfactory. Copy. GETID (), fcopyaction );
Addglobalaction (actionfactory. paste. GETID (), fpasteaction );
Addglobalaction (actionfactory. Delete. GETID ());
// Undo/Redo
Addglobalaction (actionfactory. Undo. GETID ());
Addglobalaction (actionfactory. Redo. GETID ());
// Select/find
Addglobalaction (actionfactory. select_all.getid ());
Addglobalaction (actionfactory. Find. GETID ());
// Bookmark
Addglobalaction (ideactionfactory. bookmark. GETID ());
// Save/revert
Fsaveaction = new saveaction ();
Fsaveaction. settext (pdeuimessages. editexceptions_save );
Frevertaction = new revertaction ();
Frevertaction. settext (pdeuimessages. editexceptions_revert );
Addglobalaction (actionfactory. revert. GETID (), frevertaction );
}

Public void setactiveeditor (ieditorpart targeteditor ){
If (targeteditor instanceof pdesourcepage ){
// Fixing the 'Goto line' problem-
// The action is thinking that Source Page
// Is a standalone editor and tries to activate it
// #19361
Pdesourcepage page = (pdesourcepage) targeteditor;
Pdeplugin. getactivepage (). Activate (page. geteditor ());
Return;
}
If (! (Targeteditor instanceof pdeformeditor ))
Return;

Feditor = (pdeformeditor) targeteditor;
Feditor. updateundo (getglobalaction (actionfactory. Undo. GETID (), getglobalaction (actionfactory. Redo. GETID ()));
Setactivepage (feditor. getactiveeditor ());
Updateselectableactions (feditor. getselection ());
}

// The abstract method to be implemented
Public void setactivepage (ieditorpart neweditor ){
If (feditor = NULL)
Return;
Iformpage oldpage = fpage;
Fpage = feditor. getactivepageinstance ();
If (fpage! = NULL ){
Updateactions ();
If (oldpage! = NULL &&! Oldpage. iseditor ()&&! Fpage. iseditor ()){
Getactionbars (). updateactionbars (); // update actionbar
}
}
}

Public void updateactions (){
Fsaveaction. Update ();
Frevertaction. Update ();
}

Public void updateselectableactions (iselection selection ){
If (feditor! = NULL ){
Fcutaction. selectionchanged (selection );
Fcopyaction. selectionchanged (selection );
Fpasteaction. selectionchanged (selection );
}
}

Public ieditexceptionbarcontributor getsourcecontributor (){
Return NULL;
}

Public void Init (iactionbars bars ){
Super. INIT (bars );
Makeactions (); // create actions during initialization
}

Protected isharedimages get1_dimages (){
If (f1_dimages = NULL)
F1_dimages = getpage (). getworkbenchwindow (). getworkbench (). get1_dimages ();
Return fsharedimages;
}
}

Let's look at the implementation in a multi-page editor wizard template:
I made some changes to it.
Public class multipageeditorcontributor extends
Multipageeditexceptionbarcontributor {
Private ieditorpart activeeditorpart;
Private action sampleaction;
 
Public multipageeditorcontributor (){
Super ();
Createactions ();
}

/**
* Returns the action registed with the given text editor.
*/
Protected iaction getaction (itexteditor Editor, string actionid ){
Return (editor = NULL? Null: Editor. getaction (actionid ));
}

// The newly added method defines some actions in myeditor
Protected iaction getaction2 (myeditor Editor, string actionid ){
Return (editor = NULL? Null: Editor. getaction (actionid ));
}

// Mainly refer to this method
Public void setactivepage (ieditorpart part ){
If (activeeditorpart = part)
Return;

Activeeditorpart = part;
If (activeeditorpart instanceof texteditor ){
Iactionbars actionbars = getactionbars ();
If (actionbars! = NULL ){

Itexteditor editor = (Part instanceof itexteditor )? (Itexteditor) Part
: NULL;

Actionbars. setglobalactionhandler (actionfactory. Cut. GETID (),
Getaction (Editor, itexteditexceptionconstants. Cut); // associate retarget action
Getaction (Editor, itexteditexceptionconstants. Find );
Actionbars. updateactionbars ();
}
} Else if (activeeditorpart instanceof myeditor ){
// The transformation below, so that when switching to the myeditor page, the cut menu Association also works.
Iactionbars actionbars = getactionbars ();
If (actionbars! = NULL ){
Myeditor editor = (activeeditorpart instanceof myeditor )? (Myeditor) Part
: NULL;
Actionbars. setglobalactionhandler (actionfactory. Cut. GETID (),
Getaction2 (Editor, itexteditexceptionconstants. Cut ));
Actionbars. updateactionbars ();
}

}
}

Private void createactions (){
Sampleaction = new action (){
Public void run (){
Messagedialog. openinformation (null, "test plug-in ",
"Sample action executed ");
}
};
Sampleaction. settext ("sample action ");
Sampleaction. settooltiptext ("sample action tool tip ");
Sampleaction. setimagedescriptor (platformui. getworkbench ()
. Getsharedimages (). getimagedescriptor (
IDE. nvidimages. img_objs_task_tsk ));
}

// Add a shared menu
Public void contributetomenu (imenumanager manager ){
Imenumanager menu = new menumanager ("editor contributed menu ");
Manager. prependtogroup (iworkbenchactionconstants. mb_additions, menu );
Menu. Add (sampleaction );
}

Public void contributetotoolbar (itoolbarmanager manager ){
Manager. Add (new separator ());
Manager. Add (sampleaction );
}
}

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.