Skip to main content



      Home
Home » Modeling » TMF (Xtext) » Validator Check Unique Name
Validator Check Unique Name [message #759016] Fri, 25 November 2011 10:24 Go to next message
Eclipse UserFriend
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 10:25] by Moderator

Re: Validator Check Unique Name [message #759057 is a reply to message #759016] Fri, 25 November 2011 16:01 Go to previous message
Eclipse UserFriend
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
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: Wed Jul 23 12:26:57 EDT 2025

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

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

Back to the top