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