View Javadoc

1   /**
2    * This file Copyright (c) 2011 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.templating.elements;
35  
36  import info.magnolia.cms.beans.config.ServerConfiguration;
37  import info.magnolia.cms.i18n.Messages;
38  import info.magnolia.cms.i18n.MessagesManager;
39  import info.magnolia.jcr.inheritance.InheritanceNodeWrapper;
40  import info.magnolia.rendering.context.RenderingContext;
41  import info.magnolia.rendering.engine.RenderException;
42  import info.magnolia.rendering.template.TemplateDefinition;
43  
44  import java.io.IOException;
45  
46  import javax.jcr.Node;
47  
48  import org.apache.commons.lang.StringUtils;
49  
50  
51  /**
52   * Embeds a marker within the page that instructs the page editor to place an edit bar/button at this location.
53   *
54   * @version $Id$
55   */
56  public class EditElement extends AbstractContentTemplatingElement {
57  
58      public static final String CMS_EDIT = "cms:edit";
59      public static final String FORMAT_BAR = "bar";
60      public static final String FORMAT_BUTTON = "button";
61      public static final String DEFAULT_FORMAT = FORMAT_BAR;
62  
63      private String dialog;
64      private String format = DEFAULT_FORMAT;
65  
66      public EditElement(ServerConfiguration server, RenderingContext renderingContext) {
67          super(server, renderingContext);
68      }
69  
70      @Override
71      public void begin(Appendable out) throws IOException, RenderException {
72          if (!isAdmin()) {
73              return;
74          }
75          Node content = getTargetContent();
76  
77          TemplateDefinition templateDefinition = getRequiredTemplateDefinition();
78  
79          MarkupHelper helper = new MarkupHelper(out);
80          helper.openComment(CMS_EDIT);
81  
82          if(content != null) {
83              helper.attribute("content", getNodePath(content));
84  
85              if(content instanceof InheritanceNodeWrapper) {
86                  if (((InheritanceNodeWrapper) content).isInherited()) {
87                      helper.attribute("inherited", "true");;
88                  }
89              }
90          } else {
91              //null content probably means that the area is optional and hasn't been created yet. Still we need to generate
92              //the cms:edit tag in order to render the area bar
93              helper.attribute("name", templateDefinition.getName());
94              helper.attribute("optional", "true");
95          }
96          Messages messages = MessagesManager.getMessages(templateDefinition.getI18nBasename());
97          String label = templateDefinition.getTitle();
98  
99          if (StringUtils.isNotBlank(label)) {
100             helper.attribute("label", messages.getWithDefault(label, label));
101         }
102 
103         String description = templateDefinition.getDescription();
104 
105         if (StringUtils.isNotBlank(description)) {
106             helper.attribute("description", messages.getWithDefault(description, description));
107         }
108 
109         helper.attribute("format", format);
110         String dialog = resolveDialog(templateDefinition);
111         helper.attribute("dialog", dialog);
112 
113         helper.attribute("template", templateDefinition.getId());
114 
115         helper.append(" -->\n");
116 
117     }
118 
119     private TemplateDefinition getRequiredTemplateDefinition() {
120         return (TemplateDefinition) getRenderingContext().getRenderableDefinition();
121     }
122 
123     @Override
124     public void end(Appendable out) throws IOException, RenderException {
125         if(isAdmin()) {
126             MarkupHelper helper = new MarkupHelper(out);
127             helper.closeComment(CMS_EDIT);
128         }
129     }
130 
131     private String resolveDialog(TemplateDefinition component) {
132         if (StringUtils.isNotEmpty(this.dialog)) {
133             return this.dialog;
134         }
135         String dialog = component.getDialog();
136         if (StringUtils.isNotEmpty(dialog)) {
137             return dialog;
138         }
139         return null;
140     }
141 
142     public String getDialog() {
143         return dialog;
144     }
145 
146     public void setDialog(String dialog) {
147         this.dialog = dialog;
148     }
149 
150     public String getFormat() {
151         return format;
152     }
153 
154     public void setFormat(String format) {
155         this.format = format;
156     }
157 
158 }