KickJava   Java API By Example, From Geeks To Geeks.

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


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 package info.magnolia.module.admininterface;
14
15 import info.magnolia.cms.beans.config.ContentRepository;
16 import info.magnolia.cms.core.Content;
17 import info.magnolia.cms.core.ItemType;
18 import info.magnolia.cms.security.Permission;
19 import info.magnolia.cms.util.NodeDataUtil;
20 import info.magnolia.context.MgnlContext;
21
22 import java.text.MessageFormat JavaDoc;
23 import java.util.Iterator JavaDoc;
24
25 import org.apache.commons.lang.StringUtils;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29
30 /**
31  * This class is used to create the menud
32  * @author Philipp Bracher
33  * @version $Revision: 6341 $ ($Author: philipp $)
34  */

35 public class Navigation {
36
37     Logger log = LoggerFactory.getLogger(Navigation.class);
38
39     /**
40      * The node containing the menu and submenupoints
41      */

42     Content node;
43
44     /**
45      * The name of the Javascript variable
46      */

47     String JavaDoc jsName;
48
49     /**
50      * @param path the path to the menu
51      */

52     public Navigation(String JavaDoc path, String JavaDoc jsName) {
53         try {
54             // get it with system permission
55
this.node = MgnlContext.getSystemContext().getHierarchyManager(ContentRepository.CONFIG).getContent(path);
56             this.jsName = jsName;
57         }
58         catch (Exception JavaDoc e) {
59             log.error("can't initialize the menu", e);
60         }
61     }
62
63     /**
64      * Generate the code to initialize the js navigation
65      * @param name the name of the javascript menu variable
66      * @return the javascript
67      */

68     public String JavaDoc getJavascript() {
69         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
70
71         // name, id, text, link, icon
72
String JavaDoc nodePattern = "{0}.addNode (\"{1}\", \"{2}\", \"{3}\", contextPath + \"{4}\");\n";
73         // name, parentId, id, text, link, icon
74
String JavaDoc subPattern = "{0}.getNode(\"{1}\").addNode (\"{2}\", \"{3}\", \"{4}\", contextPath + \"{5}\");\n";
75
76         // loop over the menupoints
77
for (Iterator JavaDoc iter = node.getChildren(ItemType.CONTENTNODE).iterator(); iter.hasNext();) {
78             Content mp = (Content) iter.next();
79             // check permission
80
if (isMenuPointRendered(mp)) {
81                 str.append(MessageFormat.format(nodePattern, new Object JavaDoc[]{
82                     jsName,
83                     mp.getUUID(),
84                     getLabel(mp),
85                     NodeDataUtil.getString(mp, "onclick"),
86                     NodeDataUtil.getString(mp, "icon")}));
87
88                 // sub menupoints (2 level only)
89
for (Iterator JavaDoc iterator = mp.getChildren(ItemType.CONTENTNODE).iterator(); iterator.hasNext();) {
90                     Content sub = (Content) iterator.next();
91                     if (isMenuPointRendered(sub)) {
92                         str.append(MessageFormat.format(subPattern, new Object JavaDoc[]{
93                             jsName,
94                             mp.getUUID(),
95                             sub.getUUID(),
96                             getLabel(sub),
97                             NodeDataUtil.getString(sub, "onclick"),
98                             NodeDataUtil.getString(sub, "icon")}));
99                     }
100                 }
101             }
102         }
103
104         return str.toString();
105     }
106
107     /**
108      * @param mp
109      * @return
110      */

111     protected Object JavaDoc getLabel(Content mp) {
112         return NodeDataUtil.getI18NString(mp, "label");
113     }
114
115     /**
116      * @param mp
117      * @return
118      */

119     protected boolean isMenuPointRendered(Content mp) {
120         return MgnlContext.getAccessManager(ContentRepository.CONFIG).isGranted(mp.getHandle(), Permission.READ);
121     }
122
123     /**
124      * Get the first onclick in this menu. Used as the default src in the content iframe
125      * @return the href
126      */

127     public String JavaDoc getFirstId() {
128         return getFirstId(node);
129     }
130
131     private String JavaDoc getFirstId(Content node) {
132         for (Iterator JavaDoc iter = node.getChildren(ItemType.CONTENTNODE).iterator(); iter.hasNext();) {
133             Content sub = (Content) iter.next();
134             if (isMenuPointRendered(sub)) {
135                 if (StringUtils.isNotEmpty(NodeDataUtil.getString(sub, "onclick"))) {
136                     return sub.getUUID();
137                 }
138                 String JavaDoc uuid = getFirstId(sub);
139                 if (StringUtils.isNotEmpty(uuid)) {
140                     return uuid;
141                 }
142             }
143         }
144         return "";
145     }
146
147 }
148
Popular Tags