1 /**
2 * This file Copyright (c) 2010-2010 Magnolia International
3 * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4 *
5 *
6 * This file is dual-licensed under both the Magnolia
7 * Network Agreement and the GNU General Public License.
8 * You may elect to use one or the other of these licenses.
9 *
10 * This file is distributed in the hope that it will be
11 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12 * implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14 * Redistribution, except as permitted by whichever of the GPL
15 * or MNA you select, is prohibited.
16 *
17 * 1. For the GPL license (GPL), you can redistribute and/or
18 * modify this file under the terms of the GNU General
19 * Public License, Version 3, as published by the Free Software
20 * Foundation. You should have received a copy of the GNU
21 * General Public License, Version 3 along with this program;
22 * if not, write to the Free Software Foundation, Inc., 51
23 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24 *
25 * 2. For the Magnolia Network Agreement (MNA), this file
26 * and the accompanying materials are made available under the
27 * terms of the MNA which accompanies this distribution, and
28 * is available at http://www.magnolia-cms.com/mna.html
29 *
30 * Any modifications to this file must keep this entire header
31 * intact.
32 *
33 */
34 package info.magnolia.module.templatingcomponents.components;
35
36 import info.magnolia.cms.beans.config.ServerConfiguration;
37 import info.magnolia.cms.core.AggregationState;
38 import info.magnolia.cms.core.Content;
39 import info.magnolia.cms.gui.inline.BarNew;
40 import info.magnolia.context.MgnlContext;
41
42 import javax.jcr.RepositoryException;
43 import java.io.IOException;
44 import java.io.Writer;
45 import java.util.List;
46
47 /**
48 * An hybrid between a new bar and an edit bar for non-moveable paragraphs: it's either added by the editor, edited, or removed.
49 * This is currently meant to be used in conjunction with the regular edit bar component: if the request content is present,
50 * wrappers should render their content (which should render said paragraph with an edit bar), and if not, this
51 * renders a "new bar".
52 *
53 * @author gjoseph
54 * @version $Revision: $ ($Author: $)
55 */
56 public class SingletonParagraphBar extends AbstractAuthoringUiComponent {
57 private static final String DEFAULT_ENABLE_LABEL = "buttons.enable";
58
59 /**
60 * Utility method for other components to determine if they are being rendered inside a SingletonParagraphBar.
61 */
62 public static boolean isInSingleton() {
63 return MgnlContext.hasAttribute(SingletonParagraphBar.class.getName());
64 }
65
66 /**
67 * @param serverCfg
68 * @param aggState
69 * @param contentName the name of the node which contains (or will contain) the singleton paragraph; this is a child node of {@link #currentContent()}.
70 * @param allowedParagraphs the list of paragraph definitions (their names) that are allow to be added by this component
71 * @param enableButtonLabel if null, default will be used
72 */
73 public static SingletonParagraphBar make(ServerConfiguration serverCfg, AggregationState aggState, String contentName, List<String> allowedParagraphs, String enableButtonLabel) {
74 final SingletonParagraphBar bar = new SingletonParagraphBar(serverCfg, aggState);
75 bar.setAllowedParagraphs(allowedParagraphs);
76 bar.setContentName(contentName);
77 if (enableButtonLabel != null) {
78 bar.setEnableButtonLabel(enableButtonLabel);
79 }
80 return bar;
81 }
82
83 private String contentName;
84 private List<String> allowedParagraphs;
85 private String enableButtonLabel = DEFAULT_ENABLE_LABEL;
86
87 public SingletonParagraphBar(ServerConfiguration server, AggregationState aggregationState) {
88 super(server, aggregationState);
89 }
90
91 public void setContentName(String contentName) {
92 this.contentName = contentName;
93 }
94
95 public void setAllowedParagraphs(List<String> allowedParagraphs) {
96 this.allowedParagraphs = allowedParagraphs;
97 }
98
99 public void setEnableButtonLabel(String enableButtonLabel) {
100 this.enableButtonLabel = enableButtonLabel;
101 }
102
103 @Override
104 protected void doRender(Appendable out) throws IOException, RepositoryException {
105 setupSingleton();
106
107 final Content container = currentContent();
108
109 if (container.hasContent(contentName)) {
110 // we assume there's an edit bar in the paragraph that will be rendered where this singleton was enabled
111 return;
112 }
113
114 final BarNew bar = new BarNew();
115 bar.setParagraph(asString(allowedParagraphs));
116 // TODO if (StringUtils.isBlank(bar.getParagraph())) {
117 // log.warn("No paragraph selected for new bar in {}", pageContext.getPage());
118 // don't set new button's label if there's no selectable paragraph
119 // }
120
121 bar.setPath(container.getHandle());
122 //bar.setNodeCollectionName(contentName);
123 bar.setNodeName(contentName); // see difference with NewBar
124
125 bar.setDefaultButtons();
126 bar.getButtonNew().setLabel(enableButtonLabel);
127
128 bar.placeDefaultButtons();
129 bar.drawHtml((Writer) out);
130 }
131
132 private static void setupSingleton() {
133 MgnlContext.setAttribute(SingletonParagraphBar.class.getName(), Boolean.TRUE);
134 }
135
136 public void postRender() {
137 MgnlContext.removeAttribute(SingletonParagraphBar.class.getName());
138 }
139 }