KickJava   Java API By Example, From Geeks To Geeks.

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


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.Server;
16 import info.magnolia.cms.core.Content;
17 import info.magnolia.cms.gui.inline.BarEdit;
18 import info.magnolia.cms.security.Permission;
19 import info.magnolia.cms.util.Resource;
20
21 import java.io.IOException JavaDoc;
22
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.commons.lang.exception.NestableRuntimeException;
28
29
30 /**
31  * @author Sameer Charles
32  * @author Marcel Salathe
33  * @version $Revision: 6375 $ ($Author: philipp $)
34  */

35 public class EditBar extends TagSupport JavaDoc {
36
37     /**
38      * Stable serialVersionUID.
39      */

40     private static final long serialVersionUID = 222L;
41
42     private String JavaDoc nodeName;
43
44     private String JavaDoc nodeCollectionName;
45
46     private String JavaDoc paragraph;
47
48     private String JavaDoc editLabel;
49
50     private String JavaDoc deleteLabel;
51
52     private String JavaDoc moveLabel;
53
54     /**
55      * Show links only in admin instance.
56      */

57     private boolean adminOnly;
58
59     /**
60      * set working contentNode
61      * @param name , comtainer name which will be used to access/write content
62      */

63     public void setContentNodeName(String JavaDoc name) {
64         this.nodeName = name;
65     }
66
67     /**
68      * set working contentNode
69      * @param name , comtainer name which will be used to access/write content
70      */

71     public void setContentNodeCollectionName(String JavaDoc name) {
72         this.nodeCollectionName = name;
73     }
74
75     /**
76      * set current content type, could be any developer defined name
77      * @param type , content type
78      */

79     public void setParagraph(String JavaDoc type) {
80         this.paragraph = type;
81     }
82
83     /**
84      * set the edit label (else "Edit")
85      * @param label , under which content must be stored
86      */

87     public void setEditLabel(String JavaDoc label) {
88         this.editLabel = label;
89     }
90
91     /**
92      * set the delete label (else "Delete")
93      * @param label , under which content must be stored
94      */

95     public void setDeleteLabel(String JavaDoc label) {
96         this.deleteLabel = label;
97     }
98
99     public void setMoveLabel(String JavaDoc label) {
100         this.moveLabel = label;
101     }
102
103     /**
104      * Setter for <code>adminOnly</code>.
105      * @param adminOnly The adminOnly to set.
106      */

107     public void setAdminOnly(boolean adminOnly) {
108         this.adminOnly = adminOnly;
109     }
110
111     /**
112      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
113      */

114     public int doStartTag() {
115         return EVAL_BODY_INCLUDE;
116     }
117
118     /**
119      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
120      */

121     public int doEndTag() {
122
123         if (!adminOnly || Server.isAdmin()) {
124
125             HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) this.pageContext.getRequest();
126
127             if (Server.isAdmin() && Resource.getActivePage(request).isGranted(Permission.SET)) {
128                 try {
129                     BarEdit bar = new BarEdit((HttpServletRequest JavaDoc) this.pageContext.getRequest());
130                     
131                     Content localContentNode = Resource.getLocalContentNode((HttpServletRequest JavaDoc) this.pageContext
132                         .getRequest());
133                     
134                     if (this.paragraph == null) {
135                         Content contentParagraph = localContentNode;
136                         if (contentParagraph != null) {
137                             this.paragraph = contentParagraph.getMetaData().getTemplate();
138                         }
139                     }
140                     bar.setParagraph(this.paragraph);
141
142                     if (this.nodeCollectionName == null) {
143                         this.nodeCollectionName = StringUtils.defaultString(Resource
144                             .getLocalContentNodeCollectionName((HttpServletRequest JavaDoc) this.pageContext.getRequest()));
145                     }
146                     bar.setNodeCollectionName(this.nodeCollectionName);
147
148                     if (this.nodeName == null) {
149                         if (localContentNode != null) {
150                             this.nodeName = localContentNode.getName();
151                         }
152                     }
153                     bar.setNodeName(this.nodeName);
154
155                     try {
156                         String JavaDoc path;
157                         if (localContentNode != null){
158                             path = localContentNode.getHandle();
159                             if(path.endsWith(this.nodeCollectionName + "/" + this.nodeName )){
160                                 path = StringUtils.removeEnd(path, "/" + this.nodeCollectionName + "/" + this.nodeName);
161                             }
162                         }
163                         else{
164                             path = Resource.getCurrentActivePage((HttpServletRequest JavaDoc) this.pageContext.getRequest()).getHandle();
165                         }
166                         bar.setPath(path);
167                     }
168                     catch (Exception JavaDoc re) {
169                         bar.setPath(StringUtils.EMPTY);
170                     }
171                     
172                     bar.setDefaultButtons();
173
174                     if (this.editLabel != null) {
175                         if (StringUtils.isEmpty(this.editLabel)) {
176                             bar.setButtonEdit(null);
177                         }
178                         else {
179                             bar.getButtonEdit().setLabel(this.editLabel);
180                         }
181                     }
182
183                     if (this.moveLabel != null) {
184                         if (StringUtils.isEmpty(this.moveLabel)) {
185                             bar.setButtonMove(null);
186                         }
187                         else {
188                             bar.getButtonMove().setLabel(this.moveLabel);
189                         }
190                     }
191
192                     if (this.deleteLabel != null) {
193                         if (StringUtils.isEmpty(this.deleteLabel)) {
194                             bar.setButtonDelete(null);
195                         }
196                         else {
197                             bar.getButtonDelete().setLabel(this.deleteLabel);
198                         }
199                     }
200                     bar.placeDefaultButtons();
201                     bar.drawHtml(pageContext.getOut());
202                 }
203                 catch (IOException JavaDoc e) {
204                     throw new NestableRuntimeException(e);
205                 }
206             }
207         }
208         reset();
209         
210         return EVAL_PAGE;
211     }
212     
213     protected void reset(){
214         this.nodeName = null;
215         this.nodeCollectionName = null;
216         this.paragraph = null;
217         this.editLabel = null;
218         this.deleteLabel = null;
219         this.moveLabel = null;
220         this.adminOnly = false;
221     }
222
223 }
224
Popular Tags