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