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.portlet.layoutconfiguration.util.velocity;
016    
017    import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.MethodHandler;
020    import com.liferay.portal.kernel.util.MethodKey;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.UnicodeProperties;
023    import com.liferay.portal.model.CustomizedPages;
024    import com.liferay.portal.model.Layout;
025    import com.liferay.portal.model.impl.LayoutTypePortletImpl;
026    
027    import java.io.Writer;
028    
029    import javax.servlet.http.HttpServletRequest;
030    import javax.servlet.jsp.PageContext;
031    import javax.servlet.jsp.tagext.Tag;
032    
033    /**
034     * @author Raymond Augé
035     */
036    public class CustomizationSettingsProcessor implements ColumnProcessor {
037    
038            public CustomizationSettingsProcessor(
039                    HttpServletRequest request, PageContext pageContext, Writer writer) {
040    
041                    _pageContext = pageContext;
042                    _request = request;
043                    _writer = writer;
044    
045                    Layout selLayout = (Layout)_request.getAttribute(
046                            "edit_pages.jsp-selLayout");
047    
048                    _layoutTypeSettings = selLayout.getTypeSettingsProperties();
049    
050                    _templateLayout = LayoutTypePortletImpl.getSourcePrototypeLayout(
051                            selLayout);
052            }
053    
054            public String processColumn(String columnId) throws Exception {
055                    return processColumn(columnId, StringPool.BLANK);
056            }
057    
058            public String processColumn(String columnId, String classNames)
059                    throws Exception {
060    
061                    String customizableKey = CustomizedPages.namespaceColumnId(columnId);
062    
063                    boolean templateCustomizable = true;
064    
065                    if (_templateLayout != null) {
066                            templateCustomizable = GetterUtil.getBoolean(
067                                    _templateLayout.getTypeSettingsProperties().getProperty(
068                                            customizableKey, String.valueOf(false)));
069                    }
070    
071                    boolean customizable = false;
072    
073                    if (templateCustomizable) {
074                            customizable = GetterUtil.getBoolean(
075                                    _layoutTypeSettings.getProperty(
076                                            customizableKey, String.valueOf(false)));
077                    }
078    
079                    _writer.append("<div class=\"");
080                    _writer.append(classNames);
081                    _writer.append("\">");
082    
083                    _writer.append("<h1>");
084                    _writer.append(columnId);
085                    _writer.append("</h1>");
086    
087                    Object inputTag = _inputTagClass.newInstance();
088    
089                    BeanPropertiesUtil.setProperty(
090                            inputTag, "disabled", !templateCustomizable);
091                    BeanPropertiesUtil.setProperty(inputTag, "label", "customizable");
092                    BeanPropertiesUtil.setProperty(
093                            inputTag, "name",
094                            "TypeSettingsProperties--".concat(customizableKey).concat("--"));
095                    BeanPropertiesUtil.setProperty(inputTag, "pageContext", _pageContext);
096                    BeanPropertiesUtil.setProperty(inputTag, "type", "checkbox");
097                    BeanPropertiesUtil.setProperty(inputTag, "value", customizable);
098    
099                    MethodHandler doEndMethodHandler = new MethodHandler(
100                            _doEndTagMethodKey);
101                    MethodHandler doStartMethodHandler = new MethodHandler(
102                            _doStartTagMethodKey);
103    
104                    int result = (Integer)doStartMethodHandler.invoke(inputTag);
105    
106                    if (result == Tag.EVAL_BODY_INCLUDE) {
107                            doEndMethodHandler.invoke(inputTag);
108                    }
109    
110                    _writer.append("</div>");
111    
112                    return StringPool.BLANK;
113            }
114    
115            public String processMax() throws Exception {
116                    return processMax(StringPool.BLANK);
117            }
118    
119            public String processMax(String classNames) throws Exception {
120                    return StringPool.BLANK;
121            }
122    
123            public String processPortlet(String portletId) throws Exception {
124                    _writer.append("<div class=\"portlet\">");
125                    _writer.append(portletId);
126                    _writer.append("</div>");
127    
128                    return StringPool.BLANK;
129            }
130    
131            private static MethodKey _doEndTagMethodKey = new MethodKey(
132                    "com.liferay.taglib.aui.InputTag", "doEndTag");
133            private static MethodKey _doStartTagMethodKey = new MethodKey(
134                    "com.liferay.taglib.aui.InputTag", "doStartTag");
135            private static Class<?> _inputTagClass;
136    
137            private UnicodeProperties _layoutTypeSettings;
138            private PageContext _pageContext;
139            private HttpServletRequest _request;
140            private Layout _templateLayout;
141            private Writer _writer;
142    
143            static {
144                    try {
145                            _inputTagClass = Class.forName("com.liferay.taglib.aui.InputTag");
146                    }
147                    catch (ClassNotFoundException e) {
148                    }
149            }
150    
151    }