1 16 package org.apache.myfaces.examples.listexample; 17 18 import org.apache.myfaces.component.html.ext.HtmlDataTable; 19 20 import javax.faces.event.ActionEvent; 21 import javax.faces.component.UIComponent; 22 import java.math.BigDecimal ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.HashMap ; 27 28 33 public class SimpleCountryList 34 { 35 private List _countries = new ArrayList (); 36 static 37 { 38 } 39 40 SimpleCountry getSimpleCountry(long id) 41 { 42 for (int i = 0; i < _countries.size(); i++) 43 { 44 SimpleCountry country = (SimpleCountry)_countries.get(i); 45 if (country.getId() == id) 46 { 47 return country; 48 } 49 } 50 return null; 51 } 52 53 long getNewSimpleCountryId() 54 { 55 long maxId = 0; 56 for (int i = 0; i < _countries.size(); i++) 57 { 58 SimpleCountry country = (SimpleCountry)_countries.get(i); 59 if (country.getId() > maxId) 60 { 61 maxId = country.getId(); 62 } 63 } 64 return maxId + 1; 65 } 66 67 void saveSimpleCountry(SimpleCountry simpleCountry) 68 { 69 if (simpleCountry.getId() == 0) 70 { 71 simpleCountry.setId(getNewSimpleCountryId()); 72 } 73 boolean found = false; 74 for (int i = 0; i < _countries.size(); i++) 75 { 76 SimpleCountry country = (SimpleCountry)_countries.get(i); 77 if (country.getId() == simpleCountry.getId()) 78 { 79 _countries.set(i, simpleCountry); 80 found = true; 81 } 82 } 83 if (!found) 84 { 85 _countries.add(simpleCountry); 86 } 87 } 88 89 void deleteSimpleCountry(SimpleCountry simpleCountry) 90 { 91 for (int i = 0; i < _countries.size(); i++) 92 { 93 SimpleCountry country = (SimpleCountry)_countries.get(i); 94 if (country.getId() == simpleCountry.getId()) 95 { 96 _countries.remove(i); 97 } 98 } 99 } 100 101 public SimpleCountryList() 102 { 103 _countries.add(new SimpleCountry(1, "AUSTRIA", "AT", new BigDecimal (123),new String []{"Wien","Graz","Linz","Salzburg"})); 104 _countries.add(new SimpleCountry(2, "AZERBAIJAN", "AZ", new BigDecimal (535),new String []{"Baku","Sumgait","Qabala","Agdam"})); 105 _countries.add(new SimpleCountry(3, "BAHAMAS", "BS", new BigDecimal (1345623),new String []{"Nassau","Alice Town","Church Grove","West End"})); 106 _countries.add(new SimpleCountry(4, "BAHRAIN", "BH", new BigDecimal (346),new String []{"Bahrain"})); 107 _countries.add(new SimpleCountry(5, "BANGLADESH", "BD", new BigDecimal (456),new String []{"Chittagong","Chandpur","Bogra","Feni"})); 108 _countries.add(new SimpleCountry(6, "BARBADOS", "BB", new BigDecimal (45645),new String []{"Grantley Adams"})); 109 } 110 111 public List getCountries() 112 { 113 return _countries; 114 } 115 116 public Map getCountryMap() 117 { 118 Map map = new HashMap (); 119 120 List li = getCountries(); 121 122 for (int i = 0; i < li.size(); i++) 123 { 124 SimpleCountry simpleCountry = (SimpleCountry) li.get(i); 125 map.put(simpleCountry.getIsoCode(),simpleCountry.getName()); 126 } 127 128 return map; 129 } 130 131 public void setCountries(List countries) 132 { 133 _countries = countries; 134 } 135 136 public String addCountry() 137 { 138 List list = getCountries(); 139 list.add(new SimpleCountry(list.size() + 1, "", "", new BigDecimal (0), new String [] {})); 140 return "ok"; 141 } 142 143 public void deleteCountry(ActionEvent ev) 144 { 145 UIComponent comp = ev.getComponent(); 146 HtmlDataTable dataTable = (HtmlDataTable) comp.getParent().getParent(); 147 getCountries().remove(dataTable.getRowIndex()); 148 } 149 } 150 | Popular Tags |