/** * A test class to display a UMLFigure */ public class UMLClassFigureTest { private static Display display = Display.getDefault(); public static void main(String args[]) { final Shell shell = new Shell(display); shell.setSize(400, 400); shell.setText("UMLClassFigure Test"); LightweightSystem lws = new LightweightSystem(shell); Figure rootFigure = new Figure(); XYLayout rootFigureLayout = new XYLayout(); rootFigure.setLayoutManager(rootFigureLayout); UMLClassFigure classFigure1 = getClassFigure(); UMLClassFigure classFigure2 = getClassFigure(); UMLClassFigure classFigure3 = getClassFigure(); rootFigureLayout.setConstraint(classFigure1, new Rectangle(10,10,-1,-1)); rootFigureLayout.setConstraint(classFigure2, new Rectangle(200, 200, -1, -1)); rootFigureLayout.setConstraint(classFigure3, new Rectangle(10, 200, -1, -1)); rootFigure.add(classFigure1); rootFigure.add(classFigure2); rootFigure.add(classFigure3); rootFigure.add(createConnection(classFigure1,classFigure2)); rootFigure.add(createConnection(classFigure1,classFigure3)); lws.setContents(rootFigure); shell.open(); while (!shell.isDisposed()) { while (!display.readAndDispatch()) display.sleep(); } if(!display.isDisposed()) { display.dispose(); } } private static UMLClassFigure getClassFigure() { Label classLabel1 = new Label("Table", JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_INFO)); UMLClassFigure classFigure = new UMLClassFigure(classLabel1); Label attribute1 = new Label("columnID: int", JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING)); Label attribute2 = new Label("items: List", JFaceResources.getImage(Dialog.DLG_IMG_MESSAGE_WARNING)); classFigure.getAttributesCompartment().add(attribute1); classFigure.getAttributesCompartment().add(attribute2); Label method1 = new Label("getColumns(): Column[]", JFaceResources.getImage(Dialog.DLG_IMG_HELP)); Label method2 = new Label("getRows(): Row[]", JFaceResources.getImage(Dialog.DLG_IMG_HELP)); classFigure.getMethodsCompartment().add(method1); classFigure.getMethodsCompartment().add(method2); return classFigure; } private static PolylineConnection createConnection( UMLClassFigure classFigure, UMLClassFigure classFigure2) { /* Creating the connection */ PolylineConnection connection = new PolylineConnection(); /* Creating the Anchor */ ChopboxAnchor sourceAnchor = new ChopboxAnchor(classFigure); ChopboxAnchor targetAnchor = new ChopboxAnchor(classFigure2); connection.setSourceAnchor(sourceAnchor); connection.setTargetAnchor(targetAnchor); /* Creating the decoration */ 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); connection.setSourceDecoration(decoration); /* Adding labels to the connection */ ConnectionEndpointLocator targetEndpointLocator = new ConnectionEndpointLocator(connection, true); targetEndpointLocator.setVDistance(15); Label targetMultiplicityLabel = new Label("1..*"); connection.add(targetMultiplicityLabel, targetEndpointLocator); ConnectionEndpointLocator sourceEndpointLocator = new ConnectionEndpointLocator(connection, false); sourceEndpointLocator.setVDistance(15); Label sourceMultiplicityLabel = new Label("1"); connection.add(sourceMultiplicityLabel, sourceEndpointLocator); ConnectionEndpointLocator relationshipLocator = new ConnectionEndpointLocator(connection,true); relationshipLocator.setUDistance(50); relationshipLocator.setVDistance(-80); Label relationshipLabel = new Label("contains"); connection.add(relationshipLabel,relationshipLocator); return connection; } } 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); } public CompartmentFigure getAttributesCompartment() { return attributeFigure; } public CompartmentFigure getMethodsCompartment() { return methodFigure; } } 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()); } } }
关键字:Draw2D UML
链接:http://www.eclipse.org/articles/Article-GEF-Draw2d/GEF-Draw2d.html
效果图:
源文件在附近。
更新源文件: