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.util.ParamUtil;
26  import com.liferay.portal.kernel.util.ServerDetector;
27  import com.liferay.portal.kernel.util.StringPool;
28  import com.liferay.portal.kernel.util.StringUtil;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.taglib.util.ParamAndPropertyAncestorTagImpl;
31  
32  import javax.portlet.PortletURL;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.jsp.JspException;
36  
37  /**
38   * <a href="TabsTag.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class TabsTag extends ParamAndPropertyAncestorTagImpl {
43  
44      public int doStartTag() throws JspException {
45          try {
46              HttpServletRequest request = getServletRequest();
47  
48              request.setAttribute("liferay-ui:tabs:names", _names);
49  
50              if ((_tabsValues == null) || (_tabsValues.length < _names.length)) {
51                  _tabsValues = _names;
52              }
53  
54              request.setAttribute("liferay-ui:tabs:values", _tabsValues);
55  
56              request.setAttribute("liferay-ui:tabs:formName", _formName);
57              request.setAttribute("liferay-ui:tabs:param", _param);
58  
59              if (_value == null) {
60                  if (_tabsValues.length > 0) {
61                      _value = ParamUtil.getString(
62                          request, _param, _tabsValues[0]);
63                  }
64              }
65  
66              if (Validator.isNull(_value)) {
67                  if (_tabsValues.length > 0) {
68                      _value = _tabsValues[0];
69                  }
70                  else {
71                      _value = StringPool.BLANK;
72                  }
73              }
74  
75              boolean match = false;
76  
77              for (int i = 0; i < _tabsValues.length; i++) {
78                  if (_value.equals(_tabsValues[i])) {
79                      match = true;
80                  }
81              }
82  
83              if (!match) {
84                  if (_tabsValues.length > 0) {
85                      _value = _tabsValues[0];
86                  }
87                  else {
88                      _value = StringPool.BLANK;
89                  }
90              }
91  
92              request.setAttribute("liferay-ui:tabs:value", _value);
93              request.setAttribute("liferay-ui:tabs:portletURL", _portletURL);
94              request.setAttribute("liferay-ui:tabs:url", _url);
95  
96              if (_url0 != null) {
97                  request.setAttribute("liferay-ui:tabs:url0", _url0);
98              }
99  
100             if (_url1 != null) {
101                 request.setAttribute("liferay-ui:tabs:url1", _url1);
102             }
103 
104             if (_url2 != null) {
105                 request.setAttribute("liferay-ui:tabs:url2", _url2);
106             }
107 
108             if (_url3 != null) {
109                 request.setAttribute("liferay-ui:tabs:url3", _url3);
110             }
111 
112             if (_url4 != null) {
113                 request.setAttribute("liferay-ui:tabs:url4", _url4);
114             }
115 
116             if (_url5 != null) {
117                 request.setAttribute("liferay-ui:tabs:url5", _url5);
118             }
119 
120             if (_url6 != null) {
121                 request.setAttribute("liferay-ui:tabs:url6", _url6);
122             }
123 
124             if (_url7 != null) {
125                 request.setAttribute("liferay-ui:tabs:url7", _url7);
126             }
127 
128             if (_url8 != null) {
129                 request.setAttribute("liferay-ui:tabs:url8", _url8);
130             }
131 
132             if (_url9 != null) {
133                 request.setAttribute("liferay-ui:tabs:url9", _url9);
134             }
135 
136             request.setAttribute("liferay-ui:tabs:backURL", _backURL);
137             request.setAttribute(
138                 "liferay-ui:tabs:refresh", String.valueOf(_refresh));
139             request.setAttribute(
140                 "liferay-ui:tabs:onClick", String.valueOf(_onClick));
141 
142             include(getStartPage());
143 
144             return EVAL_BODY_INCLUDE;
145         }
146         catch (Exception e) {
147             throw new JspException(e);
148         }
149     }
150 
151     public int doEndTag() throws JspException {
152         try {
153             HttpServletRequest request =
154                 (HttpServletRequest)pageContext.getRequest();
155 
156             include(getEndPage());
157 
158             request.removeAttribute("liferay-ui:tabs:names");
159             request.removeAttribute("liferay-ui:tabs:values");
160             request.removeAttribute("liferay-ui:tabs:formName");
161             request.removeAttribute("liferay-ui:tabs:param");
162             request.removeAttribute("liferay-ui:tabs:value");
163             request.removeAttribute("liferay-ui:tabs:portletURL");
164             request.removeAttribute("liferay-ui:tabs:url");
165             request.removeAttribute("liferay-ui:tabs:url0");
166             request.removeAttribute("liferay-ui:tabs:url1");
167             request.removeAttribute("liferay-ui:tabs:url2");
168             request.removeAttribute("liferay-ui:tabs:url3");
169             request.removeAttribute("liferay-ui:tabs:url4");
170             request.removeAttribute("liferay-ui:tabs:url5");
171             request.removeAttribute("liferay-ui:tabs:url6");
172             request.removeAttribute("liferay-ui:tabs:url7");
173             request.removeAttribute("liferay-ui:tabs:url8");
174             request.removeAttribute("liferay-ui:tabs:url9");
175             request.removeAttribute("liferay-ui:tabs:backURL");
176             request.removeAttribute("liferay-ui:tabs:refresh");
177             request.removeAttribute("liferay-ui:tabs:onClick");
178 
179             return EVAL_PAGE;
180         }
181         catch (Exception e) {
182             throw new JspException(e);
183         }
184         finally {
185             if (!ServerDetector.isResin()) {
186                 _startPage = null;
187                 _endPage = null;
188                 _names = null;
189                 _namesPos = 0;
190                 _tabsValues = null;
191                 _formName = StringPool.BLANK;
192                 _param = "tabs1";
193                 _value = null;
194                 _portletURL = null;
195                 _url = null;
196                 _url0 = null;
197                 _url1 = null;
198                 _url2 = null;
199                 _url3 = null;
200                 _url4 = null;
201                 _url5 = null;
202                 _url6 = null;
203                 _url7 = null;
204                 _url8 = null;
205                 _url9 = null;
206                 _backURL = null;
207                 _refresh = true;
208                 _onClick = null;
209             }
210         }
211     }
212 
213     public String getStartPage() {
214         if (Validator.isNull(_startPage)) {
215             return _START_PAGE;
216         }
217         else {
218             return _startPage;
219         }
220     }
221 
222     public void setStartPage(String startPage) {
223         _startPage = startPage;
224     }
225 
226     public String getEndPage() {
227         if (Validator.isNull(_endPage)) {
228             return _END_PAGE;
229         }
230         else {
231             return _endPage;
232         }
233     }
234 
235     public void setEndPage(String endPage) {
236         _endPage = endPage;
237     }
238 
239     public void setNames(String names) {
240         _names = StringUtil.split(names);
241     }
242 
243     public void setTabsValues(String tabsValues) {
244         _tabsValues = StringUtil.split(tabsValues);
245     }
246 
247     public void setFormName(String formName) {
248         _formName = formName;
249     }
250 
251     public String getParam() {
252         return _param;
253     }
254 
255     public void setParam(String param) {
256         _param = param;
257     }
258 
259     public void setValue(String value) {
260         _value = value;
261     }
262 
263     public void setPortletURL(PortletURL portletURL) {
264         _portletURL = portletURL;
265     }
266 
267     public void setUrl(String url) {
268         _url = url;
269     }
270 
271     public void setUrl0(String url0) {
272         _url0 = url0;
273     }
274 
275     public void setUrl1(String url1) {
276         _url1 = url1;
277     }
278 
279     public void setUrl2(String url2) {
280         _url2 = url2;
281     }
282 
283     public void setUrl3(String url3) {
284         _url3 = url3;
285     }
286 
287     public void setUrl4(String url4) {
288         _url4 = url4;
289     }
290 
291     public void setUrl5(String url5) {
292         _url5 = url5;
293     }
294 
295     public void setUrl6(String url6) {
296         _url6 = url6;
297     }
298 
299     public void setUrl7(String url7) {
300         _url7 = url7;
301     }
302 
303     public void setUrl8(String url8) {
304         _url8 = url8;
305     }
306 
307     public void setUrl9(String url9) {
308         _url9 = url9;
309     }
310 
311     public void setBackURL(String backURL) {
312         _backURL = backURL;
313     }
314 
315     public boolean isRefresh() {
316         return _refresh;
317     }
318 
319     public void setRefresh(boolean refresh) {
320         _refresh = refresh;
321     }
322 
323     public String getOnClick() {
324         return _onClick;
325     }
326 
327     public void setOnClick(String onClick) {
328         _onClick = onClick;
329     }
330 
331     public String getSectionName() {
332         if (_names.length > _namesPos) {
333             return _names[_namesPos];
334         }
335         else {
336             return StringPool.BLANK;
337         }
338     }
339 
340     public boolean getSectionSelected() {
341         if ((_names.length == 0) ||
342             ((_names.length > _namesPos) &&
343                 (_names[_namesPos].equals(_value)))) {
344 
345             return true;
346         }
347         else {
348             return false;
349         }
350     }
351 
352     public void incrementSection() {
353         _namesPos++;
354     }
355 
356     private static final String _START_PAGE = "/html/taglib/ui/tabs/start.jsp";
357 
358     private static final String _END_PAGE = "/html/taglib/ui/tabs/end.jsp";
359 
360     private String _startPage;
361     private String _endPage;
362     private String[] _names;
363     private int _namesPos;
364     private String[] _tabsValues;
365     private String _formName;
366     private String _param = "tabs1";
367     private String _value;
368     private PortletURL _portletURL;
369     private String _url;
370     private String _url0;
371     private String _url1;
372     private String _url2;
373     private String _url3;
374     private String _url4;
375     private String _url5;
376     private String _url6;
377     private String _url7;
378     private String _url8;
379     private String _url9;
380     private String _backURL;
381     private boolean _refresh = true;
382     private String _onClick;
383 
384 }