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
hi,

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:

 
	@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 = tree.next();
			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


regards,

Jens.

[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: 13416
Registered: July 2009
Senior Member
Hi,

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 {

	@Check
	public void checkGreetingStartsWithCapital(Stuff stuff) {
		checkDuplicates(stuff, MyDslPackage.Literals.STUFF__NAME);
	}
	
	@Check
	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)

~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
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: Thu Apr 02 01:09:59 GMT 2020

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

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

Back to the top