KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > module > admininterface > AdminTreeMVCHandler


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-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13
14 package info.magnolia.module.admininterface;
15
16 import info.magnolia.cms.beans.config.MIMEMapping;
17 import info.magnolia.cms.beans.config.Subscriber;
18 import info.magnolia.cms.core.ItemType;
19 import info.magnolia.cms.gui.control.Tree;
20 import info.magnolia.cms.gui.misc.Sources;
21 import info.magnolia.cms.gui.misc.Spacer;
22 import info.magnolia.cms.servlets.MVCServletHandlerImpl;
23
24 import java.io.IOException JavaDoc;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.apache.commons.lang.StringUtils;
30
31
32 /**
33  * this class wrapes the tree control. The AdminInterfaceServlet instantiates a subclass. To build your own tree you
34  * have to override the prepareTree() method
35  * @author philipp
36  * @author Fabrizio Giustina
37  */

38
39 public abstract class AdminTreeMVCHandler extends MVCServletHandlerImpl {
40
41     /**
42      * this are the used actions
43      */

44     protected static final String JavaDoc COMMAND_SHOW_TREE = "show"; //$NON-NLS-1$
45

46     protected static final String JavaDoc COMMAND_COPY_NODE = "copy"; //$NON-NLS-1$
47

48     protected static final String JavaDoc COMMAND_MOVE_NODE = "move"; //$NON-NLS-1$
49

50     protected static final String JavaDoc COMMAND_ACTIVATE = "activate"; //$NON-NLS-1$
51

52     protected static final String JavaDoc COMMAND_DEACTIVATE = "deactivate"; //$NON-NLS-1$
53

54     protected static final String JavaDoc COMMAND_CREATE_NODE = "createNode"; //$NON-NLS-1$
55

56     protected static final String JavaDoc COMMAND_DELETE_NODE = "delete"; //$NON-NLS-1$
57

58     protected static final String JavaDoc COMMAND_SAVE_VALUE = "saveValue"; //$NON-NLS-1$
59

60     /**
61      * The view names
62      */

63
64     protected static final String JavaDoc VIEW_TREE = "tree"; //$NON-NLS-1$
65

66     protected static final String JavaDoc VIEW_CREATE = "create"; //$NON-NLS-1$
67

68     protected static final String JavaDoc VIEW_VALUE = "value"; //$NON-NLS-1$
69

70     protected static final String JavaDoc VIEW_NOTHING = "nothing"; //$NON-NLS-1$
71

72     protected static final String JavaDoc VIEW_COPY_MOVE = "copymove"; //$NON-NLS-1$
73

74     /**
75      * name of the tree (not the repository)
76      */

77     private Tree tree;
78
79     private String JavaDoc path;
80
81     private String JavaDoc pathOpen;
82
83     private String JavaDoc pathSelected;
84
85     /**
86      * Used to pass the saved value to the view
87      */

88     private String JavaDoc displayValue;
89
90     private String JavaDoc newPath;
91     
92     /**
93      * Used to display the same tree in the linkbrowser
94      */

95     private boolean browseMode;
96
97     /**
98      * Override this method if you are not using the same name for the tree and the repository
99      * @return name of the repository
100      */

101     protected String JavaDoc getRepository() {
102         return getName();
103     }
104
105     public AdminTreeMVCHandler(String JavaDoc name, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
106         super(name, request, response);
107
108         tree = new Tree(name, getRepository(), request);
109         path = request.getParameter("path"); //$NON-NLS-1$
110
if (StringUtils.isEmpty(path)) {
111             path = "/"; //$NON-NLS-1$
112
}
113
114         pathOpen = request.getParameter("pathOpen"); //$NON-NLS-1$
115
pathSelected = request.getParameter("pathSelected"); //$NON-NLS-1$
116

117         this.setBrowseMode(StringUtils.equals(request.getParameter("browseMode"), "true"));
118     }
119
120     /**
121      * Depending on the request it is generating a logical command name
122      * @return name of the command
123      */

124     public String JavaDoc getCommand() {
125
126         // actions returned from the tree (pased through treeAction)
127
if (StringUtils.isNotEmpty(request.getParameter("treeAction"))) { //$NON-NLS-1$
128
int treeAction = Integer.parseInt(request.getParameter("treeAction")); //$NON-NLS-1$
129

130             if (treeAction == Tree.ACTION_COPY) {
131                 return COMMAND_COPY_NODE;
132             }
133             if (treeAction == Tree.ACTION_MOVE) {
134                 return COMMAND_MOVE_NODE;
135             }
136             if (treeAction == Tree.ACTION_ACTIVATE) {
137                 return COMMAND_ACTIVATE;
138             }
139             if (treeAction == Tree.ACTION_DEACTIVATE) {
140                 return COMMAND_DEACTIVATE;
141             }
142
143             return request.getParameter("treeAction"); //$NON-NLS-1$
144
}
145
146         // other actions depending other informations
147
if (request.getParameter("createItemType") != null) { //$NON-NLS-1$
148
return COMMAND_CREATE_NODE;
149         }
150
151         if (request.getParameter("deleteNode") != null) { //$NON-NLS-1$
152
return COMMAND_DELETE_NODE;
153         }
154
155         // editet any value directly in the columns?
156
if (request.getParameter("saveName") != null //$NON-NLS-1$
157
// value to save is a node data's value (config admin)
158
|| "true".equals(request.getParameter("isNodeDataValue")) //$NON-NLS-1$ //$NON-NLS-2$
159
// value to save is a node data's type (config admin)
160
|| "true".equals(request.getParameter("isNodeDataType"))) { //$NON-NLS-1$ //$NON-NLS-2$
161
return COMMAND_SAVE_VALUE;
162         }
163         return COMMAND_SHOW_TREE;
164     }
165
166     /**
167      * Show the tree
168      */

169     public String JavaDoc show() {
170         return VIEW_TREE;
171     }
172
173     /**
174      * Create a new node and show the tree
175      * @return
176      */

177     public String JavaDoc createNode() {
178         String JavaDoc createItemType = ItemType.NT_NODEDATA;
179         if (request.getParameter("createItemType") != null) { //$NON-NLS-1$
180
createItemType = request.getParameter("createItemType"); //$NON-NLS-1$
181
}
182
183         tree.setPath(path);
184         tree.createNode(createItemType);
185         return VIEW_TREE;
186     }
187
188     /**
189      * Copy a node
190      */

191     public String JavaDoc copy() {
192         return copyOrMove(Tree.ACTION_COPY);
193     }
194
195     /**
196      * Move a node
197      */

198     public String JavaDoc move() {
199         return copyOrMove(Tree.ACTION_MOVE);
200     }
201
202     /**
203      * @param action
204      * @return
205      */

206     private String JavaDoc copyOrMove(int action) {
207         String JavaDoc pathClipboard = request.getParameter("pathClipboard"); //$NON-NLS-1$
208
int pasteType = Integer.parseInt(request.getParameter("pasteType")); //$NON-NLS-1$
209

210         newPath = tree.pasteNode(pathClipboard, pathSelected, pasteType, action);
211         if (pasteType == Tree.PASTETYPE_SUB) {
212             pathOpen = pathSelected;
213         }
214         else {
215             // open parent path of destination path
216
pathOpen = pathSelected.substring(0, pathSelected.lastIndexOf("/")); //$NON-NLS-1$
217
}
218
219         pathSelected = null;
220         return VIEW_COPY_MOVE;
221     }
222
223     public String JavaDoc delete() {
224         String JavaDoc deleteNode = request.getParameter("deleteNode"); //$NON-NLS-1$
225
tree.deleteNode(path, deleteNode);
226         return VIEW_TREE;
227     }
228
229     public String JavaDoc activate() {
230         boolean recursive = (request.getParameter("recursive") != null); //$NON-NLS-1$
231
tree.activateNode(pathSelected, recursive);
232         return VIEW_TREE;
233     }
234
235     public String JavaDoc deactivate() {
236         tree.deActivateNode(pathSelected);
237         return VIEW_TREE;
238     }
239
240     /**
241      * Saves a value edited directly inside the tree. This can also be a lable
242      * @return name of the view
243      */

244     public String JavaDoc saveValue() {
245         String JavaDoc saveName = request.getParameter("saveName"); //$NON-NLS-1$
246

247         // value to save is a node data's value (config admin)
248
boolean isNodeDataValue = "true".equals(request.getParameter("isNodeDataValue")); //$NON-NLS-1$ //$NON-NLS-2$
249

250         // value to save is a node data's type (config admin)
251
boolean isNodeDataType = "true".equals(request.getParameter("isNodeDataType")); //$NON-NLS-1$ //$NON-NLS-2$
252

253         String JavaDoc value = StringUtils.defaultString(request.getParameter("saveValue")); //$NON-NLS-1$
254
displayValue = StringUtils.EMPTY;
255         // value to save is a content's meta information
256
boolean isMeta = "true".equals(request.getParameter("isMeta")); //$NON-NLS-1$ //$NON-NLS-2$
257
// value to save is a label (name of page, content node or node data)
258
boolean isLabel = "true".equals(request.getParameter("isLabel")); //$NON-NLS-1$ //$NON-NLS-2$
259

260         if (isNodeDataValue || isNodeDataType) {
261             tree.setPath(StringUtils.substringBeforeLast(path, "/")); //$NON-NLS-1$
262
saveName = StringUtils.substringAfterLast(path, "/"); //$NON-NLS-1$
263
}
264         else {
265             // "/modules/templating/Templates/x"
266
tree.setPath(path);
267         }
268
269         if (isLabel) {
270             displayValue = rename(value);
271         }
272         else if (isNodeDataType) {
273             int type = Integer.valueOf(value).intValue();
274             displayValue = tree.saveNodeDataType(saveName, type);
275         }
276         else {
277             displayValue = tree.saveNodeData(saveName, value, isMeta);
278         }
279
280         // if there was a displayValue passed show it instead of the written value
281
displayValue = StringUtils.defaultString(request.getParameter("displayValue"), value); //$NON-NLS-1$
282

283         // @todo should be handled in a better way but, at the moment, this is better than nothing
284
if (path.startsWith("/subscribers/")) { //$NON-NLS-1$
285
Subscriber.reload();
286         }
287         else if (path.startsWith("/server/MIMEMapping")) { //$NON-NLS-1$
288
MIMEMapping.reload();
289         }
290
291         return VIEW_VALUE;
292     }
293
294     /**
295      * Called during a renaming of a node. First is the action saveValue called
296      * @param value the new name
297      * @return return the new name (can change if there were not allowed characters passed)
298      */

299     protected String JavaDoc rename(String JavaDoc value) {
300         return tree.renameNode(value);
301     }
302
303     /**
304      * Render the tree depending on the view name.
305      * @param view
306      * @return
307      * @throws IOException
308      */

309     public void renderHtml(String JavaDoc view) throws IOException JavaDoc {
310         StringBuffer JavaDoc html = new StringBuffer JavaDoc(500);
311
312         if (VIEW_TREE.equals(view) || VIEW_CREATE.equals(view) || VIEW_COPY_MOVE.equals(view)) {
313             // if there was a node created we have not to set the pathes
314
if (view != VIEW_CREATE) {
315                 tree.setPathOpen(pathOpen);
316                 tree.setPathSelected(pathSelected);
317             }
318
319             // after moving or copying
320
if (view == VIEW_COPY_MOVE) {
321                 // pass new path to tree.js for selecting the newly created node
322
// NOTE: tree.js checks for this pattern; adapt it there, if any changes are made here
323
html.append("<input type=\"hidden\" id=\"mgnlSelectNode\" value=\"" + newPath + "\" />"); //$NON-NLS-1$ //$NON-NLS-2$
324
}
325
326             renderTree(html);
327         }
328
329         // after saving a column value
330
else if (view == VIEW_VALUE) {
331             html.append(displayValue);
332         }
333         response.getWriter().print(html);
334     }
335
336     /**
337      * Override this method to configure the tree control (define the columns, ...)
338      * @param tree
339      * @param request
340      */

341     protected abstract void prepareTree(Tree tree, HttpServletRequest JavaDoc request);
342
343     /**
344      * Prepare the context menu of the tree. This is called during renderTree
345      * @param tree
346      * @param request
347      */

348     protected abstract void prepareContextMenu(Tree tree, HttpServletRequest JavaDoc request);
349
350     /**
351      * Create the html for the tree. Calls tree.getHtml after calling prepareTree.
352      * @param html
353      */

354     protected void renderTree(StringBuffer JavaDoc html) {
355         String JavaDoc mode = StringUtils.defaultString(request.getParameter("treeMode")); //$NON-NLS-1$
356
boolean snippetMode = mode.equals("snippet"); //$NON-NLS-1$
357

358         tree.setJavascriptTree("mgnlTreeControl"); //$NON-NLS-1$
359
tree.setBrowseMode(this.isBrowseMode());
360
361         if (!snippetMode) {
362             html.append("<html><head>"); //$NON-NLS-1$
363
html.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>"); //$NON-NLS-1$
364
renderHeaderIncludes(html);
365             html.append("<title>Magnolia</title>"); //$NON-NLS-1$
366
html.append("</head>"); //$NON-NLS-1$
367
html.append("<body class=\"mgnlBgDark\" onload=\"" + tree.getJavascriptTree() + ".resizeOnload();\" >"); //$NON-NLS-1$ //$NON-NLS-2$
368
html.append(Spacer.getHtml(20, 20));
369         }
370
371         tree.setSnippetMode(snippetMode);
372         tree.setHeight(50);
373
374         tree.setPath(path);
375
376         prepareTree(tree, request);
377         prepareContextMenu(tree, request);
378
379         if (!snippetMode) {
380             html.append("<div id=\"" + tree.getJavascriptTree() + "_DivSuper\" style=\"display:block;\">"); //$NON-NLS-1$ //$NON-NLS-2$
381
}
382         html.append(tree.getHtml());
383         if (!snippetMode) {
384             html.append("</div>"); //$NON-NLS-1$
385
}
386
387         if (!snippetMode) {
388             html.append("</body></html>"); //$NON-NLS-1$
389
}
390     }
391
392     /**
393      * @param html
394      */

395     protected void renderHeaderIncludes(StringBuffer JavaDoc html) {
396         html.append(new Sources(request.getContextPath()).getHtmlJs());
397         html.append(new Sources(request.getContextPath()).getHtmlCss());
398     }
399
400     protected Tree getTree() {
401         return this.tree;
402     }
403
404     protected void setTree(Tree tree) {
405         this.tree = tree;
406     }
407
408     protected String JavaDoc getPath() {
409         return path;
410     }
411     
412     /**
413      * @return Returns the browseMode.
414      */

415     public boolean isBrowseMode() {
416         return browseMode;
417     }
418     
419     /**
420      * @param browseMode The browseMode to set.
421      */

422     public void setBrowseMode(boolean browseMode) {
423         this.browseMode = browseMode;
424     }
425
426 }
Popular Tags