KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > birt > chart > examples > api > preference > PreferenceServlet


1 /*******************************************************************************
2  * Copyright (c) 2004 Actuate Corporation.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * Actuate Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.birt.chart.examples.api.preference;
12
13
14 import java.awt.Graphics2D JavaDoc;
15 import java.awt.image.BufferedImage JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.OutputStream JavaDoc;
18 import java.util.Enumeration JavaDoc;
19
20 import javax.imageio.ImageIO JavaDoc;
21 import javax.servlet.ServletException JavaDoc;
22 import javax.servlet.http.HttpServlet JavaDoc;
23 import javax.servlet.http.HttpSession JavaDoc;
24 import javax.servlet.http.HttpServletRequest JavaDoc;
25 import javax.servlet.http.HttpServletResponse JavaDoc;
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 JavaDoc
38 {
39
40     private static final long serialVersionUID = 1L;
41
42     private Chart cm = null;
43     
44     private String JavaDoc 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     /**
57      * Deploy the render device.
58      */

59     public PreferenceServlet( )
60     {
61         final PluginSettings ps = PluginSettings.instance( );
62         try
63         {
64             idr = ps.getDevice( "dv.SWING" );//$NON-NLS-1$
65
}
66         catch ( ChartException ex )
67         {
68             ex.printStackTrace( );
69         }
70     }
71
72     public void doGet( HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response )
73             throws ServletException JavaDoc, IOException JavaDoc
74     {
75         HttpSession JavaDoc session = request.getSession( false );
76         if ( session == null )
77         {
78             response.sendRedirect( "http://localhost:8080/error.html" ); //$NON-NLS-1$
79
}
80
81         cm = ChartModels.createBarChart( );
82         
83         Enumeration JavaDoc en = request.getParameterNames( );
84         while ( en.hasMoreElements( ) )
85         {
86             String JavaDoc name = (String JavaDoc) en.nextElement( );
87             String JavaDoc value = request.getParameterValues( name )[0];
88
89             if ( name.equals( "fonts" ) )//$NON-NLS-1$
90
{
91                 fontName = value;
92             }
93             else if ( name.equals( "style" ) )//$NON-NLS-1$
94
{
95                 if ( value.equals( "Bold" ) )//$NON-NLS-1$
96
{
97                     bBold = true;
98                     bItalic = false;
99                 }
100                 else if ( value.equals( "Italic" ) )//$NON-NLS-1$
101
{
102                     bBold = false;
103                     bItalic = true;
104                 }
105             }
106             else if ( name.equals( "size" ) )//$NON-NLS-1$
107
{
108                 size = Float.parseFloat( value );
109             }
110             else if ( name.equals( "color" ) )//$NON-NLS-1$
111
{
112                 if ( value.equals( "Black" ) ) //$NON-NLS-1$
113
{
114                     cd = ColorDefinitionImpl.BLACK( );
115                 }
116                 else if ( value.equals( "Red" ) ) //$NON-NLS-1$
117
{
118                     cd = ColorDefinitionImpl.RED( );
119                 }
120                 else if ( value.equals( "Blue" ) ) //$NON-NLS-1$
121
{
122                     cd = ColorDefinitionImpl.BLUE( );
123                 }
124             }
125
126         }
127
128         response.setHeader( "Cache-Control", "no-store" ); //$NON-NLS-1$//$NON-NLS-2$
129
response.setDateHeader( "Expires", 0 ); //$NON-NLS-1$
130

131         // Set the Content-Type header for the image output
132
response.setContentType( "image/jpeg" ); //$NON-NLS-1$
133
createImage( (OutputStream JavaDoc) response.getOutputStream( ) );
134     }
135
136     public void doPost( HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response )
137             throws ServletException JavaDoc, IOException JavaDoc
138     {
139         doGet( request, response );
140     }
141
142     /**
143      * Create the chart image
144      *
145      * @param out OutputStream
146      */

147     private void createImage( OutputStream JavaDoc out )
148     {
149         // Create a buffered image
150
BufferedImage JavaDoc image = new BufferedImage JavaDoc( 600,
151                 400,
152                 BufferedImage.TYPE_INT_RGB );
153
154         // Draw the chart in the buffered image
155
Graphics2D JavaDoc g2d = (Graphics2D JavaDoc) 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         // Output the image
180
try
181         {
182             ImageIO.write( image, "JPEG", out ); //$NON-NLS-1$
183
}
184         catch ( IOException JavaDoc io )
185         {
186             io.printStackTrace( );
187         }
188
189     }
190 }
191
Popular Tags