居于gmf的图元自动对齐实现

背景:

加载模型文件之后,可以通过 以下方法获取了 资源文件

 

 

/**

     * 针对单表维护

     * @generated NOT

     */

    public static Resource createDiagram4singletable(URI diagramURI, URI modelURI,

           IProgressMonitor progressMonitor) {

       TransactionalEditingDomain editingDomain = GMFEditingDomainFactory.INSTANCE

              .createEditingDomain();

       progressMonitor.beginTask(

              Messages.PraDiagramEditorUtil_CreateDiagramProgressTask, 3);

       final Resource diagramResource = editingDomain.getResourceSet()

              .createResource(diagramURI);

      

       //

       /*final Resource modelResource = editingDomain.getResourceSet()

              .createResource(modelURI);*/

       //获取资源

       final Resource modelResource = editingDomain.getResourceSet()

              .getResource(modelURI, true);

      

       final String diagramName = diagramURI.lastSegment();

       AbstractTransactionalCommand command = new AbstractTransactionalCommand(

              editingDomain,

               Messages.PraDiagramEditorUtil_CreateDiagramCommandLabel,

              Collections.EMPTY_LIST) {

           protected CommandResult doExecuteWithResult(

                  IProgressMonitor monitor, IAdaptable info)

                  throws ExecutionException {

             

              //---------------------获取资源-------------------------

              CjsAutomation model = null;

              try{

               model = (com.cellcom.studio.pra.impl.CjsAutomationImpl)modelResource.getContents().get(0);

              }catch(Exception e){

                  e.printStackTrace();

              }

              attachModelToResource(model, modelResource);

              //------------------------------------------------------

 

              Diagram diagram = ViewService.createDiagram(model,

                     CjsAutomationEditPart.MODEL_ID,

                     PraDiagramEditorPlugin.DIAGRAM_PREFERENCES_HINT);

              if (diagram != null) {

                  diagramResource.getContents().add(diagram);

                  diagram.setName(diagramName);

                  diagram.setElement(model);

              }

 

              try {

                  modelResource

                         .save(com.cellcom.studio.pra.diagram.part.PraDiagramEditorUtil

                                .getSaveOptions());

                  diagramResource

                         .save(com.cellcom.studio.pra.diagram.part.PraDiagramEditorUtil

                                .getSaveOptions());

              } catch (IOException e) {

 

                  PraDiagramEditorPlugin.getInstance().logError(

                         "Unable to store model and diagram resources", e); //$NON-NLS-1$

              }

              return CommandResult.newOKCommandResult();

           }

       };

       try {

           OperationHistoryFactory.getOperationHistory().execute(command,

                  new SubProgressMonitor(progressMonitor, 1), null);

       } catch (ExecutionException e) {

           PraDiagramEditorPlugin.getInstance().logError(

                  "Unable to create model and diagram", e); //$NON-NLS-1$

       }

       setCharset(WorkspaceSynchronizer.getFile(modelResource));

       setCharset(WorkspaceSynchronizer.getFile(diagramResource));

       return diagramResource;

    }

 

 

将该文件打开,因为只有在打开的状态下,才可以获取父类的editpart (其实是CjsAutomationEditPart

获取要点是:((DiagramEditPart)rootEditPart[0].getContents());获取父editpart – CjsAutomationEditPart

 

 

if (diagram != null) {

                  try {

                     BzaDiagramEditorUtil.openDiagram(diagram);

                  } catch (PartInitException e) {

                     ErrorDialog.openError(getContainer().getShell(),

                            Messages.BzaCreationWizardOpenEditorError,

                            null, e.getStatus());

                  }

              }

              //Diagram d = (Diagram)diagram.getContents().get(0);

             

              //____________

              final RootEditPart[] rootEditPart = new RootEditPart[1];

              PlatformUI.getWorkbench().getDisplay().syncExec(new Runnable() {

                  public void run() {

                     IWorkbenchPage page = PlatformUI.getWorkbench()

                     .getActiveWorkbenchWindow().getActivePage();

                     if (page != null) {

                         IEditorPart editor = page.getActiveEditor();

                         if (editor instanceof GraphicalEditor) {

                            GraphicalEditor graphicalEditor = (GraphicalEditor) editor;

                            rootEditPart[0] = (RootEditPart)graphicalEditor.getAdapter(EditPart.class);

 

                         }

                     }

                  }

              }); //这段方法是获取当前editpart  --很重要

 

             

              //(CjsAutomationEditPart)de.ge

              ArrangeRequest ar = new ArrangeRequest(ActionIds.ACTION_ARRANGE_ALL);

             

               //ar.setPartsToArrange(((DiagramEditPart)rootEditPart[0].getContents()).getChildren());

              //((DiagramEditPart)rootEditPart[0].getContents()).getChildren();//获取当前流程图里面有哪些editpart

              //((DiagramEditPart)rootEditPart[0].getContents());获取父editpart - CjsAutomationEditPart

             

             

              //图元对齐

              EditPart4ArrangeCommand.ShapeContainerEditPolicy es = new EditPart4ArrangeCommand.ShapeContainerEditPolicy(((DiagramEditPart)rootEditPart[0].getContents()));

              try{

              es.getArrangeCommand(ar).execute();

              }catch(Exception e){

                  e.printStackTrace();

              }

             

              closeAndSaveDiagram(diagram);

 

 

 

发送命令后,因为文件被修改了,所以还得通过IDE来保存这些修改

/**

     * 关闭并且保存

     * @param diagram

     * @return

     * @throws PartInitException

     */

    public static boolean closeAndSaveDiagram(Resource diagram)

           throws PartInitException {

       String path = diagram.getURI().toPlatformString(true);

       IResource workspaceResource = ResourcesPlugin.getWorkspace().getRoot()

              .findMember(new Path(path));

       if (workspaceResource instanceof IFile) {

           IWorkbenchPage page = PlatformUI.getWorkbench()

                  .getActiveWorkbenchWindow().getActivePage();

          

           //保存

           //page.savePerspective();

           page.saveAllEditors(false);

           //page.saveEditor(new BzaDiagramEditor(), true);

           //关闭

           //page.close();

          

           /*return null != page.openEditor(new FileEditorInput(

                  (IFile) workspaceResource), BzaDiagramEditor.ID);*/

       }

       return false;

    }

 

 

以下是发送对齐命令的实现

 

import org.eclipse.gef.EditPart;

import org.eclipse.gef.commands.Command;

import org.eclipse.gmf.runtime.diagram.ui.editparts.ShapeEditPart;

import org.eclipse.gmf.runtime.diagram.ui.editpolicies.ContainerEditPolicy;

import org.eclipse.gmf.runtime.diagram.ui.requests.ArrangeRequest;

import org.eclipse.gmf.runtime.notation.View;

 

/**

 * 为单表维护提供图元对齐的命令

 * @author xgchen

 * TODO

 * 2009-7-22 下午03:12:07

 */

public class EditPart4ArrangeCommand extends ShapeEditPart {

 

    public EditPart4ArrangeCommand(View view) {

       super(view);

       // TODO Auto-generated constructor stub

    }

 

    public static class ShapeContainerEditPolicy

    extends ContainerEditPolicy {

 

       public ShapeContainerEditPolicy(EditPart host){

           super.setHost(host);

       }

      

        public Command getArrangeCommand(ArrangeRequest request) {   

        return super.getArrangeCommand(request);

        }

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值