java/cewolf-1.0/src/main/java/de/laures/cewolf/util/RenderingHelper.java
changeset 1 639991d0808a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/cewolf-1.0/src/main/java/de/laures/cewolf/util/RenderingHelper.java	Sat Feb 28 21:31:02 2009 +0100
     1.3 @@ -0,0 +1,100 @@
     1.4 +/* ================================================================
     1.5 + * Cewolf : Chart enabling Web Objects Framework
     1.6 + * ================================================================
     1.7 + *
     1.8 + * Project Info:  http://cewolf.sourceforge.net
     1.9 + * Project Lead:  Guido Laures (guido@laures.de);
    1.10 + *
    1.11 + * (C) Copyright 2002, by Guido Laures
    1.12 + *
    1.13 + * This library is free software; you can redistribute it and/or modify it under the terms
    1.14 + * of the GNU Lesser General Public License as published by the Free Software Foundation;
    1.15 + * either version 2.1 of the License, or (at your option) any later version.
    1.16 + *
    1.17 + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
    1.18 + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    1.19 + * See the GNU Lesser General Public License for more details.
    1.20 + *
    1.21 + * You should have received a copy of the GNU Lesser General Public License along with this
    1.22 + * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    1.23 + * Boston, MA 02111-1307, USA.
    1.24 + */
    1.25 +
    1.26 +package de.laures.cewolf.util;
    1.27 +
    1.28 +import java.awt.Color;
    1.29 +import java.awt.Font;
    1.30 +import java.awt.Graphics;
    1.31 +import java.awt.image.BufferedImage;
    1.32 +import java.io.IOException;
    1.33 +import java.io.OutputStream;
    1.34 +import java.io.PrintWriter;
    1.35 +import java.io.StringWriter;
    1.36 +import java.util.Enumeration;
    1.37 +import java.util.StringTokenizer;
    1.38 +
    1.39 +import com.sun.image.codec.jpeg.JPEGCodec;
    1.40 +import com.sun.image.codec.jpeg.JPEGEncodeParam;
    1.41 +import com.sun.image.codec.jpeg.JPEGImageEncoder;
    1.42 +
    1.43 +/**
    1.44 + * Some methods to render messages or exceptions into an image.
    1.45 + * @author glaures
    1.46 + */
    1.47 +public class RenderingHelper {
    1.48 +	
    1.49 +	private final static int PADDING_X = 5;
    1.50 +	
    1.51 +	public static String renderMessage(String msg, int width, int height, OutputStream out) throws IOException {
    1.52 +		BufferedImage image = ImageHelper.createImage(width, height);
    1.53 +		Graphics gr = image.getGraphics();
    1.54 +		gr.setColor(Color.white);
    1.55 +		gr.fillRect(0, 0, width, height);
    1.56 +		gr.setColor(Color.black);
    1.57 +		gr.drawString(msg, PADDING_X, height/2 - 7);
    1.58 +		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    1.59 +		JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
    1.60 +		param.setQuality(1.0f, true);
    1.61 +		encoder.encode(image, param);
    1.62 +		return "image/jpeg";
    1.63 +	}
    1.64 +    
    1.65 +	public static String renderException(Throwable ex, int width, int height, OutputStream out) throws IOException {
    1.66 +		BufferedImage image = ImageHelper.createImage(width, height);
    1.67 +		Graphics gr = image.getGraphics();
    1.68 +		gr.setColor(Color.white);
    1.69 +		gr.fillRect(0, 0, width, height);
    1.70 +		gr.setColor(Color.red);
    1.71 +		gr.drawString(ex.getClass().getName() + " raised:", PADDING_X, 15);
    1.72 +		gr.drawString(String.valueOf(ex.getMessage()), PADDING_X, 30);
    1.73 +		gr.setColor(Color.black);
    1.74 +		Font stFont = gr.getFont().deriveFont(9f);
    1.75 +		gr.setFont(stFont);
    1.76 +		drawStackTrace(gr, PADDING_X, 50, ex);
    1.77 +		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
    1.78 +		JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(image);
    1.79 +		param.setQuality(1.0f, true);
    1.80 +		encoder.encode(image, param);
    1.81 +		return "image/jpeg";
    1.82 +	}
    1.83 +    
    1.84 +    private static void drawStackTrace(Graphics gr, int x, int y, Throwable ex) {
    1.85 +        final int linePadding = 4;
    1.86 +        int lineHeight = gr.getFont().getSize() + linePadding;
    1.87 +        int currY = y;
    1.88 +        Enumeration lines = new StringTokenizer(getStackTrace(ex), "\n", false);
    1.89 +        while (lines.hasMoreElements()) {
    1.90 +            gr.drawString(((String)lines.nextElement()).trim(), x, currY);
    1.91 +            currY += lineHeight;
    1.92 +        }
    1.93 +    }
    1.94 +    
    1.95 +    private static String getStackTrace(Throwable t) {
    1.96 +        StringWriter sw = new StringWriter();
    1.97 +        PrintWriter pw = new PrintWriter(sw);
    1.98 +        t.printStackTrace(pw);
    1.99 +        pw.close();
   1.100 +        return sw.getBuffer().toString();
   1.101 +    }
   1.102 +    
   1.103 +}