001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)JFChartRenderer.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package org.openesb.tools.extchart.jfchart;
030:
031: import org.openesb.tools.extchart.jfchart.data.DataAccess;
032: import org.openesb.tools.extchart.jsf.ChartListener;
033: import org.openesb.tools.extchart.jsf.ExtChartComponent;
034: import org.openesb.tools.extchart.property.ChartDefaults;
035: import org.openesb.tools.extchart.property.ChartPropertyGroupsBeanBasic;
036: import org.openesb.tools.extchart.property.JFChartConstants;
037: import org.openesb.tools.extpropertysheet.IExtPropertyGroup;
038: import java.awt.image.BufferedImage;
039: import java.io.IOException;
040: import java.util.Map;
041: import javax.faces.context.FacesContext;
042: import javax.faces.context.ResponseWriter;
043: import org.jfree.chart.ChartRenderingInfo;
044: import org.jfree.chart.ChartUtilities;
045: import org.jfree.chart.JFreeChart;
046: import org.jfree.chart.entity.StandardEntityCollection;
047:
048: /**
049: *
050: * @author rdwivedi
051: */
052: public class JFChartRenderer {
053:
054: /** Creates a new instance of JFChartRenderer */
055: public JFChartRenderer() {
056: }
057:
058: public void encodeBegin(FacesContext context, ExtChartComponent comp)
059: throws IOException {
060: ChartPropertyGroupsBeanBasic mPropGroups = (ChartPropertyGroupsBeanBasic) comp
061: .getPropertyGroups();
062: DataAccess da = (DataAccess) comp.getDataAccess();
063: String uID = comp.getClientId(context)
064: + System.currentTimeMillis();
065: // preEncoding(context);
066: String imageMapName = "imageMap";
067: int width = getWidth(mPropGroups);
068: int height = getHeight(mPropGroups);
069: ResponseWriter writer = context.getResponseWriter();
070: ChartCreator creator = new ChartCreator();
071: JFreeChart chart = creator.createJFreeChart(mPropGroups, da);
072: ChartRenderingInfo info = new ChartRenderingInfo(
073: new StandardEntityCollection());
074: BufferedImage image = chart.createBufferedImage(width, height,
075: info);
076: String imMap = ChartUtilities.getImageMap(imageMapName, info);
077: saveImage(image, context, uID);
078: writer.startElement("div", comp);
079: writer.write("\n");
080: writer.write(imMap);
081: writer.write("\n");
082: writer.startElement("img", comp);
083: writer.writeAttribute("id", comp.getClientId(context), null);
084: writer.writeAttribute("width", String.valueOf(width), null);
085: writer.writeAttribute("height", String.valueOf(height), null);
086: writer.writeAttribute("USEMAP", "\"#" + imageMapName, null);
087: writer.writeAttribute("src", ChartListener.CHART_REQUEST
088: + "?id=" + uID + "&rdm=" + Math.random(), null);
089:
090: }
091:
092: private void saveImage(BufferedImage i, FacesContext context,
093: String id) {
094: Map session = FacesContext.getCurrentInstance()
095: .getExternalContext().getSessionMap();
096: session.put(id, i);
097: }
098:
099: public void encodeEnd(FacesContext context) throws IOException {
100: context.getResponseWriter().endElement("img");
101: context.getResponseWriter().endElement("div");
102: }
103:
104: private int getWidth(ChartPropertyGroupsBeanBasic mPropGroups) {
105: return getChartDefaults(mPropGroups).getImageWidth();
106: }
107:
108: private int getHeight(ChartPropertyGroupsBeanBasic mPropGroups) {
109: return getChartDefaults(mPropGroups).getImageHeight();
110: }
111:
112: public ChartDefaults getChartDefaults(
113: ChartPropertyGroupsBeanBasic grp) {
114: IExtPropertyGroup p = grp
115: .getGroupByName(JFChartConstants.CHART_COMMON_PROPERTIES);
116: return (ChartDefaults) p;
117: }
118:
119: }
|