package de.devisnik.web.client.ui;

import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DeferredCommand;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

import de.devisnik.mine.IBoard;
import de.devisnik.mine.Point;

public class BoardCanvas extends Composite {

	private final IBoard itsBoard;
	private AbsolutePanel itsPanel;
	private final ClickHandler itsClickListener;


	public BoardCanvas(final IBoard board, ClickHandler clickListener) {
		super();
		itsBoard = board;
		itsClickListener = clickListener;
		itsPanel = new AbsolutePanel();
		initWidget(itsPanel);
		setSize(Integer.toString(15*itsBoard.getDimension().x), Integer.toString(15*itsBoard.getDimension().y));
	}

	protected void onLoad() {
		super.onLoad();
		DeferredCommand.addCommand(new Command() {

			public void execute() {
				initFields();
			}});
	}
	private void initFields() {
		final Point gameDimension = itsBoard.getDimension();
		for (int i = 0; i < gameDimension.y; i++) {
			for (int j = 0; j < gameDimension.x; j++) {
				final FieldCanvas fieldCanvas = new FieldCanvas(itsBoard
						.getField(j,i), j, i);
				itsPanel.add(fieldCanvas, 15 * j, 15 * i);
				fieldCanvas.addClickHandler(itsClickListener);
			}
		}
	}

	public void dispose() {
		for (Widget child : itsPanel) {
			FieldCanvas fieldCanvas = (FieldCanvas)child;
			fieldCanvas.dispose();
		}
	}
}

@@ -29,16 +29,61 @@
 nt.ui;%0A%0A
+import com.google.gwt.core.client.Scheduler;%0A
 import c
@@ -999,33 +999,39 @@
 ;%0A%09%09
-DeferredCommand.addComman
+Scheduler.get().scheduleDeferre
 d(ne
@@ -1044,20 +1044,28 @@
 and() %7B%0A
-%0A%09%09%09
+            
 public v
@@ -1084,30 +1084,60 @@
 ) %7B%0A
-%09%09%09%09initFields();%0A%09%09%09%7D
+                initFields();%0A            %7D%0A        
 %7D);%0A

use Scheduler instead of DeferredCommand