summaryrefslogtreecommitdiffstats
path: root/rba.tool.editor/src/rba/tool/editor/validation/validators/SoundContentValidator.xtend
blob: 68f5d35f779a464a17600fc741f30b4b1ba979d9 (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
package rba.tool.editor.validation.validators

import java.util.HashSet
import java.util.function.Consumer
import org.eclipse.xtext.validation.Check
import org.eclipse.xtext.validation.EValidatorRegistrar
import rba.core.AbstractAllocatable
import rba.core.RBACorePackage
import rba.sound.SoundContent
import rba.sound.Zone
import rba.sound.ZoneSet
import rba.tool.editor.messages.Messages
import rba.tool.editor.rbaEditorModel.CTag
import rba.sound.RBASoundPackage

class SoundContentValidator extends AbstractContentValidator {

	private String SOUND_ALLOCATABLE_INVALID_TYPE = Messages.SOUND_ALLOCATABLE_INVALID_TYPE;

	private String SOUND_ALLOCATABLE_DUPLICATE = Messages.SOUND_ALLOCATABLE_DUPLICATE;

	override register(EValidatorRegistrar registrar) {
		// not needed for classes used as ComposedCheck
	}

	private Consumer<String> errorToName = [msg|error(msg, RBACorePackage.Literals.NAMED_ELEMENT__NAME, 0)];

	@Check(NORMAL)
	def checkSOUND(SoundContent soundContent) {
		var hash = new HashSet<AbstractAllocatable>
		for (var index = 0; index < soundContent.allocatable.size; index.operator_plusPlus()) {
			val AbstractAllocatable abstractAllocatable = soundContent.allocatable.get(index);
			if(!(abstractAllocatable instanceof Zone || abstractAllocatable instanceof ZoneSet)) {
				error(String.format(SOUND_ALLOCATABLE_INVALID_TYPE, soundContent.name), RBACorePackage.Literals.ABSTRACT_CONTENT__ALLOCATABLE, index);
				return;
			}
			if(hash.contains(abstractAllocatable)) {
				error(String.format(SOUND_ALLOCATABLE_DUPLICATE, soundContent.name), RBACorePackage.Literals.ABSTRACT_CONTENT__ALLOCATABLE, index);
				return;
			} else {
				hash.add(abstractAllocatable)
			}
		}

//		for (var index = 0; index < soundContent.tags.size; index.operator_plusPlus()) {
//			val Tag tag = soundContent.tags.get(index);
//			if (!tag.stereotype.targetModelName.equals(SoundContent.simpleName)) {
//				error(String.format(TARGET_MODEL_NAME_MISMATCH, SoundContent.simpleName, tag.stereotype.targetModelName), RBACorePackage.Literals.TAG__STEREOTYPE);
//				return;
//			}
//		}
	}

	@Check(NORMAL)
	def check0RequiredFields(SoundContent soundContent) {
//		println(this.class.simpleName)
		val tags = soundContent.tags;

		if(tags.isNullOrEmpty || !tags.filter(CTag).isEmpty) {
			if(!doRequiredFieldsCheck(soundContent)) return;
		}
	}

	def protected doRequiredFieldsCheck(SoundContent soundContent) {
		var passed = false;
		passed = ValidatorUtils.mustHaveLeastOneState(soundContent, errorToName);
		if(!passed) {
			return false;
		}
		passed = false;
		for (var index = 0; index < soundContent.contentGroup.size; index.operator_plusPlus()) {
			if(!soundContent.contentGroup.get(index).allocatable.empty) {
				passed = true
			}
		}
		if(!passed) {
			passed = ValidatorUtils.mustHaveLeastOneAllocatable(soundContent, errorToName);
			if(!passed) {
				return false;
			}
		}

		return true;
	}

}