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.

VHtml5DragEvent.java 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Copyright 2000-2021 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.core.client.JsArrayString;
  18. import com.google.gwt.dom.client.NativeEvent;
  19. import com.vaadin.client.extensions.DropTargetExtensionConnector;
  20. /**
  21. * Helper class to access html5 style drag events.
  22. *
  23. * @author Vaadin Ltd
  24. * @deprecated Since 8.1, no direct replacement currently, see
  25. * {@link DropTargetExtensionConnector}
  26. */
  27. @Deprecated
  28. public class VHtml5DragEvent extends NativeEvent {
  29. /** Singleton. */
  30. protected VHtml5DragEvent() {
  31. }
  32. /**
  33. * Returns type values, or {@code ["Text","Url","Html"]} if types are not
  34. * supported.
  35. *
  36. * @return types
  37. */
  38. public final native JsArrayString getTypes()
  39. /*-{
  40. // IE does not support types, return some basic values
  41. return this.dataTransfer.types ? this.dataTransfer.types : ["Text","Url","Html"];
  42. }-*/;
  43. /**
  44. * Returns the data for the given type as text.
  45. *
  46. * @param type
  47. * the type whose data to retrieve
  48. * @return the data as text
  49. */
  50. public final native String getDataAsText(String type)
  51. /*-{
  52. var v = this.dataTransfer.getData(type);
  53. return v;
  54. }-*/;
  55. /**
  56. * Works on FF 3.6 and possibly with gears.
  57. *
  58. * @param index
  59. * the index of the file to get
  60. * @return the file as text
  61. * @deprecated this method is no longer used internally
  62. */
  63. @Deprecated
  64. public final native String getFileAsString(int index)
  65. /*-{
  66. if (this.dataTransfer.files.length > 0 && this.dataTransfer.files[0].getAsText) {
  67. return this.dataTransfer.files[index].getAsText("UTF-8");
  68. }
  69. return null;
  70. }-*/;
  71. /**
  72. * Sets the drop effect value.
  73. *
  74. * @param effect
  75. * the drop effect
  76. */
  77. public final native void setDropEffect(String effect)
  78. /*-{
  79. try {
  80. this.dataTransfer.dropEffect = effect;
  81. } catch (e) {}
  82. }-*/;
  83. /**
  84. * Returns whether drop effect is allowed or not.
  85. *
  86. * @return {@code true} id drop effect is allowed, {@code false} otherwise
  87. */
  88. public final native String getEffectAllowed()
  89. /*-{
  90. return this.dataTransfer.effectAllowed;
  91. }-*/;
  92. /**
  93. * Sets whether drop effect is allowed or not.
  94. *
  95. * @param effect
  96. * {@code true} id drop effect should be allowed, {@code false}
  97. * otherwise
  98. */
  99. public final native void setEffectAllowed(String effect)
  100. /*-{
  101. this.dataTransfer.effectAllowed = effect;
  102. }-*/;
  103. /**
  104. * Returns the transfer file count.
  105. *
  106. * @return the file count
  107. */
  108. public final native int getFileCount()
  109. /*-{
  110. return this.dataTransfer.files ? this.dataTransfer.files.length : 0;
  111. }-*/;
  112. /**
  113. * Returns the file indicated by the given index.
  114. *
  115. * @param fileIndex
  116. * the index of the file
  117. * @return the file
  118. */
  119. public final native VHtml5File getFile(int fileIndex)
  120. /*-{
  121. return this.dataTransfer.files[fileIndex];
  122. }-*/;
  123. /**
  124. * Detects if dropped element is a file. <br>
  125. * Always returns <code>true</code> on Safari even if the dropped element is
  126. * a folder.
  127. *
  128. * @param fileIndex
  129. * the index of the element to check
  130. * @return {@code true} if the dropped element is a file, {@code false}
  131. * otherwise
  132. */
  133. public final native boolean isFile(int fileIndex)
  134. /*-{
  135. // Chrome >= v21 and Opera >= v?
  136. if (this.dataTransfer.items) {
  137. var item = this.dataTransfer.items[fileIndex];
  138. if (typeof item.webkitGetAsEntry == "function") {
  139. var entry = item.webkitGetAsEntry();
  140. if (typeof entry !== "undefined" && entry !== null) {
  141. return entry.isFile;
  142. }
  143. }
  144. }
  145. // Zero sized files without a type are also likely to be folders
  146. var file = this.dataTransfer.files[fileIndex];
  147. if (file.size == 0 && !file.type) {
  148. return false;
  149. }
  150. // TODO Make it detect folders on all browsers
  151. return true;
  152. }-*/;
  153. /**
  154. * Adds a data String with the given flavor identifier.
  155. *
  156. * @param flavor
  157. * the identifier
  158. * @param data
  159. * the data
  160. */
  161. public final native void setHtml5DataFlavor(String flavor, String data)
  162. /*-{
  163. this.dataTransfer.setData(flavor, data);
  164. }-*/;
  165. }