Applications generated by GMFProgramBy default, the canvas layout uses xylayout. This layout is suitable for UML class diagrams, flowcharts, and other applications, but not for sequence diagrams or tables. Currently, GMF's. gmfgraph model does not provide a place to set the layout for the entire canvas. Therefore, you can only modifyCode. Because GMF Runtime is based on GEF, that is, the code generated by GMF is essentially a GEF application, it is not difficult to find a place to modify according to the previous experience in writing GEF applications.
First, you need to modify the editpart (yourcanvaseditpart) of the canvas. This class is inherited from the diagrameditpart of GMF. Let's take a look at the createdefaulteditpolicies () of the latter () the layout_role's editpolicy Is GMF's xylayouteditpolicy:
Installeditpolicy (editpolicy. layout_role,NewXylayouteditpolicy ());
In GEF, the method for overwriting an existing editpolicy on the same editpart is to use the same role to install the new editpolicy. Therefore, we should install the new editpolicy in createdefaulteditpolicies () of yourcanvaseditpart () use layout_role to install your editpolicy. Here, the flowlayouteditpolicy provided by GEF is easy to use. If you have special requirements, you can implement your editpolicy:
Installeditpolicy (editpolicy. layout_role, New Flowlayouteditpolicy () {@ override Protected Command getcreatecommand (createrequest request ){ Return Null ;} @ Override Protected Command createmovechildcommand (editpart child, editpart after ){ Return Null ;} @ Override Protected Command createaddcommand (editpart child, editpart after ){ Return Null ;} @ Override Protected Boolean Ishorizontal (){ Return True ;}});
We know that the editpolicy installed as layout_role on each editpart in GEF should match the layoutmanager used in the figure corresponding to this editpart. For example, xylayouteditpolicy corresponds to xylayout, while flowlayouteditpolicy matches flowlayout or toolbarlayout, the biggest difference between the two layoutmanagers is that the former allows child graphs to be arranged by line breaks (columns), while the latter can only have one row (column ). Note that flowlayouteditpolicy and toolbarlayout have some problems because the passed layoutmanager parameter is flowlayout in the ishorizontal () method of flowlayouteditpolicy, therefore, we overwrite the ishorizontal () method in the above Code. The returned value is true or false based on your requirements.
Now, to match the modified flowlayouteditpolicy, We need to overwrite the createfigure () method of yourcanvaseditpart. In the parent class diagrameditpart, the figure returned by this method uses freeformlayout, what we need now is toolbarlayout, so:
ProtectedIfigure createfigure () {Figure F=NewFreeformlayer (); toolbarlayout Layout=NewToolbarlayout (); layout. setvertical (False); Layout. setspacing (10); Layout. setstretchminoraxis (True); F. setlayoutmanager (layout); F. setborder (NewMarginborder (15));ReturnF ;}
After these two modifications, your editor should already be as shown in the following figure (some details of the Code may need to be processed ):
GMF has made a lot of packaging on the basis of GEF, such as adding new editpolicies such as semanticeditpolicy and canonicaleditpolicy, and many related commands and editparts, which provide new functions for GMF applications, although it takes some time to master the GMF Runtime API, this allows us to obtain a graphic editor that meets the requirements without large-scale code writing. We recommend that you know it in advance.
Update (2007/07/17): In GMF 2.0, you can set the "layout type" attribute of Figure viewmap In the XXX. gmfgen file to "toolbar_layout" for the same purpose.