PageRenderTime 84ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 1ms

/src-gwt/org/opencms/ui/client/CmsUploadAreaConnector.java

http://github.com/alkacon/opencms-core
Java | 214 lines | 69 code | 28 blank | 117 comment | 3 complexity | fa5ccead5f1562b47e3d0b7f5768a97c MD5 | raw file
Possible License(s): MIT, Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. /*
  2. * This library is part of OpenCms -
  3. * the Open Source Content Management System
  4. *
  5. * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * For further information about Alkacon Software, please see the
  18. * company website: http://www.alkacon.com
  19. *
  20. * For further information about OpenCms, please see the
  21. * project website: http://www.opencms.org
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with this library; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. package org.opencms.ui.client;
  28. import org.opencms.ade.upload.client.I_CmsUploadContext;
  29. import org.opencms.ade.upload.client.ui.CmsDialogUploadButtonHandler;
  30. import org.opencms.gwt.client.ui.input.upload.CmsFileInfo;
  31. import org.opencms.ui.components.extensions.CmsUploadAreaExtension;
  32. import org.opencms.ui.shared.components.CmsUploadAreaState;
  33. import org.opencms.ui.shared.rpc.I_CmsUploadRpc;
  34. import java.util.ArrayList;
  35. import java.util.List;
  36. import com.google.common.base.Supplier;
  37. import com.google.gwt.core.client.JavaScriptObject;
  38. import com.google.gwt.core.client.JsArray;
  39. import com.google.gwt.user.client.ui.Widget;
  40. import com.vaadin.client.ComponentConnector;
  41. import com.vaadin.client.ServerConnector;
  42. import com.vaadin.client.extensions.AbstractExtensionConnector;
  43. import com.vaadin.shared.ui.Connect;
  44. /**
  45. * The upload area connector.<p>
  46. */
  47. @Connect(CmsUploadAreaExtension.class)
  48. public class CmsUploadAreaConnector extends AbstractExtensionConnector {
  49. /** The serial version id. */
  50. private static final long serialVersionUID = 190108090241764065L;
  51. /** The RPC proxy. */
  52. private I_CmsUploadRpc m_rpc;
  53. /** The widget to enhance. */
  54. private Widget m_widget;
  55. /**
  56. * Constructor.<p>
  57. */
  58. public CmsUploadAreaConnector() {
  59. m_rpc = getRpcProxy(I_CmsUploadRpc.class);
  60. }
  61. /**
  62. * @see com.vaadin.client.ui.AbstractConnector#getState()
  63. */
  64. @Override
  65. public CmsUploadAreaState getState() {
  66. return (CmsUploadAreaState)super.getState();
  67. }
  68. /**
  69. * @see com.vaadin.client.extensions.AbstractExtensionConnector#extend(com.vaadin.client.ServerConnector)
  70. */
  71. @Override
  72. protected void extend(ServerConnector target) {
  73. // Get the extended widget
  74. m_widget = ((ComponentConnector)target).getWidget();
  75. initUploadZone(m_widget.getElement());
  76. }
  77. /**
  78. * Called on drag out.<p>
  79. */
  80. void dragOut() {
  81. m_widget.removeStyleName("o-upload-drop");
  82. }
  83. /**
  84. * Called on drag over.<p>
  85. */
  86. void dragOver() {
  87. m_widget.addStyleName("o-upload-drop");
  88. }
  89. /**
  90. * Called once the upload is finished<p>
  91. *
  92. * @param files the uploaded files
  93. */
  94. void uploadFinished(List<String> files) {
  95. m_rpc.onUploadFinished(files);
  96. }
  97. /**
  98. * Initializes the upload drop zone event handlers.<p>
  99. *
  100. * @param element the drop zone element
  101. */
  102. private native void initUploadZone(JavaScriptObject element)/*-{
  103. // check for file api support
  104. if ((typeof FileReader == 'function' || typeof FileReader == 'object')
  105. && (typeof FormData == 'function' || typeof FormData == 'object')) {
  106. var self = this;
  107. function isFileDrag(event) {
  108. var result = true;
  109. var dt = event.dataTransfer;
  110. for (var i = 0; i < dt.types.length; i++) {
  111. // in case the types list contains text/html, we assume a DOM element is dragged, and no files
  112. if (dt.types[i] == "text/html") {
  113. result = false;
  114. break;
  115. }
  116. }
  117. return result;
  118. }
  119. function dragover(event) {
  120. if (isFileDrag(event)) {
  121. event.stopPropagation();
  122. event.preventDefault();
  123. self.@org.opencms.ui.client.CmsUploadAreaConnector::dragOver()();
  124. }
  125. }
  126. function dragleave(event) {
  127. if (isFileDrag(event)) {
  128. event.stopPropagation();
  129. event.preventDefault();
  130. self.@org.opencms.ui.client.CmsUploadAreaConnector::dragOut()();
  131. }
  132. }
  133. function drop(event) {
  134. if (isFileDrag(event)) {
  135. event.stopPropagation();
  136. event.preventDefault();
  137. self.@org.opencms.ui.client.CmsUploadAreaConnector::dragOut()();
  138. var dt = event.dataTransfer;
  139. var files = dt.files;
  140. self.@org.opencms.ui.client.CmsUploadAreaConnector::openUploadWithFiles(Lcom/google/gwt/core/client/JavaScriptObject;)(files);
  141. }
  142. }
  143. element.addEventListener("dragover", dragover, false);
  144. element.addEventListener("dragexit", dragleave, false);
  145. element.addEventListener("dragleave", dragleave, false);
  146. element.addEventListener("dragend", dragleave, false);
  147. element.addEventListener("drop", drop, false);
  148. }
  149. }-*/;
  150. /**
  151. * Opens the upload dialog with the given file references to upload.<p>
  152. *
  153. * @param files the file references
  154. */
  155. private void openUploadWithFiles(JavaScriptObject files) {
  156. if (getState().getTargetFolderRootPath() == null) {
  157. return;
  158. }
  159. JsArray<CmsFileInfo> cmsFiles = files.cast();
  160. List<CmsFileInfo> fileObjects = new ArrayList<CmsFileInfo>();
  161. for (int i = 0; i < cmsFiles.length(); ++i) {
  162. fileObjects.add(cmsFiles.get(i));
  163. }
  164. CmsDialogUploadButtonHandler buttonHandler = new CmsDialogUploadButtonHandler(
  165. new Supplier<I_CmsUploadContext>() {
  166. /**
  167. * @see com.google.common.base.Supplier#get()
  168. */
  169. public I_CmsUploadContext get() {
  170. return new I_CmsUploadContext() {
  171. public void onUploadFinished(List<String> uploadedFiles) {
  172. uploadFinished(uploadedFiles);
  173. }
  174. };
  175. }
  176. });
  177. buttonHandler.setIsTargetRootPath(true);
  178. buttonHandler.setTargetFolder(getState().getTargetFolderRootPath());
  179. buttonHandler.openDialogWithFiles(fileObjects);
  180. }
  181. }