001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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:backLabel");
044                            request.removeAttribute("liferay-ui:tabs:backURL");
045                            request.removeAttribute("liferay-ui:tabs:cssClass");
046                            request.removeAttribute("liferay-ui:tabs:formName");
047                            request.removeAttribute("liferay-ui:tabs:names");
048                            request.removeAttribute("liferay-ui:tabs:onClick");
049                            request.removeAttribute("liferay-ui:tabs:param");
050                            request.removeAttribute("liferay-ui:tabs:portletURL");
051                            request.removeAttribute("liferay-ui:tabs:refresh");
052                            request.removeAttribute("liferay-ui:tabs:type");
053                            request.removeAttribute("liferay-ui:tabs:url");
054                            request.removeAttribute("liferay-ui:tabs:url0");
055                            request.removeAttribute("liferay-ui:tabs:url1");
056                            request.removeAttribute("liferay-ui:tabs:url2");
057                            request.removeAttribute("liferay-ui:tabs:url3");
058                            request.removeAttribute("liferay-ui:tabs:url4");
059                            request.removeAttribute("liferay-ui:tabs:url5");
060                            request.removeAttribute("liferay-ui:tabs:url6");
061                            request.removeAttribute("liferay-ui:tabs:url7");
062                            request.removeAttribute("liferay-ui:tabs:url8");
063                            request.removeAttribute("liferay-ui:tabs:url9");
064                            request.removeAttribute("liferay-ui:tabs:value");
065                            request.removeAttribute("liferay-ui:tabs:values");
066    
067                            return EVAL_PAGE;
068                    }
069                    catch (Exception e) {
070                            throw new JspException(e);
071                    }
072                    finally {
073                            if (!ServerDetector.isResin()) {
074                                    _backLabel = null;
075                                    _backURL = null;
076                                    _cssClass = null;
077                                    _endPage = null;
078                                    _formName = StringPool.BLANK;
079                                    _names = null;
080                                    _namesPos = 0;
081                                    _onClick = null;
082                                    _param = "tabs1";
083                                    _portletURL = null;
084                                    _refresh = true;
085                                    _startPage = null;
086                                    _tabsValues = null;
087                                    _type = null;
088                                    _url = null;
089                                    _url0 = null;
090                                    _url1 = null;
091                                    _url2 = null;
092                                    _url3 = null;
093                                    _url4 = null;
094                                    _url5 = null;
095                                    _url6 = null;
096                                    _url7 = null;
097                                    _url8 = null;
098                                    _url9 = null;
099                                    _value = null;
100                            }
101                    }
102            }
103    
104            @Override
105            public int doStartTag() throws JspException {
106                    try {
107                            request.setAttribute("liferay-ui:tabs:names", _names);
108    
109                            if ((_tabsValues == null) || (_tabsValues.length < _names.length)) {
110                                    _tabsValues = _names;
111                            }
112    
113                            request.setAttribute("liferay-ui:tabs:param", _param);
114                            request.setAttribute("liferay-ui:tabs:values", _tabsValues);
115    
116                            if (_value == null) {
117                                    if (_tabsValues.length > 0) {
118                                            _value = ParamUtil.getString(
119                                                    request, _param, _tabsValues[0]);
120                                    }
121                            }
122    
123                            if (Validator.isNull(_value)) {
124                                    if (_tabsValues.length > 0) {
125                                            _value = _tabsValues[0];
126                                    }
127                                    else {
128                                            _value = StringPool.BLANK;
129                                    }
130                            }
131    
132                            boolean match = false;
133    
134                            if (ArrayUtil.contains(_tabsValues, _value)) {
135                                    match = true;
136                            }
137    
138                            if (!match) {
139                                    if (_tabsValues.length > 0) {
140                                            _value = _tabsValues[0];
141                                    }
142                                    else {
143                                            _value = StringPool.BLANK;
144                                    }
145                            }
146    
147                            request.setAttribute("liferay-ui:tabs:backLabel", _backLabel);
148                            request.setAttribute("liferay-ui:tabs:backURL", _backURL);
149                            request.setAttribute("liferay-ui:tabs:cssClass", _cssClass);
150                            request.setAttribute("liferay-ui:tabs:formName", _formName);
151                            request.setAttribute(
152                                    "liferay-ui:tabs:onClick", String.valueOf(_onClick));
153                            request.setAttribute("liferay-ui:tabs:portletURL", _portletURL);
154                            request.setAttribute(
155                                    "liferay-ui:tabs:refresh", String.valueOf(_refresh));
156                            request.setAttribute("liferay-ui:tabs:type", _type);
157                            request.setAttribute("liferay-ui:tabs:url", _url);
158    
159                            if (_url0 != null) {
160                                    request.setAttribute("liferay-ui:tabs:url0", _url0);
161                            }
162    
163                            if (_url1 != null) {
164                                    request.setAttribute("liferay-ui:tabs:url1", _url1);
165                            }
166    
167                            if (_url2 != null) {
168                                    request.setAttribute("liferay-ui:tabs:url2", _url2);
169                            }
170    
171                            if (_url3 != null) {
172                                    request.setAttribute("liferay-ui:tabs:url3", _url3);
173                            }
174    
175                            if (_url4 != null) {
176                                    request.setAttribute("liferay-ui:tabs:url4", _url4);
177                            }
178    
179                            if (_url5 != null) {
180                                    request.setAttribute("liferay-ui:tabs:url5", _url5);
181                            }
182    
183                            if (_url6 != null) {
184                                    request.setAttribute("liferay-ui:tabs:url6", _url6);
185                            }
186    
187                            if (_url7 != null) {
188                                    request.setAttribute("liferay-ui:tabs:url7", _url7);
189                            }
190    
191                            if (_url8 != null) {
192                                    request.setAttribute("liferay-ui:tabs:url8", _url8);
193                            }
194    
195                            if (_url9 != null) {
196                                    request.setAttribute("liferay-ui:tabs:url9", _url9);
197                            }
198    
199                            request.setAttribute("liferay-ui:tabs:value", _value);
200    
201                            include(getStartPage());
202    
203                            return EVAL_BODY_INCLUDE;
204                    }
205                    catch (Exception e) {
206                            throw new JspException(e);
207                    }
208            }
209    
210            public String getOnClick() {
211                    return _onClick;
212            }
213    
214            public String getParam() {
215                    return _param;
216            }
217    
218            public String getSectionName() {
219                    if (_names.length > _namesPos) {
220                            return _names[_namesPos];
221                    }
222                    else {
223                            return StringPool.BLANK;
224                    }
225            }
226    
227            public boolean getSectionSelected() {
228                    if ((_names.length == 0) ||
229                            ((_names.length > _namesPos) &&
230                             _names[_namesPos].equals(_value))) {
231    
232                            return true;
233                    }
234                    else {
235                            return false;
236                    }
237            }
238    
239            public void incrementSection() {
240                    _namesPos++;
241            }
242    
243            public boolean isRefresh() {
244                    return _refresh;
245            }
246    
247            public void setBackLabel(String backLabel) {
248                    _backLabel = backLabel;
249            }
250    
251            public void setBackURL(String backURL) {
252                    _backURL = backURL;
253            }
254    
255            public void setCssClass(String cssClass) {
256                    _cssClass = cssClass;
257            }
258    
259            public void setEndPage(String endPage) {
260                    _endPage = endPage;
261            }
262    
263            public void setFormName(String formName) {
264                    _formName = formName;
265            }
266    
267            public void setNames(String names) {
268                    _names = StringUtil.split(names);
269            }
270    
271            public void setOnClick(String onClick) {
272                    _onClick = onClick;
273            }
274    
275            public void setParam(String param) {
276                    _param = param;
277            }
278    
279            public void setPortletURL(PortletURL portletURL) {
280                    _portletURL = portletURL;
281            }
282    
283            public void setRefresh(boolean refresh) {
284                    _refresh = refresh;
285            }
286    
287            public void setStartPage(String startPage) {
288                    _startPage = startPage;
289            }
290    
291            public void setTabsValues(String tabsValues) {
292                    _tabsValues = StringUtil.split(tabsValues);
293            }
294    
295            public void setType(String type) {
296                    _type = type;
297            }
298    
299            public void setUrl(String url) {
300                    _url = url;
301            }
302    
303            public void setUrl0(String url0) {
304                    _url0 = url0;
305            }
306    
307            public void setUrl1(String url1) {
308                    _url1 = url1;
309            }
310    
311            public void setUrl2(String url2) {
312                    _url2 = url2;
313            }
314    
315            public void setUrl3(String url3) {
316                    _url3 = url3;
317            }
318    
319            public void setUrl4(String url4) {
320                    _url4 = url4;
321            }
322    
323            public void setUrl5(String url5) {
324                    _url5 = url5;
325            }
326    
327            public void setUrl6(String url6) {
328                    _url6 = url6;
329            }
330    
331            public void setUrl7(String url7) {
332                    _url7 = url7;
333            }
334    
335            public void setUrl8(String url8) {
336                    _url8 = url8;
337            }
338    
339            public void setUrl9(String url9) {
340                    _url9 = url9;
341            }
342    
343            public void setValue(String value) {
344                    _value = value;
345            }
346    
347            @Override
348            protected String getEndPage() {
349                    if (Validator.isNull(_endPage)) {
350                            return _END_PAGE;
351                    }
352                    else {
353                            return _endPage;
354                    }
355            }
356    
357            @Override
358            protected String getStartPage() {
359                    if (Validator.isNull(_startPage)) {
360                            return _START_PAGE;
361                    }
362                    else {
363                            return _startPage;
364                    }
365            }
366    
367            private static final String _END_PAGE = "/html/taglib/ui/tabs/end.jsp";
368    
369            private static final String _START_PAGE = "/html/taglib/ui/tabs/start.jsp";
370    
371            private String _backLabel;
372            private String _backURL;
373            private String _cssClass;
374            private String _endPage;
375            private String _formName;
376            private String[] _names;
377            private int _namesPos;
378            private String _onClick;
379            private String _param = "tabs1";
380            private PortletURL _portletURL;
381            private boolean _refresh = true;
382            private String _startPage;
383            private String[] _tabsValues;
384            private String _type;
385            private String _url;
386            private String _url0;
387            private String _url1;
388            private String _url2;
389            private String _url3;
390            private String _url4;
391            private String _url5;
392            private String _url6;
393            private String _url7;
394            private String _url8;
395            private String _url9;
396            private String _value;
397    
398    }