1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.taglib.ui;
24  
25  import com.liferay.portal.kernel.language.LanguageUtil;
26  import com.liferay.portal.kernel.servlet.StringServletResponse;
27  import com.liferay.portal.kernel.util.LocaleUtil;
28  import com.liferay.taglib.util.IncludeTag;
29  
30  import java.io.IOException;
31  
32  import java.util.Locale;
33  
34  import javax.servlet.RequestDispatcher;
35  import javax.servlet.ServletContext;
36  import javax.servlet.ServletException;
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  import javax.servlet.jsp.JspException;
40  
41  /**
42   * <a href="LanguageTag.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class LanguageTag extends IncludeTag {
48  
49      public static final int LIST_ICON = 0;
50  
51      public static final int LIST_LONG_TEXT = 1;
52  
53      public static final int LIST_SHORT_TEXT = 2;
54  
55      public static final int SELECT_BOX = 3;
56  
57      public static void doTag(
58              ServletContext servletContext, HttpServletRequest request,
59              HttpServletResponse response)
60          throws IOException, ServletException {
61  
62          doTag(
63              _PAGE, _FORM_NAME, _FORM_ACTION, _NAME, null, _DISPLAY_STYLE,
64              servletContext, request, response);
65      }
66  
67      public static void doTag(
68              String formName, String formAction, String name,
69              String[] languageIds, int displayStyle,
70              ServletContext servletContext, HttpServletRequest request,
71              HttpServletResponse response)
72          throws IOException, ServletException {
73  
74          doTag(
75              _PAGE, formName, formAction, name, languageIds, displayStyle,
76              servletContext, request, response);
77      }
78  
79      public static void doTag(
80              String page, String formName, String formAction, String name,
81              String[] languageIds, int displayStyle,
82              ServletContext servletContext, HttpServletRequest request,
83              HttpServletResponse response)
84          throws IOException, ServletException {
85  
86          request.setAttribute("liferay-ui:language:formName", formName);
87          request.setAttribute("liferay-ui:language:formAction", formAction);
88          request.setAttribute("liferay-ui:language:name", name);
89  
90          Locale[] locales = null;
91  
92          if ((languageIds == null) || (languageIds.length == 0)) {
93              locales = LanguageUtil.getAvailableLocales();
94          }
95          else {
96              locales = LocaleUtil.fromLanguageIds(languageIds);
97          }
98  
99          request.setAttribute("liferay-ui:language:locales", locales);
100 
101         request.setAttribute(
102             "liferay-ui:language:displayStyle", String.valueOf(displayStyle));
103 
104         RequestDispatcher requestDispatcher =
105             servletContext.getRequestDispatcher(page);
106 
107         requestDispatcher.include(request, response);
108     }
109 
110     public int doEndTag() throws JspException {
111         try {
112             ServletContext servletContext = getServletContext();
113             HttpServletRequest request = getServletRequest();
114             StringServletResponse stringResponse = getServletResponse();
115 
116             doTag(
117                 getPage(), _formName, _formAction, _name, _languageIds,
118                 _displayStyle, servletContext, request, stringResponse);
119 
120             pageContext.getOut().print(stringResponse.getString());
121 
122             return EVAL_PAGE;
123         }
124         catch (Exception e) {
125             throw new JspException(e);
126         }
127     }
128 
129     public void setFormName(String formName) {
130         _formName = formName;
131     }
132 
133     public void setFormAction(String formAction) {
134         _formAction = formAction;
135     }
136 
137     public void setName(String name) {
138         _name = name;
139     }
140 
141     public void setLanguageIds(String[] languageIds) {
142         _languageIds = languageIds;
143     }
144 
145     public void setDisplayStyle(int displayStyle) {
146         _displayStyle = displayStyle;
147     }
148 
149     protected String getDefaultPage() {
150         return _PAGE;
151     }
152 
153     private static final String _PAGE = "/html/taglib/ui/language/page.jsp";
154 
155     private static final String _FORM_NAME = "fm";
156 
157     private static final String _FORM_ACTION = null;
158 
159     private static final String _NAME = "languageId";
160 
161     private static final int _DISPLAY_STYLE = 0;
162 
163     private String _formName = _FORM_NAME;
164     private String _formAction = _FORM_ACTION;
165     private String _name = _NAME;
166     private String[] _languageIds;
167     private int _displayStyle = _DISPLAY_STYLE;
168 
169 }