package gwt.client;

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.widgets.drawing.DrawPane;
import com.smartgwt.client.widgets.drawing.DrawPolygon;
import com.smartgwt.client.widgets.drawing.Point;
import com.smartgwt.client.widgets.drawing.events.ClickEvent;
import com.smartgwt.client.widgets.drawing.events.ClickHandler;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;

public class SmartGwtTest implements EntryPoint {
  private DrawPane drawPane;

  public static void setBoxColor(DrawPolygon drawPolygon) {
    int color = (int)(Math.random()*0xffffff);
    drawPolygon.setFillColor("#"+Integer.toHexString(color));
  }
  
  public void onModuleLoad() {
    drawPane = new DrawPane();
    drawPane.addDrawHandler(new DrawHandler() {
      public void onDraw(DrawEvent event) {
        for( int x=0; x<10; x++ ) {
          for( int y=0; y<10; y++) {
            DrawPolygon drawPolygon = new DrawPolygon();
            drawPolygon.addClickHandler(new ClickHandler() {
              public void onClick(ClickEvent event) {
                DrawPolygon drawPolygon = (DrawPolygon)event.getSource();
                setBoxColor(drawPolygon);
              }
            });

            drawPolygon.setDrawPane(drawPane);
            Point[] drawPoints = new Point[4];
            drawPoints[0] = new Point(x*100,y*100);
            drawPoints[1] = new Point((x+1)*100,y*100);
            drawPoints[2] = new Point((x+1)*100,(y+1)*100);
            drawPoints[3] = new Point(x*100,(y+1)*100);
            drawPolygon.setPoints(drawPoints);
            setBoxColor(drawPolygon);
            drawPolygon.draw();
          }
        }
      }     
    });
    drawPane.setCanDragScroll(true);
    drawPane.setEdgeSize(5);
    drawPane.setEdgeOpacity(25);
    drawPane.setShowEdges(true);
    drawPane.setZoomLevel(1.0);
    drawPane.setWidth(1200);
    drawPane.setHeight(750);
    drawPane.draw();    
  }
}
