First of all, we need some class to represent a celebrity; it would be natural to name it Celebrity. For the first iteration of the project, this class will have only five properties:
We need to have this numeric ID because, well, theoretically speaking it is not against the laws of nature to have two celebrities named John Smith born on the same day who became famous in the same occupation. But even if that happened, we would have this ID as a way to distinguish between them. In a real life application, this could be a primary key in a database. Create a new class named Celebrity (right-click the earlier created com.devshed.tapestry.celebrities package, New -> Java Class…) and add to it five private members: private int id; private String firstName; private String lastName; private Date dateOfBirth; private String occupation; Again, make use of this handy shortcut: after typing “Date” press Ctrl-Space and select java.util.Date class. Then allow NetBeans to create getter and setter methods: right-click somewhere in your code and choose Refactor -> Encapsulate Fields… Make sure that everything is selected as the picture shows:
Press Next, and NetBeans will create an overview of the changes it is going to make and show it in the Refactoring view below the code editor. Just press Do Refactoring there, and you will see getters and setters added to the Celebrity class. Now, we need to have a source of data that will give us a number of Celebrity objects. In a real life application, this would be a database, but for now let’s leave everything as simple as possible and use a mock data source – a simple Java class that creates a List of Celebrity objects and provides methods to retrieve either the whole list or just one specified object. Later, we might replace the mock data source with a real one. Let’s add a DataSource class to the existing Java package. There is nothing Tapestry-specific or terribly clever in this class, so you can simply copy the code: package com.devshed.tapestry.celebrities;
import java.util.ArrayList; import java.util.Calendar; import java.util.Iterator; import java.util.List;
public class DataSource {
List list = new ArrayList();
/** Creates a new instance of DataSource */ public DataSource() {
Calendar calendar = Calendar.getInstance();
Celebrity fonda = new Celebrity(); fonda.setFirstName("Jane"); fonda.setLastName("Fonda"); fonda.setOccupation("Actress"); calendar.set(1937, 11, 21); fonda.setDateOfBirth(calendar.getTime()); fonda.setId(1);
list.add(fonda);
Celebrity gallo = new Celebrity(); gallo.setFirstName("Ernest"); gallo.setLastName("Gallo"); gallo.setOccupation("Wine-maker"); calendar.set(1909, 2, 18); // Months are 0-based (2 is March) gallo.setDateOfBirth(calendar.getTime()); gallo.setId(2);
list.add(gallo);
Celebrity gates = new Celebrity(); gates.setFirstName("Bill"); gates.setLastName("Gates"); gates.setOccupation("Programmer ;)"); calendar.set(1955, 9, 28); gates.setDateOfBirth(calendar.getTime()); gates.setId(3);
list.add(gates);
Celebrity jolie = new Celebrity(); jolie.setFirstName("Angelina"); jolie.setLastName("Jolie"); jolie.setOccupation("Actress"); calendar.set(1975, 5, 4); jolie.setDateOfBirth(calendar.getTime()); jolie.setId(4);
list.add(jolie);
}
public List getCelebrities() { return list; }
public Celebrity getCelebrityById(int id) {
for (Iterator i = list.iterator(); i.hasNext();) { Celebrity celebrity = (Celebrity)i.next(); if (celebrity.getId() == id) return celebrity; }
return null; } } This class simply creates a List of a few celebrities in its constructor using some hard-coded data. We can obtain the whole lot by using the getCelebrities() method, or retrieve a single celebrity by specifying his or her ID and using the getCelebrityById() method. Now that we have all supporting code ready, we can begin converting page mock ups into Tapestry templates.
blog comments powered by Disqus |
|
|
|
|
|
|
|