You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DragHandle.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Copyright 2000-2014 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.dd;
  17. import com.google.gwt.dom.client.DivElement;
  18. import com.google.gwt.dom.client.Element;
  19. import com.google.gwt.user.client.DOM;
  20. import com.google.gwt.user.client.Event;
  21. import com.google.gwt.user.client.EventListener;
  22. import com.vaadin.client.WidgetUtil;
  23. import com.vaadin.client.ui.dd.DragAndDropHandler.DragAndDropCallback;
  24. /**
  25. * Drag handle implementation. Drag handles are used for moving or resizing
  26. * widgets. This is a minimal-case component, meant to be used specifically as a
  27. * drag handle attached to another widget or element. As such, it does
  28. * <b>not</b> provide access to the events it's listening to (from the point of
  29. * view of this component, there really is no use for that). For the more
  30. * general, event-providing interface that this component is based on, see
  31. * {@link DragAndDropHandler}.
  32. *
  33. * @since 7.6
  34. */
  35. public class DragHandle {
  36. /**
  37. * Callback interface for the DragHandle event life cycle
  38. */
  39. public interface DragHandleCallback {
  40. /**
  41. * Called when dragging starts
  42. */
  43. public void onStart();
  44. /**
  45. * Called when the drag handle has moved.
  46. *
  47. * @param deltaX
  48. * change in X direction since start
  49. * @param deltaY
  50. * change in Y direction since start
  51. */
  52. public void onUpdate(double deltaX, double deltaY);
  53. /**
  54. * Called when the drag operation has been cancelled (usually by
  55. * pressing ESC)
  56. */
  57. public void onCancel();
  58. /**
  59. * Called when the drag operation completes successfully
  60. */
  61. public void onComplete();
  62. }
  63. private Element parent;
  64. private DivElement element;
  65. private String baseClassName;
  66. private DragAndDropHandler dndHandler;
  67. private DragAndDropCallback dndCallback;
  68. private DragHandleCallback userCallback;
  69. /**
  70. * Creates a new DragHandle.
  71. *
  72. * @param baseName
  73. * CSS style name to use for this DragHandle element. This
  74. * parameter is supplied to the constructor (rather than added
  75. * later) both to provide the "-dragged" style and to make sure
  76. * that the drag handle can be properly styled (it's otherwise
  77. * invisible)
  78. * @param callback
  79. * Callback object allows hooking up the drag handle to the rest
  80. * of the program logic
  81. */
  82. public DragHandle(String baseName, DragHandleCallback callback) {
  83. parent = null;
  84. element = DivElement.as(DOM.createElement("div"));
  85. baseClassName = baseName;
  86. userCallback = callback;
  87. addStyleName(baseClassName);
  88. dndCallback = new DragAndDropCallback() {
  89. private double startX;
  90. private double startY;
  91. @Override
  92. public void onDrop() {
  93. removeDraggingStyle();
  94. userCallback.onComplete();
  95. }
  96. @Override
  97. public void onDragUpdate(Event e) {
  98. double dx = WidgetUtil.getTouchOrMouseClientX(e) - startX;
  99. double dy = WidgetUtil.getTouchOrMouseClientY(e) - startY;
  100. userCallback.onUpdate(dx, dy);
  101. }
  102. @Override
  103. public boolean onDragStart(Event e) {
  104. addDraggingStyle();
  105. startX = WidgetUtil.getTouchOrMouseClientX(e);
  106. startY = WidgetUtil.getTouchOrMouseClientY(e);
  107. userCallback.onStart();
  108. return true;
  109. }
  110. @Override
  111. public void onDragEnd() {
  112. // NOP, handled in onDrop and onDragCancel
  113. }
  114. @Override
  115. public void onDragCancel() {
  116. removeDraggingStyle();
  117. userCallback.onCancel();
  118. }
  119. private void addDraggingStyle() {
  120. addStyleName(baseClassName + "-dragged");
  121. }
  122. private void removeDraggingStyle() {
  123. removeStyleName(baseClassName + "-dragged");
  124. }
  125. };
  126. dndHandler = new DragAndDropHandler();
  127. DOM.sinkEvents(element, Event.ONMOUSEDOWN | Event.ONTOUCHSTART);
  128. DOM.setEventListener(element, new EventListener() {
  129. @Override
  130. public void onBrowserEvent(Event event) {
  131. dndHandler.onDragStartOnDraggableElement(event, dndCallback);
  132. event.stopPropagation();
  133. }
  134. });
  135. }
  136. /**
  137. * Returns the current parent element for this drag handle. May be null.
  138. *
  139. * @return an Element or null
  140. */
  141. public Element getParent() {
  142. return parent;
  143. }
  144. /**
  145. * Gets the element used as actual drag handle.
  146. *
  147. * @return an Element
  148. */
  149. public Element getElement() {
  150. return element;
  151. }
  152. /**
  153. * Adds this drag handle to an HTML element.
  154. *
  155. * @param elem
  156. * an element
  157. */
  158. public void addTo(Element elem) {
  159. removeFromParent();
  160. parent = elem;
  161. parent.appendChild(element);
  162. }
  163. /**
  164. * Removes this drag handle from whatever it was attached to.
  165. */
  166. public void removeFromParent() {
  167. if (parent != null) {
  168. parent.removeChild(element);
  169. parent = null;
  170. }
  171. }
  172. /**
  173. * Adds CSS style name to the drag handle element.
  174. *
  175. * @param styleName
  176. * a CSS style name
  177. */
  178. public void addStyleName(String styleName) {
  179. element.addClassName(styleName);
  180. }
  181. /**
  182. * Removes existing style name from drag handle element.
  183. *
  184. * @param styleName
  185. * a CSS style name
  186. */
  187. public void removeStyleName(String styleName) {
  188. element.removeClassName(styleName);
  189. }
  190. }