]> source.dussan.org Git - vaadin-framework.git/commitdiff
Enabled drag & drop to Calendar #11048
authorJohn Ahlroos <john@vaadin.com>
Thu, 30 May 2013 13:19:47 +0000 (16:19 +0300)
committerVaadin Code Review <review@vaadin.com>
Mon, 3 Jun 2013 11:07:19 +0000 (11:07 +0000)
Change-Id: I2dcc1f7159a6283cfbc0baafe6cba66530bf0a45

client/src/com/vaadin/client/ui/VCalendar.java
client/src/com/vaadin/client/ui/calendar/CalendarConnector.java
client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarDropHandler.java
client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarMonthDropHandler.java
client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarWeekDropHandler.java
server/src/com/vaadin/ui/Calendar.java
uitest/src/com/vaadin/tests/components/calendar/CalendarDragAndDrop.html [new file with mode: 0644]
uitest/src/com/vaadin/tests/components/calendar/CalendarDragAndDrop.java [new file with mode: 0644]

index c5c12f2d72b4fe97f671c65ecd2c2715c81c7a98..38bcc0b14f1a5e6f52793cce85417bfd99be130b 100644 (file)
@@ -39,6 +39,8 @@ import com.vaadin.client.ui.calendar.schedule.SimpleDayToolbar;
 import com.vaadin.client.ui.calendar.schedule.SimpleWeekToolbar;
 import com.vaadin.client.ui.calendar.schedule.WeekGrid;
 import com.vaadin.client.ui.calendar.schedule.WeeklyLongEvents;
+import com.vaadin.client.ui.calendar.schedule.dd.CalendarDropHandler;
+import com.vaadin.client.ui.dd.VHasDropHandler;
 import com.vaadin.shared.ui.calendar.DateConstants;
 
 /**
@@ -47,7 +49,7 @@ import com.vaadin.shared.ui.calendar.DateConstants;
  * @since 7.1
  * @author Vaadin Ltd.
  */
-public class VCalendar extends Composite {
+public class VCalendar extends Composite implements VHasDropHandler {
 
     public static final String ATTR_FIRSTDAYOFWEEK = "firstDay";
     public static final String ATTR_LASTDAYOFWEEK = "lastDay";
@@ -96,6 +98,8 @@ public class VCalendar extends Composite {
     private int firstHour;
     private int lastHour;
 
+    private CalendarDropHandler dropHandler;
+
     /**
      * Listener interface for listening to event click events
      */
@@ -1443,4 +1447,24 @@ public class VCalendar extends Composite {
     public void setForwardNavigationEnabled(boolean enabled) {
         forwardNavigationEnabled = enabled;
     }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see com.vaadin.client.ui.dd.VHasDropHandler#getDropHandler()
+     */
+    @Override
+    public CalendarDropHandler getDropHandler() {
+        return dropHandler;
+    }
+
+    /**
+     * Set the drop handler
+     * 
+     * @param dropHandler
+     *            The drophandler to use
+     */
+    public void setDropHandler(CalendarDropHandler dropHandler) {
+        this.dropHandler = dropHandler;
+    }
 }
index be3abb39ea3e32c7a8e004ff86348956c0b4c3b4..5a83579d4633c2f057b18859c7374c791a7bf6e3 100644 (file)
@@ -19,6 +19,7 @@ import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
+import java.util.Iterator;
 import java.util.List;
 
 import com.google.gwt.core.shared.GWT;
@@ -30,6 +31,7 @@ import com.google.gwt.user.client.Element;
 import com.google.gwt.user.client.Window;
 import com.google.gwt.user.client.ui.Widget;
 import com.vaadin.client.ApplicationConnection;
+import com.vaadin.client.Paintable;
 import com.vaadin.client.TooltipInfo;
 import com.vaadin.client.UIDL;
 import com.vaadin.client.Util;
@@ -60,7 +62,8 @@ import com.vaadin.client.ui.calendar.schedule.HasTooltipKey;
 import com.vaadin.client.ui.calendar.schedule.MonthEventLabel;
 import com.vaadin.client.ui.calendar.schedule.SimpleDayCell;
 import com.vaadin.client.ui.calendar.schedule.dd.CalendarDropHandler;
-import com.vaadin.client.ui.dd.VHasDropHandler;
+import com.vaadin.client.ui.calendar.schedule.dd.CalendarMonthDropHandler;
+import com.vaadin.client.ui.calendar.schedule.dd.CalendarWeekDropHandler;
 import com.vaadin.shared.ui.Connect;
 import com.vaadin.shared.ui.Connect.LoadStyle;
 import com.vaadin.shared.ui.calendar.CalendarClientRpc;
@@ -79,16 +82,16 @@ import com.vaadin.ui.Calendar;
  */
 @Connect(value = Calendar.class, loadStyle = LoadStyle.LAZY)
 public class CalendarConnector extends AbstractComponentConnector implements
-        VHasDropHandler, ActionOwner, SimpleManagedLayout {
+        ActionOwner, SimpleManagedLayout, Paintable {
 
     private CalendarServerRpc rpc = RpcProxy.create(CalendarServerRpc.class,
             this);
 
-    private CalendarDropHandler dropHandler;
-
     private final HashMap<String, String> actionMap = new HashMap<String, String>();
     private HashMap<Object, String> tooltips = new HashMap<Object, String>();
 
+    private static final String DROPHANDLER_ACCEPT_CRITERIA_PAINT_TAG = "-ac";
+
     /**
      * 
      */
@@ -306,13 +309,16 @@ public class CalendarConnector extends AbstractComponentConnector implements
         });
     }
 
+    private boolean showingMonthView() {
+        return getState().days.size() > 7;
+    }
+
     @Override
     public void onStateChanged(StateChangeEvent stateChangeEvent) {
         super.onStateChanged(stateChangeEvent);
 
         CalendarState state = getState();
         VCalendar widget = getWidget();
-        boolean monthView = state.days.size() > 7;
 
         // Enable or disable the forward and backward navigation buttons
         widget.setForwardNavigationEnabled(hasEventListener(CalendarEventId.FORWARD));
@@ -336,10 +342,19 @@ public class CalendarConnector extends AbstractComponentConnector implements
         List<CalendarState.Day> days = state.days;
         List<CalendarState.Event> events = state.events;
 
-        if (monthView) {
+        CalendarDropHandler dropHandler = getWidget().getDropHandler();
+        if (showingMonthView()) {
             updateMonthView(days, events);
+            if (dropHandler != null
+                    && !(dropHandler instanceof CalendarMonthDropHandler)) {
+                getWidget().setDropHandler(new CalendarMonthDropHandler(this));
+            }
         } else {
             updateWeekView(days, events);
+            if (dropHandler != null
+                    && !(dropHandler instanceof CalendarWeekDropHandler)) {
+                getWidget().setDropHandler(new CalendarWeekDropHandler(this));
+            }
         }
 
         updateSizes();
@@ -355,32 +370,22 @@ public class CalendarConnector extends AbstractComponentConnector implements
      * com.vaadin.terminal.gwt.client.Paintable#updateFromUIDL(com.vaadin.terminal
      * .gwt.client.UIDL, com.vaadin.terminal.gwt.client.ApplicationConnection)
      */
+    @Override
     public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
-
-        // check for DD -related access criteria
-        // Iterator<Object> childIterator = uidl.getChildIterator();
-        // while (childIterator.hasNext()) {
-        // UIDL child = (UIDL) childIterator.next();
-        //
-        // // Drag&drop
-        // if (ACCESSCRITERIA.equals(child.getTag())) {
-        // if (monthView
-        // && !(getDropHandler() instanceof CalendarMonthDropHandler)) {
-        // setDropHandler(new CalendarMonthDropHandler());
-        //
-        // } else if (!monthView
-        // && !(getDropHandler() instanceof CalendarWeekDropHandler)) {
-        // setDropHandler(new CalendarWeekDropHandler());
-        // }
-        //
-        // getDropHandler().setCalendarPaintable(this);
-        // getDropHandler().updateAcceptRules(child);
-        //
-        // } else {
-        // setDropHandler(null);
-        // }
-        //
-        // }
+        Iterator<Object> childIterator = uidl.getChildIterator();
+        while (childIterator.hasNext()) {
+            UIDL child = (UIDL) childIterator.next();
+            if (DROPHANDLER_ACCEPT_CRITERIA_PAINT_TAG.equals(child.getTag())) {
+                if (getWidget().getDropHandler() == null) {
+                    getWidget().setDropHandler(
+                            showingMonthView() ? new CalendarMonthDropHandler(
+                                    this) : new CalendarWeekDropHandler(this));
+                }
+                getWidget().getDropHandler().updateAcceptRules(child);
+            } else {
+                getWidget().setDropHandler(null);
+            }
+        }
     }
 
     /**
@@ -449,27 +454,6 @@ public class CalendarConnector extends AbstractComponentConnector implements
                 calendarDayListOf(days));
     }
 
-    /*
-     * (non-Javadoc)
-     * 
-     * @see
-     * com.vaadin.terminal.gwt.client.ui.dd.VHasDropHandler#getDropHandler()
-     */
-    @Override
-    public CalendarDropHandler getDropHandler() {
-        return dropHandler;
-    }
-
-    /**
-     * Set the drop handler
-     * 
-     * @param dropHandler
-     *            The drophandler to use
-     */
-    public void setDropHandler(CalendarDropHandler dropHandler) {
-        this.dropHandler = dropHandler;
-    }
-
     private Action[] getActionsBetween(Date start, Date end) {
         List<Action> actions = new ArrayList<Action>();
         for (int i = 0; i < actionKeys.size(); i++) {
index aab9ca9c3867695e448eb39529c93844751fefd5..ab0c9f2e9a335fe5d5712ee8adbb30a78ebb5fa3 100644 (file)
@@ -28,15 +28,16 @@ import com.vaadin.client.ui.dd.VAbstractDropHandler;
  */
 public abstract class CalendarDropHandler extends VAbstractDropHandler {
 
-    protected CalendarConnector calendarConnector;
+    protected final CalendarConnector calendarConnector;
 
     /**
-     * Set the calendar instance
+     * Constructor
      * 
-     * @param calendarPaintable
+     * @param connector
+     *            The connector of the calendar
      */
-    public void setConnector(CalendarConnector calendarConnector) {
-        this.calendarConnector = calendarConnector;
+    public CalendarDropHandler(CalendarConnector connector) {
+        calendarConnector = connector;
     }
 
     /*
index 913477ee1438da47309877b821cc196ed5db9268..fd0be4881e021d2e0680ddd18dda6d62d27c03c2 100644 (file)
@@ -18,6 +18,7 @@ package com.vaadin.client.ui.calendar.schedule.dd;
 import com.google.gwt.user.client.DOM;
 import com.google.gwt.user.client.Element;
 import com.vaadin.client.Util;
+import com.vaadin.client.ui.calendar.CalendarConnector;
 import com.vaadin.client.ui.calendar.schedule.SimpleDayCell;
 import com.vaadin.client.ui.dd.VAcceptCallback;
 import com.vaadin.client.ui.dd.VDragEvent;
@@ -32,6 +33,10 @@ import com.vaadin.client.ui.dd.VDragEvent;
  */
 public class CalendarMonthDropHandler extends CalendarDropHandler {
 
+    public CalendarMonthDropHandler(CalendarConnector connector) {
+        super(connector);
+    }
+
     private Element currentTargetElement;
     private SimpleDayCell currentTargetDay;
 
index 0ea683dc3c76892cc87dae461f60985007526962..cede1827a2f320f78ec3ab8ec7a00c38a581b1d6 100644 (file)
@@ -18,6 +18,7 @@ package com.vaadin.client.ui.calendar.schedule.dd;
 import com.google.gwt.user.client.DOM;
 import com.google.gwt.user.client.Element;
 import com.vaadin.client.Util;
+import com.vaadin.client.ui.calendar.CalendarConnector;
 import com.vaadin.client.ui.calendar.schedule.DateCell;
 import com.vaadin.client.ui.calendar.schedule.DateCellDayEvent;
 import com.vaadin.client.ui.dd.VAcceptCallback;
@@ -36,6 +37,10 @@ public class CalendarWeekDropHandler extends CalendarDropHandler {
     private com.google.gwt.user.client.Element currentTargetElement;
     private DateCell currentTargetDay;
 
+    public CalendarWeekDropHandler(CalendarConnector connector) {
+        super(connector);
+    }
+
     /*
      * (non-Javadoc)
      * 
index 38fa355dd839c030fa2fb29a479bc24328c29ac5..c3385baa2ce9d87b35f27eaa3bc687ed30034ff8 100644 (file)
@@ -45,6 +45,8 @@ import com.vaadin.event.dd.DropHandler;
 import com.vaadin.event.dd.DropTarget;
 import com.vaadin.event.dd.TargetDetails;
 import com.vaadin.server.KeyMapper;
+import com.vaadin.server.PaintException;
+import com.vaadin.server.PaintTarget;
 import com.vaadin.shared.ui.calendar.CalendarEventId;
 import com.vaadin.shared.ui.calendar.CalendarServerRpc;
 import com.vaadin.shared.ui.calendar.CalendarState;
@@ -114,7 +116,7 @@ public class Calendar extends AbstractComponent implements
         CalendarComponentEvents.RangeSelectNotifier,
         CalendarComponentEvents.EventResizeNotifier,
         CalendarEventProvider.EventSetChangeListener, DropTarget,
-        CalendarEditableEventProvider, Action.Container {
+        CalendarEditableEventProvider, Action.Container, LegacyComponent {
 
     /**
      * Calendar can use either 12 hours clock or 24 hours clock.
@@ -1842,4 +1844,31 @@ public class Calendar extends AbstractComponent implements
             }
         }
     }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see com.vaadin.server.VariableOwner#changeVariables(java.lang.Object,
+     * java.util.Map)
+     */
+    @Override
+    public void changeVariables(Object source, Map<String, Object> variables) {
+        /*
+         * Only defined to fulfill the LegacyComponent interface used for
+         * calendar drag & drop. No implementation required.
+         */
+    }
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see
+     * com.vaadin.ui.LegacyComponent#paintContent(com.vaadin.server.PaintTarget)
+     */
+    @Override
+    public void paintContent(PaintTarget target) throws PaintException {
+        if (dropHandler != null) {
+            dropHandler.getAcceptCriterion().paint(target);
+        }
+    }
 }
\ No newline at end of file
diff --git a/uitest/src/com/vaadin/tests/components/calendar/CalendarDragAndDrop.html b/uitest/src/com/vaadin/tests/components/calendar/CalendarDragAndDrop.html
new file mode 100644 (file)
index 0000000..425f95c
--- /dev/null
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head profile="http://selenium-ide.openqa.org/profiles/test-case">
+<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+<link rel="selenium.base" href="http://localhost:7171/" />
+<title>New Test</title>
+</head>
+<body>
+<table cellpadding="1" cellspacing="1" border="1">
+<thead>
+<tr><td rowspan="1" colspan="3">New Test</td></tr>
+</thead><tbody>
+<tr>
+       <td>open</td>
+       <td>/run/com.vaadin.tests.components.calendar.CalendarDragAndDrop?restartApplication</td>
+       <td></td>
+</tr>
+<!--// Test drag and drop in month view-->
+<tr>
+       <td>drag</td>
+       <td>vaadin=runcomvaadintestscomponentscalendarCalendarDragAndDrop::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[0]/VHorizontalLayout[0]/Slot[1]/VScrollTable[0]/domChild[1]/domChild[0]/domChild[1]/domChild[0]/domChild[9]/domChild[0]/domChild[0]</td>
+       <td>76,7</td>
+</tr>
+<tr>
+       <td>drop</td>
+       <td>vaadin=runcomvaadintestscomponentscalendarCalendarDragAndDrop::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[0]/VHorizontalLayout[0]/Slot[0]/VCalendar[0]/domChild[0]/domChild[1]/domChild[1]/domChild[0]/domChild[1]/domChild[1]/domChild[5]/domChild[0]/domChild[2]</td>
+       <td>54,9</td>
+</tr>
+<tr>
+       <td>assertText</td>
+       <td>vaadin=runcomvaadintestscomponentscalendarCalendarDragAndDrop::PID_SCalendar/domChild[0]/domChild[1]/domChild[1]/domChild[0]/domChild[1]/domChild[1]/domChild[5]/domChild[0]/domChild[1]</td>
+       <td>12:00 AM Event 10</td>
+</tr>
+<!--//Test drag and drop in week view-->
+<tr>
+       <td>mouseClick</td>
+       <td>vaadin=runcomvaadintestscomponentscalendarCalendarDragAndDrop::PID_SCalendar/domChild[0]/domChild[1]/domChild[0]/domChild[0]/domChild[1]/domChild[1]/domChild[0]/domChild[0]</td>
+       <td>7,85</td>
+</tr>
+<tr>
+       <td>drag</td>
+       <td>vaadin=runcomvaadintestscomponentscalendarCalendarDragAndDrop::/VVerticalLayout[0]/Slot[1]/VVerticalLayout[0]/Slot[0]/VHorizontalLayout[0]/Slot[1]/VScrollTable[0]/domChild[1]/domChild[0]/domChild[1]/domChild[0]/domChild[11]/domChild[0]/domChild[0]</td>
+       <td>34,12</td>
+</tr>
+<tr>
+       <td>drop</td>
+       <td>vaadin=runcomvaadintestscomponentscalendarCalendarDragAndDrop::PID_SCalendar/domChild[0]/domChild[3]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[2]/domChild[0]/domChild[15]</td>
+       <td>75,9</td>
+</tr>
+<tr>
+       <td>assertText</td>
+       <td>vaadin=runcomvaadintestscomponentscalendarCalendarDragAndDrop::PID_SCalendar/domChild[0]/domChild[3]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[0]/domChild[2]/domChild[0]/domChild[48]/domChild[0]</td>
+       <td>7:30 AM: Event 13</td>
+</tr>
+
+</tbody></table>
+</body>
+</html>
diff --git a/uitest/src/com/vaadin/tests/components/calendar/CalendarDragAndDrop.java b/uitest/src/com/vaadin/tests/components/calendar/CalendarDragAndDrop.java
new file mode 100644 (file)
index 0000000..7477fc8
--- /dev/null
@@ -0,0 +1,123 @@
+/*
+ * Copyright 2000-2013 Vaadin Ltd.
+ * 
+ * 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 com.vaadin.tests.components.calendar;
+
+import java.util.GregorianCalendar;
+import java.util.Locale;
+
+import com.vaadin.event.dd.DragAndDropEvent;
+import com.vaadin.event.dd.DropHandler;
+import com.vaadin.event.dd.acceptcriteria.AcceptAll;
+import com.vaadin.event.dd.acceptcriteria.AcceptCriterion;
+import com.vaadin.server.VaadinRequest;
+import com.vaadin.tests.components.AbstractTestUI;
+import com.vaadin.ui.Calendar;
+import com.vaadin.ui.HorizontalLayout;
+import com.vaadin.ui.Table;
+import com.vaadin.ui.Table.TableDragMode;
+import com.vaadin.ui.Table.TableTransferable;
+import com.vaadin.ui.components.calendar.CalendarTargetDetails;
+import com.vaadin.ui.components.calendar.event.BasicEvent;
+
+public class CalendarDragAndDrop extends AbstractTestUI {
+
+    private Calendar calendar;
+
+    private Table table;
+
+    private class TestDropHandler implements DropHandler {
+
+        @Override
+        public void drop(DragAndDropEvent event) {
+            CalendarTargetDetails details = (CalendarTargetDetails) event
+                    .getTargetDetails();
+
+            TableTransferable transferable = (TableTransferable) event
+                    .getTransferable();
+
+            calendar.addEvent(new BasicEvent(transferable.getItemId()
+                    .toString(), "This event was dragged here", details
+                    .getDropTime()));
+
+            table.removeItem(transferable.getItemId());
+        }
+
+        @Override
+        public AcceptCriterion getAcceptCriterion() {
+            return AcceptAll.get();
+        }
+    }
+
+    @Override
+    protected void setup(VaadinRequest request) {
+        setSizeFull();
+        getContent().setSizeFull();
+        getLayout().setSizeFull();
+
+        HorizontalLayout root = new HorizontalLayout();
+        root.setSizeFull();
+        addComponent(root);
+
+        Locale locale = new Locale("en", "US");
+        GregorianCalendar cal = new GregorianCalendar(locale);
+        cal.set(2013, 0, 1);
+
+        calendar = new Calendar();
+        calendar.setId("Calendar");
+        calendar.setLocale(locale);
+        calendar.setDropHandler(new TestDropHandler());
+        calendar.setSizeFull();
+        root.addComponent(calendar);
+
+        calendar.setStartDate(cal.getTime());
+        cal.add(java.util.Calendar.MONTH, 1);
+        calendar.setEndDate(cal.getTime());
+
+        table = new Table();
+        table.setSizeFull();
+        table.setDragMode(TableDragMode.ROW);
+        table.addGeneratedColumn("COLUMN", new Table.ColumnGenerator() {
+
+            @Override
+            public Object generateCell(Table source, Object itemId,
+                    Object columnId) {
+                return itemId;
+            }
+        });
+
+        for (int eventNum = 1; eventNum < 50; eventNum++) {
+            table.addItem("Event " + eventNum);
+        }
+
+        root.addComponent(table);
+    }
+
+    @Override
+    protected String getTestDescription() {
+        // TODO Auto-generated method stub
+        return null;
+    }
+
+    @Override
+    protected Integer getTicketNumber() {
+        return 11048;
+    }
+
+}