GMF中,自动创建图元(create nodes programmatically)

            以下是创建图元的代码:

            IElementType type = XXXElementTypes.Xxxx_1001 ; //要创建的图元对象的IElementType

           CreateViewRequest createViewRequest= CreateViewRequestFactory.getCreateShapeRequest(type,                       XXXDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);

           org.eclipse.gef.commands.Command createCommand = xxxEditPart.getCommand(createViewRequest); 

           xxxEditPart.getDiagramEditDomain().getDiagramCommandStack().execute(createCommand ); //执行创建命令,其中xxxEditPart指当前编辑器的EditPart。当执行完此行代码时,会在编辑器中创建图元图形以及其资源对象。

 

            以下是删除图元的代码:

            final View xxxView = (View) xxxEditPart.getModel();

            TransactionalEditingDomain domain = TransactionUtil.getEditingDomain(xxxImpl);

            domain.getCommandStack().execute(new RecordingCommand(domain) {

            protected void doExecute() {

            ViewUtil.destroy(xxxView); // 删除要删除的图元的view,执行此操作后,在编辑器中此图元会消失,但并未在图文件中删除此图元对应的资源对象,所以如果只执行此句而不执行下一句的话,当你再次打开编辑器的时候,此图元还会显示在编辑器中

            xxxDiagramImpl.getxxx().remove(xxxImpl); // 删除要删除的图元的语义模型对象,当此句执行完后,就真正删除了资源对象。这里的xxxDiagramImpl指当前编辑器的语义模型对象
           }   
     });

 

posted @ 2012-02-26 21:48  supermmao  阅读(255)  评论(0编辑  收藏  举报