summaryrefslogtreecommitdiffstats
path: root/client/src
diff options
context:
space:
mode:
authorLeif Åstrand <leif@vaadin.com>2012-10-23 15:59:54 +0300
committerLeif Åstrand <leif@vaadin.com>2012-10-23 15:59:54 +0300
commit055563a7166f4e3891929e3c8be5799789d68ae1 (patch)
tree1dcff84136dd8dc74baf7d3e4298b1dfe9663fe6 /client/src
parent7ec5bade7299a0989f7596ac346b8f0ec698d3cb (diff)
downloadvaadin-framework-055563a7166f4e3891929e3c8be5799789d68ae1.tar.gz
vaadin-framework-055563a7166f4e3891929e3c8be5799789d68ae1.zip
FileDownloader for starting downloads with any component (#9524)
* Based on patch by Pekka Hyvönen Change-Id: I9263078ffc624f9cabec6c25264920dfdb430efe
Diffstat (limited to 'client/src')
-rw-r--r--client/src/com/vaadin/client/extensions/FileDownloaderConnector.java61
1 files changed, 61 insertions, 0 deletions
diff --git a/client/src/com/vaadin/client/extensions/FileDownloaderConnector.java b/client/src/com/vaadin/client/extensions/FileDownloaderConnector.java
new file mode 100644
index 0000000000..d76efcc046
--- /dev/null
+++ b/client/src/com/vaadin/client/extensions/FileDownloaderConnector.java
@@ -0,0 +1,61 @@
+package com.vaadin.client.extensions;
+
+import com.google.gwt.dom.client.Document;
+import com.google.gwt.dom.client.IFrameElement;
+import com.google.gwt.dom.client.Style;
+import com.google.gwt.dom.client.Style.Unit;
+import com.google.gwt.dom.client.Style.Visibility;
+import com.google.gwt.event.dom.client.ClickEvent;
+import com.google.gwt.event.dom.client.ClickHandler;
+import com.google.gwt.user.client.ui.RootPanel;
+import com.google.gwt.user.client.ui.Widget;
+import com.vaadin.client.ComponentConnector;
+import com.vaadin.client.ServerConnector;
+import com.vaadin.server.FileDownloader;
+import com.vaadin.shared.ui.Connect;
+
+@Connect(FileDownloader.class)
+public class FileDownloaderConnector extends AbstractExtensionConnector
+ implements ClickHandler {
+
+ private IFrameElement iframe;
+
+ @Override
+ protected void extend(ServerConnector target) {
+ final Widget downloadWidget = ((ComponentConnector) target).getWidget();
+
+ downloadWidget.addDomHandler(this, ClickEvent.getType());
+ }
+
+ @Override
+ public void onClick(ClickEvent event) {
+ final String url = getResourceUrl("dl");
+ if (url != null && !url.isEmpty()) {
+ if (iframe != null) {
+ // make sure it is not on dom tree already, might start
+ // multiple downloads at once
+ iframe.removeFromParent();
+ }
+ iframe = Document.get().createIFrameElement();
+
+ Style style = iframe.getStyle();
+ style.setVisibility(Visibility.HIDDEN);
+ style.setHeight(0, Unit.PX);
+ style.setWidth(0, Unit.PX);
+
+ iframe.setFrameBorder(0);
+ iframe.setTabIndex(-1);
+ iframe.setSrc(url);
+ RootPanel.getBodyElement().appendChild(iframe);
+ }
+ }
+
+ @Override
+ public void setParent(ServerConnector parent) {
+ super.setParent(parent);
+ if (parent == null) {
+ iframe.removeFromParent();
+ }
+ }
+
+}