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