定義圖形樣式(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);
}
}