001    /**
002     * Copyright (c) 2000-2013 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.portal.kernel.util.WebKeys;
020    import com.liferay.portal.theme.ThemeDisplay;
021    import com.liferay.taglib.util.IncludeTag;
022    
023    import java.util.Locale;
024    
025    import javax.servlet.http.HttpServletRequest;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class LanguageTag extends IncludeTag {
031    
032            public static final int LIST_ICON = 0;
033    
034            public static final int LIST_LONG_TEXT = 1;
035    
036            public static final int LIST_SHORT_TEXT = 2;
037    
038            public static final int SELECT_BOX = 3;
039    
040            public void setDisplayCurrentLocale(boolean displayCurrentLocale) {
041                    _displayCurrentLocale = displayCurrentLocale;
042            }
043    
044            public void setDisplayStyle(int displayStyle) {
045                    _displayStyle = displayStyle;
046            }
047    
048            public void setFormAction(String formAction) {
049                    _formAction = formAction;
050            }
051    
052            public void setFormName(String formName) {
053                    _formName = formName;
054            }
055    
056            public void setLanguageIds(String[] languageIds) {
057                    _languageIds = languageIds;
058            }
059    
060            public void setName(String name) {
061                    _name = name;
062            }
063    
064            @Override
065            protected void cleanUp() {
066                    _displayCurrentLocale = true;
067                    _displayStyle = LIST_ICON;
068                    _formAction = null;
069                    _formName = "fm";
070                    _languageIds = null;
071                    _name = "languageId";
072            }
073    
074            @Override
075            protected String getPage() {
076                    return _PAGE;
077            }
078    
079            @Override
080            protected void setAttributes(HttpServletRequest request) {
081                    request.setAttribute(
082                            "liferay-ui:language:displayCurrentLocale",
083                            String.valueOf(_displayCurrentLocale));
084                    request.setAttribute(
085                            "liferay-ui:language:displayStyle", String.valueOf(_displayStyle));
086                    request.setAttribute("liferay-ui:language:formAction", _formAction);
087                    request.setAttribute("liferay-ui:language:formName", _formName);
088    
089                    Locale[] locales = null;
090    
091                    if ((_languageIds == null) || (_languageIds.length == 0)) {
092                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
093                                    WebKeys.THEME_DISPLAY);
094    
095                            locales = LanguageUtil.getAvailableLocales(
096                                    themeDisplay.getSiteGroupId());
097                    }
098                    else {
099                            locales = LocaleUtil.fromLanguageIds(_languageIds);
100                    }
101    
102                    request.setAttribute("liferay-ui:language:locales", locales);
103                    request.setAttribute("liferay-ui:language:name", _name);
104            }
105    
106            private static final String _PAGE = "/html/taglib/ui/language/page.jsp";
107    
108            private boolean _displayCurrentLocale = true;
109            private int _displayStyle = LIST_ICON;
110            private String _formAction;
111            private String _formName = "fm";
112            private String[] _languageIds;
113            private String _name = "languageId";
114    
115    }