From 84bf5a77e678f6d8a73f71d42fea2300b4f174cc Mon Sep 17 00:00:00 2001 From: John Ahlroos Date: Thu, 30 May 2013 16:19:47 +0300 Subject: [PATCH] Enabled drag & drop to Calendar #11048 Change-Id: I2dcc1f7159a6283cfbc0baafe6cba66530bf0a45 --- .../src/com/vaadin/client/ui/VCalendar.java | 26 +++- .../client/ui/calendar/CalendarConnector.java | 88 +++++-------- .../schedule/dd/CalendarDropHandler.java | 11 +- .../schedule/dd/CalendarMonthDropHandler.java | 5 + .../schedule/dd/CalendarWeekDropHandler.java | 5 + server/src/com/vaadin/ui/Calendar.java | 31 ++++- .../calendar/CalendarDragAndDrop.html | 59 +++++++++ .../calendar/CalendarDragAndDrop.java | 123 ++++++++++++++++++ 8 files changed, 289 insertions(+), 59 deletions(-) create mode 100644 uitest/src/com/vaadin/tests/components/calendar/CalendarDragAndDrop.html create mode 100644 uitest/src/com/vaadin/tests/components/calendar/CalendarDragAndDrop.java diff --git a/client/src/com/vaadin/client/ui/VCalendar.java b/client/src/com/vaadin/client/ui/VCalendar.java index c5c12f2d72..38bcc0b14f 100644 --- a/client/src/com/vaadin/client/ui/VCalendar.java +++ b/client/src/com/vaadin/client/ui/VCalendar.java @@ -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; + } } diff --git a/client/src/com/vaadin/client/ui/calendar/CalendarConnector.java b/client/src/com/vaadin/client/ui/calendar/CalendarConnector.java index be3abb39ea..5a83579d46 100644 --- a/client/src/com/vaadin/client/ui/calendar/CalendarConnector.java +++ b/client/src/com/vaadin/client/ui/calendar/CalendarConnector.java @@ -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 actionMap = new HashMap(); private HashMap tooltips = new HashMap(); + 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 days = state.days; List 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 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 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 actions = new ArrayList(); for (int i = 0; i < actionKeys.size(); i++) { diff --git a/client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarDropHandler.java b/client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarDropHandler.java index aab9ca9c38..ab0c9f2e9a 100644 --- a/client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarDropHandler.java +++ b/client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarDropHandler.java @@ -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; } /* diff --git a/client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarMonthDropHandler.java b/client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarMonthDropHandler.java index 913477ee14..fd0be4881e 100644 --- a/client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarMonthDropHandler.java +++ b/client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarMonthDropHandler.java @@ -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; diff --git a/client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarWeekDropHandler.java b/client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarWeekDropHandler.java index 0ea683dc3c..cede1827a2 100644 --- a/client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarWeekDropHandler.java +++ b/client/src/com/vaadin/client/ui/calendar/schedule/dd/CalendarWeekDropHandler.java @@ -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) * diff --git a/server/src/com/vaadin/ui/Calendar.java b/server/src/com/vaadin/ui/Calendar.java index 38fa355dd8..c3385baa2c 100644 --- a/server/src/com/vaadin/ui/Calendar.java +++ b/server/src/com/vaadin/ui/Calendar.java @@ -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 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 index 0000000000..425f95c529 --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/calendar/CalendarDragAndDrop.html @@ -0,0 +1,59 @@ + + + + + + +New Test + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
New Test
open/run/com.vaadin.tests.components.calendar.CalendarDragAndDrop?restartApplication
dragvaadin=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]76,7
dropvaadin=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]54,9
assertTextvaadin=runcomvaadintestscomponentscalendarCalendarDragAndDrop::PID_SCalendar/domChild[0]/domChild[1]/domChild[1]/domChild[0]/domChild[1]/domChild[1]/domChild[5]/domChild[0]/domChild[1]12:00 AM Event 10
mouseClickvaadin=runcomvaadintestscomponentscalendarCalendarDragAndDrop::PID_SCalendar/domChild[0]/domChild[1]/domChild[0]/domChild[0]/domChild[1]/domChild[1]/domChild[0]/domChild[0]7,85
dragvaadin=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]34,12
dropvaadin=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]75,9
assertTextvaadin=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]7:30 AM: Event 13
+ + 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 index 0000000000..7477fc84ce --- /dev/null +++ b/uitest/src/com/vaadin/tests/components/calendar/CalendarDragAndDrop.java @@ -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; + } + +} -- 2.39.5