Author: julien(a)jboss.com
Date: 2008-07-09 17:43:10 -0400 (Wed, 09 Jul 2008)
New Revision: 11389
Added:
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/dnd/DnDController.java
Modified:
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/dnd/DnDContext.java
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/dnd/Draggable.java
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/dnd/Droppable.java
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/model/AjaxPage.java
Log:
javadoc does not hurt
Modified:
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/dnd/DnDContext.java
===================================================================
---
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/dnd/DnDContext.java 2008-07-09
21:30:21 UTC (rev 11388)
+++
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/dnd/DnDContext.java 2008-07-09
21:43:10 UTC (rev 11389)
@@ -25,14 +25,30 @@
import java.util.Iterator;
/**
+ * A context for drag and drop operations.
+ *
* @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
* @version $Revision: 630 $
*/
public interface DnDContext
{
+ /**
+ * Returns an iterator of draggables found under the specifed coordinates.
+ *
+ * @param x the x position
+ * @param y the y position
+ * @return an iterator of draggables
+ */
Iterator getDraggables(int x, int y);
+ /**
+ * Returns an iterator of droppables found under the specifed coordinates.
+ *
+ * @param x the x position
+ * @param y the y position
+ * @return an iterator of droppables
+ */
Iterator findDroppable(int x, int y);
Copied:
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/dnd/DnDController.java
(from rev 11388,
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/dnd/DnDPanel.java)
===================================================================
---
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/dnd/DnDController.java
(rev 0)
+++
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/dnd/DnDController.java 2008-07-09
21:43:10 UTC (rev 11389)
@@ -0,0 +1,275 @@
+/******************************************************************************
+ * JBoss, a division of Red Hat *
+ * Copyright 2008, Red Hat Middleware, LLC, and individual *
+ * contributors as indicated by the @authors tag. See the *
+ * copyright.txt in the distribution for a full listing of *
+ * individual contributors. *
+ * *
+ * This is free software; you can redistribute it and/or modify it *
+ * under the terms of the GNU Lesser General Public License as *
+ * published by the Free Software Foundation; either version 2.1 of *
+ * the License, or (at your option) any later version. *
+ * *
+ * This software is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with this software; if not, write to the Free *
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org. *
+ ******************************************************************************/
+package org.jboss.portal.presentation.ajax.client.dnd;
+
+import com.google.gwt.user.client.ui.SimplePanel;
+import com.google.gwt.user.client.DOM;
+import com.google.gwt.user.client.Event;
+import com.google.gwt.user.client.Element;
+import com.google.gwt.user.client.EventPreview;
+import com.google.gwt.user.client.Window;
+import org.jboss.portal.presentation.ajax.client.util.logging.Logger;
+
+import java.util.Iterator;
+
+/**
+ * The drag and drop controller where all the magic happens. More generally this takes
care of coordinating
+ * dnd operations provided by the dnd context.
+ *
+ * @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
+ * @version $Revision: 630 $
+ */
+public class DnDController extends SimplePanel implements EventPreview
+{
+
+ /** . */
+ private final Logger log = Logger.getLogger(DnDController.class);
+
+ /** . */
+ private Drag drag;
+
+ /** . */
+ private Droppable droppable;
+
+ /** . */
+ private DnDContext context;
+
+ public DnDController(DnDContext context)
+ {
+ DOM.sinkEvents(getElement(), Event.MOUSEEVENTS);
+ DOM.addEventPreview(this);
+
+ //
+ this.context = context;
+ }
+
+ public boolean onEventPreview(Event event)
+ {
+ if (DOM.eventGetType(event) == Event.ONMOUSEDOWN)
+ {
+ int x = DOM.eventGetClientX(event);
+ int y = DOM.eventGetClientY(event);
+ Draggable draggable = findDraggable(x + Window.getScrollLeft(), y +
Window.getScrollTop());
+
+ //
+ if (draggable != null)
+ {
+ DOM.eventPreventDefault(event);
+ }
+ }
+
+ //
+ return true;
+ }
+
+ private Draggable findDraggable(int x, int y)
+ {
+ Iterator draggables = context.getDraggables(x, y);
+ if (draggables.hasNext())
+ {
+ return (Draggable)draggables.next();
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ private Droppable findDroppable(int x, int y)
+ {
+ Iterator droppables = context.findDroppable(x, y);
+
+ //
+ while (droppables.hasNext())
+ {
+ Droppable droppable = (Droppable)droppables.next();
+
+ //
+ if (!droppable.getId().equals(drag.draggable.getId()))
+ {
+ return droppable;
+ }
+ }
+
+ //
+ return null;
+ }
+
+ public void onBrowserEvent(Event event)
+ {
+ if (context != null)
+ {
+ Element element = DOM.eventGetTarget(event);
+
+ //
+ switch (DOM.eventGetType(event))
+ {
+ case Event.ONMOUSEDOWN:
+ {
+ int x = DOM.eventGetClientX(event);
+ int y = DOM.eventGetClientY(event);
+ Draggable draggable = findDraggable(x + Window.getScrollLeft(), y +
Window.getScrollTop());
+
+ //
+ if (draggable != null)
+ {
+ Element containerElement = draggable.getContainer();
+ int left = DOM.getAbsoluteLeft(draggable.getContainer()) -
Window.getScrollLeft();
+ int top = DOM.getAbsoluteTop(draggable.getContainer()) -
Window.getScrollTop();
+ int deltaX = x - left;
+ int deltaY = y - top;
+
+ //
+ log.debug("Starting drag element=(" + left + "," +
top + "), mouse=(" + x +
+ "," + y + "), delta=(" + deltaX + ","
+ deltaY + ") with draggable " + draggable);
+
+ //
+ draggable.startDragging();
+ DOM.setStyleAttribute(containerElement, "position",
"fixed");
+ DOM.setIntStyleAttribute(containerElement, "left", left);
+ DOM.setIntStyleAttribute(containerElement, "top", top);
+
+ //
+ drag = new Drag(
+ deltaX,
+ deltaY,
+ element,
+ draggable
+ );
+
+ //
+ updateDroppable(x + Window.getScrollLeft(), y +
Window.getScrollTop());
+
+ //
+ // DOM.setCapture(element);
+ }
+
+ //
+ break;
+ }
+ case Event.ONMOUSEUP:
+ {
+ if (drag != null)
+ {
+ stopDrag();
+ }
+ break;
+ }
+ case Event.ONMOUSEMOVE:
+ {
+ if (drag != null)
+ {
+ int x = DOM.eventGetClientX(event);
+ int y = DOM.eventGetClientY(event);
+ int newX = x - drag.deltaX;
+ int newY = y - drag.deltaY;
+ DOM.setIntStyleAttribute(drag.draggable.getContainer(),
"left", newX);
+ DOM.setIntStyleAttribute(drag.draggable.getContainer(),
"top", newY);
+ log.debug("Dragging at position (" + newX + "," +
newY + ") with draggable " + drag.draggable +
+ " on element " + element);
+
+ //
+ updateDroppable(x + Window.getScrollLeft(), y +
Window.getScrollTop());
+ }
+
+ //
+ break;
+ }
+ case Event.ONMOUSEOVER:
+ case Event.ONMOUSEOUT:
+ {
+// if (drag != null && element == getElement())
+// {
+// stopDrag();
+// }
+ break;
+ }
+ }
+
+
+ }
+ }
+
+ private void updateDroppable(int x, int y)
+ {
+ Droppable newDroppable = findDroppable(x, y);
+
+ //
+ if (newDroppable != null)
+ {
+ log.debug("Found droppable " + newDroppable);
+ }
+ //
+ if (droppable != newDroppable)
+ {
+ if (droppable != null)
+ {
+ droppable.leave(drag.draggable);
+ }
+ if (newDroppable != null)
+ {
+ newDroppable.enter(drag.draggable);
+ }
+ }
+
+ //
+ droppable = newDroppable;
+ }
+
+ private void stopDrag()
+ {
+ log.debug("Stopping drag with draggable " + drag.draggable + " and
droppable " + droppable);
+
+ //
+ if (droppable != null)
+ {
+ droppable.leave(drag.draggable);
+ drag.draggable.stopDragging();
+
+ //
+ if (droppable.accept(drag.draggable))
+ {
+ log.debug("Droppable " + droppable + " accepted the draggable
" + drag.draggable);
+ drag.draggable.accepted(droppable);
+ }
+ else
+ {
+ log.debug("Droppable " + droppable + " rejected the draggable
" + drag.draggable);
+ drag.draggable.rejected(droppable);
+ }
+
+ //
+ droppable = null;
+ }
+ else
+ {
+ drag.draggable.stopDragging();
+ }
+
+ // DOM.releaseCapture(drag.element);
+ Element containerElement = drag.draggable.getContainer();
+ DOM.setStyleAttribute(containerElement, "position", null);
+ drag = null;
+ }
+
+}
Modified:
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/dnd/Draggable.java
===================================================================
---
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/dnd/Draggable.java 2008-07-09
21:30:21 UTC (rev 11388)
+++
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/dnd/Draggable.java 2008-07-09
21:43:10 UTC (rev 11389)
@@ -25,31 +25,65 @@
import com.google.gwt.user.client.Element;
/**
+ * Something that is draggable.
+ *
* @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
* @version $Revision: 630 $
*/
public interface Draggable
{
+ /**
+ * Identifies the draggable. Note that the id can be used to compare a draggable
against
+ * a droppable.
+ *
+ * @return the id
+ */
String getId();
/**
- * Returns the element container that will be used for moving.
+ * Returns the element container that will be used for performing the visual move of
the drag operation.
*
* @return the element container
*/
Element getContainer();
+ /**
+ * Signal the start of a drag operation.
+ */
void startDragging();
+ /**
+ * Signal the stop of an on going drag operation.
+ */
void stopDragging();
+ /**
+ * Signals that the draggable has entered a droppable during a drag.
+ *
+ * @param droppable the droppable
+ */
void enter(Droppable droppable);
+ /**
+ * Signals that the draggable has left a droppable during a drag.
+ *
+ * @param droppable the droppable
+ */
void leave(Droppable droppable);
+ /**
+ * Signals that a droppable has accepted this draggable.
+ *
+ * @param droppable the droppable
+ */
void accepted(Droppable droppable);
+ /**
+ * Signals that a droppable has rejected this draggable.
+ *
+ * @param droppable the droppable
+ */
void rejected(Droppable droppable);
}
Modified:
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/dnd/Droppable.java
===================================================================
---
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/dnd/Droppable.java 2008-07-09
21:30:21 UTC (rev 11388)
+++
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/dnd/Droppable.java 2008-07-09
21:43:10 UTC (rev 11389)
@@ -23,18 +23,42 @@
package org.jboss.portal.presentation.ajax.client.dnd;
/**
+ * Something that is droppable.
+ *
* @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
* @version $Revision: 630 $
*/
public interface Droppable
{
+ /**
+ * Identifies the droppable. Note that the id can be used to compare a droppable
against
+ * a draggable.
+ *
+ * @return the id
+ */
String getId();
+ /**
+ * Signals that a draggable has entered this draggable.
+ *
+ * @param draggable the draggable
+ */
void enter(Draggable draggable);
+ /**
+ * Signals that a draggable has left this draggable.
+ *
+ * @param draggable the draggable
+ */
void leave(Draggable draggable);
+ /**
+ * Returns true if the droppable accepts the draggable.
+ *
+ * @param draggable the draggable
+ * @return true if the draggable is accepted
+ */
boolean accept(Draggable draggable);
}
Modified:
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/model/AjaxPage.java
===================================================================
---
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/model/AjaxPage.java 2008-07-09
21:30:21 UTC (rev 11388)
+++
modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/model/AjaxPage.java 2008-07-09
21:43:10 UTC (rev 11389)
@@ -31,10 +31,9 @@
import java.util.List;
import java.util.ArrayList;
-import org.jboss.portal.presentation.ajax.client.dnd.DnDPanel;
+import org.jboss.portal.presentation.ajax.client.dnd.DnDController;
import org.jboss.portal.presentation.ajax.client.dnd.DnDContext;
import org.jboss.portal.presentation.ajax.client.dnd.Draggable;
-import org.jboss.portal.presentation.ajax.client.dnd.Droppable;
/**
* @author <a href="mailto:julien@jboss-portal.org">Julien
Viet</a>
@@ -47,7 +46,7 @@
private VerticalPanel widget;
/** . */
- private DnDPanel dndPanel;
+ private DnDController dndController;
public AjaxPage(String id, Map properties)
{
@@ -56,7 +55,7 @@
protected void doRefresh(boolean force)
{
- doRefresh(dndPanel);
+ doRefresh(dndController);
}
public Widget getWidget()
@@ -69,12 +68,12 @@
Label title = new Label("Page " + getId());
MetaWidget meta = new MetaWidget(this);
VerticalPanel widget = new VerticalPanel();
- DnDPanel dndPanel = new DnDPanel(dndContext);
+ DnDController dndController = new DnDController(dndContext);
//
meta.add(title);
widget.add(meta);
- widget.add(dndPanel);
+ widget.add(dndController);
//
title.setStyleName("pf-Title");
@@ -82,7 +81,7 @@
//
this.widget = widget;
- this.dndPanel = dndPanel;
+ this.dndController = dndController;
}
protected void doDestroyWidget()