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 setLanguageId(String languageId) {
058                    _languageId = languageId;
059            }
060    
061            public void setLanguageIds(String[] languageIds) {
062                    _languageIds = languageIds;
063            }
064    
065            public void setName(String name) {
066                    _name = name;
067            }
068    
069            @Override
070            protected void cleanUp() {
071                    _displayCurrentLocale = true;
072                    _displayStyle = LIST_ICON;
073                    _formAction = null;
074                    _formName = "fm";
075                    _languageId = null;
076                    _languageIds = null;
077                    _name = "languageId";
078            }
079    
080            @Override
081            protected String getPage() {
082                    return _PAGE;
083            }
084    
085            @Override
086            protected void setAttributes(HttpServletRequest request) {
087                    request.setAttribute(
088                            "liferay-ui:language:displayCurrentLocale",
089                            String.valueOf(_displayCurrentLocale));
090                    request.setAttribute(
091                            "liferay-ui:language:displayStyle", String.valueOf(_displayStyle));
092                    request.setAttribute("liferay-ui:language:formAction", _formAction);
093                    request.setAttribute("liferay-ui:language:formName", _formName);
094                    request.setAttribute("liferay-ui:language:languageId", _languageId);
095    
096                    Locale[] locales = null;
097    
098                    if (ArrayUtil.isEmpty(_languageIds)) {
099                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
100                                    WebKeys.THEME_DISPLAY);
101    
102                            locales = LanguageUtil.getAvailableLocales(
103                                    themeDisplay.getSiteGroupId());
104                    }
105                    else {
106                            locales = LocaleUtil.fromLanguageIds(_languageIds);
107                    }
108    
109                    request.setAttribute("liferay-ui:language:locales", locales);
110                    request.setAttribute("liferay-ui:language:name", _name);
111            }
112    
113            private static final String _PAGE = "/html/taglib/ui/language/page.jsp";
114    
115            private boolean _displayCurrentLocale = true;
116            private int _displayStyle = LIST_ICON;
117            private String _formAction;
118            private String _formName = "fm";
119            private String _languageId;
120            private String[] _languageIds;
121            private String _name = "languageId";
122    
123    }