123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- package com.vaadin.ui;
-
- import java.util.Map;
-
- import com.vaadin.event.DataBoundTransferable;
- import com.vaadin.event.Transferable;
- import com.vaadin.event.dd.DragAndDropEvent;
- import com.vaadin.event.dd.DropHandler;
- import com.vaadin.event.dd.DropTarget;
- import com.vaadin.event.dd.DropTargetDetails;
- import com.vaadin.event.dd.DropTargetDetailsImpl;
- import com.vaadin.event.dd.acceptCriteria.AcceptAll;
- import com.vaadin.event.dd.acceptCriteria.AcceptCriterion;
- import com.vaadin.terminal.PaintException;
- import com.vaadin.terminal.PaintTarget;
- import com.vaadin.terminal.gwt.client.MouseEventDetails;
-
- /**
- * Test class that implements various component related Drag and Drop features.
- *
- * TODO consider implementing this kind of general purpose layout class:
- *
- * - extend simple component (CssLayout or CustomComponent, instead of absolute
- * layout)
- *
- * - implement both drag source and drop handler with server side api
- *
- * - implement html5 drop target etc
- *
- * - include a lots of details for drop event (coordinates & sizes of drop
- * position and widget/Paintable hierarchy up to drop handler)
- *
- * This way we could have one rather complex dd component that could be used (by
- * wrapping layouts) in most common situations with server side api. Core
- * layouts wouldn't need changes (and have regression risk/ performance
- * penalty).
- *
- * @deprecated use {@link DragAndDropWrapper} instead
- *
- */
- @Deprecated
- @SuppressWarnings("serial")
- @ClientWidget(com.vaadin.terminal.gwt.client.ui.VDragDropPane.class)
- public class DragDropPane extends AbsoluteLayout implements DropTarget {
-
- private DropHandler dropHandler;
-
- public DragDropPane(DropHandler dropHandler) {
- setWidth("400px");
- setHeight("300px");
-
- if (dropHandler == null) {
- this.dropHandler = new ImportPrettyMuchAnything();
- } else {
- this.dropHandler = dropHandler;
- }
-
- }
-
- public DragDropPane() {
- this(null);
- }
-
- @Override
- public void paintContent(PaintTarget target) throws PaintException {
- super.paintContent(target);
- dropHandler.getAcceptCriterion().paint(target);
- }
-
- public DropHandler getDropHandler() {
- return dropHandler;
- }
-
- public static class ImportPrettyMuchAnything implements DropHandler {
- public void drop(DragAndDropEvent event) {
- DragDropPane pane = (DragDropPane) event.getDropTargetDetails()
- .getTarget();
-
- DragEventDetails ed = (DragEventDetails) event
- .getDropTargetDetails();
- Transferable ctr = event.getTransferable();
- if (ctr.getSourceComponent() != null) {
- // use "component" (from DragDropPane) if available, else take
- // the source component
- Component component = (Component) ctr.getData("component");
- if (component == null) {
- component = ctr.getSourceComponent();
- }
-
- if (component.getParent() != pane) {
- if (ctr instanceof DataBoundTransferable) {
- // Item has been dragged, construct a Label from
- // Item id
- Label l = new Label();
- l.setSizeUndefined();
- l.setValue("ItemId : "
- + ((DataBoundTransferable) ctr).getItemId());
- pane.addComponent(l);
- component = l;
-
- } else {
- // we have a component that is been dragged, add
- // it
- // to
- // this
- pane.addComponent(component);
- }
-
- Integer left = ed.getAbsoluteLeft();
- Integer top = ed.getAbsoluteTop();
-
- MouseEventDetails eventDetails = ed.getMouseEvent();
-
- int clientX = eventDetails.getClientX();
- int clientY = eventDetails.getClientY();
-
- try {
- pane.getPosition(component).setTopValue(clientY - top);
- pane.getPosition(component)
- .setLeftValue(clientX - left);
- } catch (Exception e) {
- // TODO: handle exception
- }
- } else {
- // drag ended inside the this Pane
-
- MouseEventDetails start = ed.getMouseDownEvent();
- MouseEventDetails eventDetails = ed.getMouseEvent();
-
- int deltaX = eventDetails.getClientX() - start.getClientX();
- int deltaY = eventDetails.getClientY() - start.getClientY();
-
- ComponentPosition p = pane.getPosition(component);
- p.setTopValue(p.getTopValue() + deltaY);
- p.setLeftValue(p.getLeftValue() + deltaX);
-
- }
-
- } else {
- // drag coming outside of Vaadin
- String object = (String) ctr.getData("text/plain");
-
- String content = (String) ctr.getData("fileContents");
-
- Label l = new Label();
- l.setCaption("Generated from HTML5 drag:");
- if (object != null) {
- l.setValue(object);
- } else {
- l.setValue("HTML5 dd");
- }
-
- l.setDescription(content);
- l.setSizeUndefined();
-
- pane.addComponent(l);
-
- }
- return;
- }
-
- public AcceptCriterion getAcceptCriterion() {
- return AcceptAll.get();
- }
- }
-
- class DragEventDetails extends DropTargetDetailsImpl {
-
- public DragEventDetails(Map<String, Object> rawVariables) {
- super(rawVariables);
- }
-
- public Integer getAbsoluteTop() {
- return (Integer) getData("absoluteTop");
- }
-
- public Integer getAbsoluteLeft() {
- return (Integer) getData("absoluteLeft");
- }
-
- public MouseEventDetails getMouseDownEvent() {
- return MouseEventDetails.deSerialize((String) getData("mouseDown"));
- }
-
- public MouseEventDetails getMouseEvent() {
- return MouseEventDetails
- .deSerialize((String) getData("mouseEvent"));
- }
-
- }
-
- public DropTargetDetails translateDropTargetDetails(
- Map<String, Object> clientVariables) {
- return new DragEventDetails(clientVariables);
- }
-
- public void setDropHandler(DropHandler dropHandler2) {
- dropHandler = dropHandler2;
- requestRepaint();
- }
-
- }
|