重命名EMF资源的功能

时间:2015-02-24 05:09:44

标签: eclipse-emf emf

我正在使用一个项目,我在其中有EMF模型'A',在许多其他模型'B','C'中引用...等等。我想要的是我想给出一个重命名功能这些资源。因此,当用户重命名“A”时,必须更新其引用。

请提供一些想法,如果有任何框架工作或我必须获得所有参考,然后以编程方式迭代和更新参考。

1 个答案:

答案 0 :(得分:0)

I solved the same problem in another way.

The fundamental problem is that a referenced resource file might be renamed, and this breaks the references.

Instead of a refactoring that automatically updates all references I created a Repair File References command, which the user can invoke on an edited model.

The command performs these steps:

  1. Prompts the user to select a missing resource to repair
  2. Prompts the user to select a replacement file
  3. Updates all objects in the model that has a proxy URI that matches the missing resource. Replaces proxies with resolved objects in the new resource.

If you still want to make a refactoring instead, I think you anyway can use my code as a starting point.

/**
 * Locates and fixes unresolved references in a model.
 */
public class ReferenceRepairer {
    public static final String COMMAND_ID = Activator.PLUGIN_ID + ".commands.repairReferences";

    /**
     * 1) Prompts the user to select a missing resource to repair
     * 2) Prompts the user to select a replacement file
     * 3) Updates all objects in the model with a proxy URI that matches the missing resource. Replaces proxies
     *    with resolved objects in the new resource.  
     */
    public static void repairResourceReference(Shell shell, EditingDomain editingDomain) {
        Resource res = promptMissingResource(shell, editingDomain);

        if (res == null) return;

        IFile newFile = promptReplacementFile(shell);

        if (newFile == null) return;

        repairReferences(editingDomain, res, URI.createPlatformResourceURI(newFile.getFullPath().toString(), true));
    }

    private static void repairReferences(final EditingDomain editingDomain, Resource missingRes, final URI newUri) {
        URI missingUri = missingRes.getURI();

        // Create new resource for the replacement file
        Resource newRes = editingDomain.getResourceSet().getResource(newUri, true);

        Map<EObject, Collection<Setting>> proxies = UnresolvedProxyCrossReferencer.find(editingDomain.getResourceSet());

        CompoundCommand repairRefsCommand =  new CompoundCommand("Repair references") {
            /**
             * Disallow undo. The model changes could be undone, but it seems impossible to
             * recreate a non-existent resource in the resource set. 
             */
            @Override
            public boolean canUndo() {
                return false;
            }
        };

        // Resolve all proxies from this resource and repair reference to those objects

        for (Entry<EObject, Collection<Setting>> entry : proxies.entrySet()) {

            EObject proxy = entry.getKey();
            URI proxyUri = EcoreUtil.getURI(proxy);
            if (!proxyUri.trimFragment().equals(missingUri)) continue;

            EObject resolved = newRes.getEObject(proxyUri.fragment());

            if (resolved.eIsProxy()) continue;

            // Update all objects that have references to the resolved proxy

            for (Setting sett : entry.getValue()) {
                if (sett.getEStructuralFeature().isMany()) {
                    @SuppressWarnings("unchecked")
                    EList<Object> valueList = (EList<Object>) sett.get(true);
                    int proxyIx = valueList.indexOf(proxy);

                    repairRefsCommand.append(SetCommand.create(editingDomain,
                        sett.getEObject(), sett.getEStructuralFeature(), resolved, proxyIx));
                } else {
                    repairRefsCommand.append(SetCommand.create(editingDomain,
                        sett.getEObject(), sett.getEStructuralFeature(), resolved));
                }
            }
        }

        if (!repairRefsCommand.isEmpty()) {
            editingDomain.getCommandStack().execute(repairRefsCommand);
        }

        // Remove the 
        editingDomain.getResourceSet().getResources().remove(missingRes);
    }

    private static IFile promptReplacementFile(Shell shell) {
        ElementTreeSelectionDialog dialog = new ElementTreeSelectionDialog(shell, 
            new WorkbenchLabelProvider(), new WorkbenchContentProvider()); 

        dialog.setInput(ResourcesPlugin.getWorkspace().getRoot());
        dialog.setTitle("Select Replacement Resource");
        dialog.setMessage("Select a file which will replace the missing file.");

        dialog.setValidator(new ISelectionStatusValidator() {
            @Override
            public IStatus validate(Object[] selection) {
                if (selection.length == 0 || !(selection[0] instanceof IFile)) {
                    return ValidationStatus.error("The selected object is not a file.");
                }

                return new Status(IStatus.OK, Activator.PLUGIN_ID, "");
            }
        });


        if (dialog.open() != Window.OK) return null;

        return (IFile) dialog.getFirstResult();
    }

    private static Resource promptMissingResource(Shell shell, EditingDomain editingDomain) {
        ElementListSelectionDialog dialog = new ElementListSelectionDialog(shell, 
                new LabelProvider() {
                    @Override
                    public String getText(Object elem) {
                        return ((Resource) elem).getURI().toString();
                    }
            })
        {
            /** Make dialog OK button enabled when there are errors, instead of vise-versa. */ 
            @Override
            protected void updateButtonsEnableState(IStatus status) {
                Button okButton = getOkButton();
                if (okButton != null && !okButton.isDisposed()) {
                    okButton.setEnabled(!status.isOK());
                }
            }

            /** Disable filter text field */
            @Override
            protected Text createFilterText(Composite parent) {
                Text text = super.createFilterText(parent);
                text.setSize(0, 0);
                text.setLayoutData(GridDataFactory.swtDefaults().exclude(true).create());
                text.setVisible(false);
                return text;
            }
        };

        dialog.setTitle("Select Missing Resource");
        dialog.setMessage(
            "Select a URI of a missing resource file that should be replaced by an URI to an existing file.");
        dialog.setElements(getMissingResources(editingDomain.getResourceSet().getResources()).toArray());

        if (dialog.open() != Window.OK) return null;

        return (Resource) dialog.getFirstResult();
    }

    private static List<Resource> getMissingResources(List<Resource> resources) {
        List<Resource> missingResources = new ArrayList<>();
        for (Resource res : resources) {
            try {
                if (res.getURI().isPlatformPlugin()) continue;
                URL url = FileLocator.toFileURL(new URL(res.getURI().toString()));
                java.net.URI uri = new java.net.URI(url.getProtocol(), "", "/" + url.getPath(), null);
                if (!Files.exists(Paths.get(uri))) {
                    missingResources.add(res);
                }
            } catch (InvalidPathException | IOException | URISyntaxException exc) {
                // Ignore. There mighe be weird Sirius resource in the resources set which we can't recognice
            }

        }
        return missingResources;
    }
}