1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.taglib.ui;
16  
17  import com.liferay.portal.kernel.language.LanguageUtil;
18  import com.liferay.portal.kernel.servlet.StringServletResponse;
19  import com.liferay.portal.kernel.util.LocaleUtil;
20  import com.liferay.taglib.util.IncludeTag;
21  
22  import java.util.Locale;
23  
24  import javax.servlet.RequestDispatcher;
25  import javax.servlet.ServletContext;
26  import javax.servlet.http.HttpServletRequest;
27  import javax.servlet.http.HttpServletResponse;
28  import javax.servlet.jsp.JspException;
29  
30  /**
31   * <a href="LanguageTag.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class LanguageTag extends IncludeTag {
36  
37      public static final int LIST_ICON = 0;
38  
39      public static final int LIST_LONG_TEXT = 1;
40  
41      public static final int LIST_SHORT_TEXT = 2;
42  
43      public static final int SELECT_BOX = 3;
44  
45      public static void doTag(
46              ServletContext servletContext, HttpServletRequest request,
47              HttpServletResponse response)
48          throws Exception {
49  
50          doTag(
51              _PAGE, _FORM_NAME, _FORM_ACTION, _NAME, null, _DISPLAY_STYLE,
52              servletContext, request, response);
53      }
54  
55      public static void doTag(
56              String formName, String formAction, String name,
57              String[] languageIds, int displayStyle,
58              ServletContext servletContext, HttpServletRequest request,
59              HttpServletResponse response)
60          throws Exception {
61  
62          doTag(
63              _PAGE, formName, formAction, name, languageIds, displayStyle,
64              servletContext, request, response);
65      }
66  
67      public static void doTag(
68              String page, String formName, String formAction, String name,
69              String[] languageIds, int displayStyle,
70              ServletContext servletContext, HttpServletRequest request,
71              HttpServletResponse response)
72          throws Exception {
73  
74          request.setAttribute("liferay-ui:language:formName", formName);
75          request.setAttribute("liferay-ui:language:formAction", formAction);
76          request.setAttribute("liferay-ui:language:name", name);
77  
78          Locale[] locales = null;
79  
80          if ((languageIds == null) || (languageIds.length == 0)) {
81              locales = LanguageUtil.getAvailableLocales();
82          }
83          else {
84              locales = LocaleUtil.fromLanguageIds(languageIds);
85          }
86  
87          request.setAttribute("liferay-ui:language:locales", locales);
88  
89          request.setAttribute(
90              "liferay-ui:language:displayStyle", String.valueOf(displayStyle));
91  
92          RequestDispatcher requestDispatcher =
93              servletContext.getRequestDispatcher(page);
94  
95          requestDispatcher.include(request, response);
96      }
97  
98      public int doEndTag() throws JspException {
99          try {
100             ServletContext servletContext = getServletContext();
101             HttpServletRequest request = getServletRequest();
102             StringServletResponse stringResponse = getServletResponse();
103 
104             doTag(
105                 getPage(), _formName, _formAction, _name, _languageIds,
106                 _displayStyle, servletContext, request, stringResponse);
107 
108             pageContext.getOut().print(stringResponse.getString());
109 
110             return EVAL_PAGE;
111         }
112         catch (Exception e) {
113             throw new JspException(e);
114         }
115     }
116 
117     public void setFormName(String formName) {
118         _formName = formName;
119     }
120 
121     public void setFormAction(String formAction) {
122         _formAction = formAction;
123     }
124 
125     public void setName(String name) {
126         _name = name;
127     }
128 
129     public void setLanguageIds(String[] languageIds) {
130         _languageIds = languageIds;
131     }
132 
133     public void setDisplayStyle(int displayStyle) {
134         _displayStyle = displayStyle;
135     }
136 
137     protected String getDefaultPage() {
138         return _PAGE;
139     }
140 
141     private static final String _PAGE = "/html/taglib/ui/language/page.jsp";
142 
143     private static final String _FORM_NAME = "fm";
144 
145     private static final String _FORM_ACTION = null;
146 
147     private static final String _NAME = "languageId";
148 
149     private static final int _DISPLAY_STYLE = 0;
150 
151     private String _formName = _FORM_NAME;
152     private String _formAction = _FORM_ACTION;
153     private String _name = _NAME;
154     private String[] _languageIds;
155     private int _displayStyle = _DISPLAY_STYLE;
156 
157 }