Eclipse 4 - 当我保存我的视角时,如何保存脏部分

时间:2017-07-21 04:37:14

标签: java eclipse

我使用E4XMIResourceFactory在我的eclipse 4应用程序中保存透视图。但是当我从xmi文件加载我的视角时,我可以在部件中找到我的数据。在其他世界,我想在我的xmi文件中保存脏部分。有人可以帮助我,我无法在网上找到任何解决方案来解决我的问题。

public class SaveHandler {
    @Execute
    public void execute(EModelService modelService, MWindow window, MApplication app, Shell shell) {

        MPerspective savePerspective = modelService.getActivePerspective(window);
        E4XMIResourceFactory e4xmiResourceFactory = new E4XMIResourceFactory();
        Resource resource = e4xmiResourceFactory.createResource(null); 

        MUIElement clonedPerspective = modelService.cloneElement(savePerspective, window);

        resource.getContents().add((EObject) clonedPerspective);

         FileDialog dialog = new FileDialog(shell, SWT.OPEN);
         dialog.setFilterExtensions(new String [] {"*.xmi"});
         //dialog.setFilterPath("c:\\temp");
         String result = dialog.open();

        FileOutputStream outputStream = null;
        try {

            // Use a stream to save the model element
            outputStream = new FileOutputStream(result);

            resource.save(outputStream, null);
            if (outputStream != null)
                outputStream.close();

        } catch (IOException e) {
            e.printStackTrace();
        } 
    }
}

加载器方法是

public class OpenHandler {
    @Execute
    public void execute(EPartService partService,EModelService modelService,MWindow window, MApplication app, Shell shell) {
        E4XMIResourceFactory e4xmiResourceFactory = new E4XMIResourceFactory();
        Resource resource = e4xmiResourceFactory.createResource(null);

        FileInputStream inputStream = null;

        try {

             FileDialog dialog = new FileDialog(shell, SWT.OPEN);
             dialog.setFilterExtensions(new String [] {"*.xmi"});
             //dialog.setFilterPath("c:\\temp");
             String result = dialog.open();

            inputStream = new FileInputStream(result);
            resource.load(inputStream, null);
             if (!resource.getContents().isEmpty()) {

                 MPerspective loadedPerspective = (MPerspective) resource.getContents().get(0); 

                 //MPerspective perspective = (MPerspective)modelService.find("ktool_aie.perspective.connectionandmaps",app);
                    MPerspective perspective = modelService.getActivePerspective(window);
                 MElementContainer<MUIElement> perspectiveParent = perspective.getParent();

                 List<MPerspective> alreadyPresentPerspective = modelService.findElements(window,loadedPerspective.getElementId(), MPerspective.class, null);

                 for (MPerspective i_perspective : alreadyPresentPerspective) {
                        modelService.removePerspectiveModel(i_perspective, window);
                    }

                    // add the loaded perspective and switch to it
                 perspectiveParent.getChildren().add(loadedPerspective);

                 partService.switchPerspective(loadedPerspective);
             }


            if (inputStream != null) inputStream.close();

        } catch(IOException e) {
            e.printStackTrace();

        }

    }
}

0 个答案:

没有答案