package fortiss.gui.listeners.textfield;

import fortiss.components.Building;
import fortiss.components.Component;
import fortiss.gui.DesignerPanel;
import fortiss.gui.listeners.helper.InsertionVerifier;

public abstract class NameListener extends TextFieldListener {
	
	public NameListener() {
		super("Error. This field can not be empty nor duplicated");
	}

	@Override
	boolean isValidField(String text) {
		
		return !text.isEmpty() && isUnique(text);
	}

	private boolean isUnique(String text) {
		for(Building building: DesignerPanel.buildings.values()) {
			if(building.getName().equals(text)) {
				return false;
			}
			for(Component component : building.getComponents()) {
				if(component.getName().equals(text)) {
					return false;
				}
			}
		}
		return true;
	}

	@Override
	boolean isValidCharacter(char c, String text) {
		InsertionVerifier v = new InsertionVerifier();
		return v.isTextWithoutSpaces(c);
	}

	@Override
	abstract void update(String text);

	@Override
	boolean isValidLength(String text) {
		return true;
	}

	@Override
	abstract String getAttribute();
}

@@ -261,17 +261,16 @@
 tener %7B%0A
-%09
 %0A%09public
@@ -401,18 +401,16 @@
 text) %7B%0A
-%09%09
 %0A%09%09retur
@@ -493,19 +493,94 @@
 xt) %7B%0A%09%09
-for
+boolean compareBuilding = component == null;%0A%09%0A%09%09if(compareBuilding) %7B%0A%09%09%09for 
 (Buildin
@@ -589,16 +589,17 @@
 building
+ 
 : Design
@@ -631,16 +631,17 @@
 )) %7B%0A%09%09%09
+%09
 if(build
@@ -662,28 +662,77 @@
 equals(text)
+ && !TextFieldListener.building.equals(building)
 ) %7B%0A
+%09
 %09%09%09%09return f
@@ -744,16 +744,19 @@
 %0A%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09for
+ 
 (Com
@@ -806,16 +806,17 @@
 ) %7B%0A%09%09%09%09
+%09
 if(compo
@@ -843,19 +843,342 @@
 s(text))
- %7B%0A
+%0A%09%09%09%09%09%09return false;%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D else %7B%0A%09%09%09for (Building building : DesignerPanel.buildings.values()) %7B%0A%09%09%09%09if(building.getName().equals(text)) %7B%0A%09%09%09%09%09return false;%0A%09%09%09%09%7D%0A%09%09%09%09for (Component component : building.getComponents()) %7B%0A%09%09%09%09%09if(component.getName().equals(text) && !TextFieldListener.component.equals(component))%0A%09
 %09%09%09%09%09ret

Fix nameListener bug