001    /**
002     * Copyright (c) 2000-2011 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.servlet.PortalIncludeUtil;
018    import com.liferay.portal.kernel.servlet.taglib.BaseBodyTagSupport;
019    import com.liferay.portal.kernel.util.IntegerWrapper;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.util.PwdGenerator;
023    
024    import javax.servlet.http.HttpServletRequest;
025    import javax.servlet.jsp.JspException;
026    import javax.servlet.jsp.tagext.BodyTag;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class PanelFloatingContainerTag
032            extends BaseBodyTagSupport implements BodyTag {
033    
034            @Override
035            public int doStartTag() {
036                    HttpServletRequest request =
037                            (HttpServletRequest)pageContext.getRequest();
038    
039                    if (Validator.isNull(_id)) {
040                            _id = PwdGenerator.getPassword(PwdGenerator.KEY3, 4);
041                    }
042    
043                    request.setAttribute(
044                            "liferay-ui:panel-floating-container:id", _id);
045                    request.setAttribute(
046                            "liferay-ui:panel-floating-container:trigger", _trigger);
047                    request.setAttribute(
048                            "liferay-ui:panel-floating-container:accordion",
049                            String.valueOf(_accordion));
050                    request.setAttribute(
051                            "liferay-ui:panel-floating-container:persistState",
052                            String.valueOf(_persistState));
053                    request.setAttribute(
054                            "liferay-ui:panel-floating-container:paging",
055                            String.valueOf(_paging));
056                    request.setAttribute(
057                            "liferay-ui:panel-floating-container:pagingElements",
058                            _pagingElements);
059                    request.setAttribute(
060                            "liferay-ui:panel-floating-container:resultsPerPage",
061                            String.valueOf(_resultsPerPage));
062                    request.setAttribute(
063                            "liferay-ui:panel-floating-container:width",
064                            String.valueOf(_width));
065                    request.setAttribute(
066                            "liferay-ui:panel-floating-container:extended", _extended);
067                    request.setAttribute(
068                            "liferay-ui:panel-floating-container:cssClass", _cssClass);
069                    request.setAttribute(
070                            "liferay-ui:panel-container:panelCount" + _id,
071                            new IntegerWrapper());
072    
073                    return EVAL_BODY_BUFFERED;
074            }
075    
076            @Override
077            public int doAfterBody() {
078                    HttpServletRequest request =
079                            (HttpServletRequest)pageContext.getRequest();
080    
081                    IntegerWrapper panelCount = (IntegerWrapper)request.getAttribute(
082                            "liferay-ui:panel-container:panelCount" + _id);
083    
084                    if ((panelCount != null) && (panelCount.getValue() == 1)) {
085    
086                            bodyContent.clearBody();
087    
088                            return EVAL_BODY_AGAIN;
089                    }
090                    else {
091                            return SKIP_BODY;
092                    }
093            }
094    
095            @Override
096            public int doEndTag() throws JspException {
097                    try {
098                            HttpServletRequest request =
099                                    (HttpServletRequest)pageContext.getRequest();
100    
101                            IntegerWrapper panelCount = (IntegerWrapper)request.getAttribute(
102                                    "liferay-ui:panel-container:panelCount" + _id);
103    
104                            request.removeAttribute(
105                                    "liferay-ui:panel-container:panelCount" + _id);
106    
107                            if ((panelCount != null) && (panelCount.getValue() >= 1)) {
108                                    PortalIncludeUtil.include(pageContext, getStartPage());
109                            }
110    
111                            writeBodyContent(pageContext.getOut());
112    
113                            if ((panelCount != null) && (panelCount.getValue() >= 1)) {
114                                    PortalIncludeUtil.include(pageContext, getEndPage());
115                            }
116    
117                            request.removeAttribute("liferay-ui:panel-floating-container:id");
118                            request.removeAttribute(
119                                    "liferay-ui:panel-floating-container:trigger");
120                            request.removeAttribute(
121                                    "liferay-ui:panel-floating-container:accordion");
122                            request.removeAttribute(
123                                    "liferay-ui:panel-floating-container:persistState");
124                            request.removeAttribute(
125                                    "liferay-ui:panel-floating-container:paging");
126                            request.removeAttribute(
127                                    "liferay-ui:panel-floating-container:pagingElements");
128                            request.removeAttribute(
129                                    "liferay-ui:panel-floating-container:resultsPerPage");
130                            request.removeAttribute(
131                                    "liferay-ui:panel-floating-container:width");
132                            request.removeAttribute(
133                                    "liferay-ui:panel-floating-container:extended");
134                            request.removeAttribute(
135                                    "liferay-ui:panel-floating-container:cssClass");
136    
137                            return EVAL_PAGE;
138                    }
139                    catch (Exception e) {
140                            throw new JspException(e);
141                    }
142            }
143    
144            public String getId() {
145                    return _id;
146            }
147    
148            protected String getStartPage() {
149                    if (Validator.isNull(_startPage)) {
150                            return _START_PAGE;
151                    }
152                    else {
153                            return _startPage;
154                    }
155            }
156    
157            public void setStartPage(String startPage) {
158                    _startPage = startPage;
159            }
160    
161            protected String getEndPage() {
162                    if (Validator.isNull(_endPage)) {
163                            return _END_PAGE;
164                    }
165                    else {
166                            return _endPage;
167                    }
168            }
169    
170            public void setEndPage(String endPage) {
171                    _endPage = endPage;
172            }
173    
174            public void setId(String id) {
175                    _id = id;
176            }
177    
178            public void setTrigger(String trigger) {
179                    _trigger = trigger;
180            }
181    
182            public void setAccordion(boolean accordion) {
183                    _accordion = accordion;
184            }
185    
186            public void setPersistState(boolean persistState) {
187                    _persistState = persistState;
188            }
189    
190            public void setPaging(boolean paging) {
191                    _paging = paging;
192            }
193    
194            public void setPagingElements(String pagingElements) {
195                    _pagingElements = pagingElements;
196            }
197    
198            public void setResultsPerPage(int resultsPerPage) {
199                    _resultsPerPage = resultsPerPage;
200            }
201    
202            public void setWidth(int width) {
203                    _width = width;
204            }
205    
206            public void setExtended(Boolean extended) {
207                    _extended = extended;
208            }
209    
210            public void setCssClass(String cssClass) {
211                    _cssClass = cssClass;
212            }
213    
214            private static final String _START_PAGE =
215                    "/html/taglib/ui/panel_floating_container/start.jsp";
216    
217            private static final String _END_PAGE =
218                    "/html/taglib/ui/panel_floating_container/end.jsp";
219    
220            private String _startPage;
221            private String _endPage;
222            private String _id;
223            private String _trigger;
224            private boolean _accordion;
225            private boolean _persistState;
226            private boolean _paging;
227            private String _pagingElements = "ul";
228            private int _resultsPerPage = 1;
229            private int _width = 300;
230            private Boolean _extended;
231            private String _cssClass = StringPool.BLANK;
232    
233    }