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.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 setDisplayStyle(int displayStyle) {
039                    _displayStyle = displayStyle;
040            }
041    
042            public void setFormAction(String formAction) {
043                    _formAction = formAction;
044            }
045    
046            public void setFormName(String formName) {
047                    _formName = formName;
048            }
049    
050            public void setLanguageIds(String[] languageIds) {
051                    _languageIds = languageIds;
052            }
053    
054            public void setName(String name) {
055                    _name = name;
056            }
057    
058            @Override
059            protected void cleanUp() {
060                    _displayStyle = LIST_ICON;
061                    _formAction = null;
062                    _formName = "fm";
063                    _languageIds = null;
064                    _name = "languageId";
065            }
066    
067            @Override
068            protected String getPage() {
069                    return _PAGE;
070            }
071    
072            @Override
073            protected void setAttributes(HttpServletRequest request) {
074                    request.setAttribute(
075                            "liferay-ui:language:displayStyle", String.valueOf(_displayStyle));
076                    request.setAttribute("liferay-ui:language:formAction", _formAction);
077                    request.setAttribute("liferay-ui:language:formName", _formName);
078    
079                    Locale[] locales = null;
080    
081                    if ((_languageIds == null) || (_languageIds.length == 0)) {
082                            locales = LanguageUtil.getAvailableLocales();
083                    }
084                    else {
085                            locales = LocaleUtil.fromLanguageIds(_languageIds);
086                    }
087    
088                    request.setAttribute("liferay-ui:language:locales", locales);
089                    request.setAttribute("liferay-ui:language:name", _name);
090            }
091    
092            private static final String _PAGE = "/html/taglib/ui/language/page.jsp";
093    
094            private int _displayStyle = LIST_ICON;
095            private String _formAction;
096            private String _formName = "fm";
097            private String[] _languageIds;
098            private String _name = "languageId";
099    
100    }