webtier@glassfish.java.net

Re: Integrating chart generation engine into JSF 2.0

From: <webtier_at_javadesktop.org>
Date: Sat, 31 Oct 2009 15:07:48 PDT

Thanks for the reply, somehow I missed it, I thought I would get notified.
After a few days of trial and error I converted the component to JSF 2.0 - of cause into a java based one (with use of
annotations I was able to get rid of half of the files). I don't remember much, one thing which puzzled me is that I still have to call setRendererType(DEFAULT_RENDERER);


@FacesComponent(value = UIChart.COMPONENT_TYPE)
public class UIChart extends AbstractComponent {
    public static final String COMPONENT_TYPE = "net.sf.jsfcomp.chartcreator.component.UIChart";
    public static final String COMPONENT_FAMILY = "net.sf.jsfcomp.chartcreator.component.UIChart";
    public static final String DEFAULT_RENDERER = ChartRenderer.RENDERER_TYPE;

    public UIChart() {
        super();
        setRendererType(DEFAULT_RENDERER);
    }

    public String getFamily() {
        return COMPONENT_FAMILY;
    }

    public boolean getRendersChildren() {
        return true;
    }

    
even though there is


@FacesRenderer(rendererType = ChartRenderer.RENDERER_TYPE, componentFamily = UIChart.COMPONENT_FAMILY)
public class ChartRenderer extends Renderer {
    public static final String RENDERER_TYPE = "net.sf.jsfcomp.chartcreator.renderkit.ChartRenderer";
...

But my question was more about what new unobvious features of JSF 2.0 could be applicable here. For instance it uses a Phase listener to serve images, may be that could be handled in a better way via resources (some custom ones(?))?


public class ChartListener implements PhaseListener {

    public void afterPhase(PhaseEvent phaseEvent) {
        String chartId = phaseEvent.getFacesContext()
                .getExternalContext().getRequestParameterMap().get("chartId");
        if (chartId != null) {
            handleChartRequest(phaseEvent, chartId);
        }
    }

    private void handleChartRequest(PhaseEvent phaseEvent, String id) {
        FacesContext facesContext = phaseEvent.getFacesContext();
        ExternalContext externalContext = facesContext.getExternalContext();
        Map sessionMap = externalContext.getSessionMap();

        ChartData chartData = (ChartData) sessionMap.get(id);
        JFreeChart chart = ChartUtils.createChartWithType(chartData);

        try {
            if (externalContext.getResponse() instanceof HttpServletResponse) {
                writeChartWithServletResponse((HttpServletResponse) externalContext.getResponse(), chart, chartData);
            }
        } catch (IOException e) {
            Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, " IO Error in handleChartRequest ", e);
        } finally {
            emptySession(sessionMap, id);
            facesContext.responseComplete();
        }
    }

    public void beforePhase(PhaseEvent phaseEvent) {
        // nothing to do here yet
    }

    public PhaseId getPhaseId() {
        return PhaseId.RESTORE_VIEW;
    }

    private void writeChartWithServletResponse(HttpServletResponse response, JFreeChart chart, ChartData chartData)
            throws IOException {
        OutputStream stream = response.getOutputStream();
        response.setContentType(ChartUtils.resolveContentType(chartData.getOutput()));
        writeChart(stream, chart, chartData);
    }

    private void writeChart(OutputStream stream, JFreeChart chart, ChartData chartData) throws IOException {
        if (chartData.getOutput().equalsIgnoreCase("png")) {
            ChartUtilities.writeChartAsPNG(stream, chart, chartData.getWidth(), chartData.getHeight());
        } else if (chartData.getOutput().equalsIgnoreCase("jpeg")) {
            ChartUtilities.writeChartAsJPEG(stream, chart, chartData.getWidth(), chartData.getHeight());
        }

        stream.flush();
        stream.close();
    }

    private void emptySession(Map sessionMap, String id) {
        sessionMap.remove(id);
    }
}
[Message sent by forum member 'nzinoviev' (nikita.zinoviev_at_sun.com)]

http://forums.java.net/jive/thread.jspa?messageID=370074