package com.mjt.flow.diagram.figure; import org.eclipse.draw2d.Figure; import org.eclipse.draw2d.Graphics; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.swt.graphics.Image; import com.mjt.flow.diagram.part.FlowDiagramEditorPlugin; public class StartActivityFigure extends Figure { @Override protected void paintFigure(Graphics graphics) { setFigureImage(graphics, "icons/custom/start.png",32,3); //$NON-NLS-1$ } protected void setFigureImage(Graphics graphics, String imagePath, int width, int height){ super.paintFigure(graphics); ImageDescriptor descriptor = FlowDiagramEditorPlugin.findImageDescriptor(imagePath); Image image = descriptor.createImage();//创建图形 graphics.drawImage(image, getLocation());//绘制图标 setPreferredSize(width, height); //设置首选(默认)大小 } } |
/** * @generated NOT */ protected NodeFigure createNodePlate() { DefaultSizeNodeFigure result = new DefaultSizeNodeFigure(26,26); return result; } |
/** * 创建图形时自动适应PreferredSize大小。 * @generated NOT */ @Override protected void handleNotificationEvent(Notification notification) { setToPreferredSize(notification, getFigure(), (View) getModel(), getEditingDomain(), getDiagramEditDomain()); super.handleNotificationEvent(notification); } /** * Resize Constraint属性值改为NONE时,创建图形拖动时大小如果大于PreferredSize时, * 不会将图形适合到PreferredSize大小, 此方法为了解决这个问题 * @generated NOT */ public static void setToPreferredSize(Notification notification, IFigure figure, View model, TransactionalEditingDomain editingDomain, IDiagramEditDomain diagramEditDomain){ if(notification.getEventType()==Notification.SET && !figure.getSize().equals(figure.getPreferredSize())){ SetBoundsCommand boundsCommand = new SetBoundsCommand(editingDomain,"", //$NON-NLS-1$ new EObjectAdapter(model),figure.getPreferredSize()); diagramEditDomain.getDiagramCommandStack().execute(new ICommandProxy(boundsCommand)); } } |
/** * @generated NOT */ @Override public ConnectionAnchor getSourceConnectionAnchor( ConnectionEditPart connEditPart) { return new EllipseAnchor(getFigure()); } /** * @generated NOT */ @Override public ConnectionAnchor getSourceConnectionAnchor(Request request) { return new EllipseAnchor(getFigure()); } /** * @generated NOT */ @Override public ConnectionAnchor getTargetConnectionAnchor( ConnectionEditPart connEditPart) { return new EllipseAnchor(getFigure()); } /** * @generated NOT */ @Override public ConnectionAnchor getTargetConnectionAnchor(Request request) { return new EllipseAnchor(getFigure()); } |
package com.mjt.flow.diagram.figure; import org.eclipse.draw2d.ColorConstants; import org.eclipse.draw2d.Graphics; import org.eclipse.draw2d.Shape; import org.eclipse.draw2d.geometry.Dimension; import org.eclipse.draw2d.geometry.Rectangle; import org.eclipse.swt.graphics.Color; import org.eclipse.swt.graphics.Path; import org.eclipse.swt.graphics.RGB; /** * 自动活动自定义Figure */ public class AutoActivityFigure extends Shape { protected Dimension corner = new Dimension(20, 20); @Override protected void fillShape(Graphics graphics) { //通过路径绘制圆角矩形 Path path = new Path(null); path.addArc(getBounds().x, getBounds().y, corner.width, corner.height, 180, -90); path.addArc(getBounds().x+getBounds().width-corner.width, getBounds().y, corner.width, corner.height, 90,-90); path.addArc(getBounds().x+getBounds().width-corner.width, getBounds().y+getBounds().height-corner.height, corner.width, corner.height, 0,-90); path.addArc(getBounds().x, getBounds().y+getBounds().height-corner.height, corner.width, corner.height, 270,-90); graphics.setClip(path); //渐变填充 graphics.setForegroundColor(ColorConstants.white); graphics.setBackgroundColor(new Color(null, new RGB(200,220,230))); graphics.fillGradient(getBounds(), true); } @Override protected void outlineShape(Graphics graphics) { float lineInset = Math.max(1.0f, getLineWidthFloat()) / 2.0f; int inset1 = (int) Math.floor(lineInset); int inset2 = (int) Math.ceil(lineInset); Rectangle r = Rectangle.SINGLETON.setBounds(getBounds()); r.x += inset1; r.y += inset1; r.width -= inset1 + inset2; r.height -= inset1 + inset2; graphics.setForegroundColor(new Color(null, new RGB(40,100,120))); graphics.drawRoundRectangle(r, Math.max(0, corner.width - (int) lineInset), Math .max(0, corner.height - (int) lineInset)); } } |
由于平时比较忙,可能写的比较简单,有些地方没有详细说明,一笔带过,如果有什么不明白之处,欢迎通过邮件联系。
本系列教程尽量会在最短的时间内写出来。