Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

CalendarWeekDropHandler.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright 2000-2013 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client.ui.calendar.schedule.dd;
  17. import com.google.gwt.user.client.DOM;
  18. import com.vaadin.client.Util;
  19. import com.vaadin.client.ui.calendar.CalendarConnector;
  20. import com.vaadin.client.ui.calendar.schedule.DateCell;
  21. import com.vaadin.client.ui.calendar.schedule.DateCellDayEvent;
  22. import com.vaadin.client.ui.dd.VAcceptCallback;
  23. import com.vaadin.client.ui.dd.VDragEvent;
  24. /**
  25. * Handles DD when the weekly view is showing in the Calendar. In the weekly
  26. * view, drops are only allowed in the the time slots for each day. The slot
  27. * index and the day index are included in the drop details sent to the server.
  28. *
  29. * @since 7.1
  30. * @author Vaadin Ltd.
  31. */
  32. public class CalendarWeekDropHandler extends CalendarDropHandler {
  33. private com.google.gwt.user.client.Element currentTargetElement;
  34. private DateCell currentTargetDay;
  35. public CalendarWeekDropHandler(CalendarConnector connector) {
  36. super(connector);
  37. }
  38. /*
  39. * (non-Javadoc)
  40. *
  41. * @see
  42. * com.vaadin.terminal.gwt.client.ui.dd.VAbstractDropHandler#dragAccepted
  43. * (com.vaadin.terminal.gwt.client.ui.dd.VDragEvent)
  44. */
  45. @Override
  46. protected void dragAccepted(VDragEvent drag) {
  47. deEmphasis();
  48. currentTargetElement = drag.getElementOver();
  49. currentTargetDay = Util
  50. .findWidget(currentTargetElement, DateCell.class);
  51. emphasis();
  52. }
  53. /**
  54. * Removes the CSS style name from the emphasized element
  55. */
  56. private void deEmphasis() {
  57. if (currentTargetElement != null) {
  58. currentTargetDay.removeEmphasisStyle(currentTargetElement);
  59. currentTargetElement = null;
  60. }
  61. }
  62. /**
  63. * Add a CSS stylen name to current target element
  64. */
  65. private void emphasis() {
  66. currentTargetDay.addEmphasisStyle(currentTargetElement);
  67. }
  68. /*
  69. * (non-Javadoc)
  70. *
  71. * @see
  72. * com.vaadin.terminal.gwt.client.ui.dd.VAbstractDropHandler#dragOver(com
  73. * .vaadin.terminal.gwt.client.ui.dd.VDragEvent)
  74. */
  75. @Override
  76. public void dragOver(final VDragEvent drag) {
  77. if (isLocationValid(drag.getElementOver())) {
  78. validate(new VAcceptCallback() {
  79. @Override
  80. public void accepted(VDragEvent event) {
  81. dragAccepted(drag);
  82. }
  83. }, drag);
  84. }
  85. }
  86. /**
  87. * Checks if the location is a valid drop location
  88. *
  89. * @param elementOver
  90. * The element to check
  91. * @return
  92. */
  93. private boolean isLocationValid(
  94. com.google.gwt.user.client.Element elementOver) {
  95. com.google.gwt.user.client.Element weekGridElement = calendarConnector
  96. .getWidget().getWeekGrid().getElement();
  97. com.google.gwt.user.client.Element timeBarElement = calendarConnector
  98. .getWidget().getWeekGrid().getTimeBar().getElement();
  99. com.google.gwt.user.client.Element todayBarElement = null;
  100. if (calendarConnector.getWidget().getWeekGrid().hasToday()) {
  101. todayBarElement = (com.google.gwt.user.client.Element) calendarConnector
  102. .getWidget().getWeekGrid().getDateCellOfToday()
  103. .getTodaybarElement();
  104. }
  105. // drops are not allowed in:
  106. // - weekday header
  107. // - allday event list
  108. // - todaybar
  109. // - timebar
  110. // - events
  111. return DOM.isOrHasChild(weekGridElement, elementOver)
  112. && !DOM.isOrHasChild(timeBarElement, elementOver)
  113. && todayBarElement != elementOver
  114. && (Util.findWidget(elementOver, DateCellDayEvent.class) == null);
  115. }
  116. /*
  117. * (non-Javadoc)
  118. *
  119. * @see
  120. * com.vaadin.terminal.gwt.client.ui.dd.VAbstractDropHandler#dragEnter(com
  121. * .vaadin.terminal.gwt.client.ui.dd.VDragEvent)
  122. */
  123. @Override
  124. public void dragEnter(VDragEvent drag) {
  125. // NOOP, we determine drag acceptance in dragOver
  126. }
  127. /*
  128. * (non-Javadoc)
  129. *
  130. * @see
  131. * com.vaadin.terminal.gwt.client.ui.dd.VAbstractDropHandler#drop(com.vaadin
  132. * .terminal.gwt.client.ui.dd.VDragEvent)
  133. */
  134. @Override
  135. public boolean drop(VDragEvent drag) {
  136. if (isLocationValid(drag.getElementOver())) {
  137. updateDropDetails(drag);
  138. deEmphasis();
  139. return super.drop(drag);
  140. } else {
  141. deEmphasis();
  142. return false;
  143. }
  144. }
  145. /**
  146. * Update the drop details sent to the server
  147. *
  148. * @param drag
  149. * The drag event
  150. */
  151. private void updateDropDetails(VDragEvent drag) {
  152. int slotIndex = currentTargetDay.getSlotIndex(currentTargetElement);
  153. int dayIndex = calendarConnector.getWidget().getWeekGrid()
  154. .getDateCellIndex(currentTargetDay);
  155. drag.getDropDetails().put("dropDayIndex", dayIndex);
  156. drag.getDropDetails().put("dropSlotIndex", slotIndex);
  157. }
  158. /*
  159. * (non-Javadoc)
  160. *
  161. * @see
  162. * com.vaadin.terminal.gwt.client.ui.dd.VAbstractDropHandler#dragLeave(com
  163. * .vaadin.terminal.gwt.client.ui.dd.VDragEvent)
  164. */
  165. @Override
  166. public void dragLeave(VDragEvent drag) {
  167. deEmphasis();
  168. super.dragLeave(drag);
  169. }
  170. }