CVS: TapestryBook/examples/src/java/examples/l10n LocaleModel.java,NONE,1.1 L10N.java,NONE,1.1

Howard Lewis Ship <[email protected]> Fri, 11 Jul 2003 21:16:48 -0700
Newsgroups gmane.comp.java.tapestry.cvs
Message-ID <[email protected]>
Update of /cvsroot/tapestry/TapestryBook/examples/src/java/examples/l10n
In directory sc8-pr-cvs1:/tmp/cvs-serv6871/examples/src/java/examples/l10n

Added Files:
	LocaleModel.java L10N.java 
Log Message:
Reorganize examples application to reflect standard organization

--- NEW FILE: LocaleModel.java ---
package examples.l10n;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import org.apache.tapestry.form.IPropertySelectionModel;

/**
 *  A model that presents as a list all the available locales
 *  except the current one.
 *
 *  @author Howard Lewis Ship
 *  @version $Id: LocaleModel.java,v 1.1 2003/07/12 04:16:46 hship Exp $
 *  @since 2.4
 *
 **/

public class LocaleModel implements IPropertySelectionModel
{
    private Locale _activeLocale;
    private List _locales = new ArrayList();

    private static final Locale[] AVAILABLE_LOCALES =
        { Locale.ENGLISH, Locale.FRENCH, new Locale("es"), Locale.GERMAN };

    public LocaleModel(Locale activeLocale)
    {
        _activeLocale = activeLocale;

        String activeLanguage = activeLocale.getLanguage();

        for (int i = 0; i < AVAILABLE_LOCALES.length; i++)
        {
            if (AVAILABLE_LOCALES[i].getLanguage().equals(activeLanguage))
                continue;

            _locales.add(AVAILABLE_LOCALES[i]);
        }
    }

    public int getOptionCount()
    {
        return _locales.size();
    }

    public Object getOption(int index)
    {
        return _locales.get(index);
    }

    public String getLabel(int index)
    {
        Locale l = (Locale) _locales.get(index);

        return l.getDisplayLanguage(_activeLocale);
    }

    public String getValue(int index)
    {
        return Integer.toString(index);
    }

    public Object translateValue(String value)
    {
        int index = Integer.parseInt(value);

        return getOption(index);
    }

}

--- NEW FILE: L10N.java ---
package examples.l10n;

import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.form.IPropertySelectionModel;
import org.apache.tapestry.html.BasePage;

public class L10N extends BasePage
{
    private IPropertySelectionModel _localeModel;

    public IPropertySelectionModel getLocaleModel()
    {
        if (_localeModel == null)
            _localeModel = new LocaleModel(getLocale());

        return _localeModel;
    }

    public void formSubmit(IRequestCycle cycle)
    {
        // This page is already loaded, with whatever the old locale was.
        // engine.locale has been updated with the new locale.
        // Load and render a different page to see the change.

        cycle.activate("L10NResult");
    }
}



-------------------------------------------------------
This SF.Net email sponsored by: Parasoft
Error proof Web apps, automate testing & more.
Download & eval WebKing and get a free book.
www.parasoft.com/bulletproofapps1