Router路由

Router路由:設定線的軌跡,在Connection接口中定義了添加路由的方式,所以實現了Connection接口的Figure都可以添加路由。另外在ConnectionLayer中有一個方法,是爲此Layer下的所有實現了Connection接口的Figure添加一個路由。當然,我們還可以自定義一些Figure添加路由。

?

ConnectionRouter接口:路由的基礎接口,所有的路由必須實現此接口。

?

以下是draw2D中常用的幾個路由:

1.NullConnectionRouter:ConnectionRouter接口默認提供的實現,直線路由,其實就是不設定任何規則。

?

2<span style="color: #333333;">.</span><span style="color: #333333; font-family: Arial; font-size: 14px; line-height: 26px;">[size=medium;][size=small;]<span style="color: #ff0000;">AutomaticRouter[/size],這是一個抽象類,要用的話得用它的實現類[/size]</span><a style="color: #336699; text-decoration: none;" title="class in org.eclipse.draw2d" href="http://writeblog.csdn.net/org/eclipse/draw2d/FanRouter.html">[size=medium; color: #ff0000;][size=small;]FanRouter[/size][/size]</a>[size=medium;][size=small;]?,這個路由會判斷兩結點間是否有重複線,有的話會把重複的線分開,? 變成兩條線~~[/size][/size]</span>

?

<span style="color: #333333; font-family: Arial; line-height: 26px; font-size: small;">3.<span style="color: #ff0000;">BendpointConnectionRouter</span>,[b]<span style="color: #000000;">增加連接線的路由點 ,讓你的連線有路由點,從而可以方便的改變方向</span>[/b]</span>

?

4.<span style="color: #333333; font-family: Arial; line-height: 26px; font-size: small;">?<span style="color: #ff0000;">ManhattanConnectionRouter</span>,這個路由能保證畫出來的線是垂直或水平的,如果你需要這樣的效果就可以設置成這個路由。</span>

?

<span style="color: #333333; font-family: Arial; line-height: 26px; font-size: small;">5.?<span style="color: #ff0000;">ShortestPathConnectionRouter</span>?最短路徑路由器,這個路由還是比較強大的,可以方便的避免連線穿越結點,它會以最近的路徑繞開其它結點,完成連線。</span>

?

?

自定義路由的實現思路:

<span style="font-family: arial, nsimsun, sans-serif;">顯然我們無法憑空的計算出線路的走向,一條連線的具體路線和很多因素有關,比如錨點、圖形的位置和大小,圖形之間的相互關係,等等。所以我們需要能夠訪問到這些必須的信息,在Connection接口中,我們有getTargetAnchor()和getSourceAnchor()可以讓我們得到錨點,而在ConnectionAnchor接口中(參見本系列第一部分),我們有getOwner()這樣的方法,可以得到圖形。這些必要的方法爲我們實現路由器提供了可能。</span>

?


Object getConstraint(Connection connection);

void setConstraint(Connection connection, Object constraint);

void invalidate(Connection connection);

void route(Connection connection);

void remove(Connection connection);


?

<span style="font-family: arial, nsimsun, sans-serif;">setConstraint和getConstraint用來設置/得到連接上的Constraint(約束),所謂Constraint是指加在某個連線上的一些參數。我們可以看到constraint是一個Object類型,因爲不同的路由器可能對constraint有不同的要求,對於ShortestPathConnectionRouter來說,constraint需要是一個List對象,裏面包含了所有的轉折點。</span>

<span style="font-family: arial, nsimsun, sans-serif;">invalidate方法可以將一個連線置爲無效,這樣在下一次佈局操作時,無效的連接將被重新路由。remove方法是將連線從路由器中刪除,也就是路由器不會再負責這條連線的佈局,一般只有在刪除一條連線的時候纔會調用到,我們可以在裏面做一些清除工作,比如釋放和連線相關的cache。route方法是路由操作真正發生的地方,我們一般只需要實現route方法就可以了,如果你還想做一些其他的操作,可以考慮實現其他方法。同樣,一般是不推薦直接實現ConnectionRouter接口的,我們可以繼承AbstractRouter類,這個類提供了一些簡單的或者空的實現,還提供了兩個額外的方法getStartPoint()和getEndPoint()方便我們得到連線的兩個端點。</span>

?


public void route(Connection conn) {
// 清空連線的所有點
PointList points = conn.getPoints();
points.removeAllPoints();

// 得到目標和源參考點
Point sourceRef = conn.getSourceAnchor().getReferencePoint();
Point targetRef = conn.getTargetAnchor().getReferencePoint();
A_POINT.setLocation(sourceRef.x, targetRef.y);

// 得到起始點和結束點
Point startPoint = conn.getSourceAnchor().getLocation(A_POINT);
Point endPoint = conn.getTargetAnchor().getLocation(A_POINT);

// 添加起始點
A_POINT.setLocation(startPoint);
conn.translateToRelative(A_POINT);
points.addPoint(A_POINT);

// 添加轉折點
A_POINT.setLocation(sourceRef.x, targetRef.y);
conn.translateToRelative(A_POINT);
points.addPoint(A_POINT);

// 添加結束點
A_POINT.setLocation(endPoint);
conn.translateToRelative(A_POINT);
points.addPoint(A_POINT);

// 設置連線經過的所有點
conn.setPoints(points);
}

?
<span style="font-family: arial, nsimsun, sans-serif;">一條連線實際上是通過一系列的點來描述的,而route方法的實際任務也就是計算出這些點的位置。所以我們一開始就得到了這條連線的點序列(PointList對象),然後清空它,重新計算這些點。在我們這個路由器的設計裏,一條連線由三個點組成:分別是起始點,轉折點和結束點,它們構成了兩條垂直的直線。起始點和結束點(也就是錨點)我們都已經瞭解如何得到了,中間的轉折點,也很容易得出,我們就不解釋了。要指出的是,我們需要把它們的座標轉換爲相對座標再添加,同時在添加完成之後,我們還需要調用setPoints()方法,這樣纔會生效。</span>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章