KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > war > common > JdkTimeZonesList


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.war.common;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Arrays JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Locale JavaDoc;
29 import java.util.Map JavaDoc;
30 import java.util.TimeZone JavaDoc;
31
32 import org.apache.commons.collections.ReferenceMap;
33 import org.springframework.beans.factory.InitializingBean;
34
35 import com.jaspersoft.jasperserver.api.JSException;
36 import com.jaspersoft.jasperserver.war.dto.StringOption;
37
38 /**
39  * @author Lucian Chirita (lucianc@users.sourceforge.net)
40  * @version $Id: JdkTimeZonesList.java 3834 2006-06-26 06:41:17Z lucian $
41  */

42 public class JdkTimeZonesList implements TimeZonesList, InitializingBean {
43     
44     private List JavaDoc timeZonesIds;
45     private final Map JavaDoc userTimeZonesLists;
46     
47     public JdkTimeZonesList() {
48         userTimeZonesLists = Collections.synchronizedMap(new ReferenceMap(ReferenceMap.HARD, ReferenceMap.SOFT));
49     }
50
51     public void afterPropertiesSet() throws Exception JavaDoc {
52         if (timeZonesIds == null) {
53             String JavaDoc[] availableIDs = TimeZone.getAvailableIDs();
54             timeZonesIds = Arrays.asList(availableIDs);
55         }
56     }
57
58     public List JavaDoc getTimeZones(Locale JavaDoc userLocale) {
59         List JavaDoc timeZones = (List JavaDoc) userTimeZonesLists.get(userLocale);
60         if (timeZones == null) {
61             timeZones = createTimeZones(userLocale);
62             userTimeZonesLists.put(userLocale, timeZones);
63         }
64         return timeZones;
65     }
66
67     protected List JavaDoc createTimeZones(Locale JavaDoc userLocale) {
68         List JavaDoc timeZones = new ArrayList JavaDoc(timeZonesIds.size());
69         for (Iterator JavaDoc it = timeZonesIds.iterator(); it.hasNext();) {
70             String JavaDoc id = (String JavaDoc) it.next();
71             String JavaDoc description = getTimeZoneDescription(id, userLocale);
72             StringOption option = new StringOption(id, description);
73             timeZones.add(option);
74         }
75         return timeZones;
76     }
77
78     protected String JavaDoc getTimeZoneDescription(String JavaDoc id, Locale JavaDoc userLocale) {
79         TimeZone JavaDoc timeZone = TimeZone.getTimeZone(id);
80         if (timeZone == null) {
81             throw new JSException("Unknown time zone \"" + id + "\"");
82         }
83         return timeZone.getDisplayName(userLocale);
84     }
85
86     public List JavaDoc getTimeZonesIds() {
87         return timeZonesIds;
88     }
89
90     public void setTimeZonesIds(List JavaDoc timeZonesIds) {
91         this.timeZonesIds = timeZonesIds;
92         userTimeZonesLists.clear();
93     }
94
95 }
96
Popular Tags