定义图形样式(custom style)的三种方法:
1、通过扩展 StyleConfiguration扩展点 2、CustomStyle以及EditPartProvider GMF扩展点 3、GMF提供的扩展点
通过扩展CustomStyle以及EditPartProvider 扩展点来定义custom style
1、EditPart描述了Node的appearance,EditPart是一个GEF类,它reference了它所要展示的东西的Model element和shape。
2、GMF在GEF的基础上添加了一层,然后将GMF它自己的EditPart API暴露在外。这个API的基类型是org.eclipse.gmf.runtime.diagram.ui.editparts当中的IGraphicalEditPart。
3、所有定制样式的Edit Part都必须实现IStyleEditPart接口。从语义上来说,一种定制的Edit part应该是不可选择的,只有当包含它的形状或者连接被选中的时候,它才是可以被选中的。所以,所有的定制的样式(custom style)都必须重载isselectable()方法并且返回false。也可以继承抽象类AbstractNotSelectableShapeNodeEditPart,这个类已经重载了这个方法。
应用示例
先通过sirius specification editor 给某个Node定义一个CustomStyle
每一个CustomStyle只用一个Id来标识,通过这个Id来引用具体的CustomStyle。
定义一个Custom Style
public class InstanceRoleStyleEditPart extends AbstractNotSelectableShapeNodeEditPart implements IStyleEditPart {
/**
* the content pane.
*/
protected IFigure contentPane;
/**
* the primary shape.
*/
protected ImageFigure primaryShape;
/**
* Create a new {@link ChangingImageEditPart}.
*
* @param view
* the view.
*/
public InstanceRoleStyleEditPart(View view) {
super(view);
}
public DragTracker getDragTracker(Request request) {
return getParent().getDragTracker(request);
}
protected NodeFigure createNodeFigure() {
NodeFigure figure = createNodePlate();
figure.setLayoutManager(new XYLayout());
IFigure shape = createNodeShape();
figure.add(shape);
contentPane = setupContentPane(shape);
return figure;
}
private NodeFigure createNodePlate() {
DefaultSizeNodeFigure result = new AirStyleDefaultSizeNodeFigure(getMapMode().DPtoLP(40), getMapMode().DPtoLP(40));
return result;
}
/**
* Create the instance role figure.
*
* @return the created figure.
*/
protected ImageFigure createNodeShape() {
if (primaryShape == null) {
primaryShape = new ImageFigure();
}
return primaryShape;
}
/**
* Return the instance role figure.
*
* @return the instance role figure.
*/
public ImageFigure getPrimaryShape() {
return primaryShape;
}
/**
* Default implementation treats passed figure as content pane. Respects
* layout one may have set for generated figure.
*
* @param nodeShape
* instance of generated figure class
* @return the figure
*/
protected IFigure setupContentPane(IFigure nodeShape) {
return nodeShape; // use nodeShape itself as contentPane
}
public IFigure getContentPane() {
if (contentPane != null) {
return contentPane;
}
return super.getContentPane();
}
protected void refreshVisuals() {
CustomStyle customStyle = (CustomStyle) this.resolveSemanticElement();
if (customStyle.eContainer() instanceof DNode) {
this.getPrimaryShape().setImage(SiriusPlugin.getDefault().getBundledImage(((DNode) customStyle.eContainer()).getName()));
}
}
protected void createDefaultEditPolicies() {
// empty.
}
}
引用扩展点,定义一个edit part provider
(扩展点的编辑可以在编辑框里面编辑)
<extension point="org.eclipse.gmf.runtime.diagram.ui.editpartProviders">
<editpartProvider class="com.example.diagseq.provider.DiagSeqEditPartProvider">
<Priority name="High"/>
</editpartProvider>
</extension>
(EditPartProvider 代码段)
public class DiagSeqEditPartProvider extends AbstractEditPartProvider {
@Override
protected Class getNodeEditPartClass(View view) {
if (view.getElement() instanceof CustomStyle) {
CustomStyle customStyle = (CustomStyle) view.getElement();
//通过Id来确定引用哪一个Edit Part Provider
if (customStyle.getId().equals(DiagSeqConstants.INSTANCE_ROLE_STYLE_ID)) {
return InstanceRoleStyleEditPart.class;
}
}
return super.getNodeEditPartClass(view);
}
}