Let's begin by creating a new class, CelebrityModel, and let it implement the IPropertySelection interface: public class CelebrityModel implements IPropertySelectionModel {
/** Creates a new instance of CelebrityModel */ public CelebrityModel() { } } At first, NetBeans will not recognize the interface, but press Alt-Shift-F, and it will add an appropriate import statement automatically. Now NetBeans will complain that the class doesn't implement the methods of the interface, and there will be a little bulb to the left of the underlined line of code. Click that bulb, and the IDE will suggest adding the required methods. Let it do so, and you will see that five methods were added to the new class. We need to slightly modify the code to get rid of red underlining. Make it look like so: public class CelebrityModel implements IPropertySelectionModel {
public CelebrityModel() { }
public int getOptionCount() { return 0; }
public Object getOption(int i) { return null; }
public String getLabel(int i) { return null; }
public String getValue(int i) { return null; }
public Object translateValue(String value) { return null; }
} Now let's modify this code to make it work with our celebrities, and learn what different methods do along the way. First of all, we need to pass our list of celebrities to the model. This is convenient to do in the constructor. Add a private List property and modify the constructor to look like this: private List celebrities;
public CelebrityModel(List celebrities) { this.celebrities = celebrities; } Now let me tell you about the getOptionCount(() method. It serves to tell the PropertySelection how many options it is going to display. All we need to do is return the size of the list of celebrities: public int getOptionCount() { return celebrities.size(); } The next method, getOption(int i), takes the index of an option and returns the object corresponding to this index. Say we've told PropertySelection in the previous method that there are four options to display. It will number them, starting from zero: 0, 1, 2, 3. Now it asks the model: which Celebrity object corresponds to, say, option 2? We can answer this question easily: public Object getOption(int i) { return celebrities.get(i); } The next method, getLabel(int i), is equally simple. It answers a PropertySelection's question like "What should be the label for option number 0?" Say we want to display the full names of celebrities as labels, in which case the answer will be: public String getLabel(int i) { Celebrity c = (Celebrity) celebrities.get(i); return c.getFirstName() + " " + c.getLastName(); } The next method, getValue(), answers a question like "What should contain the value attribute for option number 3?" The simplest possible approach is to use the index itself as a value -- just convert it into String: public String getValue(int i) { return Integer.toString(i); } The final method, translateValue(), will work when the user has made some selection and submitted the form. His or her choice will arrive at the server as the contents of the value attribute of the selected option. As we have specified in the previous method, this is going to be a string representation of the option's index, like "0," or "1," or "2." The translateValue() method should define how to find the appropriate Celebrity object using the submitted value, which is quite simple in our case: public Object translateValue(String value) { int index = Integer.parseInt(value); return celebrities.get(index); } Finally, we need to change the getCelebrityModel() method of CelebritiesList page so that it returns an instance of the just created CelebrityModel class: public IPropertySelectionModel getCelebrityModel() { return new CelebrityModel(getCelebrities()); } You can run the application and see that it works, and the drop-down list is filled with celebrities from the DataSource. When we press the Save button, the selected celebrity (i.e. a Celebrity object filled with his or here data) is stored as an Application State Object, but at the moment we can't see that and can't be sure of that. To complete what we've planned to do, we need to display the Celebrity of the Week at the Home page, and perhaps display that page after the selection was made. Add the following piece of HTML to the contents of the Home.html file, wherever you see as appropriate: <p><strong>Celebrity of the Week</strong>: <span jwcid="celebrityOfTheWeek">John Smith</span></p> The component we are using here is an Insert. Let's configure it in the Home.page specification: <component id="celebrityOfTheWeek" type="Insert"> <binding name="value" value="fullName"/> </component> And then add to the Home class a method returning the full name: public String getFullName() { Celebrity c = getTheCelebrity(); return c.getFirstName() + " " + c.getLastName(); } As you see, we also need a method in the Home class which will give us a reference to the celebrityOfTheWeek ASO: @InjectState("celebrityOfTheWeek") public abstract Celebrity getCelebrityOfTheWeek(); Finally, after the Celebrity of the Week was selected at the CelebritiesList page, we want to show the Home page, and this turns our attention back to the CelebritiesList class. This class should have some listener method invoked when the user presses the Save button. We don't have a listener yet, so let's add it. Here is the addition to the CelebritiesList.html template: <form action="" jwcid="@Form" listener="listener:onSaveCelebrityOfTheWeek"> <select jwcid="selectCelebrity"> <option value="1">Angelina Jolie</option> <option value="2">Bill Gates</option> </select> <input type="submit" value="Save"/> </form> And here is the listener method itself: public String onSaveCelebrityOfTheWeek() { return "Home"; }
blog comments powered by Disqus |
|
|
|
|
|
|
|