package BlitzEdit.core;

import java.util.ArrayList;
import java.util.Collection;

import BlitzEdit.core.Element;
import BlitzEdit.core.Component;

public final class Connector extends Element
{
	public ArrayList<Connector> getConnections()
	{
		return _connections;
	}
	
	//TODO: move-methode implementieren
	@Override
	public Connector move(int x, int y)
	{
		return this;
	}
	
	//Nimmt conn in die Verbindungsliste auf
	public void connect(Connector conn)
	{
		if (this.connected())
		{
			//Überprüft, ob Verbindung schon vorhanden ist
			for (Connector c : _connections)
			{
				if (c == conn)
					return;
			}
		}
		else
			_connected = true;
		
		_connections.add(conn);
	}
	
	//Nimmt alle Connectoren aus conns in die Verbindungsliste auf
	//(wenn nicht schon vorhanden)
	public void connect(Collection<Connector> conns)
	{
		if (this.connected())
		{
			//Überprüft, ob Verbindungen schon vorhanden sind
			for (Connector c : conns)
			{
				if (_connections.contains(c))
					continue;
				else
					_connections.add(c);
			}
		}
		else
			_connected = true;
		
		_connections.addAll(conns);
	}
	
	//Löst die Verbindung zum übergebenen Connecor
	public void disconnect(Connector conn)
	{
		if (this.connected())
		{
			for (Connector c : _connections)
			{
				if (c == conn)
					_connections.remove(conn);
			}
		}
		if (_connections.isEmpty())
			_connected = false;
	}
	
	//
	public void disconnect(Collection<Connector> conns)
	{
		if (this.connected())
		{
			for (Connector c : conns)
			{
				if (_connections.contains(c))
					_connections.remove(c);
			}
		}
		if (_connections.isEmpty())
			_connected = false;
	}
	
	public void disconnect(Component comp)
	{
		if (this.connected())
		{
			for (Connector c : comp.getConnectors())
			{
				if (_connections.contains(c))
					_connections.remove(c);
			}
		}
		if (_connections.isEmpty())
			_connected = false;
	}
	/*
	public void disconnect(Collection<Component> comps)
	{
		if (this.connected())
		{
			for (Component co : comps)
			{
				for (Connector c : comp.getConnectors())
				{
					if (_connections.contains(c))
						_connections.remove(c);
				}
			}
		}
		if (_connections.isEmpty())
			_connected = false;
	}
	*/
	public boolean connected()
	{
		if(_connected)
			return true;
		else return false;
	}
	
	public boolean isConnectedTo(Connector con)
	{
		return _connections.contains(con);
	}
	
	public boolean isConnectedTo(Component comp)
	{
		for (Connector conn : _connections)
		{
			if (conn.getOwner() == comp)
				return true;
		}
		return false;
	}
	
	public Component getOwner()
	{
		return _owner;
	}
	
	public Connector(Component owner)
	{
		super();
		if (owner != null)
		{
			_owner = owner;
		}
		_connected = false;
		_connections = new ArrayList<Connector>();
	}
	
	public Connector(int x, int y, Component owner)
	{
		super(x+3, y+3, 6, 6); //Standartwerte: breite 6, höhe 6
		if (owner != null)
		{
			_owner = owner;
		}
		_connected = false;
		_connections = new ArrayList<Connector>();
	}
	
	private ArrayList<Connector> _connections;
	private boolean _connected;
	private Component _owner;
}

@@ -1383,16 +1383,73 @@
 %09%7D%0A%09%0A%09//
+ Disconnects all Elements of elems%0A%09// that are connected
 %0A%09public
@@ -1472,39 +1472,37 @@
 (Collection%3C
-Connector%3E conn
+Element%3E elem
 s)%0A%09%7B%0A%09%09if (
@@ -1535,94 +1535,160 @@
 or (
-Connector c : conns)%0A%09%09%09%7B%0A%09%09%09%09if (_connections.contains(c)
+Element e : elems)%0A%09%09%09%7B%0A%09%09%09%09if (e instanceof Connector)%0A%09%09%09%09%09disconnect((Connector)e);%0A%09%09%09%09else if (e instanceof Component
 )%0A%09%09%09%09%09
-_
+dis
 connect
-ions.remove(c
+((Component)e
 );%0A%09
@@ -1745,32 +1745,72 @@
 d = false;%0A%09%7D%0A%09%0A
+%09// Disconnects all Connections to comp%0A
 %09public void dis
@@ -2046,320 +2046,8 @@
 %09%7D%0A%09
-/*%0A%09public void disconnect(Collection%3CComponent%3E comps)%0A%09%7B%0A%09%09if (this.connected())%0A%09%09%7B%0A%09%09%09for (Component co : comps)%0A%09%09%09%7B%0A%09%09%09%09for (Connector c : comp.getConnectors())%0A%09%09%09%09%7B%0A%09%09%09%09%09if (_connections.contains(c))%0A%09%09%09%09%09%09_connections.remove(c);%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09if (_connections.isEmpty())%0A%09%09%09_connected = false;%0A%09%7D%0A%09*/
 %0A%09pu

Update Connector.java