Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Validator Check Unique Name
Validator Check Unique Name [message #759016] Fri, 25 November 2011 15:24 Go to next message
Jens Missing name is currently offline Jens Missing nameFriend
Messages: 27
Registered: November 2011
Junior Member

I tried to implement a check to make sure that my elements have a unique name in the current scope. But my implementation is far from perfect. I would have to create a check for every element (TabItem, LayoutManager ..).

Here is my check:

	public void checkIfTabItemNameAlreadyExists(TabItem ti)
		EObject container = ti.eContainer();
		while(!(container instanceof Window)) {
			container = container.eContainer();
		TreeIterator<EObject> tree = container.eAllContents();
		while(tree.hasNext()) {
			EObject temp =;
			if(temp instanceof TabItem) {
				TabItem item = (TabItem) temp;
				if(item.getName().equals(ti.getName()) && !item.equals(ti)) {
					error("Tabitem name has to be unique!", ti, null, 0);

Do you have a better solution for that? Thanks in advance



[Updated on: Fri, 25 November 2011 15:25]

Report message to a moderator

Re: Validator Check Unique Name [message #759057 is a reply to message #759016] Fri, 25 November 2011 21:01 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13761
Registered: July 2009
Senior Member

first you could make use of some utility classes what are already there
(1) org.eclipse.xtext.EcoreUtil2.getContainerOfType(EObject, Class<T>)
(2) org.eclipse.xtext.EcoreUtil2.getAllContentsOfType(EObject, Class<T>)
and use a generic "helper method"

public class MyDslJavaValidator extends AbstractMyDslJavaValidator {

	public void checkGreetingStartsWithCapital(Stuff stuff) {
		checkDuplicates(stuff, MyDslPackage.Literals.STUFF__NAME);
	public void checkGreetingStartsWithCapital(Greeting g) {
		checkDuplicates(g, MyDslPackage.Literals.GREETING__NAME);
	private <T extends EObject> void checkDuplicates(T t, EStructuralFeature f) {
		String name = SimpleAttributeResolver.NAME_RESOLVER.apply(t);
		Window window = EcoreUtil2.getContainerOfType(t, Window.class);
		if (window != null) {
			for (EObject tx : EcoreUtil2.getAllContentsOfType(window, t.getClass())) {
				String tx_name = SimpleAttributeResolver.NAME_RESOLVER.apply(tx);
				if (tx_name.equals(name) && tx != t) {
					error("duplicate " + t.eClass().getName() + " " + name, f);


I you have control on the qualified names of your stuff
use NamesAreUniqueValidator + a customized NamesAreUniqueValidationHelper(getClusterTypes)


Need professional support for Xtext, Xpand, EMF?
Go to:
Twitter : @chrdietrich
Blog :
Previous Topic:use glpk jar in org.xtext.example.mydsl.ui.popup.actions
Next Topic:Combining the IJvmModelInferrer with custom code generation
Goto Forum:

Current Time: Tue Mar 02 21:49:15 GMT 2021

Powered by FUDForum. Page generated in 0.02235 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top