]> source.dussan.org Git - vaadin-framework.git/commitdiff
Attach drag'n'drop drag image to overlay container (#10384) 50/450/1
authorArtur Signell <artur@vaadin.com>
Sun, 2 Dec 2012 11:11:35 +0000 (13:11 +0200)
committerArtur Signell <artur@vaadin.com>
Sun, 2 Dec 2012 13:16:37 +0000 (15:16 +0200)
Change-Id: I8f47f1d7e88c3a36088e18cb72ff2c65ce65f22b

client/src/com/vaadin/client/ui/VOverlay.java
client/src/com/vaadin/client/ui/dd/VDragAndDropManager.java

index dc6c7e4dffee799f35de634bdc4343a070aea858..ec09fbab69599249ab8a1dfd79f558e70b4d255a 100644 (file)
@@ -34,6 +34,7 @@ import com.vaadin.client.ApplicationConnection;
 import com.vaadin.client.BrowserInfo;
 import com.vaadin.client.ComponentConnector;
 import com.vaadin.client.Util;
+import com.vaadin.client.VConsole;
 
 /**
  * In Vaadin UI this Overlay should always be used for all elements that
@@ -637,32 +638,48 @@ public class VOverlay extends PopupPanel implements CloseHandler<PopupPanel> {
     }
 
     /**
-     * Gets the 'overlay container' element pertaining to the given
-     * {@link ApplicationConnection}. Each overlay should be created in a
-     * overlay container element, so that the correct theme and styles can be
-     * applied.
+     * Gets the 'overlay container' element. Tries to find the current
+     * {@link ApplicationConnection} using {@link #getApplicationConnection()}.
      * 
-     * @param ac
-     * @return
+     * @return the overlay container element for the current
+     *         {@link ApplicationConnection} or another element if the current
+     *         {@link ApplicationConnection} cannot be determined.
      */
     public Element getOverlayContainer() {
         ApplicationConnection ac = getApplicationConnection();
         if (ac == null) {
-            // could not figure out which one we belong to, styling might fail
+            // could not figure out which one we belong to, styling will
+            // probably fail
+            VConsole.error("Could not determin ApplicationConnection for Overlay. Overlay will be attached directly to the root panel");
             return RootPanel.get().getElement();
         } else {
-            String id = ac.getConfiguration().getRootPanelId();
-            id = id += "-overlays";
-            Element container = DOM.getElementById(id);
-            if (container == null) {
-                container = DOM.createDiv();
-                container.setId(id);
-                String styles = ac.getUIConnector().getWidget().getParent()
-                        .getStyleName();
-                container.setClassName(styles);
-                RootPanel.get().getElement().appendChild(container);
-            }
-            return container;
+            return getOverlayContainer(ac);
         }
     }
+
+    /**
+     * Gets the 'overlay container' element pertaining to the given
+     * {@link ApplicationConnection}. Each overlay should be created in a
+     * overlay container element, so that the correct theme and styles can be
+     * applied.
+     * 
+     * @param ac
+     *            A reference to {@link ApplicationConnection}
+     * @return The overlay container
+     */
+    public static Element getOverlayContainer(ApplicationConnection ac) {
+        String id = ac.getConfiguration().getRootPanelId();
+        id = id += "-overlays";
+        Element container = DOM.getElementById(id);
+        if (container == null) {
+            container = DOM.createDiv();
+            container.setId(id);
+            String styles = ac.getUIConnector().getWidget().getParent()
+                    .getStyleName();
+            container.setClassName(styles);
+            RootPanel.get().getElement().appendChild(container);
+        }
+        return container;
+    }
+
 }
index 6667dc7438ce84910bd2fbf9d328df2ed2b5542f..ae14218fff34aec5086510764cb68122482a4d4b 100644 (file)
@@ -39,7 +39,9 @@ import com.vaadin.client.ComponentConnector;
 import com.vaadin.client.MouseEventDetailsBuilder;
 import com.vaadin.client.UIDL;
 import com.vaadin.client.Util;
+import com.vaadin.client.VConsole;
 import com.vaadin.client.ValueMap;
+import com.vaadin.client.ui.VOverlay;
 import com.vaadin.shared.ApplicationConstants;
 import com.vaadin.shared.MouseEventDetails;
 import com.vaadin.shared.ui.dd.DragEventType;
@@ -567,7 +569,7 @@ public class VDragAndDropManager {
     private void clearDragElement() {
         if (dragElement != null) {
             if (dragElement.getParentElement() != null) {
-                RootPanel.getBodyElement().removeChild(dragElement);
+                dragElement.removeFromParent();
             }
             dragElement = null;
         }
@@ -708,7 +710,15 @@ public class VDragAndDropManager {
         @Override
         public void run() {
             if (dragElement != null && dragElement.getParentElement() == null) {
-                RootPanel.getBodyElement().appendChild(dragElement);
+                ApplicationConnection connection = getCurrentDragApplicationConnection();
+                Element dragImageParent;
+                if (connection == null) {
+                    VConsole.error("Could not determine ApplicationConnection for current drag operation. The drag image will likely look broken");
+                    dragImageParent = RootPanel.getBodyElement();
+                } else {
+                    dragImageParent = VOverlay.getOverlayContainer(connection);
+                }
+                dragImageParent.appendChild(dragElement);
             }
 
         }
@@ -720,6 +730,19 @@ public class VDragAndDropManager {
         return serverCallback != null;
     }
 
+    protected ApplicationConnection getCurrentDragApplicationConnection() {
+        if (currentDrag == null) {
+            return null;
+        }
+
+        final ComponentConnector dragSource = currentDrag.getTransferable()
+                .getDragSource();
+        if (dragSource == null) {
+            return null;
+        }
+        return dragSource.getConnection();
+    }
+
     /**
      * Method to que tasks until all dd related server visits are done
      *