KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > util > StrutsUtil


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * 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. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 package org.apache.roller.ui.core.util;
19
20 import java.util.ArrayList JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Locale JavaDoc;
24 import java.util.TimeZone JavaDoc;
25 import java.util.TreeSet JavaDoc;
26
27 import org.apache.struts.util.LabelValueBean;
28 import org.apache.roller.util.LocaleComparator;
29 import org.apache.roller.util.TimeZoneComparator;
30
31 public class StrutsUtil
32 {
33     public static ArrayList JavaDoc locales;
34     public static ArrayList JavaDoc timeZones;
35     
36     //-----------------------------------------------------------------------
37
/**
38      * LabelValueBeans are Comparable but violate the
39      * equals() part of the TreeSet requirements.
40      * And the html:options tag won't recognize
41      * toString as a property. So we have to put the
42      * Locales into a TreeSet to sort them, then convert
43      * them to LabelValueBeans to display them.
44      * Glad we only have to do this once.
45      *
46      * @return List of LabelValueBeans, one for each locale available from the JVM
47      */

48     public static List JavaDoc getLocaleBeans()
49     {
50         if (locales == null)
51         {
52             locales = new ArrayList JavaDoc();
53             TreeSet JavaDoc locTree = new TreeSet JavaDoc(new LocaleComparator());
54             Locale JavaDoc[] localeArray = Locale.getAvailableLocales();
55             for (int i=0; i<localeArray.length; i++)
56             {
57                 locTree.add(localeArray[i]);
58             }
59             java.util.Iterator JavaDoc it = locTree.iterator();
60             while (it.hasNext())
61             {
62                 Locale JavaDoc loc = (Locale JavaDoc)it.next();
63                 locales.add(new LabelValueBean(
64                    loc.getDisplayName(),
65                    loc.toString()));
66             }
67
68         }
69         return locales;
70     }
71
72     //-----------------------------------------------------------------------
73
/**
74      * html:options tag recognizes "ID" as a property
75      * so we don't have to go through all the rigamarole (sp?)
76      * that we did for Locales.
77      */

78     public static List JavaDoc getTimeZoneBeans()
79     {
80         if (timeZones == null)
81         {
82             Date JavaDoc today = new Date JavaDoc();
83             timeZones = new ArrayList JavaDoc();
84             TreeSet JavaDoc zoneTree = new TreeSet JavaDoc(new TimeZoneComparator());
85             String JavaDoc[] zoneArray = TimeZone.getAvailableIDs();
86             for (int i=0; i<zoneArray.length; i++)
87             {
88                 zoneTree.add((TimeZone JavaDoc)TimeZone.getTimeZone(zoneArray[i]));
89             }
90             java.util.Iterator JavaDoc it = zoneTree.iterator();
91             while (it.hasNext())
92             {
93                 StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
94                 TimeZone JavaDoc zone = (TimeZone JavaDoc)it.next();
95                 sb.append(zone.getDisplayName(zone.inDaylightTime(today), TimeZone.SHORT));
96                 sb.append(" - ");
97                 sb.append(zone.getID());
98                 timeZones.add(new LabelValueBean(
99                    sb.toString(),
100                    zone.getID()));
101             }
102         }
103         return timeZones;
104     }
105 }
106
Popular Tags