原先我們的GefPractice應用程式是不允許一條連接配接線的起點和終點都是同一個圖形的,因為這樣會導緻連接配接線縮成一個點隐藏在圖形下方,使用者并不知道它的存在。當時我們在CreateConnectionCommand類的canExecute()方法裡進行了如下判斷:
public boolean canExecute() {
if (source.equals(target))
return false;
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZuQ3bk9CXzV2Zh1WSvwVbvNmLzd2bsJmbj5yd3d3Lc9CX6MHc0RHaiojIsJye.gif)
}
是以現在首先要把這兩句删除。然後在execute()方法裡對自身連接配接的這種情況稍做處理,處理的方法是給這條連接配接線在适當位置增加三個Bendpoint,你也可以根據想要的連接配接線形狀修改Bendpoint的數目和位置。
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 cbp = 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);
}
現在使用者隻要選擇連接配接工具,然後在一個節點上連續點兩下就可以建立自身連接配接了,如下圖所示。
自身連接配接