GEF常见问题4:非矩形图元
现在假设要把原来GefPractice例子里的矩形图元节点换成用椭圆形表示,都需要做哪些改动呢?很显然,首先要把原来继承RectangleFigure的NodeFigure类改为继承Ellipse:
public class NodeFigure extends Ellipse /*RectangleFigure*/{
![](https://www.cnblogs.com/Images/dot.gif)
}
![](https://www.cnblogs.com/Images/dot.gif)
}
这样修改后可以看到编辑器中的图元已经变成椭圆形了。但如果用户点选一个图元,表示选中的边框(选择框)仍然是矩形的,如图1所示:
图1 椭圆形的节点和矩形选择框
如果觉得矩形的选择框不太协调,可以通过覆盖DiagramLayoutEditPolicy的createChildEditPolicy()方法修改。缺省情况下这个方法返回一个ResizableEditPolicy,我们要定义自己的子类(EllipseResizableEditPolicy)来替代它作为返回值。
EllipseResizableEditPolicy里需要覆盖ResizableEditPolicy的两个方法,createSelectionHandles()方法决定“控制柄”(ResizeHandle)和“选择框”(MoveHandle)的相关情况,我们的实现如下:
protected List createSelectionHandles() {
List list = new ArrayList();
//添加选择框
//ResizableHandleKit.addMoveHandle((GraphicalEditPart) getHost(), list);
list.add(new MoveHandle((GraphicalEditPart) getHost()) {
protected void initialize() {
super.initialize();
setBorder(new LineBorder(1) {
public void paint(IFigure figure, Graphics graphics, Insets insets) {
tempRect.setBounds(getPaintRectangle(figure, insets));
if (getWidth() % 2 == 1) {
tempRect.width--;
tempRect.height--;
}
tempRect.shrink(getWidth() / 2, getWidth() / 2);
graphics.setLineWidth(getWidth());
if (getColor() != null)
graphics.setForegroundColor(getColor());
//用椭圆形替代矩形
//graphics.drawRectangle(tempRect);
graphics.drawOval(tempRect);
}
});
}
});
//添加控制柄
ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.EAST);
ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.SOUTH);
ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.WEST);
ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.NORTH);
return list;
}
List list = new ArrayList();
//添加选择框
//ResizableHandleKit.addMoveHandle((GraphicalEditPart) getHost(), list);
list.add(new MoveHandle((GraphicalEditPart) getHost()) {
protected void initialize() {
super.initialize();
setBorder(new LineBorder(1) {
public void paint(IFigure figure, Graphics graphics, Insets insets) {
tempRect.setBounds(getPaintRectangle(figure, insets));
if (getWidth() % 2 == 1) {
tempRect.width--;
tempRect.height--;
}
tempRect.shrink(getWidth() / 2, getWidth() / 2);
graphics.setLineWidth(getWidth());
if (getColor() != null)
graphics.setForegroundColor(getColor());
//用椭圆形替代矩形
//graphics.drawRectangle(tempRect);
graphics.drawOval(tempRect);
}
});
}
});
//添加控制柄
ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.EAST);
ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.SOUTH);
ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.WEST);
ResizableHandleKit.addHandle((GraphicalEditPart) getHost(), list, PositionConstants.NORTH);
return list;
}
createDragSourceFeedbackFigure()方法决定用户拖动图形时,随鼠标移动的半透明图形(即“鬼影”)的形状和颜色,因此我们覆盖这个方法以显示椭圆形的鬼影。
protected IFigure createDragSourceFeedbackFigure() {
//用椭圆替代矩形
//RectangleFigure r = new RectangleFigure();
Ellipse r = new Ellipse();
FigureUtilities.makeGhostShape(r);
r.setLineStyle(Graphics.LINE_DOT);
r.setForegroundColor(ColorConstants.white);
r.setBounds(getInitialFeedbackBounds());
addFeedback(r);
return r;
}
//用椭圆替代矩形
//RectangleFigure r = new RectangleFigure();
Ellipse r = new Ellipse();
FigureUtilities.makeGhostShape(r);
r.setLineStyle(Graphics.LINE_DOT);
r.setForegroundColor(ColorConstants.white);
r.setBounds(getInitialFeedbackBounds());
addFeedback(r);
return r;
}
经过以上这些修改,可以看到选择框和鬼影都是椭圆的了,如图2所示。
图2 与节点形状相同的选择框和鬼影
点此下载工程,此工程修改自GEF应用实例中的GefPractice,目标文件的扩展名改为.gefpracticeel。