494
|
Chapter 12, Miscellany
#98 Add Velocity for Dynamic HTML
HACK
When you run Velocity with this context and this template, it will print out:
Jonathan is a total Rockstar
Create the HTML
You’ll probably want to create your pages in an HTML editor like Dream-
weaver. Better yet, have your graphics designers handle design, and then you
can add VTL tags to the HTML they create.
For each of the three days in the display, you need temperature, humidity,
pressure, and the name of the day. For each measurement, you need a vari-
able in VTL—call these
TEMP
,
HUMIDITY
, and
PRESSURE
—and preface them
with
DAY and the day number (like DAY1). Figure 12-14 is a screenshot from
Dreamweaver, where I built the page with all of the VTL. The dynamic data
for the day, temperature, humidity, and pressure are provided by VTL tags:
the first day’s name is
${DAY1}, its temperature is ${DAY1_TEMP}, etc.
Create a Data Object
Now, it’s time to leave template land and get into some Java code. You need
to write a data object to represent the weather for a particular day. You’ll
find this useful when you build up your context. To keep things simple,
Example 12-20 uses an immutable object.
Figure 12-14. Weather web page in Dreamweaver
Example 12-20. A simple weather data object
public class Weather {
private BigDecimal temperature;
private BigDecimal humidity;
private BigDecimal pressure;
private String day;
Add Velocity for Dynamic HTML #98
Chapter 12, Miscellany
|
495
HACK
Of course, you need to display all of this visual wizardry, so create a class
that contains a
JEditorPane for displaying the HTML. You can use which-
ever HTML renderer you choose, but
JEditorPane is a good choice because
it’s built into Swing. Example 12-21 is a basic container that contains a
JEditorPane and configures it to render HTML.
public Weather(BigDecimal temperature, BigDecimal humidity, BigDecimal
pressure, String day) {
this.temperature = temperature;
this.humidity = humidity;
this.pressure = pressure;
this.day = day;
}
public BigDecimal getTemperature( ) {
return temperature;
}
public BigDecimal getHumidity( ) {
return humidity;
}
public BigDecimal getPressure( ) {
return pressure;
}
public String getDay( ) {
return day;
}
}
Example 12-21. Panel to contain the HTML display pane
public class WeatherPanel {
private JEditorPane htmlPane;
public WeatherPanel( ) {
htmlPane = createHtmlPanel( );
}
private JEditorPane createHtmlPanel( ) {
JEditorPane editorPane = new JEditorPane( );
HTMLEditorKit editorKit = new HTMLEditorKit( );
editorKit.install(editorPane);
editorPane.setEditorKit(editorKit);
editorPane.setEditable(false);
return editorPane;
}
Example 12-20. A simple weather data object (continued)
496
|
Chapter 12, Miscellany
#98 Add Velocity for Dynamic HTML
HACK
Next, you need to add a method to reconfigure the htmlPane with a collec-
tion of
Weather objects:
public void displayWeather(String html, Collection weather){
String result = html;
try {
VelocityContext context = createContext(weather);
result = processString(context, html);
} catch (Exception e){
e.printStackTrace( );
}
htmlPane.setText(result);
}
You need to read in the HTML file you created, and that’s where
displayWeatherByFile( ) comes in. You supply it the filename, and it reads
the HTML in that file:
public void displayWeatherByFile(String fileName, Collection weather){
displayWeather(readFile(fileName), weather);
}
private String readFile(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( );
}
public Component getComponent( ) {
return new JScrollPane(htmlPane);
}
}
Example 12-21. Panel to contain the HTML display pane (continued)

Get Swing Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.