import java.awt.*;

public class TableController
{
	protected TableRepresentation table;
	protected TableView view;
	protected Label message;
	
	TableController(Label m)
	{
		message = m;
	}
	
	TableController(TableRepresentation t, TableView v, Label m)
	{
		table = t;
		view = v;
		message = m;
	}
	
	public void setTable(TableRepresentation t)
	{
		table = t;
	}
	
	public void setView(TableView v)
	{
		view = v;
	}
	
	public void labelChanged(int i, String s)
	{
		table.label[i] = s;
		view.setLabel(i, s);
	}
	
	public void cellChanged(int x, int y, String s)
	{
		Relation r;
		try {r = (Relation)table.cell[x][y].clone();}
		catch (CloneNotSupportedException e)
		{throw new RuntimeException(
			"Unexpected CloneNotSupportedException: " + e.toString());}
		try
		{
			r.set(s);
			message.setText("");
			this.cellChanged(x, y, r);
		}
		catch (FormatException e)
		{
			r = table.cell[x][y];
			message.setText(s + 
				" is not a valid relation. Old value restored.");
			view.updateCell(x, y);
		}
	}

	public void cellChanged(int x, int y, Relation r)
	{
		table.cell[x][y] = r;
		table.cell[y][x] = r.inverse();
		view.updateCell(x, y);
	}
	
	public void exception(String s)
	{
		message.setText(s);
	}
}