KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > myfaces > examples > listexample > SimpleCountryList


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.HashMap JavaDoc;
27
28 /**
29  * DOCUMENT ME!
30  * @author Thomas Spiegl (latest modification by $Author: matzew $)
31  * @version $Revision: 1.1 $ $Date: 2005/03/24 16:47:10 $
32  */

33 public class SimpleCountryList
34 {
35     private List JavaDoc _countries = new ArrayList JavaDoc();
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 JavaDoc(123),new String JavaDoc[]{"Wien","Graz","Linz","Salzburg"}));
104         _countries.add(new SimpleCountry(2, "AZERBAIJAN", "AZ", new BigDecimal JavaDoc(535),new String JavaDoc[]{"Baku","Sumgait","Qabala","Agdam"}));
105         _countries.add(new SimpleCountry(3, "BAHAMAS", "BS", new BigDecimal JavaDoc(1345623),new String JavaDoc[]{"Nassau","Alice Town","Church Grove","West End"}));
106         _countries.add(new SimpleCountry(4, "BAHRAIN", "BH", new BigDecimal JavaDoc(346),new String JavaDoc[]{"Bahrain"}));
107         _countries.add(new SimpleCountry(5, "BANGLADESH", "BD", new BigDecimal JavaDoc(456),new String JavaDoc[]{"Chittagong","Chandpur","Bogra","Feni"}));
108         _countries.add(new SimpleCountry(6, "BARBADOS", "BB", new BigDecimal JavaDoc(45645),new String JavaDoc[]{"Grantley Adams"}));
109     }
110
111     public List JavaDoc getCountries()
112     {
113         return _countries;
114     }
115     
116     public Map JavaDoc getCountryMap()
117     {
118         Map JavaDoc map = new HashMap JavaDoc();
119
120         List JavaDoc 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 JavaDoc countries)
132     {
133         _countries = countries;
134     }
135
136     public String JavaDoc addCountry()
137     {
138         List JavaDoc list = getCountries();
139         list.add(new SimpleCountry(list.size() + 1, "", "", new BigDecimal JavaDoc(0), new String JavaDoc[] {}));
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