1 11 package org.eclipse.birt.chart.examples.api.preference; 12 13 14 import java.awt.Graphics2D ; 15 import java.awt.image.BufferedImage ; 16 import java.io.IOException ; 17 import java.io.OutputStream ; 18 import java.util.Enumeration ; 19 20 import javax.imageio.ImageIO ; 21 import javax.servlet.ServletException ; 22 import javax.servlet.http.HttpServlet ; 23 import javax.servlet.http.HttpSession ; 24 import javax.servlet.http.HttpServletRequest ; 25 import javax.servlet.http.HttpServletResponse ; 26 27 import org.eclipse.birt.chart.device.IDeviceRenderer; 28 import org.eclipse.birt.chart.exception.ChartException; 29 import org.eclipse.birt.chart.factory.Generator; 30 import org.eclipse.birt.chart.model.Chart; 31 import org.eclipse.birt.chart.model.attribute.Bounds; 32 import org.eclipse.birt.chart.model.attribute.ColorDefinition; 33 import org.eclipse.birt.chart.model.attribute.impl.BoundsImpl; 34 import org.eclipse.birt.chart.model.attribute.impl.ColorDefinitionImpl; 35 import org.eclipse.birt.chart.util.PluginSettings; 36 37 public class PreferenceServlet extends HttpServlet 38 { 39 40 private static final long serialVersionUID = 1L; 41 42 private Chart cm = null; 43 44 private String fontName = null; 45 46 private float size = (float)0.0; 47 48 private boolean bBold = true; 49 50 private boolean bItalic = false; 51 52 private ColorDefinition cd = null; 53 54 private IDeviceRenderer idr = null; 55 56 59 public PreferenceServlet( ) 60 { 61 final PluginSettings ps = PluginSettings.instance( ); 62 try 63 { 64 idr = ps.getDevice( "dv.SWING" ); } 66 catch ( ChartException ex ) 67 { 68 ex.printStackTrace( ); 69 } 70 } 71 72 public void doGet( HttpServletRequest request, HttpServletResponse response ) 73 throws ServletException , IOException 74 { 75 HttpSession session = request.getSession( false ); 76 if ( session == null ) 77 { 78 response.sendRedirect( "http://localhost:8080/error.html" ); } 80 81 cm = ChartModels.createBarChart( ); 82 83 Enumeration en = request.getParameterNames( ); 84 while ( en.hasMoreElements( ) ) 85 { 86 String name = (String ) en.nextElement( ); 87 String value = request.getParameterValues( name )[0]; 88 89 if ( name.equals( "fonts" ) ) { 91 fontName = value; 92 } 93 else if ( name.equals( "style" ) ) { 95 if ( value.equals( "Bold" ) ) { 97 bBold = true; 98 bItalic = false; 99 } 100 else if ( value.equals( "Italic" ) ) { 102 bBold = false; 103 bItalic = true; 104 } 105 } 106 else if ( name.equals( "size" ) ) { 108 size = Float.parseFloat( value ); 109 } 110 else if ( name.equals( "color" ) ) { 112 if ( value.equals( "Black" ) ) { 114 cd = ColorDefinitionImpl.BLACK( ); 115 } 116 else if ( value.equals( "Red" ) ) { 118 cd = ColorDefinitionImpl.RED( ); 119 } 120 else if ( value.equals( "Blue" ) ) { 122 cd = ColorDefinitionImpl.BLUE( ); 123 } 124 } 125 126 } 127 128 response.setHeader( "Cache-Control", "no-store" ); response.setDateHeader( "Expires", 0 ); 131 response.setContentType( "image/jpeg" ); createImage( (OutputStream ) response.getOutputStream( ) ); 134 } 135 136 public void doPost( HttpServletRequest request, HttpServletResponse response ) 137 throws ServletException , IOException 138 { 139 doGet( request, response ); 140 } 141 142 147 private void createImage( OutputStream out ) 148 { 149 BufferedImage image = new BufferedImage ( 600, 151 400, 152 BufferedImage.TYPE_INT_RGB ); 153 154 Graphics2D g2d = (Graphics2D ) image.getGraphics( ); 156 idr.setProperty( IDeviceRenderer.GRAPHICS_CONTEXT, g2d ); 157 158 Bounds bo = BoundsImpl.create( 0, 0, 600, 400 ); 159 bo.scale( 72d / idr.getDisplayServer( ).getDpiResolution( ) ); 160 161 Generator gr = Generator.instance( ); 162 163 try 164 { 165 gr.render( idr, gr.build( idr.getDisplayServer( ), 166 cm, 167 bo, 168 null, 169 null, 170 new LabelStyleProcessor( fontName, size, bBold, bItalic, cd ) ) ); 171 } 172 catch ( ChartException ex ) 173 { 174 ex.printStackTrace( ); 175 } 176 177 g2d.dispose( ); 178 179 try 181 { 182 ImageIO.write( image, "JPEG", out ); } 184 catch ( IOException io ) 185 { 186 io.printStackTrace( ); 187 } 188 189 } 190 } 191 | Popular Tags |