/*
 * Copyright 2012 JBoss Inc
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.uberfire.client.workbench.widgets.dnd;

import javax.enterprise.context.Dependent;
import javax.enterprise.event.Event;
import javax.inject.Inject;

import org.uberfire.client.workbench.Position;
import org.uberfire.client.workbench.WorkbenchPanelPresenter;
import org.uberfire.client.workbench.WorkbenchPartPresenter;
import org.uberfire.client.workbench.model.PanelDefinition;
import org.uberfire.client.workbench.model.PartDefinition;
import org.uberfire.client.workbench.widgets.events.WorkbenchPartDroppedEvent;
import org.uberfire.client.workbench.widgets.panels.PanelManager;

import com.allen_sauer.gwt.dnd.client.DragContext;
import com.allen_sauer.gwt.dnd.client.VetoDragException;
import com.allen_sauer.gwt.dnd.client.drop.DropController;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;

/**
 * A Drop Controller covering the entire WorkbenchPanel that renders a Compass
 * with which to select the target position of the drag operation.
 */
@Dependent
public class CompassDropController
    implements
    DropController {

    private final CompassWidget              compass = CompassWidget.getInstance();

    private WorkbenchPanelPresenter.View     dropTarget;

    @Inject
    private PanelManager                     panelManager;

    @Inject
    private WorkbenchDragAndDropManager      dndManager;

    @Inject
    private Event<WorkbenchPartDroppedEvent> workbenchPartDroppedEvent;

    public void setup(final WorkbenchPanelPresenter.View view) {
        dropTarget = view;
    }

    @Override
    //When entering a WorkbenchPanel show the Compass
    public void onEnter(DragContext context) {
        compass.onEnter( context );
    }

    @Override
    //Hide the WorkbenchPanel's Compass
    public void onLeave(DragContext context) {
        compass.onLeave( context );
    }

    @Override
    public void onMove(DragContext context) {
        compass.onMove( context );
    }

    @Override
    public void onDrop(DragContext context) {

        //If not dropTarget has been identified do nothing
        Position p = compass.getDropPosition();
        if ( p == Position.NONE ) {
            return;
        }

        compass.onDrop( context );

        //Move Part from source to target 
        final WorkbenchPartPresenter.View view = (WorkbenchPartPresenter.View) context.draggable;
        final WorkbenchDragContext workbenchContext = dndManager.getWorkbenchContext();
        final PartDefinition sourcePart = workbenchContext.getSourcePart();
        final PanelDefinition sourcePanel = workbenchContext.getSourcePanel();
        final IsWidget tabWidget = workbenchContext.getTabWidget();
        final Integer height = workbenchContext.getHeight();
        final Integer width = workbenchContext.getWidth();
        final Integer minHeight = workbenchContext.getMinHeight();
        final Integer minWidth = workbenchContext.getMinWidth();

        final PanelDefinition dropPanel = dropTarget.getPresenter().getDefinition();

        //If the Target Panel is the same as the Source we're trying to reposition the 
        //Source's tab within itself. If the Source Panel has only one Tab there is no 
        //net effect. If we're trying to drop as a new tab there is no net effect.
        if ( sourcePanel.equals( dropPanel ) ) {
            if ( sourcePanel.getParts().size() == 1 ) {
                return;
            }
            if ( p == Position.SELF ) {
                return;
            }
        }

        workbenchPartDroppedEvent.fire( new WorkbenchPartDroppedEvent( sourcePart ) );
        final PanelDefinition targetPanel = panelManager.addWorkbenchPanel( dropPanel,
                                                                            p,
                                                                            height,
                                                                            width,
                                                                            minHeight,
                                                                            minWidth );
        panelManager.addWorkbenchPart( tabWidget,
                                       sourcePart,
                                       targetPanel,
                                       view.getWrappedWidget() );
    }

    @Override
    public Widget getDropTarget() {
        return this.dropTarget.asWidget();
    }

    @Override
    public void onPreviewDrop(DragContext context) throws VetoDragException {
    }

}

@@ -1641,16 +1641,20 @@
 troller%0A
+    
     impl
@@ -1660,16 +1660,20 @@
 lements%0A
+    
     Drop
@@ -1718,29 +1718,16 @@
 sWidget 
-             
 compass 
@@ -1799,20 +1799,16 @@
 er.View 
-    
 dropTarg
@@ -1849,36 +1849,16 @@
 Manager 
-                    
 panelMan
@@ -1916,21 +1916,16 @@
 Manager 
-     
 dndManag
@@ -2036,16 +2036,17 @@
 d setup(
+ 
 final Wo
@@ -2076,16 +2076,17 @@
 iew view
+ 
 ) %7B%0A    
@@ -2207,16 +2207,17 @@
 onEnter(
+ 
 DragCont
@@ -2219,32 +2219,33 @@
 gContext context
+ 
 ) %7B%0A        comp
@@ -2353,16 +2353,17 @@
 onLeave(
+ 
 DragCont
@@ -2365,32 +2365,33 @@
 gContext context
+ 
 ) %7B%0A        comp
@@ -2458,16 +2458,17 @@
  onMove(
+ 
 DragCont
@@ -2474,24 +2474,25 @@
 text context
+ 
 ) %7B%0A        
@@ -2558,24 +2558,25 @@
 void onDrop(
+ 
 DragContext 
@@ -2582,16 +2582,17 @@
  context
+ 
 ) %7B%0A%0A   
@@ -3285,36 +3285,12 @@
 t = 
-workbenchContext.getHeight()
+null
 ;%0A  
@@ -3321,35 +3321,12 @@
 h = 
-workbenchContext.getWidth()
+null
 ;%0A  
@@ -4998,16 +4998,17 @@
 iewDrop(
+ 
 DragCont
@@ -5018,16 +5018,17 @@
  context
+ 
 ) throws

Fix DnD issues (including that reported by porcelli!)