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:urls");
055                            request.removeAttribute("liferay-ui:tabs:value");
056                            request.removeAttribute("liferay-ui:tabs:values");
057    
058                            return EVAL_PAGE;
059                    }
060                    catch (Exception e) {
061                            throw new JspException(e);
062                    }
063                    finally {
064                            if (!ServerDetector.isResin()) {
065                                    _backLabel = null;
066                                    _backURL = null;
067                                    _cssClass = null;
068                                    _endPage = null;
069                                    _formName = StringPool.BLANK;
070                                    _names = null;
071                                    _namesPos = 0;
072                                    _onClick = null;
073                                    _param = "tabs1";
074                                    _portletURL = null;
075                                    _refresh = true;
076                                    _startPage = null;
077                                    _tabsValues = null;
078                                    _type = null;
079                                    _url = null;
080                                    _url0 = null;
081                                    _url1 = null;
082                                    _url2 = null;
083                                    _url3 = null;
084                                    _url4 = null;
085                                    _url5 = null;
086                                    _url6 = null;
087                                    _url7 = null;
088                                    _url8 = null;
089                                    _url9 = null;
090                                    _urls = null;
091                                    _value = null;
092                            }
093                    }
094            }
095    
096            @Override
097            public int doStartTag() throws JspException {
098                    try {
099                            request.setAttribute("liferay-ui:tabs:names", _names);
100    
101                            if ((_tabsValues == null) || (_tabsValues.length < _names.length)) {
102                                    _tabsValues = _names;
103                            }
104    
105                            request.setAttribute("liferay-ui:tabs:param", _param);
106                            request.setAttribute("liferay-ui:tabs:values", _tabsValues);
107    
108                            if ((_urls == null) &&
109                                    ((_url0 != null) || (_url1 != null) || (_url2 != null) ||
110                                     (_url3 != null) || (_url4 != null) || (_url5 != null) ||
111                                     (_url6 != null) || (_url7 != null) || (_url8 != null) ||
112                                     (_url9 != null))) {
113    
114                                    _urls = new String[10];
115    
116                                    if (_url0 != null) {
117                                            _urls[0] = _url0;
118                                    }
119    
120                                    if (_url1 != null) {
121                                            _urls[1] = _url1;
122                                    }
123    
124                                    if (_url2 != null) {
125                                            _urls[2] = _url2;
126                                    }
127    
128                                    if (_url3 != null) {
129                                            _urls[3] = _url3;
130                                    }
131    
132                                    if (_url4 != null) {
133                                            _urls[4] = _url4;
134                                    }
135    
136                                    if (_url5 != null) {
137                                            _urls[5] = _url5;
138                                    }
139    
140                                    if (_url6 != null) {
141                                            _urls[6] = _url6;
142                                    }
143    
144                                    if (_url7 != null) {
145                                            _urls[7] = _url7;
146                                    }
147    
148                                    if (_url8 != null) {
149                                            _urls[8] = _url8;
150                                    }
151    
152                                    if (_url9 != null) {
153                                            _urls[9] = _url9;
154                                    }
155                            }
156    
157                            if (_value == null) {
158                                    if (_tabsValues.length > 0) {
159                                            _value = ParamUtil.getString(
160                                                    request, _param, _tabsValues[0]);
161                                    }
162                            }
163    
164                            if (Validator.isNull(_value)) {
165                                    if (_tabsValues.length > 0) {
166                                            _value = _tabsValues[0];
167                                    }
168                                    else {
169                                            _value = StringPool.BLANK;
170                                    }
171                            }
172    
173                            boolean match = false;
174    
175                            if (ArrayUtil.contains(_tabsValues, _value)) {
176                                    match = true;
177                            }
178    
179                            if (!match) {
180                                    if (_tabsValues.length > 0) {
181                                            _value = _tabsValues[0];
182                                    }
183                                    else {
184                                            _value = StringPool.BLANK;
185                                    }
186                            }
187    
188                            request.setAttribute("liferay-ui:tabs:backLabel", _backLabel);
189                            request.setAttribute("liferay-ui:tabs:backURL", _backURL);
190                            request.setAttribute("liferay-ui:tabs:cssClass", _cssClass);
191                            request.setAttribute("liferay-ui:tabs:formName", _formName);
192                            request.setAttribute(
193                                    "liferay-ui:tabs:onClick", String.valueOf(_onClick));
194                            request.setAttribute("liferay-ui:tabs:portletURL", _portletURL);
195                            request.setAttribute(
196                                    "liferay-ui:tabs:refresh", String.valueOf(_refresh));
197                            request.setAttribute("liferay-ui:tabs:type", _type);
198                            request.setAttribute("liferay-ui:tabs:url", _url);
199                            request.setAttribute("liferay-ui:tabs:urls", _urls);
200    
201                            request.setAttribute("liferay-ui:tabs:value", _value);
202    
203                            include(getStartPage());
204    
205                            return EVAL_BODY_INCLUDE;
206                    }
207                    catch (Exception e) {
208                            throw new JspException(e);
209                    }
210            }
211    
212            public String getOnClick() {
213                    return _onClick;
214            }
215    
216            public String getParam() {
217                    return _param;
218            }
219    
220            public String getSectionName() {
221                    if (_names.length > _namesPos) {
222                            return _names[_namesPos];
223                    }
224                    else {
225                            return StringPool.BLANK;
226                    }
227            }
228    
229            public boolean getSectionSelected() {
230                    if ((_names.length == 0) ||
231                            ((_names.length > _namesPos) &&
232                             _names[_namesPos].equals(_value))) {
233    
234                            return true;
235                    }
236                    else {
237                            return false;
238                    }
239            }
240    
241            public void incrementSection() {
242                    _namesPos++;
243            }
244    
245            public boolean isRefresh() {
246                    return _refresh;
247            }
248    
249            public void setBackLabel(String backLabel) {
250                    _backLabel = backLabel;
251            }
252    
253            public void setBackURL(String backURL) {
254                    _backURL = backURL;
255            }
256    
257            public void setCssClass(String cssClass) {
258                    _cssClass = cssClass;
259            }
260    
261            public void setEndPage(String endPage) {
262                    _endPage = endPage;
263            }
264    
265            public void setFormName(String formName) {
266                    _formName = formName;
267            }
268    
269            public void setNames(String names) {
270                    _names = StringUtil.split(names);
271            }
272    
273            public void setOnClick(String onClick) {
274                    _onClick = onClick;
275            }
276    
277            public void setParam(String param) {
278                    _param = param;
279            }
280    
281            public void setPortletURL(PortletURL portletURL) {
282                    _portletURL = portletURL;
283            }
284    
285            public void setRefresh(boolean refresh) {
286                    _refresh = refresh;
287            }
288    
289            public void setStartPage(String startPage) {
290                    _startPage = startPage;
291            }
292    
293            public void setTabsValues(String tabsValues) {
294                    _tabsValues = StringUtil.split(tabsValues);
295            }
296    
297            public void setType(String type) {
298                    _type = type;
299            }
300    
301            public void setUrl(String url) {
302                    _url = url;
303            }
304    
305            /**
306             * @deprecated As of 7.0.0, replaced by {@link #setUrls(String[])}
307             */
308            @Deprecated
309            public void setUrl0(String url0) {
310                    _url0 = url0;
311            }
312    
313            /**
314             * @deprecated As of 7.0.0, replaced by {@link #setUrls(String[])}
315             */
316            @Deprecated
317            public void setUrl1(String url1) {
318                    _url1 = url1;
319            }
320    
321            /**
322             * @deprecated As of 7.0.0, replaced by {@link #setUrls(String[])}
323             */
324            @Deprecated
325            public void setUrl2(String url2) {
326                    _url2 = url2;
327            }
328    
329            /**
330             * @deprecated As of 7.0.0, replaced by {@link #setUrls(String[])}
331             */
332            @Deprecated
333            public void setUrl3(String url3) {
334                    _url3 = url3;
335            }
336    
337            /**
338             * @deprecated As of 7.0.0, replaced by {@link #setUrls(String[])}
339             */
340            @Deprecated
341            public void setUrl4(String url4) {
342                    _url4 = url4;
343            }
344    
345            /**
346             * @deprecated As of 7.0.0, replaced by {@link #setUrls(String[])}
347             */
348            @Deprecated
349            public void setUrl5(String url5) {
350                    _url5 = url5;
351            }
352    
353            /**
354             * @deprecated As of 7.0.0, replaced by {@link #setUrls(String[])}
355             */
356            @Deprecated
357            public void setUrl6(String url6) {
358                    _url6 = url6;
359            }
360    
361            /**
362             * @deprecated As of 7.0.0, replaced by {@link #setUrls(String[])}
363             */
364            @Deprecated
365            public void setUrl7(String url7) {
366                    _url7 = url7;
367            }
368    
369            /**
370             * @deprecated As of 7.0.0, replaced by {@link #setUrls(String[])}
371             */
372            @Deprecated
373            public void setUrl8(String url8) {
374                    _url8 = url8;
375            }
376    
377            /**
378             * @deprecated As of 7.0.0, replaced by {@link #setUrls(String[])}
379             */
380            @Deprecated
381            public void setUrl9(String url9) {
382                    _url9 = url9;
383            }
384    
385            public void setUrls(String[] urls) {
386                    _urls = urls;
387            }
388    
389            public void setValue(String value) {
390                    _value = value;
391            }
392    
393            @Override
394            protected String getEndPage() {
395                    if (Validator.isNull(_endPage)) {
396                            return _END_PAGE;
397                    }
398                    else {
399                            return _endPage;
400                    }
401            }
402    
403            @Override
404            protected String getStartPage() {
405                    if (Validator.isNull(_startPage)) {
406                            return _START_PAGE;
407                    }
408                    else {
409                            return _startPage;
410                    }
411            }
412    
413            private static final String _END_PAGE = "/html/taglib/ui/tabs/end.jsp";
414    
415            private static final String _START_PAGE = "/html/taglib/ui/tabs/start.jsp";
416    
417            private String _backLabel;
418            private String _backURL;
419            private String _cssClass;
420            private String _endPage;
421            private String _formName;
422            private String[] _names;
423            private int _namesPos;
424            private String _onClick;
425            private String _param = "tabs1";
426            private PortletURL _portletURL;
427            private boolean _refresh = true;
428            private String _startPage;
429            private String[] _tabsValues;
430            private String _type;
431            private String _url;
432            private String _url0;
433            private String _url1;
434            private String _url2;
435            private String _url3;
436            private String _url4;
437            private String _url5;
438            private String _url6;
439            private String _url7;
440            private String _url8;
441            private String _url9;
442            private String[] _urls;
443            private String _value;
444    
445    }