001    /**
002     * Copyright (c) 2000-2011 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.ParamUtil;
018    import com.liferay.portal.kernel.util.ServerDetector;
019    import com.liferay.portal.kernel.util.StringPool;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.taglib.util.IncludeTag;
023    
024    import javax.portlet.PortletURL;
025    
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.jsp.JspException;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class TabsTag extends IncludeTag {
033    
034            @Override
035            public int doStartTag() throws JspException {
036                    try {
037                            HttpServletRequest request = getServletRequest();
038    
039                            request.setAttribute("liferay-ui:tabs:names", _names);
040    
041                            if ((_tabsValues == null) || (_tabsValues.length < _names.length)) {
042                                    _tabsValues = _names;
043                            }
044    
045                            request.setAttribute("liferay-ui:tabs:values", _tabsValues);
046    
047                            request.setAttribute("liferay-ui:tabs:formName", _formName);
048                            request.setAttribute("liferay-ui:tabs:param", _param);
049    
050                            if (_value == null) {
051                                    if (_tabsValues.length > 0) {
052                                            _value = ParamUtil.getString(
053                                                    request, _param, _tabsValues[0]);
054                                    }
055                            }
056    
057                            if (Validator.isNull(_value)) {
058                                    if (_tabsValues.length > 0) {
059                                            _value = _tabsValues[0];
060                                    }
061                                    else {
062                                            _value = StringPool.BLANK;
063                                    }
064                            }
065    
066                            boolean match = false;
067    
068                            for (int i = 0; i < _tabsValues.length; i++) {
069                                    if (_value.equals(_tabsValues[i])) {
070                                            match = true;
071                                    }
072                            }
073    
074                            if (!match) {
075                                    if (_tabsValues.length > 0) {
076                                            _value = _tabsValues[0];
077                                    }
078                                    else {
079                                            _value = StringPool.BLANK;
080                                    }
081                            }
082    
083                            request.setAttribute("liferay-ui:tabs:value", _value);
084                            request.setAttribute("liferay-ui:tabs:portletURL", _portletURL);
085                            request.setAttribute("liferay-ui:tabs:url", _url);
086    
087                            if (_url0 != null) {
088                                    request.setAttribute("liferay-ui:tabs:url0", _url0);
089                            }
090    
091                            if (_url1 != null) {
092                                    request.setAttribute("liferay-ui:tabs:url1", _url1);
093                            }
094    
095                            if (_url2 != null) {
096                                    request.setAttribute("liferay-ui:tabs:url2", _url2);
097                            }
098    
099                            if (_url3 != null) {
100                                    request.setAttribute("liferay-ui:tabs:url3", _url3);
101                            }
102    
103                            if (_url4 != null) {
104                                    request.setAttribute("liferay-ui:tabs:url4", _url4);
105                            }
106    
107                            if (_url5 != null) {
108                                    request.setAttribute("liferay-ui:tabs:url5", _url5);
109                            }
110    
111                            if (_url6 != null) {
112                                    request.setAttribute("liferay-ui:tabs:url6", _url6);
113                            }
114    
115                            if (_url7 != null) {
116                                    request.setAttribute("liferay-ui:tabs:url7", _url7);
117                            }
118    
119                            if (_url8 != null) {
120                                    request.setAttribute("liferay-ui:tabs:url8", _url8);
121                            }
122    
123                            if (_url9 != null) {
124                                    request.setAttribute("liferay-ui:tabs:url9", _url9);
125                            }
126    
127                            request.setAttribute("liferay-ui:tabs:backLabel", _backLabel);
128                            request.setAttribute("liferay-ui:tabs:backURL", _backURL);
129                            request.setAttribute(
130                                    "liferay-ui:tabs:refresh", String.valueOf(_refresh));
131                            request.setAttribute(
132                                    "liferay-ui:tabs:onClick", String.valueOf(_onClick));
133    
134                            include(getStartPage());
135    
136                            return EVAL_BODY_INCLUDE;
137                    }
138                    catch (Exception e) {
139                            throw new JspException(e);
140                    }
141            }
142    
143            @Override
144            public int doEndTag() throws JspException {
145                    try {
146                            HttpServletRequest request =
147                                    (HttpServletRequest)pageContext.getRequest();
148    
149                            include(getEndPage());
150    
151                            request.removeAttribute("liferay-ui:tabs:names");
152                            request.removeAttribute("liferay-ui:tabs:values");
153                            request.removeAttribute("liferay-ui:tabs:formName");
154                            request.removeAttribute("liferay-ui:tabs:param");
155                            request.removeAttribute("liferay-ui:tabs:value");
156                            request.removeAttribute("liferay-ui:tabs:portletURL");
157                            request.removeAttribute("liferay-ui:tabs:url");
158                            request.removeAttribute("liferay-ui:tabs:url0");
159                            request.removeAttribute("liferay-ui:tabs:url1");
160                            request.removeAttribute("liferay-ui:tabs:url2");
161                            request.removeAttribute("liferay-ui:tabs:url3");
162                            request.removeAttribute("liferay-ui:tabs:url4");
163                            request.removeAttribute("liferay-ui:tabs:url5");
164                            request.removeAttribute("liferay-ui:tabs:url6");
165                            request.removeAttribute("liferay-ui:tabs:url7");
166                            request.removeAttribute("liferay-ui:tabs:url8");
167                            request.removeAttribute("liferay-ui:tabs:url9");
168                            request.removeAttribute("liferay-ui:tabs:backLabel");
169                            request.removeAttribute("liferay-ui:tabs:backURL");
170                            request.removeAttribute("liferay-ui:tabs:refresh");
171                            request.removeAttribute("liferay-ui:tabs:onClick");
172    
173                            return EVAL_PAGE;
174                    }
175                    catch (Exception e) {
176                            throw new JspException(e);
177                    }
178                    finally {
179                            if (!ServerDetector.isResin()) {
180                                    _startPage = null;
181                                    _endPage = null;
182                                    _names = null;
183                                    _namesPos = 0;
184                                    _tabsValues = null;
185                                    _formName = StringPool.BLANK;
186                                    _param = "tabs1";
187                                    _value = null;
188                                    _portletURL = null;
189                                    _url = null;
190                                    _url0 = null;
191                                    _url1 = null;
192                                    _url2 = null;
193                                    _url3 = null;
194                                    _url4 = null;
195                                    _url5 = null;
196                                    _url6 = null;
197                                    _url7 = null;
198                                    _url8 = null;
199                                    _url9 = null;
200                                    _backLabel = null;
201                                    _backURL = null;
202                                    _refresh = true;
203                                    _onClick = null;
204                            }
205                    }
206            }
207    
208            @Override
209            protected String getStartPage() {
210                    if (Validator.isNull(_startPage)) {
211                            return _START_PAGE;
212                    }
213                    else {
214                            return _startPage;
215                    }
216            }
217    
218            public void setStartPage(String startPage) {
219                    _startPage = startPage;
220            }
221    
222            @Override
223            protected 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 setBackLabel(String backLabel) {
309                    _backLabel = backLabel;
310            }
311    
312            public void setBackURL(String backURL) {
313                    _backURL = backURL;
314            }
315    
316            public boolean isRefresh() {
317                    return _refresh;
318            }
319    
320            public void setRefresh(boolean refresh) {
321                    _refresh = refresh;
322            }
323    
324            public String getOnClick() {
325                    return _onClick;
326            }
327    
328            public void setOnClick(String onClick) {
329                    _onClick = onClick;
330            }
331    
332            public String getSectionName() {
333                    if (_names.length > _namesPos) {
334                            return _names[_namesPos];
335                    }
336                    else {
337                            return StringPool.BLANK;
338                    }
339            }
340    
341            public boolean getSectionSelected() {
342                    if ((_names.length == 0) ||
343                            ((_names.length > _namesPos) &&
344                                    (_names[_namesPos].equals(_value)))) {
345    
346                            return true;
347                    }
348                    else {
349                            return false;
350                    }
351            }
352    
353            public void incrementSection() {
354                    _namesPos++;
355            }
356    
357            private static final String _START_PAGE = "/html/taglib/ui/tabs/start.jsp";
358    
359            private static final String _END_PAGE = "/html/taglib/ui/tabs/end.jsp";
360    
361            private String _startPage;
362            private String _endPage;
363            private String[] _names;
364            private int _namesPos;
365            private String[] _tabsValues;
366            private String _formName;
367            private String _param = "tabs1";
368            private String _value;
369            private PortletURL _portletURL;
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 _backLabel;
382            private String _backURL;
383            private boolean _refresh = true;
384            private String _onClick;
385    
386    }