KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > dispatcher > ChartResult


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 /*
6  * Created on Sep 20, 2003
7  *
8  * To change the template for this generated file go to
9  * Window - Preferences - Java - Code Generation - Code and Comments
10  */

11 package com.opensymphony.webwork.dispatcher;
12
13 import com.opensymphony.webwork.ServletActionContext;
14 import com.opensymphony.xwork.ActionInvocation;
15 import com.opensymphony.xwork.Result;
16 import org.jfree.chart.ChartUtilities;
17 import org.jfree.chart.JFreeChart;
18
19 import javax.servlet.http.HttpServletResponse JavaDoc;
20 import java.io.OutputStream JavaDoc;
21
22
23 /**
24  * A custom Result type for chart data. Built on top of
25  * <a HREF="http://www.jfree.org/jfreechart/" target="_blank">JFreeChart</a>. When executed
26  * this Result will write the given chart as a PNG to the servlet output stream.
27  *
28  * @author Bernard Choi
29  */

30 public class ChartResult implements Result {
31     //~ Instance fields ////////////////////////////////////////////////////////
32

33     JFreeChart chart;
34     boolean chartSet = false;
35     private int height;
36     private int width;
37
38     //~ Methods ////////////////////////////////////////////////////////////////
39

40     /**
41      * Sets the JFreeChart to use.
42      *
43      * @param chart a JFreeChart object.
44      */

45     public void setChart(JFreeChart chart) {
46         this.chart = chart;
47         chartSet = true;
48     }
49
50     /**
51      * Sets the chart height.
52      *
53      * @param height the height of the chart in pixels.
54      */

55     public void setHeight(int height) {
56         this.height = height;
57     }
58
59     /**
60      * Sets the chart width.
61      *
62      * @param width the width of the chart in pixels.
63      */

64     public void setWidth(int width) {
65         this.width = width;
66     }
67
68     /**
69      * Executes the result. Writes the given chart as a PNG to the servlet output stream.
70      *
71      * @param invocation an encapsulation of the action execution state.
72      * @throws Exception if an error occurs when creating or writing the chart to the servlet output stream.
73      */

74     public void execute(ActionInvocation invocation) throws Exception JavaDoc {
75         JFreeChart chart = null;
76
77         if (chartSet) {
78             chart = this.chart;
79         } else {
80             chart = (JFreeChart) invocation.getStack().findValue("chart");
81         }
82
83         if (chart == null) {
84             throw new NullPointerException JavaDoc("No chart found");
85         }
86
87         HttpServletResponse JavaDoc response = ServletActionContext.getResponse();
88         OutputStream JavaDoc os = response.getOutputStream();
89         ChartUtilities.writeChartAsPNG(os, chart, width, height);
90         os.flush();
91     }
92 }
93
Popular Tags