1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
53
54
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
92
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 }