package ch.pmm.gwttest.client;

/*
 * 1  Fall: Overlapping Problem
 * 2. Fall: Border mit ClickHandler
 * 3. Falle: In Version 4.1p wird der letzte DrawRect nicht clickbar gemacht 
 */

import com.google.gwt.core.client.EntryPoint;
import com.smartgwt.client.types.Cursor;
import com.smartgwt.client.types.KnobType;
import com.smartgwt.client.types.LinePattern;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.widgets.drawing.DrawLabel;
import com.smartgwt.client.widgets.drawing.DrawPane;
import com.smartgwt.client.widgets.drawing.DrawRect;
import com.smartgwt.client.widgets.drawing.events.DragResizeMoveEvent;
import com.smartgwt.client.widgets.drawing.events.DragResizeMoveHandler;
import com.smartgwt.client.widgets.drawing.events.MovedEvent;
import com.smartgwt.client.widgets.drawing.events.MovedHandler;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;
import com.smartgwt.client.widgets.layout.VLayout;

public class GWTTest implements EntryPoint {
    private DrawPane m_oDrawPane;
    private DrawRect m_oDR1 = null;
    private DrawRect m_oDR2 = null;
    private DrawRect m_oDR3 = null;
    private DrawRect m_oDR4 = null;

    public void onModuleLoad() {
        m_oDrawPane = new DrawPane();
        m_oDrawPane.setHeight(450);
        m_oDrawPane.setWidth(700);
        m_oDrawPane.setLeft(25);
        m_oDrawPane.setShowEdges(true);
        m_oDrawPane.setEdgeSize(4);
        m_oDrawPane.setBackgroundColor("papayawhip");
        m_oDrawPane.setOverflow(Overflow.HIDDEN);
        m_oDrawPane.setCursor(Cursor.AUTO);
        m_oDrawPane.addDrawHandler(new DrawHandler()
        {
          public void onDraw(DrawEvent drawEvent)
          {
            buildDrawPane(drawEvent);            
          }
        });
        VLayout layout = new VLayout();
        layout.addMember(m_oDrawPane);        
        layout.draw();
    }
    
    /****************************************************************************
     * buildDrawPane
     * @param event
     ***************************************************************************/
    private void buildDrawPane(DrawEvent event) {     
      DrawPane drawPane = (DrawPane) event.getSource();
  
      DrawLabel oDrawLabelBug1 = new DrawLabel();
      oDrawLabelBug1.setDrawPane(m_oDrawPane);            
      oDrawLabelBug1.setTop(10);
      oDrawLabelBug1.setLeft(50);
      oDrawLabelBug1.setContents("Bug 1");
      oDrawLabelBug1.draw();
      m_oDR1 = new DrawRect();
      m_oDR1.setID("m_oDR1_RED");
      m_oDR1.setDrawPane(drawPane);
      m_oDR1.setLeft(50);
      m_oDR1.setTop(50);
      m_oDR1.setWidth(150);
      m_oDR1.setHeight(100);
      m_oDR1.setCanDrag(true);
      m_oDR1.setFillColor("RED");
      m_oDR1.setFillOpacity(0.8f);
      addHandlersToDrawRect(m_oDR1);
      m_oDR1.draw();
  
      m_oDR2 = new DrawRect();
      m_oDR2.setID("m_oDR2_BLUE");
      m_oDR2.setDrawPane(drawPane);
      m_oDR2.setLeft(100);
      m_oDR2.setTop(100);
      m_oDR2.setWidth(150);
      m_oDR2.setHeight(100);
      m_oDR2.setCanDrag(true);
      m_oDR2.setFillColor("BLUE");
      m_oDR2.setFillOpacity(0.8f);
      addHandlersToDrawRect(m_oDR2);
      m_oDR2.draw();
      
      m_oDR3 = new DrawRect();
      m_oDR3.setID("m_oDR3_GREEN");
      m_oDR3.setDrawPane(drawPane);
      m_oDR3.setLeft(150);
      m_oDR3.setTop(150);
      m_oDR3.setWidth(150);
      m_oDR3.setHeight(100);
      m_oDR3.setCanDrag(true);
      m_oDR3.setFillColor("GREEN");
      m_oDR3.setFillOpacity(0.8f);           
      addHandlersToDrawRect(m_oDR3);
      m_oDR3.draw();

      DrawLabel oDrawLabelBug2 = new DrawLabel();
      oDrawLabelBug2.setDrawPane(m_oDrawPane);
      oDrawLabelBug2.setTop(10);
      oDrawLabelBug2.setLeft(350);
      oDrawLabelBug2.setContents("Bug 2");
      oDrawLabelBug2.draw();
      m_oDR4 = new DrawRect();
      m_oDR4.setID("m_oDR4_YELLOW");
      m_oDR4.setDrawPane(drawPane);
      m_oDR4.setLeft(350);
      m_oDR4.setTop(50);
      m_oDR4.setWidth(150);
      m_oDR4.setHeight(100);
      m_oDR4.setCanDrag(true);
      m_oDR4.setFillColor("YELLOW");
      m_oDR4.setFillOpacity(0.8f);
      m_oDR4.setLinePattern(LinePattern.DASH);
      addHandlersToDrawRect(m_oDR4);
      m_oDR4.draw();
    }
    
    /**********************************************************************************
     * addHandlersToDrawRects
     * @param oDrawRect
     * @param oContainer
     **********************************************************************************/
    private void addHandlersToDrawRect(final DrawRect oDrawRect) {
      
      //------------------------------------------------------------------------------------------
      oDrawRect.addClickHandler(new com.smartgwt.client.widgets.drawing.events.ClickHandler() {
        public void onClick(com.smartgwt.client.widgets.drawing.events.ClickEvent clickEvent)
        {
          System.out.println("DrawRect: onclick RectID: "+oDrawRect.getID());
          clickEvent.cancel();                 
          manageRectClick(oDrawRect);
        }
      });
      
      //-----------------------------------------------------------------------------------------------    
      oDrawRect.addDragResizeMoveHandler(new DragResizeMoveHandler() {
        @Override
        public void onDragResizeMove(DragResizeMoveEvent event)
        {       
          System.out.println("DrawRect: onDragResizeMove");         
        } 
      });
      
      //-----------------------------------------------------------------------------------------------    
      oDrawRect.addMovedHandler(new MovedHandler() {
        @Override
        public void onMoved(MovedEvent event)
        {
          System.out.println("DrawRect: onMoved");        
        }
      });
    }
    
    /******************************************************************************************
     * manageRectClick
     * Manages the current selected Rect by showing the move/resize knobs around
     * it
     * 
     * @param oDrawRect
     ******************************************************************************************/
    public void manageRectClick(final DrawRect oDrawRect)
    {            
        m_oDR1.hideKnobs(KnobType.RESIZE);
        m_oDR2.hideKnobs(KnobType.RESIZE);
        m_oDR3.hideKnobs(KnobType.RESIZE);
        oDrawRect.showKnobs(KnobType.RESIZE); 
    }
}


