org.eclipse.xtext.ui.editor.IDirtyResource Java Examples

The following examples show how to use org.eclipse.xtext.ui.editor.IDirtyResource. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PrevStateAwareDirtyStateManager.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
private DocumentBasedDirtyResource reflectiveGetInnerResource(final IDirtyResource dirtyResource) {
	Field[] declaredFields = dirtyResource.getClass().getDeclaredFields();
	DocumentBasedDirtyResource myDirtyResource = null;
	try {
		Field field = declaredFields[0];
		field.setAccessible(true);

		Object fieldValue = field.get(dirtyResource);

		if (fieldValue instanceof DirtyStateEditorSupport) {
			myDirtyResource = ((DirtyStateEditorSupport) fieldValue).getDirtyResource();
		}
	} catch (IllegalArgumentException | IllegalAccessException e) {
		// ignore
	}
	return myDirtyResource;
}
 
Example #2
Source File: PrevStateAwareDirtyStateManager.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private IResourceDescription extractOldDescription(final IDirtyResource dirtyResource) {
	IDirtyResource myDirtyResource = reflectiveGetInnerResource(dirtyResource);
	if (myDirtyResource instanceof PrevStateAwareDocumentBasedDirtyResource) {
		return ((PrevStateAwareDocumentBasedDirtyResource) myDirtyResource).getPrevDescription();
	}
	return null;
}
 
Example #3
Source File: ContentAssistProcessorTestBuilder.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
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);
}
 
Example #4
Source File: DirtyStateListener.java    From statecharts with Eclipse Public License 1.0 5 votes vote down vote up
protected boolean createAndRegisterDirtyState(XMIResource resource) {
	IDirtyResource dirtyResource = createDirtyResource(resource);
	if (dirtyResource == null) {
		return true;
	} else {
		boolean isSuccess = dirtyStateManager
				.manageDirtyState(dirtyResource);
		if (isSuccess) {
			uri2dirtyResource.put(resource.getURI(), dirtyResource);
		}
		return isSuccess;
	}
}
 
Example #5
Source File: DirtyStateListener.java    From statecharts with Eclipse Public License 1.0 5 votes vote down vote up
protected IDirtyResource createDirtyResource(XMIResource resource) {
	IResourceServiceProvider resourceServiceProvider = IResourceServiceProvider.Registry.INSTANCE
			.getResourceServiceProvider(resource.getURI());
	if (resourceServiceProvider == null) {
		return null;
	}
	return new XMIDirtyResource(resource, resourceServiceProvider);
}
 
Example #6
Source File: DirtyStateListener.java    From statecharts with Eclipse Public License 1.0 5 votes vote down vote up
public void dispose() {
	if (uri2dirtyResource != null) {
		for (IDirtyResource dirtyResource : uri2dirtyResource.values()) {
			dirtyStateManager.discardDirtyState(dirtyResource);
		}
		uri2dirtyResource = null;
	}
}
 
Example #7
Source File: DirtyStateAwareDiagramDocumentEditor.java    From statecharts with Eclipse Public License 1.0 5 votes vote down vote up
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
	
}
 
Example #8
Source File: AbstractScopeResourceDescriptionsTest.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
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);
	}
}