001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.ui;
016    
017    import com.liferay.portal.kernel.util.ArrayUtil;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.kernel.util.ServerDetector;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.taglib.util.IncludeTag;
024    
025    import javax.portlet.PortletURL;
026    
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.jsp.JspException;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class TabsTag extends IncludeTag {
034    
035            @Override
036            public int doEndTag() throws JspException {
037                    try {
038                            HttpServletRequest request =
039                                    (HttpServletRequest)pageContext.getRequest();
040    
041                            include(getEndPage());
042    
043                            request.removeAttribute("liferay-ui:tabs:names");
044                            request.removeAttribute("liferay-ui:tabs:values");
045                            request.removeAttribute("liferay-ui:tabs:formName");
046                            request.removeAttribute("liferay-ui:tabs:param");
047                            request.removeAttribute("liferay-ui:tabs:value");
048                            request.removeAttribute("liferay-ui:tabs:portletURL");
049                            request.removeAttribute("liferay-ui:tabs:url");
050                            request.removeAttribute("liferay-ui:tabs:url0");
051                            request.removeAttribute("liferay-ui:tabs:url1");
052                            request.removeAttribute("liferay-ui:tabs:url2");
053                            request.removeAttribute("liferay-ui:tabs:url3");
054                            request.removeAttribute("liferay-ui:tabs:url4");
055                            request.removeAttribute("liferay-ui:tabs:url5");
056                            request.removeAttribute("liferay-ui:tabs:url6");
057                            request.removeAttribute("liferay-ui:tabs:url7");
058                            request.removeAttribute("liferay-ui:tabs:url8");
059                            request.removeAttribute("liferay-ui:tabs:url9");
060                            request.removeAttribute("liferay-ui:tabs:backLabel");
061                            request.removeAttribute("liferay-ui:tabs:backURL");
062                            request.removeAttribute("liferay-ui:tabs:refresh");
063                            request.removeAttribute("liferay-ui:tabs:onClick");
064    
065                            return EVAL_PAGE;
066                    }
067                    catch (Exception e) {
068                            throw new JspException(e);
069                    }
070                    finally {
071                            if (!ServerDetector.isResin()) {
072                                    _startPage = null;
073                                    _endPage = null;
074                                    _names = null;
075                                    _namesPos = 0;
076                                    _tabsValues = null;
077                                    _formName = StringPool.BLANK;
078                                    _param = "tabs1";
079                                    _value = null;
080                                    _portletURL = null;
081                                    _url = null;
082                                    _url0 = null;
083                                    _url1 = null;
084                                    _url2 = null;
085                                    _url3 = null;
086                                    _url4 = null;
087                                    _url5 = null;
088                                    _url6 = null;
089                                    _url7 = null;
090                                    _url8 = null;
091                                    _url9 = null;
092                                    _backLabel = null;
093                                    _backURL = null;
094                                    _refresh = true;
095                                    _onClick = null;
096                            }
097                    }
098            }
099    
100            @Override
101            public int doStartTag() throws JspException {
102                    try {
103                            request.setAttribute("liferay-ui:tabs:names", _names);
104    
105                            if ((_tabsValues == null) || (_tabsValues.length < _names.length)) {
106                                    _tabsValues = _names;
107                            }
108    
109                            request.setAttribute("liferay-ui:tabs:values", _tabsValues);
110    
111                            request.setAttribute("liferay-ui:tabs:formName", _formName);
112                            request.setAttribute("liferay-ui:tabs:param", _param);
113    
114                            if (_value == null) {
115                                    if (_tabsValues.length > 0) {
116                                            _value = ParamUtil.getString(
117                                                    request, _param, _tabsValues[0]);
118                                    }
119                            }
120    
121                            if (Validator.isNull(_value)) {
122                                    if (_tabsValues.length > 0) {
123                                            _value = _tabsValues[0];
124                                    }
125                                    else {
126                                            _value = StringPool.BLANK;
127                                    }
128                            }
129    
130                            boolean match = false;
131    
132                            if (ArrayUtil.contains(_tabsValues, _value)) {
133                                    match = true;
134                            }
135    
136                            if (!match) {
137                                    if (_tabsValues.length > 0) {
138                                            _value = _tabsValues[0];
139                                    }
140                                    else {
141                                            _value = StringPool.BLANK;
142                                    }
143                            }
144    
145                            request.setAttribute("liferay-ui:tabs:value", _value);
146                            request.setAttribute("liferay-ui:tabs:portletURL", _portletURL);
147                            request.setAttribute("liferay-ui:tabs:url", _url);
148    
149                            if (_url0 != null) {
150                                    request.setAttribute("liferay-ui:tabs:url0", _url0);
151                            }
152    
153                            if (_url1 != null) {
154                                    request.setAttribute("liferay-ui:tabs:url1", _url1);
155                            }
156    
157                            if (_url2 != null) {
158                                    request.setAttribute("liferay-ui:tabs:url2", _url2);
159                            }
160    
161                            if (_url3 != null) {
162                                    request.setAttribute("liferay-ui:tabs:url3", _url3);
163                            }
164    
165                            if (_url4 != null) {
166                                    request.setAttribute("liferay-ui:tabs:url4", _url4);
167                            }
168    
169                            if (_url5 != null) {
170                                    request.setAttribute("liferay-ui:tabs:url5", _url5);
171                            }
172    
173                            if (_url6 != null) {
174                                    request.setAttribute("liferay-ui:tabs:url6", _url6);
175                            }
176    
177                            if (_url7 != null) {
178                                    request.setAttribute("liferay-ui:tabs:url7", _url7);
179                            }
180    
181                            if (_url8 != null) {
182                                    request.setAttribute("liferay-ui:tabs:url8", _url8);
183                            }
184    
185                            if (_url9 != null) {
186                                    request.setAttribute("liferay-ui:tabs:url9", _url9);
187                            }
188    
189                            request.setAttribute("liferay-ui:tabs:backLabel", _backLabel);
190                            request.setAttribute("liferay-ui:tabs:backURL", _backURL);
191                            request.setAttribute(
192                                    "liferay-ui:tabs:refresh", String.valueOf(_refresh));
193                            request.setAttribute(
194                                    "liferay-ui:tabs:onClick", String.valueOf(_onClick));
195    
196                            include(getStartPage());
197    
198                            return EVAL_BODY_INCLUDE;
199                    }
200                    catch (Exception e) {
201                            throw new JspException(e);
202                    }
203            }
204    
205            public String getOnClick() {
206                    return _onClick;
207            }
208    
209            public String getParam() {
210                    return _param;
211            }
212    
213            public String getSectionName() {
214                    if (_names.length > _namesPos) {
215                            return _names[_namesPos];
216                    }
217                    else {
218                            return StringPool.BLANK;
219                    }
220            }
221    
222            public boolean getSectionSelected() {
223                    if ((_names.length == 0) ||
224                            ((_names.length > _namesPos) &&
225                             _names[_namesPos].equals(_value))) {
226    
227                            return true;
228                    }
229                    else {
230                            return false;
231                    }
232            }
233    
234            public void incrementSection() {
235                    _namesPos++;
236            }
237    
238            public boolean isRefresh() {
239                    return _refresh;
240            }
241    
242            public void setBackLabel(String backLabel) {
243                    _backLabel = backLabel;
244            }
245    
246            public void setBackURL(String backURL) {
247                    _backURL = backURL;
248            }
249    
250            public void setEndPage(String endPage) {
251                    _endPage = endPage;
252            }
253    
254            public void setFormName(String formName) {
255                    _formName = formName;
256            }
257    
258            public void setNames(String names) {
259                    _names = StringUtil.split(names);
260            }
261    
262            public void setOnClick(String onClick) {
263                    _onClick = onClick;
264            }
265    
266            public void setParam(String param) {
267                    _param = param;
268            }
269    
270            public void setPortletURL(PortletURL portletURL) {
271                    _portletURL = portletURL;
272            }
273    
274            public void setRefresh(boolean refresh) {
275                    _refresh = refresh;
276            }
277    
278            public void setStartPage(String startPage) {
279                    _startPage = startPage;
280            }
281    
282            public void setTabsValues(String tabsValues) {
283                    _tabsValues = StringUtil.split(tabsValues);
284            }
285    
286            public void setUrl(String url) {
287                    _url = url;
288            }
289    
290            public void setUrl0(String url0) {
291                    _url0 = url0;
292            }
293    
294            public void setUrl1(String url1) {
295                    _url1 = url1;
296            }
297    
298            public void setUrl2(String url2) {
299                    _url2 = url2;
300            }
301    
302            public void setUrl3(String url3) {
303                    _url3 = url3;
304            }
305    
306            public void setUrl4(String url4) {
307                    _url4 = url4;
308            }
309    
310            public void setUrl5(String url5) {
311                    _url5 = url5;
312            }
313    
314            public void setUrl6(String url6) {
315                    _url6 = url6;
316            }
317    
318            public void setUrl7(String url7) {
319                    _url7 = url7;
320            }
321    
322            public void setUrl8(String url8) {
323                    _url8 = url8;
324            }
325    
326            public void setUrl9(String url9) {
327                    _url9 = url9;
328            }
329    
330            public void setValue(String value) {
331                    _value = value;
332            }
333    
334            @Override
335            protected String getEndPage() {
336                    if (Validator.isNull(_endPage)) {
337                            return _END_PAGE;
338                    }
339                    else {
340                            return _endPage;
341                    }
342            }
343    
344            @Override
345            protected String getStartPage() {
346                    if (Validator.isNull(_startPage)) {
347                            return _START_PAGE;
348                    }
349                    else {
350                            return _startPage;
351                    }
352            }
353    
354            private static final String _END_PAGE = "/html/taglib/ui/tabs/end.jsp";
355    
356            private static final String _START_PAGE = "/html/taglib/ui/tabs/start.jsp";
357    
358            private String _backLabel;
359            private String _backURL;
360            private String _endPage;
361            private String _formName;
362            private String[] _names;
363            private int _namesPos;
364            private String _onClick;
365            private String _param = "tabs1";
366            private PortletURL _portletURL;
367            private boolean _refresh = true;
368            private String _startPage;
369            private String[] _tabsValues;
370            private String _url;
371            private String _url0;
372            private String _url1;
373            private String _url2;
374            private String _url3;
375            private String _url4;
376            private String _url5;
377            private String _url6;
378            private String _url7;
379            private String _url8;
380            private String _url9;
381            private String _value;
382    
383    }