邋遢居

          我的Java天堂

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            61 Posts :: 2 Stories :: 90 Comments :: 0 Trackbacks

          公告

          啟航吧,我親愛(ài)的Jet!

          常用鏈接

          留言簿(18)

          我參與的團(tuán)隊(duì)

          搜索

          •  

          積分與排名

          • 積分 - 151070
          • 排名 - 409

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          今天有幸碰到關(guān)于Connection以及Router使用的問(wèn)題。覺(jué)得有點(diǎn)意思就把它記了下來(lái)。以背后查。

          看到一個(gè)例子中看到圖 -- 1所示的功能。


          圖-1.JPG

          其中圖-1中的連線是自適應(yīng)的會(huì)保持該線段是最短的(其實(shí)他是使用的ShortestPathConnectionRouter,這個(gè)時(shí)候我還不知道,我是一個(gè)新手大家見(jiàn)笑了)。經(jīng)過(guò)一番調(diào)查以后發(fā)現(xiàn)原來(lái)是在EditPart中的refreshVisuals方法中有如下代碼。

          代碼--1

          if ?(spRouter? == ? null )?{
          ????????????ConnectionLayer?cLayer?
          = ?(ConnectionLayer)?getLayer(LayerConstants.CONNECTION_LAYER);
          ????????????FanRouter?router?
          = ? new ?FanRouter();
          ????????????router.setSeparation(
          30 );
          ????????????spRouter?
          = ? new ?ShortestPathConnectionRouter(getFigure());
          ????????????router.setNextRouter(spRouter);
          ????????????cLayer.setConnectionRouter(router);
          ????????}

          當(dāng)時(shí)就猜啊,他肯定是在給一個(gè)特定的Layer加上一個(gè)什么玩意。然后就通過(guò)這個(gè)玩意來(lái)完成對(duì)于路徑的計(jì)算(其實(shí)這些東西完全是從代碼的字面意思而得到的)。我這些東西加到我的代碼中了。但是我運(yùn)行的效果還是沒(méi)有起作用。他依舊是以前的那幅得行。我抓,抓也沒(méi)有用。就是達(dá)不到我要的效果。

          抱著試一下的想法我打開(kāi)了我的ConnectionEditPart(就是連線的那個(gè)EditPart),發(fā)現(xiàn)在createFigure中我是這么寫(xiě)的。

          代碼 -- 2

          PolylineConnection?connection? = ?(PolylineConnection) super .createFigure();
          ???????
          ????????connection.setConnectionRouter(
          new ?BendpointConnectionRouter(){
          ????????????
          public ? void ?route(Connection?conn)?{
          ????????????????GraphAnimation.recordInitialState(conn);
          ????????????????
          if ?( ! GraphAnimation.playbackState(conn))
          ????????????????????
          super .route(conn);
          ????????????}
          ????????});

          很明顯我在這里給connection賦了一個(gè)ConnectionRouter。最終其效果的是這一個(gè)ConnectionRouter起作用了。

          Md剛掉他就萬(wàn)事大吉了。

          到這里代碼部分其實(shí)就完了。但是他背后的還有一點(diǎn)故事。

          這里有三個(gè)角色:

          1Connection

          2ConnectionAnchor.

          3 ConnectionRouter

          這個(gè)類(lèi)是用來(lái)顯示兩點(diǎn)之間的線段 (Line), 他的起點(diǎn)和終點(diǎn)是通過(guò) ConnectionAnchor 來(lái)定義的。至于他的其他點(diǎn)是通過(guò) ConnectionRouter 計(jì)算設(shè)置的。從這個(gè)地方來(lái)看 ConnectionRouter 是一個(gè)負(fù)責(zé)計(jì)算的工具類(lèi)。這樣完全可以讓所有的 Connection 使用一個(gè) ConnectionRouter 實(shí)例(這就是今天最要記下的部分)。

          gef 中他就為我們提供了這樣的機(jī)制。下面的這段話是 Gef help 中的一句話。他就說(shuō)明了這個(gè)道理。

          A convenient way to share the router with all connections and to place connections above the drawing is to use a ConnectionLayer. The layer has a connection router property which it shares with every child that's a connection. You can update this property and easily change every connection's router at once.

          這個(gè)道理要轉(zhuǎn)換成代碼的話就是代碼 – 1 了。

          這個(gè)東西很簡(jiǎn)單。希望對(duì)像我一樣的新手有點(diǎn)幫助。

          posted on 2006-05-22 22:21 Jet Geng 閱讀(1498) 評(píng)論(2)  編輯  收藏 所屬分類(lèi): Eclipse

          Feedback

          # re: 關(guān)于Connection以及Router 2008-05-27 10:27 stonefox
          Thanks very much for your help.  回復(fù)  更多評(píng)論
            

          # re: 關(guān)于Connection以及Router 2008-05-30 17:51 stonefox
          啊 這個(gè)是我留的 不過(guò)不知道是您啊 呵呵  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 潜山县| 玉环县| 广饶县| 长武县| 科技| 洛隆县| 南部县| 柳林县| 华容县| 木里| 内黄县| 湖南省| 鲁山县| 汕头市| 宁南县| 逊克县| 当阳市| 内乡县| 巴南区| 溆浦县| 修武县| 武宁县| 昌图县| 霸州市| 洱源县| 枣阳市| 河东区| 蒙山县| 栖霞市| 镇原县| 开江县| 察哈| 都匀市| 陈巴尔虎旗| 茌平县| 平罗县| 枣强县| 抚远县| 霍林郭勒市| 桐柏县| 武穴市|