GEF Getting Started (Six, Add Menu and Toolbus)

xiaoxiao2021-03-05  21

I found that once a little bit of GEF, it will be naturally attracted by it. Not only because of its graphical interface, but more importantly, the most complex and subtle problem in the UI, in the GEF design, there is no thoughtful consideration and solved in an appropriate pattern, when you understand these, completely These solutions can be converted to solve design problems in other areas. Last year, Huang Lao was ending after an GEF project, still did not give up on its continued research, and now uses spare time to develop GEF-based SWT / JFACE enhancement package, Eclipse and GEF's charm can be seen. I believe that in the next two years, due to the maturity of RCP / GEF, Java Standalone app must develop, and play an important role in the part of the B / S mode difficult to implement.

The subject of this article is to implement menu features, because Eclipse's scalable design, add menu to the GEF application, so I first introduced some concepts about menus in Eclipse, and then describe how to describe how to describe how to in Gef Add menus, toolbars, and context menu.

We know that Eclipse itself is just a platform (Platform), the user does not work directly, its role is to provide an underlying environment for those who provide actual functions, all components construct interface and use resources through platform specified. . In Eclipse, these components are called Plugins, such as Java Development Environment (JDT), ANT Support, CVS Clients, and Help Systems, etc. are all plugins, because we downloaded from eclipse.org. Eclipse itself already contains These common plugins, so there is no need for additional installation, as if Windows itself already contains notepad, drawing and other tools. If we need new features, we will install them on the Eclipse platform by downloading installation or online update, as common as XML editing, Properties file editor, J2EE development support, etc., including the GEF development package is also such plug-ins. . The plug-in is generally installed in the plugins subdirectory of the Eclipse installation directory, or you can also install it in other locations.

An excellent thing in the Eclipse platform is that such a large plug-in can be integrated into the same environment. To know that each plugin may have an editor, view, menu, toolbar, file association, etc. Complex element, It is easy to make peace to share. To this end, Eclipse provides a series of mechanisms to resolve all kinds of problems. Due to space limitations, you can only simply talk about the part of the menu and toolbar, please refer to Eclipse randomly available plugin development help documentation.

In most cases, we say that developing an Eclipse-based application means developing an Eclipse plugin (plugin), each plugin in Eclipse has a file named plugin.xml to define various elements in the plugin, For example, all editors of this plugin, which views, etc. Use the menu and toolbar in the view, please refer to the previous post, this article only describes the editor, because the GEF application is mostly based on the editor.

Figure 1 Several components of the Eclipse platform

First, you must introduce the concept of Retarget Action, which is an action with a certain semantic but no practical function. Its only role is to occupy an item on the main menu or main toolbar, the editor can have an actual function. Map to a Retarget Action, when this editor is activated, the Retarget Action on the main menu / toolbar will have the function of that action. For example, Eclipse provides iWorkBenChatConstants.copy this Retarget Action, its text and icons are pre-defined, assuming that our editor requires a "copy node to the clipboard" function because "copy node" and "copy "The semantics of the two words are very similar, so you can create a copyAdition (Extends action) with the actual function, then call the following code to implement the following mappings: IActionbars.SetglobalActionHandler (iActionBarstients.copy

When this editor is activated, Eclipse checks this mapping, allowing the COPY item to be available, and when the user performs the operation defined in the CopyNodection, the code in the run () method. The purpose of Eclipse introducing Retarget Action is to minimize the reconstruction consumption of the main menu / toolbar, and facilitate user use consistency. In the Gef application, because multiple views (such as editing views and outline views), even if there is only one view, it is necessary to extend the possibility of expansion into multiple, and each view should be able to complete Operation, for example, in the view of the tree structure, you should also delete the selected node like editing a view, so the general operation should be established in a manner mapped to retarget action.

Main menu / main toolbar

Unlike the view window, the editor does not have its own menu bar and toolbar, and its menu can only be added to the main menu. Since a editor can have multiple instances, they should have the same menu and toolbar, so when a editor is defined in Plugin.xml,

The element has a ContributorClass property that is a full name of the class that implements the IEDITORActionBarContributor interface, which can be referred to as "Menu Toolbar Address". In the Address, you can add yours needed to the main menu / main toolbar of Eclipse. Or use our project as an example, it requires that each operation can be revoked / red, and each element on the canvas can be deleted, and each node element can be set to high, medium, low, low, low three levels . So we have to add these six retardient actions, the following is part of the DiagramActionBarControlTributor class:

Public Class DiagramActionBarcontributor Extends ActionBarContributor {Protected

Void

BuildActions () {addretarget (

New

UndoreTargetAction (); addretargetAction

New

RedoreTargetAction ()); AddRetargetAction

New

Deleteretargetivity ()); AddRetargetAction

New

Priority RetargetAction (iConstants.Priority_High); AddRetarget (New

Priority RetargetAction (iConstants.Priority_Medium); AddRetargetAction

New

Priority RetargetAction (iconStants.Priority_low);} protected

Void

DeclareglobalActionKeys () {} public

Void

ContributeToolbar (Itoolbarmanager Toolbarmanager) {...} public

Void

ContributeTomenu (Imenumanager Menumanager) {Imenumanager Mgr

=

New

Menumanager

"

& Node

"

,

"

Node

"

); MenuManager.insertAfter (IWorkbenchActionConstants.M_EDIT, mgr); mgr.add (getAction (IConstants.ACTION_MARK_PRIORITY_HIGH)); mgr.add (getAction (IConstants.ACTION_MARK_PRIORITY_MEDIUM)); mgr.add (getAction (IConstants.ACTION_MARK_PRIORITY_LOW));} }

It can be seen that the DiagramActionBarcontributor class inherits the class ActionBarContributor provided by GEF, which is an abstract class that implements the IeditOrActionBarContributor interface. The buildActions () method is used to create Retarget actions that are to add to the main menu / toolbar, and register them into a specialized registry; and the code in the ContributeTomenu () method adds these Retarget Actions to the main menu bar. Using iMenumanager.InsertAfter () is to let the newly added menu appear behind the specified system menu, the CONTRIBUTETOOLBAR () is the code added to the main toolbar.

Figure 2 Add an action on the main menu and main toolbar

GEF maintains two lists in RetargetActions and GlobalActionKutors in ActionBarContributor, where the latter is a retaTARGET Action ID, and the addretargetAction () method will add a retaret action to both, for existing retarget actions, we should The AddGlobalActionKey () method calls the addGlobalActionKey () method, when an editor is activated, the ID of those IDs in GlobalActionKeys has the same ID value (with actual functions) Action will be contacted to Retarget Action corresponding to the ID. Therefore, there is no need to explicitly call the setglobalActionHandler () method, and the mapping can be implemented as long as the id's ID is the same.

GEF has built-in revoked / redo and deletes these three operations Retarget Action (because it is too common), their IDs are iWorkbenChthctionConstants.undo, Redo and Delete, so there is no problem. And set the priority this Action has a ready-made Retarget action available, so we must first define a priorityretAction, as follows (no international processing): public class priorityretaTAction Extends labelretarget {public Priority RetargetAction

int

PRIORITY) {super

NULL

,

NULL

);

Switch

PRIORITY {

Case

IconStants.Priority_High: setId (iconstants.action_mark_priority_high); setText

"

High priority

"

);

Break

;

Case

IconStants.Priority_Medium: setId (iconstants.action_mark_priority_medium); setText

"

Medium priority

"

);

Break

;

Case

IConstants.Priority_low: setId (iconstants.action_mark_priority_low); setText

"

Low priority

"

);

Break

;

DEFAULT

:

Break

}}}

Next, it is necessary to build Actions with actual features in the CBMeditor's CreateActions (), which should be a subclass of SelectionAction (GEF) because we need to get the currently selected node. The code will be given later, the code of the CreateActions () method of the editor is as follows:

protected

Void

CreateActions () {super.createActions ();

//

high priority

IAction action

=

New

PriorityAction

THIS

, Iconstants.priority_high; action.setid (iconstants.action_mark_priority_high); getActionregistry (); getSelectionAction (). Add (action.getid ());

//

Medium priority

Action

=

New

PriorityAction

THIS

, Iconstants.priority_medium; action.setid (iconstants.action_mark_priority_medium); getActionregistry (). Registeraction; getSelectionActions (). Add (action.getid ());

//

Low priority

Action

=

New

PriorityAction

THIS

, Iconstants.priority_low; action.setid (iconstants.action_mark_priority_low); getActionregistry (); registeraction (); getSelectionActionActions (); add (action.getid ());} Please note that Id in this method Both correspondence to the id of Retarget Action, otherwise it will not correspond to Retarget Actions in main menu and main toolbar. You may have discovered, here we only create three Actions of setting priority without establishing an action responsible for undo / redo and delete. In fact, GEF has created these common Actions in this class's parent Class, including undo / redo, all, save, print, etc., so as long as you don't forget to call super.createActions ().

The Undo / Redo / Delete, which is provided by GEF, will automatically determine if you are available according to the currently selected editpart (s), and our defined Action is calculated in the Action's CalculateEnabled () method. In addition, in order to achieve the cancellation / red-done function, the general action is executed to create a Command, add the latter to the CommandStack, then execute this Command object, instead of writing the execution code in the run () method of the execution code. Here is some of our setup priority priorityAction, which inherits from SelectionAction:

public

Void

Run () {execute (createCommand ());} private command createCommand () {list objects

=

GetSelectedObjects ();

IF

(Objects.Isempty ())

Return

NULL

;

for

(Iterator ITER

=

Objects.iterator (); it.hasnext ();) {Object Obj

=

iTer.next ();

IF

(("

!

(Obj

INSTANCEOF

NodePart))

&&&&

(

!

(Obj

INSTANCEOF

NodetreeEditpart)))))))))))))))))

Return

NULL

(} CompoundCommand CompoundCMD

=

New

CompoundCommand (GefMessages.DeleteAction_ActionDeleteCommandName);

for

(

int

i

=

0

I

<

Objects.size (); i

) {EditPart Object

=

(Editpart) Objects.get (i); ChangePriorityCommand CMD

=

New

ChangePriorityCommand (); cmd.setnode ((node) object.getmodel ()); cmd.setnewpriority (priority); CompoundCmd.Add (cmd);}

Return

Compoundcmd;} protected

Boolean

CalculateEnabled () {Command CMD

=

CreateCommand ();

IF

(cmd

==

NULL

)

Return

False

;

Return

Cmd.canexecute ();

Because the user is allowed to set priority to multiple selected nodes, we have created multiple Command objects in this action, and add them to a CompoundCommand object, the benefit is to cancel the / red, can also be one-time Complete, not a node.

Context menu

It is very convenient to implement the context menu that implements the right-click pop-up, as long as you write a custom class of inheritance org.eclipse.gef. ContextMenuProvider, write the code to add menu items in its buildContextMenu () method, then in the editor Call GraphicalViewer. SetContextMenu (). GEF pre-defines some menu items used in advance to distinguish between different purposes, each group appears as a partition line, such as the undo group, a COPY group, and a Print group, and more. If your menu item is not suitable in any group, you can put it in the Others group, of course, if your menu items are many, you can define new components to classify.

Figure 3 Context menu

Suppose we have to implement the context menu as shown above, and have created and in ActionRegistry, completed in Editor's CreateActions () method), and ContextMenuProvider should write like this:

public class CbmEditorContextMenuProvider extends ContextMenuProvider {private ActionRegistry actionRegistry; public CbmEditorContextMenuProvider (EditPartViewer viewer, ActionRegistry registry) {super (viewer); actionRegistry

=

Registry;} public

Void

BuildContextMenu (Imenumanager Menu) {

//

Add Standard Action Groups to the menu

GefactionConstants.addStandardActionGroups (MENU);

//

Add actions to the menu

menu.appendToGroup (GEFActionConstants.GROUP_UNDO, getAction (ActionFactory.UNDO.getId ())); menu.appendToGroup (GEFActionConstants.GROUP_UNDO, getAction (ActionFactory.REDO.getId ())); menu.appendToGroup (GEFActionConstants.GROUP_EDIT, getAction ( ActionFactory.DELETE.getId ())); menu.appendToGroup (GEFActionConstants.GROUP_REST, getAction (IConstants.ACTION_MARK_PRIORITY_HIGH)); menu.appendToGroup (GEFActionConstants.GROUP_REST, getAction (IConstants.ACTION_MARK_PRIORITY_MEDIUM)); menu.appendToGroup (GEFActionConstants.GROUP_REST, GetAction (iconstants.action_mark_priority_low);} private IAction getAction (String ActionID) {Return

ActionRegistry.getAction (ActionID);}}

Note that the first sentence in the buildContextMenu () is to create the default group. If you do not ignore the statement behind this step, you can see how the GEF is in groups through this method. What groups are there. Let the editor use this class code to be written in the ConfigureGraphicalViewer () method.

Because by the way, some basic concepts of Eclipse are introduced, plus the code is more, so this post looks relatively long, in fact, by viewing GEF's implementation of the built-in undo / redo, it will understand the use of menus.

转载请注明原文地址:https://www.9cbs.com/read-38739.html

New Post(0)