两个GMF常用方法

        1.根据领域模型element,在模型所在的编辑器上选中图元
    public static void selectDiagramEditPart(DiagramEditPart diagramPart,
    EObject element) {
    if (diagramPart == null || element == null)
    return;
    EditPartViewer partViewer = diagramPart.getViewer();
    Map<?,?> map = partViewer.getEditPartRegistry();
    for (Entry<?, ?> entry : map.entrySet()) {
    Object key = entry.getKey();
    if (!(key instanceof View)) {
    continue;
    }
    View view = (View) key;
    EObject tempElement = view.getElement();
                        //过滤非领域模型
    if (!(element instanceof **Node || tempElement instanceof **Link)) {
    return ;
    }
    if (element.equals(tempElement)) {
    Object value = entry.getValue();
    if (!(value instanceof EditPart)) {
    continue;
    }
    EditPart tempEditPart = (EditPart) value;
    EditPart parent = tempEditPart.getParent();
    if (parent instanceof **DiagramEditor) {
    partViewer.select(tempEditPart);
    partViewer.reveal(tempEditPart);
    }
    }
    }
    }
   
        2.根据领域模型找到模型所在编辑器的EditPart
    public static EditPart getEditPart(DiagramEditPart diagramPart,EObject element){
            if (diagramPart == null || element == null)
                return null;
            EditPartViewer partViewer = diagramPart.getViewer();
            Map<?,?> map = partViewer.getEditPartRegistry();
            for (Entry<?, ?> entry : map.entrySet()) {
                Object key = entry.getKey();
                if (!(key instanceof View)) {
                    continue;
                }
                View view = (View) key;
                EObject tempElement = view.getElement();
                //过滤非领域模型
                if (!(tempElement instanceof **Node || tempElement instanceof **Link) {
                    continue;
                }
                if (element.equals(tempElement)) {
                    Object value = entry.getValue();
                    //过滤EditPart
                    if (value instanceof **EditPart) {
                        return (EditPart) value;
                    }
                }
            }
            return null;
    }

你可能感兴趣的:(领域模型)