001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.taglib.ui;
022:
023: import com.liferay.portal.kernel.language.LanguageUtil;
024: import com.liferay.portal.kernel.servlet.StringServletResponse;
025: import com.liferay.portal.kernel.util.LocaleUtil;
026: import com.liferay.taglib.util.IncludeTag;
027:
028: import java.io.IOException;
029:
030: import java.util.Locale;
031:
032: import javax.servlet.RequestDispatcher;
033: import javax.servlet.ServletContext;
034: import javax.servlet.ServletException;
035: import javax.servlet.http.HttpServletRequest;
036: import javax.servlet.http.HttpServletResponse;
037: import javax.servlet.jsp.JspException;
038:
039: /**
040: * <a href="LanguageTag.java.html"><b><i>View Source</i></b></a>
041: *
042: * @author Brian Wing Shun Chan
043: *
044: */
045: public class LanguageTag extends IncludeTag {
046:
047: public static final int LIST_ICON = 0;
048:
049: public static final int LIST_LONG_TEXT = 1;
050:
051: public static final int LIST_SHORT_TEXT = 2;
052:
053: public static final int SELECT_BOX = 3;
054:
055: public static void doTag(ServletContext ctx,
056: HttpServletRequest req, HttpServletResponse res)
057: throws IOException, ServletException {
058:
059: doTag(_PAGE, _FORM_NAME, _FORM_ACTION, _NAME, null,
060: _DISPLAY_STYLE, ctx, req, res);
061: }
062:
063: public static void doTag(String formName, String formAction,
064: String name, String[] languageIds, int displayStyle,
065: ServletContext ctx, HttpServletRequest req,
066: HttpServletResponse res) throws IOException,
067: ServletException {
068:
069: doTag(_PAGE, formName, formAction, name, languageIds,
070: displayStyle, ctx, req, res);
071: }
072:
073: public static void doTag(String page, String formName,
074: String formAction, String name, String[] languageIds,
075: int displayStyle, ServletContext ctx,
076: HttpServletRequest req, HttpServletResponse res)
077: throws IOException, ServletException {
078:
079: req.setAttribute("liferay-ui:language:formName", formName);
080: req.setAttribute("liferay-ui:language:formAction", formAction);
081: req.setAttribute("liferay-ui:language:name", name);
082:
083: Locale[] locales = null;
084:
085: if ((languageIds == null) || (languageIds.length == 0)) {
086: locales = LanguageUtil.getAvailableLocales();
087: } else {
088: locales = LocaleUtil.fromLanguageIds(languageIds);
089: }
090:
091: req.setAttribute("liferay-ui:language:locales", locales);
092:
093: req.setAttribute("liferay-ui:language:displayStyle", String
094: .valueOf(displayStyle));
095:
096: RequestDispatcher rd = ctx.getRequestDispatcher(page);
097:
098: rd.include(req, res);
099: }
100:
101: public int doEndTag() throws JspException {
102: try {
103: ServletContext ctx = getServletContext();
104: HttpServletRequest req = getServletRequest();
105: StringServletResponse res = getServletResponse();
106:
107: doTag(_formName, _formAction, _name, _languageIds,
108: _displayStyle, ctx, req, res);
109:
110: pageContext.getOut().print(res.getString());
111:
112: return EVAL_PAGE;
113: } catch (Exception e) {
114: throw new JspException(e);
115: }
116: }
117:
118: public void setFormName(String formName) {
119: _formName = formName;
120: }
121:
122: public void setFormAction(String formAction) {
123: _formAction = formAction;
124: }
125:
126: public void setName(String name) {
127: _name = name;
128: }
129:
130: public void setLanguageIds(String[] languageIds) {
131: _languageIds = languageIds;
132: }
133:
134: public void setDisplayStyle(int displayStyle) {
135: _displayStyle = displayStyle;
136: }
137:
138: protected String getDefaultPage() {
139: return _PAGE;
140: }
141:
142: private static final String _PAGE = "/html/taglib/ui/language/page.jsp";
143:
144: private static final String _FORM_NAME = "fm";
145:
146: private static final String _FORM_ACTION = null;
147:
148: private static final String _NAME = "languageId";
149:
150: private static final int _DISPLAY_STYLE = 0;
151:
152: private String _formName = _FORM_NAME;
153: private String _formAction = _FORM_ACTION;
154: private String _name = _NAME;
155: private String[] _languageIds;
156: private int _displayStyle = _DISPLAY_STYLE;
157:
158: }
|