001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.ui;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.LocaleUtil;
019    import com.liferay.taglib.util.IncludeTag;
020    
021    import java.util.Locale;
022    
023    import javax.servlet.http.HttpServletRequest;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class LanguageTag extends IncludeTag {
029    
030            public static final int LIST_ICON = 0;
031    
032            public static final int LIST_LONG_TEXT = 1;
033    
034            public static final int LIST_SHORT_TEXT = 2;
035    
036            public static final int SELECT_BOX = 3;
037    
038            public void setDisplayCurrentLocale(boolean displayCurrentLocale) {
039                    _displayCurrentLocale = displayCurrentLocale;
040            }
041    
042            public void setDisplayStyle(int displayStyle) {
043                    _displayStyle = displayStyle;
044            }
045    
046            public void setFormAction(String formAction) {
047                    _formAction = formAction;
048            }
049    
050            public void setFormName(String formName) {
051                    _formName = formName;
052            }
053    
054            public void setLanguageIds(String[] languageIds) {
055                    _languageIds = languageIds;
056            }
057    
058            public void setName(String name) {
059                    _name = name;
060            }
061    
062            @Override
063            protected void cleanUp() {
064                    _displayCurrentLocale = true;
065                    _displayStyle = LIST_ICON;
066                    _formAction = null;
067                    _formName = "fm";
068                    _languageIds = null;
069                    _name = "languageId";
070            }
071    
072            @Override
073            protected String getPage() {
074                    return _PAGE;
075            }
076    
077            @Override
078            protected void setAttributes(HttpServletRequest request) {
079                    request.setAttribute(
080                            "liferay-ui:language:displayCurrentLocale",
081                            String.valueOf(_displayCurrentLocale));
082                    request.setAttribute(
083                            "liferay-ui:language:displayStyle", String.valueOf(_displayStyle));
084                    request.setAttribute("liferay-ui:language:formAction", _formAction);
085                    request.setAttribute("liferay-ui:language:formName", _formName);
086    
087                    Locale[] locales = null;
088    
089                    if ((_languageIds == null) || (_languageIds.length == 0)) {
090                            locales = LanguageUtil.getAvailableLocales();
091                    }
092                    else {
093                            locales = LocaleUtil.fromLanguageIds(_languageIds);
094                    }
095    
096                    request.setAttribute("liferay-ui:language:locales", locales);
097                    request.setAttribute("liferay-ui:language:name", _name);
098            }
099    
100            private static final String _PAGE = "/html/taglib/ui/language/page.jsp";
101    
102            private boolean _displayCurrentLocale = true;
103            private int _displayStyle = LIST_ICON;
104            private String _formAction;
105            private String _formName = "fm";
106            private String[] _languageIds;
107            private String _name = "languageId";
108    
109    }