From portal-commits at lists.jboss.org Wed Jul 9 17:43:11 2008 Content-Type: multipart/mixed; boundary="===============4416811593989087994==" MIME-Version: 1.0 From: portal-commits at lists.jboss.org To: portal-commits at lists.jboss.org Subject: [portal-commits] JBoss Portal SVN: r11389 - in modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client: model and 1 other directory. Date: Wed, 09 Jul 2008 17:43:10 -0400 Message-ID: --===============4416811593989087994== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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/presentat= ion/ajax/client/dnd/DnDController.java Modified: modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentat= ion/ajax/client/dnd/DnDContext.java modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentat= ion/ajax/client/dnd/Draggable.java modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentat= ion/ajax/client/dnd/Droppable.java modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presentat= ion/ajax/client/model/AjaxPage.java Log: javadoc does not hurt Modified: modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/pr= esentation/ajax/client/dnd/DnDContext.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presenta= tion/ajax/client/dnd/DnDContext.java 2008-07-09 21:30:21 UTC (rev 11388) +++ modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presenta= tion/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 Julien Viet * @version $Revision: 630 $ */ public interface DnDContext { = + /** + * Returns an iterator of draggables found under the specifed coordinat= es. + * + * @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 coordinat= es. + * + * @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/pres= entation/ajax/client/dnd/DnDController.java (from rev 11388, modules/presen= tation/trunk/ajax/src/main/java/org/jboss/portal/presentation/ajax/client/d= nd/DnDPanel.java) =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presenta= tion/ajax/client/dnd/DnDController.java (rev 0) +++ modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presenta= tion/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 generall= y this takes care of coordinating + * dnd operations provided by the dnd context. + * + * @author Julien Viet + * @version $Revision: 630 $ + */ +public class DnDController extends SimplePanel implements EventPreview +{ + + /** . */ + private final Logger log =3D 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 =3D context; + } + + public boolean onEventPreview(Event event) + { + if (DOM.eventGetType(event) =3D=3D Event.ONMOUSEDOWN) + { + int x =3D DOM.eventGetClientX(event); + int y =3D DOM.eventGetClientY(event); + Draggable draggable =3D findDraggable(x + Window.getScrollLeft(),= y + Window.getScrollTop()); + + // + if (draggable !=3D null) + { + DOM.eventPreventDefault(event); + } + } + + // + return true; + } + + private Draggable findDraggable(int x, int y) + { + Iterator draggables =3D context.getDraggables(x, y); + if (draggables.hasNext()) + { + return (Draggable)draggables.next(); + } + else + { + return null; + } + } + + private Droppable findDroppable(int x, int y) + { + Iterator droppables =3D context.findDroppable(x, y); + + // + while (droppables.hasNext()) + { + Droppable droppable =3D (Droppable)droppables.next(); + + // + if (!droppable.getId().equals(drag.draggable.getId())) + { + return droppable; + } + } + + // + return null; + } + + public void onBrowserEvent(Event event) + { + if (context !=3D null) + { + Element element =3D DOM.eventGetTarget(event); + + // + switch (DOM.eventGetType(event)) + { + case Event.ONMOUSEDOWN: + { + int x =3D DOM.eventGetClientX(event); + int y =3D DOM.eventGetClientY(event); + Draggable draggable =3D findDraggable(x + Window.getScrollL= eft(), y + Window.getScrollTop()); + + // + if (draggable !=3D null) + { + Element containerElement =3D draggable.getContainer(); + int left =3D DOM.getAbsoluteLeft(draggable.getContainer(= )) - Window.getScrollLeft(); + int top =3D DOM.getAbsoluteTop(draggable.getContainer())= - Window.getScrollTop(); + int deltaX =3D x - left; + int deltaY =3D y - top; + + // + log.debug("Starting drag element=3D(" + left + "," + top= + "), mouse=3D(" + x + + "," + y + "), delta=3D(" + deltaX + "," + deltaY + ")= with draggable " + draggable); + + // + draggable.startDragging(); + DOM.setStyleAttribute(containerElement, "position", "fix= ed"); + DOM.setIntStyleAttribute(containerElement, "left", left); + DOM.setIntStyleAttribute(containerElement, "top", top); + + // + drag =3D new Drag( + deltaX, + deltaY, + element, + draggable + ); + + // + updateDroppable(x + Window.getScrollLeft(), y + Window.g= etScrollTop()); + + // + // DOM.setCapture(element); + } + + // + break; + } + case Event.ONMOUSEUP: + { + if (drag !=3D null) + { + stopDrag(); + } + break; + } + case Event.ONMOUSEMOVE: + { + if (drag !=3D null) + { + int x =3D DOM.eventGetClientX(event); + int y =3D DOM.eventGetClientY(event); + int newX =3D x - drag.deltaX; + int newY =3D 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.g= etScrollTop()); + } + + // + break; + } + case Event.ONMOUSEOVER: + case Event.ONMOUSEOUT: + { +// if (drag !=3D null && element =3D=3D getElement()) +// { +// stopDrag(); +// } + break; + } + } + + + } + } + + private void updateDroppable(int x, int y) + { + Droppable newDroppable =3D findDroppable(x, y); + + // + if (newDroppable !=3D null) + { + log.debug("Found droppable " + newDroppable); + } + // + if (droppable !=3D newDroppable) + { + if (droppable !=3D null) + { + droppable.leave(drag.draggable); + } + if (newDroppable !=3D null) + { + newDroppable.enter(drag.draggable); + } + } + + // + droppable =3D newDroppable; + } + + private void stopDrag() + { + log.debug("Stopping drag with draggable " + drag.draggable + " and d= roppable " + droppable); + + // + if (droppable !=3D 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 =3D null; + } + else + { + drag.draggable.stopDragging(); + } + + // DOM.releaseCapture(drag.element); + Element containerElement =3D drag.draggable.getContainer(); + DOM.setStyleAttribute(containerElement, "position", null); + drag =3D null; + } + +} Modified: modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/pr= esentation/ajax/client/dnd/Draggable.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presenta= tion/ajax/client/dnd/Draggable.java 2008-07-09 21:30:21 UTC (rev 11388) +++ modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presenta= tion/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 Julien Viet * @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 v= isual 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/pr= esentation/ajax/client/dnd/Droppable.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presenta= tion/ajax/client/dnd/Droppable.java 2008-07-09 21:30:21 UTC (rev 11388) +++ modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presenta= tion/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 Julien Viet * @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/pr= esentation/ajax/client/model/AjaxPage.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presenta= tion/ajax/client/model/AjaxPage.java 2008-07-09 21:30:21 UTC (rev 11388) +++ modules/presentation/trunk/ajax/src/main/java/org/jboss/portal/presenta= tion/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 Julien Viet @@ -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 =3D new Label("Page " + getId()); MetaWidget meta =3D new MetaWidget(this); VerticalPanel widget =3D new VerticalPanel(); - DnDPanel dndPanel =3D new DnDPanel(dndContext); + DnDController dndController =3D 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 =3D widget; - this.dndPanel =3D dndPanel; + this.dndController =3D dndController; } = protected void doDestroyWidget() --===============4416811593989087994==--