From the line connection conversion to the line connection that can increase or decrease any turning point, because new elements need to be added to the model, the model, editpart, and image part must be modified, which is a little cumbersome, but there are actually manyCodeIs generic. This process is divided into the following parts:
1. Add the class corresponding to the turning point in the model (these turning points are called bendpoint in GEF). There must be two dimension types in the class to record the position of the bendpoint relative to the start and end of the connection line. In the connection class, you need to maintain a bendpoint list and provide access methods. Due to the length relationship, only the methods in the connection class are listed.
Public Void Addbendpoint ( Int Index, connectionbendpoint point ){
Getbendpoints (). Add (index, point );
Firepropertychange (prop_bendpoint, Null , Null );
}
/**
* Zhanghao: to send events after two dimension updates, the movebendpointcommand must use this method to set new coordinates,
* Instead of directly using the methods in bendpoint.
*/
Public Void Setbendpointrelativedimensions ( Int Index, dimension D1, dimension D2 ){
Connectionbendpoint = (Connectionbendpoint) getbendpoints (). Get (INDEX );
CBP. setrelativedimensions (D1, D2 );
Firepropertychange (prop_bendpoint, Null , Null );
}
Public VoidRemovebendpoint (IntIndex ){
Getbendpoints (). Remove (INDEX );
Firepropertychange (prop_bendpoint,Null,Null);
}
2. In the original connection mode, because the connection line does not need to be refreshed, make sure that the editpart implements the propertychangelistener interface and overwrites activate () like other editparts () and deactivate () to receive events that change the bendpoint.
Public Void Activate (){
Super . Activate ();
(Connection) GetModel (). addpropertychangelistener ( This );
}
Public void deactivate () {
super . deactivate ();
(connection) GetModel ()). removepropertychangelistener ( This );
}
Public VoidPropertychange (propertychangeevent event ){
String Property=Event. getpropertyname ();
If(Connection. prop_bendpoint.equals (property )){
Refreshbendpoints ();
}
}
Add a onbendpointeditpolicy subclass inherited from bendpointeditpolicy to the editpart of the model connection class. The content of this class will be discussed later.
Protected Void Createeditpolicies (){
Installeditpolicy (editpolicy. connection_bendpoints_role, New Connectionbendpointeditpolicy ());
}
In the case of a straight line connection, we do not need to be responsible for refreshing the connection, but after adding bendpoint, We must refresh the display of the connection line when the bendpoint changes. Therefore, you need to add some code in the refreshreceivals () method of the editpart to convert the bendpoint in the model to the relativebendpoint on the graph.
Protected Void Refreshreceivals (){
Connection Conn = (Connection) GetModel ();
List modelconstraint = Conn. getbendpoints ();
List figureconstraint = New Arraylist ();
For ( Int I = 0 ; I < Modelconstraint. Size (); I ++ ){
Connectionbendpoint = (Connectionbendpoint) modelconstraint
. Get (I );
Relativebendpoint RBP = New Relativebendpoint (getconnectionfigure ());
RBP. setrelativedimensions (White. getfirstrelativedimension (), white
. Getsecondrelativedimension ());
RBP. setweight (I + 1 ) / (( Float ) Modelconstraint. Size () + 1 ));
Figureconstraint. Add (RBP );
}
Getconnectionfigure (). setroutingconstraint (figureconstraint );
}
3. Create three classes: createbendpointcommand, movebendpointcommand, and deletebendpointcommand. You can create a base class bendpointcommand as in the logic example to inherit the class. For example, the content of bendpointcommand is as follows.
Public Class Bendpointcommand Extends Command {
Protected IntIndex;
ProtectedConnection connection;
ProtectedDimension D1, D2;
Public VoidSetconnection (connection ){
This. Connection=Connection;
}
Public VoidRedo (){
Execute ();
}
Public void setrelativedimensions (dimension dim1, dimension dim2) {
d1 = dim1;
D2 = dim2;
}
Public VoidSetindex (IntI ){
Index=I;
}
}
4. Implement the three methods defined by bendpointeditpolicy in connectionbendpointeditpolicy to create, move, and delete bendpoint.
Public Class Connectionbendpointeditpolicy Extends Bendpointeditpolicy {
ProtectedCommand getcreatebendpointcommand (bendpointrequest request ){
Createbendpointcommand cmd= NewCreatebendpointcommand ();
Point P=Request. getlocation ();
Connection Conn=Getconnection ();
Conn. translatetorelative (P );
Point ref1=Getconnection (). getsourceanchor (). getreferencepoint ();
Point ref2=Getconnection (). gettargetanchor (). getreferencepoint ();
Conn. translatetorelative (ref1 );
Conn. translatetorelative (ref2 );
Cmd. setrelativedimensions (P. getdifference (ref1), P. getdifference (ref2 ));
Cmd. setconnection (COM. example. model. Connection) request. getsource ()
. GetModel ());
Cmd. setindex (request. getindex ());
ReturnCMD;
}
Protected Command getdeletebendpointcommand (bendpointrequest request ){
Bendpointcommand cmd = New Deletebendpointcommand ();
Point P = Request. getlocation ();
Cmd. setconnection (COM. example. model. Connection) request. getsource (). GetModel ());
Cmd. setindex (request. getindex ());
Return CMD;
}
ProtectedCommand getmovebendpointcommand (bendpointrequest request ){
Movebendpointcommand cmd= NewMovebendpointcommand ();
Point P=Request. getlocation ();
Connection Conn=Getconnection ();
Conn. translatetorelative (P );
Point ref1=Getconnection (). getsourceanchor (). getreferencepoint ();
Point ref2=Getconnection (). gettargetanchor (). getreferencepoint ();
Conn. translatetorelative (ref1 );
Conn. translatetorelative (ref2 );
Cmd. setrelativedimensions (P. getdifference (ref1), P. getdifference (ref2 ));
Cmd. setconnection (COM. example. model. Connection) request. getsource ()
. GetModel ());
Cmd. setindex (request. getindex ());
ReturnCMD;
}
}
Shows the modified editor.
Transition Line in the editor
Click here to download the project. This project is modified from gefpractice in the GEF application instance, and the extension of the target file is changed to. gefpracticebp.