summaryrefslogtreecommitdiffstats
path: root/rba.tool.editor/src/rba/tool/editor/validation/ContentAllocatableListValidationHelper.xtend
blob: d9f1911f2305077e69e61d9c9e3697eaa5828b14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package rba.tool.editor.validation

import com.google.inject.Inject
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EStructuralFeature
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.xtext.resource.IResourceServiceProvider
import org.eclipse.xtext.resource.impl.ResourceDescriptionsProvider
import org.eclipse.xtext.service.OperationCanceledManager
import org.eclipse.xtext.util.CancelIndicator
import org.eclipse.xtext.validation.ValidationMessageAcceptor
import rba.core.AbstractContent
import rba.core.RBACorePackage
import rba.sound.SoundContent
import rba.tool.editor.messages.Messages
import rba.tool.editor.resource.IRBAModelResourceLoader
import rba.view.ViewContent

class ContentAllocatableListValidationHelper {

	@Inject
	private IResourceServiceProvider.Registry resourceServiceProviderRegistry = IResourceServiceProvider.Registry.INSTANCE;

	@Inject
	private ResourceDescriptionsProvider resourceDescriptionsProvider;

	@Inject
	private IRBAModelResourceLoader resourceLoader;

	@Inject
	private ResourceSetImpl resourceSet;

	@Inject
	private OperationCanceledManager operationCanceledManager = new OperationCanceledManager();

	private String CONTENT_ALLOCATABLE_SIZE = Messages.CONTENT_ALLOCATABLE_SIZE;

	private String SOUND_ALLOCATABLE_SIZE = Messages.SOUND_ALLOCATABLE_SIZE;

	def public void checkCrossReferenceEmpty(Resource resource, CancelIndicator cancelIndicator, ValidationMessageAcceptor acceptor) {
		val resourceServiceProvider = resourceServiceProviderRegistry.getResourceServiceProvider(resource.getURI());
		if (resourceServiceProvider === null) {
			return;
		}

		demandGetResources(resource.getURI());
		if (resourceSet === null) {
			return;
		}

		val loadedResource = resourceSet.getResource(resource.getURI(), false);
		if (loadedResource !== null) {
			checkForAbstractContent_getAllocatableList(loadedResource, cancelIndicator, acceptor);
		}
	}

	def private void demandGetResources(URI uri) {
		if (!resourceSet.resources.isEmpty()) {
			resourceSet.resources.clear();
		}

		val projectName = if (uri.segmentCount > 2) URI.decode(uri.segment(1)) else "";
		val resourceDescriptions = resourceDescriptionsProvider.createResourceDescriptions();
		val URIs = resourceDescriptions.allResourceDescriptions.map(d|d.URI).filter(u|projectName.equals(if (u.segmentCount > 2) URI.decode(u.segment(1)) else null));
		resourceLoader.loadAndResolveResource(resourceSet, URIs, null);
	}

	def protected void checkForAbstractContent_getAllocatableList(Resource resource, CancelIndicator cancelIndicator, ValidationMessageAcceptor acceptor) {
		resource.allContents.toIterable.filter(ViewContent).forEach [ viewContent |
			if (viewContent.allocatableList.isEmpty) {
				val feature = getContainmentFeature(viewContent);
				if (feature !== null) {
					acceptor.acceptError(String.format(CONTENT_ALLOCATABLE_SIZE, viewContent.name), viewContent, feature, ValidationMessageAcceptor.INSIGNIFICANT_INDEX,
						getErrorCode());
				}
			}
			operationCanceledManager.checkCanceled(cancelIndicator);
		];
		resource.allContents.toIterable.filter(SoundContent).forEach [ soundContent |
			if (soundContent.allocatableList.isEmpty) {
				val feature = getContainmentFeature(soundContent);
				if (feature !== null) {
					acceptor.acceptError(String.format(SOUND_ALLOCATABLE_SIZE, soundContent.name), soundContent, feature, ValidationMessageAcceptor.INSIGNIFICANT_INDEX,
						getErrorCode());
				}
			}
			operationCanceledManager.checkCanceled(cancelIndicator);
		];
	}

	def protected EStructuralFeature getContainmentFeature(EObject object) {
		switch (object) {
			AbstractContent:
				return RBACorePackage.Literals.ABSTRACT_CONTENT__ALLOCATABLE
			default:
				return null
		}
	}

	def protected String getErrorCode() {
		return null;
	}
}