FileDocCategorySizeDatePackage
VelocityHtmlPanel.javaAPI DocExample3018Mon Jan 09 11:02:02 GMT 2006None

VelocityHtmlPanel

public class VelocityHtmlPanel extends Object

Fields Summary
private JEditorPane
htmlPane
Constructors Summary
public VelocityHtmlPanel()

        htmlPane = createHtmlPanel();

        String htmlText = readFile("html/today.html");
        String htmlTextPostTemplate = runTemplateAgainstHtml(htmlText);

        htmlPane.setText(htmlTextPostTemplate);


    
Methods Summary
private org.apache.velocity.VelocityContextcreateContext(Weather weather)

        VelocityContext context = new VelocityContext();
        context.put("TEMP", weather.getTemperature() + " F");
        context.put("HUMIDITY", weather.getHumidity() + " %");
        context.put("PRESSURE", "10" + " bars");
        return context;
    
private javax.swing.JEditorPanecreateHtmlPanel()

        JEditorPane editorPane = new JEditorPane();
        HTMLEditorKit editorKit = new HTMLEditorKit();
        editorKit.install(editorPane);
        editorPane.setEditorKit(editorKit);
        editorPane.setEditable(false);
        return editorPane;
    
public java.awt.ComponentgetComponent()

        return new JScrollPane(htmlPane);
    
private java.lang.StringprocessString(org.apache.velocity.VelocityContext context, java.lang.String htmlText)

        StringWriter writer = new StringWriter();
        Properties properties = new Properties();
        properties.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.NullLogSystem");
        Velocity.init(properties);
        Velocity.evaluate(context,
                writer,
                "LOG", // used for logging
                htmlText);
        return writer.getBuffer().toString();
    
private java.lang.StringreadFile(java.lang.String fileName)

        StringBuffer htmlBuffer = new StringBuffer();

        try {
            InputStream inputStream = VelocityHtmlPanel.class.getResourceAsStream(fileName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

            while (true){
                String line = reader.readLine();
                if (line != null){
                    htmlBuffer.append(line);
                } else {
                    break;
                }
            }
        } catch (IOException iox){
            iox.printStackTrace();
        }
        return htmlBuffer.toString();
    
private java.lang.StringrunTemplateAgainstHtml(java.lang.String htmlText)

        String result = htmlText;
        try {

            Weather weather = new Weather(new BigDecimal("86.9"), new BigDecimal("68"), new BigDecimal("5"));
            VelocityContext context = createContext(weather);
            result = processString(context, htmlText);
        } catch (Exception e){
            e.printStackTrace();
        }

        return result;