001    /**
002     * Copyright (c) 2000-present 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.language.LanguageUtil;
018    import com.liferay.portal.kernel.servlet.taglib.ui.QuickAccessEntry;
019    import com.liferay.portal.kernel.util.ServerDetector;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.WebKeys;
022    import com.liferay.taglib.BaseBodyTagSupport;
023    
024    import java.util.ArrayList;
025    import java.util.List;
026    
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.jsp.JspException;
029    import javax.servlet.jsp.tagext.BodyTag;
030    
031    /**
032     * @author Eudaldo Alonso
033     */
034    public class QuickAccessEntryTag extends BaseBodyTagSupport implements BodyTag {
035    
036            @Override
037            public int doEndTag() throws JspException {
038                    try {
039                            return processEndTag();
040                    }
041                    catch (Exception e) {
042                            throw new JspException(e);
043                    }
044                    finally {
045                            if (!ServerDetector.isResin()) {
046                                    _label = null;
047                                    _onClick = null;
048                                    _url = null;
049                            }
050                    }
051            }
052    
053            public void setLabel(String label) {
054                    _label = LanguageUtil.get(getRequest(), label);
055            }
056    
057            public void setOnClick(String onClick) {
058                    _onClick = onClick;
059            }
060    
061            public void setUrl(String url) {
062                    _url = url;
063            }
064    
065            protected String getEndPage() {
066                    return _END_PAGE;
067            }
068    
069            protected String getStartPage() {
070                    return _START_PAGE;
071            }
072    
073            protected int processEndTag() throws Exception {
074                    HttpServletRequest request =
075                            (HttpServletRequest)pageContext.getRequest();
076    
077                    List<QuickAccessEntry> quickAccessEntries =
078                            (List<QuickAccessEntry>)request.getAttribute(
079                                    WebKeys.PORTLET_QUICK_ACCESS_ENTRIES);
080    
081                    if (quickAccessEntries == null) {
082                            quickAccessEntries = new ArrayList<QuickAccessEntry>();
083    
084                            request.setAttribute(
085                                    WebKeys.PORTLET_QUICK_ACCESS_ENTRIES, quickAccessEntries);
086                    }
087    
088                    QuickAccessEntry quickAccessEntry = new QuickAccessEntry();
089    
090                    quickAccessEntry.setBody(getBodyContentAsStringBundler());
091                    quickAccessEntry.setId(StringUtil.randomId());
092                    quickAccessEntry.setLabel(_label);
093                    quickAccessEntry.setOnClick(_onClick);
094                    quickAccessEntry.setURL(_url);
095    
096                    quickAccessEntries.add(quickAccessEntry);
097    
098                    return EVAL_PAGE;
099            }
100    
101            private static final String _END_PAGE =
102                    "/html/taglib/ui/quick_access_entry/end.jsp";
103    
104            private static final String _START_PAGE =
105                    "/html/taglib/ui/quick_access_entry/start.jsp";
106    
107            private String _label;
108            private String _onClick;
109            private String _url;
110    
111    }