1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class LanguageTag extends IncludeTag {
47  
48      public static final int LIST_ICON = 0;
49  
50      public static final int LIST_LONG_TEXT = 1;
51  
52      public static final int LIST_SHORT_TEXT = 2;
53  
54      public static final int SELECT_BOX = 3;
55  
56      public static void doTag(
57              ServletContext servletContext, HttpServletRequest request,
58              HttpServletResponse response)
59          throws IOException, ServletException {
60  
61          doTag(
62              _PAGE, _FORM_NAME, _FORM_ACTION, _NAME, null, _DISPLAY_STYLE,
63              servletContext, request, response);
64      }
65  
66      public static void doTag(
67              String formName, String formAction, String name,
68              String[] languageIds, int displayStyle,
69              ServletContext servletContext, HttpServletRequest request,
70              HttpServletResponse response)
71          throws IOException, ServletException {
72  
73          doTag(
74              _PAGE, formName, formAction, name, languageIds, displayStyle,
75              servletContext, request, response);
76      }
77  
78      public static void doTag(
79              String page, String formName, String formAction, String name,
80              String[] languageIds, int displayStyle,
81              ServletContext servletContext, HttpServletRequest request,
82              HttpServletResponse response)
83          throws IOException, ServletException {
84  
85          request.setAttribute("liferay-ui:language:formName", formName);
86          request.setAttribute("liferay-ui:language:formAction", formAction);
87          request.setAttribute("liferay-ui:language:name", name);
88  
89          Locale[] locales = null;
90  
91          if ((languageIds == null) || (languageIds.length == 0)) {
92              locales = LanguageUtil.getAvailableLocales();
93          }
94          else {
95              locales = LocaleUtil.fromLanguageIds(languageIds);
96          }
97  
98          request.setAttribute("liferay-ui:language:locales", locales);
99  
100         request.setAttribute(
101             "liferay-ui:language:displayStyle", String.valueOf(displayStyle));
102 
103         RequestDispatcher requestDispatcher =
104             servletContext.getRequestDispatcher(page);
105 
106         requestDispatcher.include(request, response);
107     }
108 
109     public int doEndTag() throws JspException {
110         try {
111             ServletContext servletContext = getServletContext();
112             HttpServletRequest request = getServletRequest();
113             StringServletResponse stringResponse = getServletResponse();
114 
115             doTag(
116                 getPage(), _formName, _formAction, _name, _languageIds,
117                 _displayStyle, servletContext, request, stringResponse);
118 
119             pageContext.getOut().print(stringResponse.getString());
120 
121             return EVAL_PAGE;
122         }
123         catch (Exception e) {
124             throw new JspException(e);
125         }
126     }
127 
128     public void setFormName(String formName) {
129         _formName = formName;
130     }
131 
132     public void setFormAction(String formAction) {
133         _formAction = formAction;
134     }
135 
136     public void setName(String name) {
137         _name = name;
138     }
139 
140     public void setLanguageIds(String[] languageIds) {
141         _languageIds = languageIds;
142     }
143 
144     public void setDisplayStyle(int displayStyle) {
145         _displayStyle = displayStyle;
146     }
147 
148     protected String getDefaultPage() {
149         return _PAGE;
150     }
151 
152     private static final String _PAGE = "/html/taglib/ui/language/page.jsp";
153 
154     private static final String _FORM_NAME = "fm";
155 
156     private static final String _FORM_ACTION = null;
157 
158     private static final String _NAME = "languageId";
159 
160     private static final int _DISPLAY_STYLE = 0;
161 
162     private String _formName = _FORM_NAME;
163     private String _formAction = _FORM_ACTION;
164     private String _name = _NAME;
165     private String[] _languageIds;
166     private int _displayStyle = _DISPLAY_STYLE;
167 
168 }