Posted on 2008-01-10 09:46
笑看人生 閱讀(1389)
評論(2) 編輯 收藏 所屬分類:
Java插件開發
這一節主要介紹如何在轉移上增加拐點,要實現這功能,首先要修改轉移的模型,增加一個列表屬性,維護轉移上所有的拐點,轉移模型為實現拐點功能而增加的代碼如下:
public static final String BENDPOINT_PROP = "Transition.BendPoint";
private List bendPoints = new ArrayList();//存放轉移上的所有拐點的坐標
//向轉移上增加拐點(拐點在轉移上是有順序的)
//index 拐點的順序
//point 觀點的坐標

public void addBendPoint(int index,Point point)
{

if(point == null)
{
throw new IllegalArgumentException();
}
bendPoints.add(index,point);
//通知轉移控制器,轉移的拐點屬性變化了,讓它刷新視圖
firePropertyChange(BENDPOINT_PROP, null, point);
}
//從轉移上刪除指定位置的拐點

public void removeBendPoint(int index)
{

if(index<0)
{
throw new IllegalArgumentException();
}
bendPoints.remove(index);
//通知轉移控制器,轉移的拐點屬性變化了,讓它刷新視圖
firePropertyChange(BENDPOINT_PROP, null, null);
}
//得到轉移上的拐點列表

public List getBendPoints()
{
return new ArrayList(bendPoints);
}
要讓轉移控制器能接受增加拐點的請求,應該在轉移的控制器上安裝相應的策略,代碼如下:
//add or remove or move the BendPoint
installEditPolicy(EditPolicy.CONNECTION_BENDPOINTS_ROLE,new TransitionBendpointEditPolicy());
在這里安裝了TransitionBendpointEditPolicy策略,這個策略使的轉移控制器能接受增加,刪除和移動拐點的請求,這個類的代碼如下:
package com.example.workflow.policy;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.BendpointEditPolicy;
import org.eclipse.gef.requests.BendpointRequest;
import com.example.workflow.commands.AbstractBendpointCommand;
import com.example.workflow.commands.CreateBendpointCommand;
import com.example.workflow.commands.DeleteBendpointCommand;
import com.example.workflow.commands.MoveBendpointCommand;
import com.example.workflow.model.Transition;

/** *//**
* EditPolicy for the BendPoint used by this edit part.
*/

public class TransitionBendpointEditPolicy extends BendpointEditPolicy
{

/** *//**新建拐點 */

protected Command getCreateBendpointCommand(BendpointRequest request)
{
Point point = request.getLocation(); //得到拐點的坐標
int index = request.getIndex(); //得到拐點的順序
Transition tran = (Transition)getHost().getModel();//得到要增加拐點的轉移
//創建一個增加拐點的命令,給這命令設置相應的參數
AbstractBendpointCommand cmd = new CreateBendpointCommand();
cmd.setIndex(index);
cmd.setPoint(point);
cmd.setTran(tran);
return cmd;
}

/** *//**刪除拐點 */

protected Command getDeleteBendpointCommand(BendpointRequest request)
{
int index = request.getIndex();//得到拐點的順序
Transition tran = (Transition)getHost().getModel();//得到要刪除拐點的轉移
//創建一個刪除拐點的命令,給這命令設置相應的參數
AbstractBendpointCommand cmd = new DeleteBendpointCommand();
cmd.setIndex(index);
cmd.setTran(tran);
return cmd;
}

/** *//**移動拐點 */

protected Command getMoveBendpointCommand(BendpointRequest request)
{
int index = request.getIndex();//得到拐點的順序
Point point = request.getLocation(); //得到拐點的坐標
Transition tran = (Transition)getHost().getModel();//得到要移動拐點的轉移
//創建一個移動拐點的命令,給這命令設置相應的參數
AbstractBendpointCommand cmd = new MoveBendpointCommand();
cmd.setIndex(index);
cmd.setPoint(point);
cmd.setTran(tran);
return cmd;
}
}
在上面的策略類中,我們覆蓋了父類的三個方法,在這三個方法中,我們分別新建了CreateBendpointCommand,DeleteBendpointCommand,MoveBendpointCommand命令,由于這三個類有一些公共屬性和方法,因此我們進行了抽象,把公共屬性和方法抽象到父類中,這幾個命令的代碼如下:
父類:

package com.example.workflow.commands;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.commands.Command;
import com.example.workflow.model.Transition;

public class AbstractBendpointCommand extends Command
{
protected int index;//拐點的次序
protected Transition tran;//轉移對象
protected Point point;//拐點的絕對位置
//命令的重做方法

public void redo()
{
execute();
}

public void setPoint(Point point)
{
this.point = point;
}

public void setIndex(int index)
{
this.index = index;
}

public void setTran(Transition tran)
{
this.tran = tran;
}
}
新建拐點命令
package com.example.workflow.commands;

/** *//**createabendpointforthetransition*/

publicclass CreateBendpointCommand extends AbstractBendpointCommand
{
//如果轉移對象或者拐點的坐標為空,不能執行這個命令

publicboolean canExecute()
{
return (tran != null) && (point != null);
}
//命令的執行方法,在轉移維護的拐點列表中增加拐點信息,拐點位置作為索引,拐點坐標作//為值

publicvoid execute()
{
tran.addBendPoint(index, point);
}
//命令的撤銷,從在轉移維護的拐點列表中刪除指定位置的拐點

publicvoid undo()
{
tran.removeBendPoint(index);
}
//標志執行命令的名稱

public String getLabel()
{
return"create bendpoint";
}
}
刪除拐點的命令
package com.example.workflow.commands;
import org.eclipse.draw2d.geometry.Point;

/** *//**deleteabendpointforthetransition*/

publicclass DeleteBendpointCommand extends AbstractBendpointCommand
{
//如果轉移對象為空,不能執行這個命令

publicboolean canExecute()
{
return (tran != null);
}
//首先取出要刪除拐點的坐標,以備撤銷操作時使用,然后再從列表中刪除指定位置的拐點

publicvoid execute()
{
point = (Point)tran.getBendPoints().get(index);
tran.removeBendPoint(index);
}
//撤銷操作,在列表的指定位置,增加剛才刪除的拐點

publicvoid undo()
{
tran.addBendPoint(index, point);
}
//標志執行命令的名稱

public String getLabel()
{
return"delete bendpoint";
}
}
移動拐點的命令
package com.example.workflow.commands;
import org.eclipse.draw2d.geometry.Point;

/** *//**deleteabendpointforthetransition*/

publicclass MoveBendpointCommand extends AbstractBendpointCommand
{
//移動之前拐點的坐標
private Point oldPoint = null;
//如果轉移對象或者拐點的坐標為空,不能執行這個命令

publicboolean canExecute()
{
return (tran != null) && (point != null);
}
//首先得到移動之前的拐點的坐標,以備撤銷操作使用
//然后再刪除這個拐點
//最后再增加新的拐點

publicvoid execute()
{
oldPoint = (Point)tran.getBendPoints().get(index);
tran.removeBendPoint(index);
tran.addBendPoint(index, point);
}
//撤銷操作,首先刪除新的拐點,然后再增加上原來拐點

publicvoid undo()
{
tran.removeBendPoint(index);
tran.addBendPoint(index, oldPoint);
}
//標志執行命令的名稱

public String getLabel()
{
return"move bendpoint";
}
}
以前我們新建轉移時,都不用刷新轉移的Figure,現在在轉移上增加了拐點,要想顯示出拐點的效果,就必須覆蓋轉移控制器的refreshVisuals方法,代碼如下:
//因為在轉移模型的addBendPoint和removeBendPoint方法中,都通知轉移控制器
//轉移的BENDPOINT_PROP屬性發生變化,所以在這兒我們當
//變化屬性的名稱是BENDPOINT_PROP,我們刷新視圖

public void propertyChange(PropertyChangeEvent evt)
{
String prop = evt.getPropertyName();

if(Transition.BENDPOINT_PROP.equals(prop))
{
refreshVisuals();
}
}
//刷新轉移對應的視圖,首先得到轉移模型中的拐點列表,然后把每個拐點的坐標
//構造成AbsoluteBendpoint對象,放在一個新的列表中,然后設置轉移圖形的
//路由

protected void refreshVisuals()
{
List constraint = new ArrayList();
List list = getCastedModel().getBendPoints();

for(int i=0;i<list.size();i++)
{
constraint.add(new AbsoluteBendpoint((Point)list.get(i)));
}
getConnectionFigure().setRoutingConstraint(constraint);
}
要想使轉移視圖展示出拐點,我們還必須修改createFigure方法,修改如下:

protected IFigure createFigure()
{
PolylineConnection connection = (PolylineConnection) super.createFigure();
connection.setTargetDecoration(new PolygonDecoration()); // arrow at target endpoint
connection.setConnectionRouter(new BendpointConnectionRouter());//為了顯示拐點,設置轉移的路由
return connection;
}
這樣,我們運行程序,就能看到轉移上增加拐點的效果了
至此,拐點有關的內容,我們就介紹完了,下面我們介紹如何在為編輯器增加大綱視圖。