KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > taglibs > EditButton


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.taglibs;
14
15 import info.magnolia.cms.beans.config.ParagraphManager;
16 import info.magnolia.cms.beans.config.Server;
17 import info.magnolia.cms.core.Content;
18 import info.magnolia.cms.gui.inline.ButtonEdit;
19 import info.magnolia.cms.security.Permission;
20 import info.magnolia.cms.util.Resource;
21
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.jsp.JspWriter JavaDoc;
24 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31 /**
32  * @author Marcel Salathe
33  * @author Fabrizio Giustina
34  * @version $Revision $ ($Author $)
35  */

36 public class EditButton extends TagSupport JavaDoc {
37
38     /**
39      * Stable serialVersionUID.
40      */

41     private static final long serialVersionUID = 222L;
42
43     /**
44      * Logger.
45      */

46     private static Logger log = LoggerFactory.getLogger(EditButton.class);
47
48     private String JavaDoc nodeName;
49
50     private String JavaDoc nodeCollectionName;
51
52     private String JavaDoc paragraph;
53
54     private String JavaDoc label;
55
56     private String JavaDoc displayHandler;
57
58     private boolean small = true;
59
60     /**
61      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
62      */

63     public int doStartTag() {
64         this.displayHandler = StringUtils.EMPTY;
65         return EVAL_BODY_INCLUDE;
66     }
67
68     /**
69      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
70      */

71     public int doEndTag() {
72         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) this.pageContext.getRequest();
73
74         if (Server.isAdmin() && Resource.getActivePage(request).isGranted(Permission.SET)) {
75
76             try {
77                 if (this.getNodeCollectionName() != null && this.getNodeName() == null) {
78                     // cannot draw edit button with nodeCllection and without node
79
return EVAL_PAGE;
80                 }
81                 JspWriter JavaDoc out = pageContext.getOut();
82                 ButtonEdit button = new ButtonEdit(((HttpServletRequest JavaDoc) pageContext.getRequest()));
83                 button.setPath(this.getPath());
84                 button.setParagraph(this.getParagraph());
85                 button.setNodeCollectionName(this.getNodeCollectionName());
86                 button.setNodeName(this.getNodeName());
87                 button.setDefaultOnclick((HttpServletRequest JavaDoc) this.pageContext.getRequest());
88                 if (this.getLabel() != null) {
89                     button.setLabel(this.getLabel());
90                 }
91                 if (this.small) {
92                     button.setSmall(true);
93                 }
94                 button.drawHtml(out);
95             }
96             catch (Exception JavaDoc e) {
97                 log.error(e.getMessage(), e);
98             }
99         }
100         return EVAL_PAGE;
101     }
102
103     /**
104      * set working contentNode
105      * @param name , comtainer name which will be used to access/write content
106      */

107     public void setContentNodeName(String JavaDoc name) {
108         this.nodeName = name;
109     }
110
111     /**
112      *
113      */

114     private String JavaDoc getNodeName() {
115         if (this.nodeName == null) {
116             if (Resource.getLocalContentNode(((HttpServletRequest JavaDoc) pageContext.getRequest())) == null) {
117                 return null;
118             }
119             return Resource.getLocalContentNode(((HttpServletRequest JavaDoc) pageContext.getRequest())).getName();
120         }
121         return this.nodeName;
122     }
123
124     /**
125      * set working contentNode
126      * @param name , comtainer name which will be used to access/write content
127      */

128     public void setContentNodeCollectionName(String JavaDoc name) {
129         this.nodeCollectionName = name;
130     }
131
132     /**
133      * @return content node collection name
134      */

135     private String JavaDoc getNodeCollectionName() {
136         if (this.nodeCollectionName == null) {
137             return Resource.getLocalContentNodeCollectionName(((HttpServletRequest JavaDoc) pageContext.getRequest()));
138         }
139         return this.nodeCollectionName;
140     }
141
142     /**
143      * @deprecated set current content type, could be any developer defined name
144      * @param type , content type
145      */

146     public void setParFile(String JavaDoc type) {
147         this.setParagraph(type);
148     }
149
150     /**
151      * set current content type, could be any developer defined name
152      * @param type , content type
153      */

154     public void setParagraph(String JavaDoc type) {
155         this.paragraph = type;
156     }
157
158     /**
159      * @return String paragraph (type of par)
160      */

161     private String JavaDoc getParagraph() {
162         if (this.paragraph == null) {
163
164             return Resource.getLocalContentNode(((HttpServletRequest JavaDoc) pageContext.getRequest())).getNodeData(
165                 "paragraph").getString(); //$NON-NLS-1$
166

167         }
168         return this.paragraph;
169     }
170
171     /**
172      * set display handler (JSP / Servlet), needs to know the relative path from WEB-INF
173      * @param path , relative to WEB-INF
174      */

175     public void setTemplate(String JavaDoc path) {
176         this.displayHandler = path;
177     }
178
179     /**
180      * @return template path
181      */

182     public String JavaDoc getTemplate() {
183         if (this.displayHandler == null) {
184             Content localContainer = Resource.getLocalContentNode(((HttpServletRequest JavaDoc) pageContext.getRequest()));
185             String JavaDoc templateName = localContainer.getNodeData("paragraph").getString(); //$NON-NLS-1$
186
return ParagraphManager.getInstance().getInfo(templateName).getTemplatePath();
187         }
188         return this.displayHandler;
189     }
190
191     /**
192      * get the content path (Page or Node)
193      * @return String path
194      */

195     private String JavaDoc getPath() {
196         try {
197             return Resource.getCurrentActivePage(((HttpServletRequest JavaDoc) pageContext.getRequest())).getHandle();
198         }
199         catch (Exception JavaDoc re) {
200             return StringUtils.EMPTY;
201         }
202     }
203
204     /**
205      * @deprecated set the edit label (default "Edit")
206      * @param label , under which content must be stored
207      */

208     public void setEditLabel(String JavaDoc label) {
209         this.setLabel(label);
210     }
211
212     /**
213      * set the edit label (default "Edit")
214      * @param label , under which content must be stored
215      */

216     public void setLabel(String JavaDoc label) {
217         this.label = label;
218     }
219
220     /**
221      * @return String , label for the edit bar
222      */

223     private String JavaDoc getLabel() {
224         return this.label;
225     }
226
227     /**
228      * sets the size of the button
229      * @param s <code>true</code> for a small button (default), <code>false</code> for a large
230      */

231     public void setSmall(boolean small) {
232         this.small = small;
233     }
234
235     /**
236      * @see javax.servlet.jsp.tagext.TagSupport#release()
237      */

238     public void release() {
239         this.nodeName = null;
240         this.nodeCollectionName = null;
241         this.paragraph = null;
242         this.label = null;
243         this.displayHandler = null;
244         this.small = true;
245         super.release();
246     }
247 }
248
Popular Tags