List of usage examples for com.vaadin.server ConnectorResource ConnectorResource
ConnectorResource
From source file:com.foc.vaadin.gui.components.chart.JFreeChartWrapper_Modified.java
License:Apache License
@Override public Resource getSource() { if (res == null) { res = new ConnectorResource() { private ByteArrayInputStream bytestream = null; ByteArrayInputStream getByteStream() { if (chart != null && bytestream == null) { int widht = getGraphWidth(); int height = getGraphHeight(); if (mode == RenderingMode.SVG) { //VAADIN 7 /* DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory .newInstance(); DocumentBuilder docBuilder = null; try {/*from www. j a v a 2s . co m*/ docBuilder = docBuilderFactory .newDocumentBuilder(); } catch (ParserConfigurationException e1) { throw new RuntimeException(e1); } Document document = docBuilder.newDocument(); Element svgelem = document.createElement("svg"); document.appendChild(svgelem); // Create an instance of the SVG Generator SVGGraphics2D svgGenerator = new SVGGraphics2D(document); // draw the chart in the SVG generator chart.draw(svgGenerator, new Rectangle(widht, height)); Element el = svgGenerator.getRoot(); el.setAttributeNS(null, "viewBox", "0 0 " + widht + " " + height + ""); el.setAttributeNS(null, "style", "width:100%;height:100%;"); el.setAttributeNS(null, "preserveAspectRatio", getSvgAspectRatio()); // Write svg to buffer ByteArrayOutputStream baoutputStream = new ByteArrayOutputStream(); Writer out; try { OutputStream outputStream = gzipEnabled ? new GZIPOutputStream( baoutputStream) : baoutputStream; out = new OutputStreamWriter(outputStream, "UTF-8"); // * don't use css, FF3 can'd deal with the result // * perfectly: wrong font sizes boolean useCSS = false; svgGenerator.stream(el, out, useCSS, false); outputStream.flush(); outputStream.close(); bytestream = new ByteArrayInputStream( baoutputStream.toByteArray()); } catch (Exception e) { e.printStackTrace(); } */ } else { // Draw png to bytestream try { byte[] bytes = ChartUtilities.encodeAsPNG(chart.createBufferedImage(widht, height)); bytestream = new ByteArrayInputStream(bytes); } catch (IOException e) { e.printStackTrace(); } } } else { bytestream.reset(); } return bytestream; } @Override public String getMIMEType() { if (mode == RenderingMode.PNG) { return "image/png"; } else { return "image/svg+xml"; } } @Override public DownloadStream getStream() { DownloadStream downloadStream = new DownloadStream(getByteStream(), getMIMEType(), getFilename()); if (gzipEnabled && mode == RenderingMode.SVG) { downloadStream.setParameter("Content-Encoding", "gzip"); } return downloadStream; } @Override public String getFilename() { if (mode == RenderingMode.PNG) { return "graph.png"; } else { return gzipEnabled ? "graph.svgz" : "graph.svg"; } } }; } return res; }