/** Download actual file */ public void download() { StreamResource resource = new StreamResource(source, fileName); resource .getStream() .setParameter("Content-Disposition", "attachment;filename=\"" + fileName + "\""); resource.setMIMEType("application/octet-stream"); resource.setCacheTime(0); FileDownloader downloader = new FileDownloader(resource); downloader.extend(ui); }
private void showNonMobileNotification() { try { URL appUrl = Page.getCurrent().getLocation().toURL(); String myIp = Inet4Address.getLocalHost().getHostAddress(); final String qrCodeUrl = appUrl.toString().replaceAll("localhost", myIp); Label info = new Label( "You appear to be running this demo on a non-portable device. " + "Parking is intended for touch devices primarily. " + "Please read the QR code on your touch device to access the demo."); info.setWidth("310px"); Image qrCode = new Image(); qrCode.addStyleName("qrcode-image"); StreamResource resource = new StreamResource( new StreamSource() { @Override public InputStream getStream() { InputStream result = null; try { final Map<EncodeHintType, ErrorCorrectionLevel> hintMap = Maps.newHashMap(); hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L); final QRCodeWriter qrCodeWriter = new QRCodeWriter(); final BitMatrix byteMatrix = qrCodeWriter.encode(qrCodeUrl, BarcodeFormat.QR_CODE, 150, 150, hintMap); final int width = byteMatrix.getWidth(); final BufferedImage image = new BufferedImage(width, width, BufferedImage.TYPE_INT_RGB); image.createGraphics(); final Graphics2D graphics = (Graphics2D) image.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, width, width); graphics.setColor(Color.BLACK); for (int i = 0; i < width; i++) { for (int j = 0; j < width; j++) { if (byteMatrix.get(i, j)) { graphics.fillRect(i, j, 1, 1); } } } final ByteArrayOutputStream baos = new ByteArrayOutputStream(); try { ImageIO.write(image, "png", baos); } catch (final IOException e) { e.printStackTrace(); } result = new ByteArrayInputStream(baos.toByteArray()); } catch (WriterException e) { e.printStackTrace(); } return result; } }, "qrcode-" + new Date().getTime() + ".png"); resource.setCacheTime(0); qrCode.setSource(resource); CssLayout qrCodeLayout = new CssLayout(qrCode, info); qrCodeLayout.setSizeFull(); Window window = new Window(null, qrCodeLayout); window.setWidth(500.0f, Unit.PIXELS); window.setHeight(200.0f, Unit.PIXELS); window.addStyleName("qr-code"); window.setModal(true); window.setResizable(false); window.setDraggable(false); addWindow(window); window.center(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (UnknownHostException e) { e.printStackTrace(); } }