Source code: com/obinary/cms/taglibs/EditButton.java
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-2003 obinary Ltd. (http://www.obinary.com) All rights reserved.
11 *
12 * */
13
14
15
16
17
18 package com.obinary.cms.taglibs;
19
20
21
22 import com.obinary.cms.beans.ServerInfo;
23 import com.obinary.cms.beans.ParagraphInfo;
24 import com.obinary.cms.util.Resource;
25
26 import javax.servlet.jsp.JspWriter;
27 import javax.servlet.jsp.tagext.TagSupport;
28 import javax.servlet.http.HttpServletRequest;
29 import javax.jcr.RepositoryException;
30 import javax.jcr.access.Permission;
31 import java.io.IOException;
32
33
34 /**
35 * Date: Apr 28, 2003
36 * Time: 11:20:59 AM
37 * @author Marcel Salathe
38 * @version 1.0
39 */
40
41
42 public class EditButton extends TagSupport {
43
44
45 private static final String EDIT_DIALOG_HANDLE = "/.CMSadmin/dialogs/editbox.html";
46 private static final String DEFAULT_EDIT_LABEL = "Edit";
47 private static final String DEFAULT_DELETE_LABEL = "Delete";
48
49 private String containerName;
50 private String containerListName;
51 private String contentType;
52 private String editLabel;
53 private String deleteLabel;
54 private String displayHandler;
55 private HttpServletRequest request;
56
57
58
59 /**
60 * <p>starts Edit tag</p>
61 *
62 * @return int
63 */
64 public int doStartTag() {
65 this.displayHandler = "";
66 this.request = (HttpServletRequest)pageContext.getRequest();
67 return EVAL_BODY_INCLUDE;
68 }
69
70
71
72 /**
73 * <p>print out</p>
74 *
75 * @return int
76 */
77 public int doEndTag() {
78 if (!ServerInfo.isAdmin() || Resource.showPreview(this.request))
79 return EVAL_PAGE;
80 if (!Resource.getActivePage(this.request).isGranted(Permission.WRITE_PROPERTY))
81 return EVAL_PAGE;
82 try {
83 this.display();
84 } catch (Exception e) {}
85 return EVAL_PAGE;
86 }
87
88
89
90 /**
91 * <p>set working container</p>
92 *
93 * @param name , comtainer name which will be used to access/write content
94 */
95 public void setContainerName(String name) {
96 this.containerName = name;
97 }
98
99
100
101 /**
102 *
103 */
104 private String getContainerName() {
105 if (this.containerName == null)
106 return Resource.getLocalContainer(this.request).getName();
107 else
108 return this.containerName;
109 }
110
111
112 /**
113 * <p>set working container list</p>
114 *
115 * @param name , comtainer list name
116 */
117 public void setContainerListName(String name) {
118 this.containerListName = name;
119 }
120
121
122
123 /**
124 * <p>set current content type, could be any developer defined name</p>
125 *
126 * @param type , content type
127 */
128 public void setParFile(String type) {
129 this.contentType = type;
130 }
131
132
133
134 /**
135 * @return content type
136 */
137 private String getParFile() {
138 if (this.contentType == null) {
139 String uuid = Resource.getLocalContainer(this.request).getAtom("UUID").getString();
140 try {
141 return ParagraphInfo.getInfo(uuid).getPath();
142 } catch (Exception e) {return "";}
143 }
144 return this.contentType;
145 }
146
147
148
149 /**
150 * <p>set display handler (JSP / Servlet), needs to know the relative path from WEB-INF</p>
151 *
152 * @param path , relative to WEB-INF
153 */
154 public void setTemplate(String path) {
155 this.displayHandler = path;
156 }
157
158
159
160 /**
161 * @return template path
162 */
163 private String getTemplate() {
164 if (this.displayHandler == null) {
165 return Resource.getLocalContainer(this.request).getTemplate();
166 }
167 return this.displayHandler;
168 }
169
170
171
172 /**
173 * <p>get the content path (Page or Node)</p>
174 *
175 * @return String path
176 */
177 private String getPath() {
178 try {
179 return Resource.getCurrentActivePage(this.request).getHandle();
180 } catch (RepositoryException re) {return "";}
181 }
182
183
184
185 /**
186 * <p>set the edit label (else "Edit")</p>
187 *
188 * @param label , under which content must be stored
189 */
190 public void setEditLabel(String label) {
191 this.editLabel = label;
192 }
193
194
195
196 /**
197 *
198 * @return String , label for the edit bar
199 */
200 private String getEditLabel() {
201 if (this.editLabel == null)
202 return EditButton.DEFAULT_EDIT_LABEL;
203 return this.editLabel;
204 }
205
206
207
208 /**
209 * <p>set the delete label (else "Delete")</p>
210 *
211 * @param label , under which content must be stored
212 */
213 public void setDeleteLabel(String label) {
214 this.deleteLabel = label;
215 }
216
217
218
219 /**
220 *
221 * @return String , label for the edit bar
222 */
223 private String getDeleteLabel() {
224 if (this.deleteLabel == null)
225 return EditButton.DEFAULT_DELETE_LABEL;
226 return this.deleteLabel;
227 }
228
229
230
231 /**
232 * @return containerlist Name
233 */
234 private String getContainerListName() {
235 if (this.containerListName == null) {
236 if (Resource.getLocalContainerListName(this.request) == null)
237 return "";
238 else
239 return Resource.getLocalContainerListName(this.request);
240 }
241 return this.containerListName;
242 }
243
244
245
246 /**
247 * <p>construct and return edit handle</p>
248 *
249 * @return String , URL for the edit box with all posted values
250 */
251 private String getEditHandle() {
252 return EDIT_DIALOG_HANDLE+"?path="+this.getPath()+"&clname="+this.getContainerListName()+"&name="+this.getContainerName()+"&type="+this.getParFile()+"&ck="+System.currentTimeMillis();
253 }
254
255
256
257 /**
258 * <p>displays edit bar</p>
259 *
260 * @throws IOException
261 */
262 private void display() throws IOException {
263 JspWriter out = pageContext.getOut();
264 out.println("<table class='obinaryEditBar' border='0' cellspacing='0' cellpadding='0' width='1%' bgcolor='c0c0c0'>");
265 out.print("<tr><td><nobr><a href=\"javascript:openWin('"+this.getEditHandle()+"','editwin','width=500,height=500,scrollbars')\" >");
266 out.print(this.getEditLabel());
267 out.print("</a></nobr></td></tr></table>");
268 }
269
270
271
272 }