Draw2d入门系列(四、 UML的实现)

Draw2d可以通过图形和连线表示图的关系,本节将用实例介绍如何通过图形和连线绘制UML的关系图
表关系实例中药包苦熬表和列的UML图形(Figure),其中表和列又包含属性和方法子图形。
表和列之间要建立一个以对多的关系连线
package com.heming.table.editor.figure;

import org.eclipse.draw2d.AbstractBorder;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.draw2d.geometry.Insets;

/**
* 方法和属性的图形
*
* @author 何明
*
*/
public class CompartmentFigure extends Figure {

public CompartmentFigure() {

ToolbarLayout layout = new ToolbarLayout();
layout.setMinorAlignment(ToolbarLayout.ALIGN_TOPLEFT);
layout.setStretchMinorAxis(false);
// 设置子图形的间距
layout.setSpacing(2);
// 设置布局管理器
setLayoutManager(layout);
// 设置图形的边框
setBorder(new CompartmentFigureBorder());

}

// 图形的边框类
public class CompartmentFigureBorder extends AbstractBorder {
public Insets getInsets(IFigure figure) {
return new Insets(1, 0, 0, 0);
}

// 重画图形的边框
public void paint(IFigure figure, Graphics graphics, Insets insets) {
graphics.drawLine(getPaintRectangle(figure, insets).getTopLeft(),
tempRect.getTopRight());
}
}
}

package com.heming.table.editor.figure;

import org.eclipse.draw2d.ColorConstants;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LineBorder;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.swt.graphics.Color;

/**
* UMLClassFigure是UML Class的图形,每个UMLClassFigure包含两个CompartmentFigure
* @author 何明
*
*/
public class UMLClassFigure extends Figure {

public static Color classColor = new Color(null,255,255,206);
//属性图形
private CompartmentFigure attributeFigure = new CompartmentFigure();
//子方法图形
private CompartmentFigure methodFigure = new CompartmentFigure();


public UMLClassFigure(Label name){

ToolbarLayout layout = new ToolbarLayout();
//设置布局管理器
setLayoutManager(layout);
//设置图形的边框
setBorder(new LineBorder(ColorConstants.black,1));
//设置背景色
setBackgroundColor(classColor);
//设置图形是否透明
setOpaque(true);

//添加名字的标签
add(name);
//添加属性的图形
add(attributeFigure);
//添加方法的图形
add(methodFigure);

}

/**
* 得到属性图形
* @return
*/
public CompartmentFigure getAttributesCompartment(){

return attributeFigure;

}

/**
* 得到子方法图形
* @return
*/
public CompartmentFigure getMethodsCompartment(){

return methodFigure;

}

}

package com.heming.table.editor.figure;

import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ConnectionEndpointLocator;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.LightweightSystem;
import org.eclipse.draw2d.PolygonDecoration;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.XYLayout;
import org.eclipse.draw2d.geometry.PointList;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
* 连线及测试类
*
* @author 何明
*
*/
public class UMLClassFigureTest {

public static void main(String args[]) {

Display d = new Display();
final Shell shell = new Shell(d);
shell.setSize(400, 400);
shell.setText("UMLClassFigure Test");

LightweightSystem lws = new LightweightSystem(shell);
// 新建底层的图形
Figure contents = new Figure();
// 新建底层图的布局方式为XYLaout
XYLayout contentsLayout = new XYLayout();
contents.setLayoutManager(contentsLayout);

Font classFont = new Font(null, "Arial", 12, SWT.BOLD);
// 添加表的现实文本及显示的图标
Label classLabel1 = new Label("Table", new Image(d,
UMLClassFigureTest.class.getResourceAsStream("class_obj.gif")));
classLabel1.setFont(classFont);

Label classLabel2 = new Label("Column", new Image(d,
UMLClassFigureTest.class.getResourceAsStream("class_obj.gif")));
classLabel2.setFont(classFont);

// 新建表和列的图形
final UMLClassFigure classFigure = new UMLClassFigure(classLabel1);
final UMLClassFigure classFigure2 = new UMLClassFigure(classLabel2);

// 添加属性的显示文本及显示图标
Label attribute1 = new Label("columns:Column[]", new Image(d,
UMLClassFigure.class
.getResourceAsStream("field_private_obj.gif")));
Label attribute2 = new Label("rows:Row[]", new Image(d,
UMLClassFigure.class
.getResourceAsStream("field_private_obj.gif")));
Label attribute3 = new Label("columnID:int", new Image(d,
UMLClassFigure.class
.getResourceAsStream("field_private_obj.gif")));
Label attribute4 = new Label("items:List", new Image(d,
UMLClassFigure.class
.getResourceAsStream("field_private_obj.gif")));

// 添加方法和属性的标签
classFigure.getAttributesCompartment().add(attribute1);
classFigure.getAttributesCompartment().add(attribute2);
classFigure2.getAttributesCompartment().add(attribute3);
classFigure2.getAttributesCompartment().add(attribute4);

Label method1 = new Label("getColumns():Column[]", new Image(d,
UMLClassFigure.class.getResourceAsStream("methpub_obj.gif")));
Label method2 = new Label("getRows:Row[]", new Image(d,
UMLClassFigure.class.getResourceAsStream("methpub_obj.gif")));
Label method3 = new Label("getColumnID():int", new Image(d,
UMLClassFigure.class.getResourceAsStream("methpub_obj.gif")));
Label method4 = new Label("getItems():List]", new Image(d,
UMLClassFigure.class.getResourceAsStream("methpub_obj.gif")));

classFigure.getMethodsCompartment().add(method1);
classFigure.getMethodsCompartment().add(method2);
classFigure2.getMethodsCompartment().add(method3);
classFigure2.getMethodsCompartment().add(method4);

contentsLayout
.setConstraint(classFigure, new Rectangle(10, 10, -1, -1));
contentsLayout.setConstraint(classFigure2, new Rectangle(200, 200, -1,
-1));

// 新建连线
PolylineConnection c = new PolylineConnection();
// 添加图形的锚点
ChopboxAnchor sourceAnchor = new ChopboxAnchor(classFigure);
ChopboxAnchor targetAnchor = new ChopboxAnchor(classFigure2);
c.setSourceAnchor(sourceAnchor);
c.setTargetAnchor(targetAnchor);

// 添加连线的装饰器
PolygonDecoration decoration = new PolygonDecoration();
PointList decorationPointList = new PointList();
decorationPointList.addPoint(0, 0);
decorationPointList.addPoint(-2, 2);
decorationPointList.addPoint(-4, 0);
decorationPointList.addPoint(-2, -2);

decoration.setTemplate(decorationPointList);
c.setSourceDecoration(decoration);

// 添加连线的Locator
ConnectionEndpointLocator targetEndpointLocator = new ConnectionEndpointLocator(
c, true);
targetEndpointLocator.setVDistance(15);
Label targetMultiplicityLabel = new Label("1..*");
c.add(targetMultiplicityLabel, targetEndpointLocator);
// 添加连线到Locator
ConnectionEndpointLocator sourceEndpointLocator = new ConnectionEndpointLocator(
c, false);
sourceEndpointLocator.setVDistance(15);
Label sourceMultiplicityLabel = new Label("1");
c.add(sourceMultiplicityLabel, sourceEndpointLocator);
// 添加连线到Locator
ConnectionEndpointLocator relationshipLocator = new ConnectionEndpointLocator(
c, true);
relationshipLocator.setVDistance(10);
relationshipLocator.setVDistance(-20);
Label relationshipLabel = new Label("contains");
c.add(relationshipLabel, relationshipLocator);

// 把表、列和连线(即关系)添加到底层图形上
contents.add(classFigure);
contents.add(classFigure2);
contents.add(c);

lws.setContents(contents);
shell.open();
while (!shell.isDisposed())
while (!d.readAndDispatch())
d.sleep();

}

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值