FileDocCategorySizeDatePackage
WeatherPanel.javaAPI DocExample3077Mon Jan 09 11:02:02 GMT 2006None

WeatherPanel

public class WeatherPanel extends Object

Fields Summary
private JEditorPane
htmlPane
Constructors Summary
public WeatherPanel()

        htmlPane = createHtmlPanel();
    
Methods Summary
private org.apache.velocity.VelocityContextcreateContext(java.util.Collection weatherCollection)

        VelocityContext context = new VelocityContext();
        int index = 1;

        for (Iterator iterator = weatherCollection.iterator(); iterator.hasNext();) {
            Weather weather = (Weather) iterator.next();
            String day = "DAY" + index;
            context.put(day, weather.getDay());
            context.put(day + "_TEMP", weather.getTemperature());
            context.put(day + "_HUMIDITY", weather.getHumidity());
            context.put(day + "_PRESSURE", weather.getPressure());
            index++;
        }

        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 voiddisplayWeather(java.lang.String html, java.util.Collection weather)

        String result = html;
        try {
            VelocityContext context = createContext(weather);
            result = processString(context, html);
        } catch (Exception e){
            e.printStackTrace();
        }
        htmlPane.setText(result);
    
public voiddisplayWeatherByFile(java.lang.String fileName, java.util.Collection weather)

        displayWeather(readFile(fileName), weather);
    
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();
        Velocity.init(properties);
        Velocity.evaluate(context,
                writer,
                null,
                htmlText);
        return writer.getBuffer().toString();
    
private java.lang.StringreadFile(java.lang.String fileName)

        StringBuffer htmlBuffer = new StringBuffer();

        try {
            InputStream inputStream = WeatherPanel.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();