In the class diagram, we can see that some objects have their own references. These references are usually used to express the tree structure, that is, both parent and child nodes are the same class objects. Using GEF to draw such a connection line is generally achieved through a turning point (bendpoint), if your GEF applicationProgramYou cannot use bendpoint. Follow the steps described in the previous article to add support for bendpoint.
In the past, our gefpractice application did not allow the start and end of a connection line to be in the same graph, because this would cause the connection line to shrink into a point and hide it below the graph, the user does not know its existence. At that time, we made the following judgment in the canexecute () method of the createconnectioncommand class:
Public Boolean Canexecute (){
If (Source. Equals (target ))
Return False ;
}
Therefore, we need to delete these two sentences first. Then, the execute () method is used to slightly process the connection itself. The processing method is to add three bendpoints to the connection line at the appropriate position, you can also modify the number and position of bendpoint Based on the desired connection line shape.
Public Void Execute (){
Connection = New Connection (source, target );
If (Source = Target ){
// The start and end points of our connection are both at the center of the rectangle,
// So the two relative dimensions are equal.
Connectionbendpoint = New Connectionbendpoint ();
CBP. setrelativedimensions ( New Dimension ( 0 , - 60 ), New Dimension ( 0 , - 60 ));
Connection. addbendpoint ( 0 , CBP );
Connectionbendpoint cbp2 = New Connectionbendpoint ();
Cbp2.setrelativedimensions ( New Dimension ( 100 , - 60 ), New Dimension ( 100 , - 60 ));
Connection. addbendpoint ( 1 , Cbp2 );
Connectionbendpoint cbp3 = New Connectionbendpoint ();
Cbp3.setrelativedimensions ( New Dimension ( 100 , 0 ), New Dimension ( 100 , 0 ));
Connection. addbendpoint ( 2 , Cbp3 );
}
}
Now, you only need to select the Connection Tool and click two consecutive points on a node to create your own connection, as shown in.
Self-connection
Click here to download the project, which is modified from gefpractice-BP in gef faq 2. the target file extension is. gefpracticesc.