/* * FruitInfoBean.java * * Created on January 16, 2007, 10:21 AM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package simpleDynamicFaces; import javax.faces.component.UISelectOne; import javax.faces.component.UISelectItems; import javax.faces.context.FacesContext; import javax.faces.model.SelectItem; import javax.faces.event.ValueChangeEvent; import java.util.ResourceBundle; /** * * @author Jennifer Ball */ public class FruitInfoBean { /** Creates a new instance of FruitInfoBean */ private ResourceBundle fruitNames = null; private ResourceBundle fruitMessages = null; private String fruit = null; private String variety = null; private String varietyInfo = null; // // Constructors // public FruitInfoBean() { this.init(); } private void init() { FacesContext context = FacesContext.getCurrentInstance(); fruit = "Pear"; variety = "Aurora"; // This bundle holds the names of the varieties for each fruit, keyed by the fruit name. fruitNames = ResourceBundle.getBundle("simpleZones.FruitNames", context.getViewRoot().getLocale()); // This bundle holds the information for each variety, keyed by the variety name. fruitMessages = ResourceBundle.getBundle("simpleZones.FruitMessages", context.getViewRoot().getLocale()); varietyInfo = fruitMessages.getString(variety); } /* * Handler method called when user selects a variety from the menu. This * method updates the variety component model value and sets the * varietyInfo model value to the appropriate message. */ public String updateVariety(ValueChangeEvent e) { String newVariety = (String)e.getNewValue(); String currentFruit = getFruit(); setVarietyInfo(fruitMessages.getString(newVariety)); return null; } public String getFruit(){ return this.fruit; } public void setFruit(String fruit){ this.fruit = fruit; } protected ArrayList fruits = null; public ArrayList getFruits() { fruits = new ArrayList(); fruits.add(new SelectItem("Pear", "Pear")); fruits.add(new SelectItem("Apple", "Apple")); fruits.add(new SelectItem("Orange", "Orange")); fruits.add(new SelectItem("Peach", "Peach")); return fruits; } public String getVariety(){ return this.variety; } public void setVariety(String variety){ this.variety = variety; } protected ArrayList varieties = null; public ArrayList getVarieties(){ varieties = new ArrayList(); String[] fruitVarieties = getFruitVarieties(this.fruit); for(int i = 0; i < fruitVarieties.length; i++){ varieties.add(new SelectItem(fruitVarieties[i], fruitVarieties[i])); } return varieties; } /* * Handler method for the event of selecting a fruit. This method sets the * model value for the variety component and sets the varietyInfo model value * to the appropriate message. */ public void changeFruit(ValueChangeEvent e){ String fruitValue = (String)e.getNewValue(); String[] varieties = getFruitVarieties(fruitValue); setVarietyInfo(fruitMessages.getString(varieties[0])); setVariety(varieties[0]); } public String getVarietyInfo(){ return this.varietyInfo; } public void setVarietyInfo(String varietyInfo){ this.varietyInfo = varietyInfo; } /* * Gets the list of varieties for a particular fruit, saves them in an array and returns the array. */ private String[] getFruitVarieties(String key){ try { return fruitNames.getString(key).split(","); } catch (Exception e) { return null; } } /* The following code is used with the installDeferredAjaxTransaction use case */ private String fruitQuiz = null; public String getFruitQuiz(){ return this.fruitQuiz; } public void setFruitQuiz(String fruit){ this.fruitQuiz = fruit; } }