解读roller系列之----地区和时区的处理

来源:互联网 发布:ubuntu下中文输入法 编辑:程序博客网 时间:2024/05/17 21:43

    在roller中获得地区和时区的关键类是StrutsUtil, LocaleComparatorTimeZoneComparator为辅助类,主要是实现对地区和时区的排序功能。

StrutsUtil类:

    该类将用户可以获得的地区和时区封装成ArrayList类,并实现了LabelValueBean接口,以便在采用Struts框架中显示。

代码如下:

package org.roller.presentation.util;

 

import java.util.ArrayList;

import java.util.Date;

import java.util.List;

import java.util.Locale;

import java.util.TimeZone;

import java.util.TreeSet;

 

import org.apache.struts.util.LabelValueBean;

import org.roller.util.LocaleComparator;

import org.roller.util.TimeZoneComparator;

 

public class StrutsUtil

{

    public static ArrayList locales;

    public static ArrayList timeZones;

   

    //-----------------------------------------------------------------------

    /**

     * LabelValueBeans are Comparable but violate the

     * equals() part of the TreeSet requirements.

     * And the html:options tag won't recognize

     * toString as a property.  So we have to put the

     * Locales into a TreeSet to sort them, then convert

     * them to LabelValueBeans to display them.

     * Glad we only have to do this once.

     *

     * @return List of LabelValueBeans, one for each locale available from the JVM

     */

    public static List getLocaleBeans()

    {

        if (locales == null)

        {

locales = new ArrayList();

//有关TreeSet的知识请参考“软道—java语言”栏目。

            TreeSet locTree = new TreeSet(new LocaleComparator());

            Locale[] localeArray = Locale.getAvailableLocales();

            for (int i=0; i<localeArray.length; i++)

            {

                locTree.add(localeArray[i]);

            }

            java.util.Iterator it = locTree.iterator();

            while (it.hasNext())

            {

                Locale loc = (Locale)it.next();

                locales.add(new LabelValueBean(

                   loc.getDisplayName(),

                   loc.toString()));

            }

 

        }

        return locales;

    }

 

    //-----------------------------------------------------------------------

    /**

     * html:options tag recognizes "ID" as a property

     * so we don't have to go through all the rigamarole (sp?)

     * that we did for Locales.

     */

    public static List getTimeZoneBeans()

    {

        if (timeZones == null)

        {

            Date today = new Date();

timeZones = new ArrayList();

//有关TreeSet的知识请参考“软道—java语言”栏目。

            TreeSet zoneTree = new TreeSet(new TimeZoneComparator());

            String[] zoneArray = TimeZone.getAvailableIDs();

            for (int i=0; i<zoneArray.length; i++)

            {

                zoneTree.add((TimeZone)TimeZone.getTimeZone(zoneArray[i]));

            }

            java.util.Iterator it = zoneTree.iterator();

            while (it.hasNext())

            {

                StringBuffer sb = new StringBuffer();

                TimeZone zone = (TimeZone)it.next();

                sb.append(zone.getDisplayName(zone.inDaylightTime(today), TimeZone.SHORT));

                sb.append(" - ");

                sb.append(zone.getID());

                timeZones.add(new LabelValueBean(

                   sb.toString(),

                   zone.getID()));

            }

        }

        return timeZones;

    }

}

 

package org.roller.util;

import java.util.Locale;

import java.util.Comparator;

import java.io.Serializable;

public class LocaleComparator implements Comparator, Serializable

{

    public int compare(Object obj1, Object obj2)

    {

        if (obj1 instanceof Locale && obj2 instanceof Locale)

        {

            Locale locale1 = (Locale)obj1;

            Locale locale2 = (Locale)obj2;

            int compName = locale1.getDisplayName().compareTo(locale2.getDisplayName());

            if (compName == 0)

            {

                return locale1.toString().compareTo(locale2.toString());

            }

            return compName;

        }

        return 0;

    }

/* Do Comparators need to implement equals()? -Lance

    public boolean equals(Object obj)

    {

        if (obj instanceof LocaleComparator)

        {

            if (obj.equals(this)) return true;

        }

        return false;

    }

*/

}

 

package org.roller.util;

import java.util.TimeZone;

import java.util.Comparator;

import java.io.Serializable;

public class TimeZoneComparator implements Comparator, Serializable

{

    public int compare(Object obj1, Object obj2)

    {

        if (obj1 instanceof TimeZone && obj2 instanceof TimeZone)

        {

            TimeZone zone1 = (TimeZone)obj1;

            TimeZone zone2 = (TimeZone)obj2;

            int compName = zone1.getDisplayName().compareTo(zone2.getDisplayName());

            if (compName == 0)

            {

                            return zone1.getID().compareTo(zone2.getID());

                     }

            return compName;

        }

        return 0;

    }

    /* Do Comparators need to implement equals()? -Lance

    public boolean equals(Object obj)

    {

        if (obj instanceof TimeZoneComparator)

        {

            if (obj.equals(this)) return true;

        }

        return false;

    }

    */

}

原创粉丝点击