Search in sources :

Example 1 with IDirtyResource

use of org.eclipse.xtext.ui.editor.IDirtyResource in project xtext-eclipse by eclipse.

the class ContentAssistProcessorTestBuilder method getDocument.

protected IXtextDocument getDocument(final String currentModelToParse) {
    final XtextResource xtextResource = loadHelper.getResourceFor(new StringInputStream(Strings.emptyIfNull(currentModelToParse)));
    if (announceDirtyState) {
        dirtyResource = new IDirtyResource() {

            @Override
            public String getContents() {
                return currentModelToParse;
            }

            @Override
            public String getActualContents() {
                return currentModelToParse;
            }

            @Override
            public IResourceDescription getDescription() {
                return xtextResource.getResourceServiceProvider().getResourceDescriptionManager().getResourceDescription(xtextResource);
            }

            @Override
            public URI getURI() {
                return xtextResource.getURI();
            }
        };
        dirtyStateManager.manageDirtyState(dirtyResource);
    }
    return getDocument(xtextResource, currentModelToParse);
}
Also used : IDirtyResource(org.eclipse.xtext.ui.editor.IDirtyResource) StringInputStream(org.eclipse.xtext.util.StringInputStream) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) XtextResource(org.eclipse.xtext.resource.XtextResource) URI(org.eclipse.emf.common.util.URI)

Example 2 with IDirtyResource

use of org.eclipse.xtext.ui.editor.IDirtyResource in project statecharts by Yakindu.

the class DirtyStateListener method resourceSetChanged.

public void resourceSetChanged(ResourceSetChangeEvent event) {
    List<URI> remainingURIs = Lists.newArrayList(uri2dirtyResource.keySet());
    for (Resource currentResource : event.getEditingDomain().getResourceSet().getResources()) {
        if (currentResource instanceof XMIResource) {
            XMIResource resource = (XMIResource) currentResource;
            remainingURIs.remove(resource.getURI());
            IDirtyResource dirtyResource = uri2dirtyResource.get(resource.getURI());
            if (resource.isModified()) {
                if (dirtyResource == null) {
                    createAndRegisterDirtyState(resource);
                }
            } else {
                if (dirtyResource != null) {
                    uri2dirtyResource.remove(resource.getURI());
                    dirtyStateManager.discardDirtyState(dirtyResource);
                }
            }
        }
    }
    for (URI remainingURI : remainingURIs) {
        IDirtyResource dirtyResource = uri2dirtyResource.get(remainingURI);
        dirtyStateManager.discardDirtyState(dirtyResource);
        uri2dirtyResource.remove(remainingURI);
    }
}
Also used : IDirtyResource(org.eclipse.xtext.ui.editor.IDirtyResource) XMIResource(org.eclipse.emf.ecore.xmi.XMIResource) Resource(org.eclipse.emf.ecore.resource.Resource) IDirtyResource(org.eclipse.xtext.ui.editor.IDirtyResource) XMIResource(org.eclipse.emf.ecore.xmi.XMIResource) URI(org.eclipse.emf.common.util.URI)

Example 3 with IDirtyResource

use of org.eclipse.xtext.ui.editor.IDirtyResource in project statecharts by Yakindu.

the class DirtyStateAwareDiagramDocumentEditor method removeDirtyResource.

private void removeDirtyResource(Resource resource) {
    IDirtyResource dirtyResource = uri2dirtyResource.get(resource.getURI());
    dirtyStateManager.discardDirtyState(dirtyResource);
    uri2dirtyResource.remove(resource.getURI());
// TODO: remove adapter EcoreUtil.getExistingAdapter(resource, DirtyResourceUpdater.class) == null
}
Also used : IDirtyResource(org.eclipse.xtext.ui.editor.IDirtyResource)

Example 4 with IDirtyResource

use of org.eclipse.xtext.ui.editor.IDirtyResource in project xtext-eclipse by eclipse.

the class AbstractScopeResourceDescriptionsTest method assertDirtyState.

protected void assertDirtyState(boolean enabled) throws IOException {
    String dirtyResourceName = "test/test." + fileExt.getPrimaryFileExtension();
    final URI dirtyResourceURI = URI.createPlatformResourceURI(dirtyResourceName, true);
    final String testModel = "stuff foo stuff bar refs foo";
    IDirtyResource mockDirtyResource = new IDirtyResource() {

        @Override
        public String getActualContents() {
            return testModel;
        }

        @Override
        public String getContents() {
            return testModel;
        }

        @Override
        public IResourceDescription getDescription() {
            return new URIBasedTestResourceDescription(dirtyResourceURI);
        }

        @Override
        public URI getURI() {
            return dirtyResourceURI;
        }
    };
    try {
        assertTrue(dirtyStateManager.manageDirtyState(mockDirtyResource));
        Resource dirtyResource = resourceSet.getResource(dirtyResourceURI, true);
        assertTrue(dirtyResource instanceof XtextResource);
        assertTrue(dirtyResource.isLoaded());
        assertFalse(dirtyResource.getContents().isEmpty());
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        dirtyResource.save(byteArrayOutputStream, null);
        assertEquals(testModel, new String(byteArrayOutputStream.toByteArray()));
        assertTrue(enabled);
    } catch (Throwable t) {
        assertFalse(enabled);
        boolean isResourceException = false;
        for (Throwable tx : Throwables.getCausalChain(t)) {
            if (tx instanceof org.eclipse.core.internal.resources.ResourceException) {
                isResourceException = true;
                break;
            }
        }
        if (!isResourceException)
            Exceptions.throwUncheckedException(t);
    } finally {
        dirtyStateManager.discardDirtyState(mockDirtyResource);
    }
}
Also used : IDirtyResource(org.eclipse.xtext.ui.editor.IDirtyResource) URIBasedTestResourceDescription(org.eclipse.xtext.junit4.util.URIBasedTestResourceDescription) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) IDirtyResource(org.eclipse.xtext.ui.editor.IDirtyResource) XtextResource(org.eclipse.xtext.resource.XtextResource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URI(org.eclipse.emf.common.util.URI)

Example 5 with IDirtyResource

use of org.eclipse.xtext.ui.editor.IDirtyResource in project xtext-eclipse by eclipse.

the class ContentAssistProcessorTestBuilder method getDocument.

protected IXtextDocument getDocument(final String currentModelToParse) {
    StringInputStream in;
    try {
        in = new StringInputStream(Strings.emptyIfNull(currentModelToParse), getEncoding());
    } catch (UnsupportedEncodingException e) {
        in = new StringInputStream(Strings.emptyIfNull(currentModelToParse));
    }
    final XtextResource xtextResource = loadHelper.getResourceFor(in);
    if (announceDirtyState) {
        dirtyResource = new IDirtyResource() {

            @Override
            public String getContents() {
                return currentModelToParse;
            }

            @Override
            public String getActualContents() {
                return currentModelToParse;
            }

            @Override
            public IResourceDescription getDescription() {
                return xtextResource.getResourceServiceProvider().getResourceDescriptionManager().getResourceDescription(xtextResource);
            }

            @Override
            public URI getURI() {
                return xtextResource.getURI();
            }
        };
        dirtyStateManager.manageDirtyState(dirtyResource);
    }
    return getDocument(xtextResource, currentModelToParse);
}
Also used : IDirtyResource(org.eclipse.xtext.ui.editor.IDirtyResource) StringInputStream(org.eclipse.xtext.util.StringInputStream) IResourceDescription(org.eclipse.xtext.resource.IResourceDescription) UnsupportedEncodingException(java.io.UnsupportedEncodingException) XtextResource(org.eclipse.xtext.resource.XtextResource) URI(org.eclipse.emf.common.util.URI)

Aggregations

IDirtyResource (org.eclipse.xtext.ui.editor.IDirtyResource)5 URI (org.eclipse.emf.common.util.URI)4 XtextResource (org.eclipse.xtext.resource.XtextResource)3 Resource (org.eclipse.emf.ecore.resource.Resource)2 IResourceDescription (org.eclipse.xtext.resource.IResourceDescription)2 StringInputStream (org.eclipse.xtext.util.StringInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 XMIResource (org.eclipse.emf.ecore.xmi.XMIResource)1 URIBasedTestResourceDescription (org.eclipse.xtext.junit4.util.URIBasedTestResourceDescription)1