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 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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.core.client.JsArrayString;
  18. import com.google.gwt.dom.client.NativeEvent;
  19. /**
  20. * Helper class to access html5 style drag events.
  21. *
  22. * TODO Gears support ?
  23. */
  24. public class VHtml5DragEvent extends NativeEvent {
  25. protected VHtml5DragEvent() {
  26. }
  27. public final native JsArrayString getTypes()
  28. /*-{
  29. // IE does not support types, return some basic values
  30. return this.dataTransfer.types ? this.dataTransfer.types : ["Text","Url","Html"];
  31. }-*/;
  32. public final native String getDataAsText(String type)
  33. /*-{
  34. var v = this.dataTransfer.getData(type);
  35. return v;
  36. }-*/;
  37. /**
  38. * Works on FF 3.6 and possibly with gears.
  39. *
  40. * @param index
  41. * @return
  42. */
  43. public final native String getFileAsString(int index)
  44. /*-{
  45. if(this.dataTransfer.files.length > 0 && this.dataTransfer.files[0].getAsText) {
  46. return this.dataTransfer.files[index].getAsText("UTF-8");
  47. }
  48. return null;
  49. }-*/;
  50. public final native void setDropEffect(String effect)
  51. /*-{
  52. try {
  53. this.dataTransfer.dropEffect = effect;
  54. } catch (e){}
  55. }-*/;
  56. public final native String getEffectAllowed()
  57. /*-{
  58. return this.dataTransfer.effectAllowed;
  59. }-*/;
  60. public final native void setEffectAllowed(String effect)
  61. /*-{
  62. this.dataTransfer.effectAllowed = effect;
  63. }-*/;
  64. public final native int getFileCount()
  65. /*-{
  66. return this.dataTransfer.files ? this.dataTransfer.files.length : 0;
  67. }-*/;
  68. public final native VHtml5File getFile(int fileIndex)
  69. /*-{
  70. return this.dataTransfer.files[fileIndex];
  71. }-*/;
  72. /**
  73. * Detects if dropped element is a file. <br>
  74. * Always returns <code>true</code> on Safari even if the dropped element is
  75. * a folder.
  76. */
  77. public final native boolean isFile(int fileIndex)
  78. /*-{
  79. // Chrome >= v21 and Opera >= v?
  80. if (this.dataTransfer.items) {
  81. var item = this.dataTransfer.items[fileIndex];
  82. if (item.webkitGetAsEntry) {
  83. return item.webkitGetAsEntry().isFile;
  84. }
  85. }
  86. // Zero sized files without a type are also likely to be folders
  87. var file = this.dataTransfer.files[fileIndex];
  88. if (file.size == 0 && !file.type) {
  89. return false;
  90. }
  91. // TODO Make it detect folders on all browsers
  92. return true;
  93. }-*/;
  94. public final native void setHtml5DataFlavor(String flavor, String data)
  95. /*-{
  96. this.dataTransfer.setData(flavor, data);
  97. }-*/;
  98. }