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

public void addBendPoint(int index,Point point)
{

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

public void removeBendPoint(int index)
{

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

public List getBendPoints()
{
return new ArrayList(bendPoints);
}
要讓轉(zhuǎn)移控制器能接受增加拐點(diǎn)的請(qǐng)求,應(yīng)該在轉(zhuǎn)移的控制器上安裝相應(yīng)的策略,代碼如下:
//add or remove or move the BendPoint
installEditPolicy(EditPolicy.CONNECTION_BENDPOINTS_ROLE,new TransitionBendpointEditPolicy());
在這里安裝了TransitionBendpointEditPolicy策略,這個(gè)策略使的轉(zhuǎn)移控制器能接受增加,刪除和移動(dòng)拐點(diǎn)的請(qǐng)求,這個(gè)類的代碼如下:
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
{

/** *//**新建拐點(diǎn) */

protected Command getCreateBendpointCommand(BendpointRequest request)
{
Point point = request.getLocation(); //得到拐點(diǎn)的坐標(biāo)
int index = request.getIndex(); //得到拐點(diǎn)的順序
Transition tran = (Transition)getHost().getModel();//得到要增加拐點(diǎn)的轉(zhuǎn)移
//創(chuàng)建一個(gè)增加拐點(diǎn)的命令,給這命令設(shè)置相應(yīng)的參數(shù)
AbstractBendpointCommand cmd = new CreateBendpointCommand();
cmd.setIndex(index);
cmd.setPoint(point);
cmd.setTran(tran);
return cmd;
}

/** *//**刪除拐點(diǎn) */

protected Command getDeleteBendpointCommand(BendpointRequest request)
{
int index = request.getIndex();//得到拐點(diǎn)的順序
Transition tran = (Transition)getHost().getModel();//得到要?jiǎng)h除拐點(diǎn)的轉(zhuǎn)移
//創(chuàng)建一個(gè)刪除拐點(diǎn)的命令,給這命令設(shè)置相應(yīng)的參數(shù)
AbstractBendpointCommand cmd = new DeleteBendpointCommand();
cmd.setIndex(index);
cmd.setTran(tran);
return cmd;
}

/** *//**移動(dòng)拐點(diǎn) */

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

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;//拐點(diǎn)的次序
protected Transition tran;//轉(zhuǎn)移對(duì)象
protected Point point;//拐點(diǎn)的絕對(duì)位置
//命令的重做方法

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;
}
}
新建拐點(diǎn)命令
package com.example.workflow.commands;

/** *//**createabendpointforthetransition*/

publicclass CreateBendpointCommand extends AbstractBendpointCommand
{
//如果轉(zhuǎn)移對(duì)象或者拐點(diǎn)的坐標(biāo)為空,不能執(zhí)行這個(gè)命令

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

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

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

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

/** *//**deleteabendpointforthetransition*/

publicclass DeleteBendpointCommand extends AbstractBendpointCommand
{
//如果轉(zhuǎn)移對(duì)象為空,不能執(zhí)行這個(gè)命令

publicboolean canExecute()
{
return (tran != null);
}
//首先取出要?jiǎng)h除拐點(diǎn)的坐標(biāo),以備撤銷操作時(shí)使用,然后再從列表中刪除指定位置的拐點(diǎn)

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

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

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

/** *//**deleteabendpointforthetransition*/

publicclass MoveBendpointCommand extends AbstractBendpointCommand
{
//移動(dòng)之前拐點(diǎn)的坐標(biāo)
private Point oldPoint = null;
//如果轉(zhuǎn)移對(duì)象或者拐點(diǎn)的坐標(biāo)為空,不能執(zhí)行這個(gè)命令

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

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

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

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

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

if(Transition.BENDPOINT_PROP.equals(prop))
{
refreshVisuals();
}
}
//刷新轉(zhuǎn)移對(duì)應(yīng)的視圖,首先得到轉(zhuǎn)移模型中的拐點(diǎn)列表,然后把每個(gè)拐點(diǎn)的坐標(biāo)
//構(gòu)造成AbsoluteBendpoint對(duì)象,放在一個(gè)新的列表中,然后設(shè)置轉(zhuǎn)移圖形的
//路由

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);
}
要想使轉(zhuǎn)移視圖展示出拐點(diǎn),我們還必須修改createFigure方法,修改如下:

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