Source code: com/obinary/cms/beans/admin/AdminNavigation.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.beans.admin;
19
20 import java.util.*;
21 import com.obinary.cms.admin.*;
22 import com.obinary.cms.beans.*;
23 import com.obinary.cms.core.*;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.jcr.*;
26
27
28 /**
29 * User: marcelsalathe
30 * Date: Sep 28, 2003
31 * Time: 09:30:04 AM
32 * @author Marcel Salathe
33 * @version 1.0
34 */
35
36
37
38 public class AdminNavigation implements java.io.Serializable {
39
40 private String context = "";
41 private String html = "";
42 private HttpServletRequest request;
43 private Content actpage;
44 private int level;
45 private int maxLevel;
46 private String path = "/";
47 private String currentPath;
48
49 public AdminNavigation() {
50 this.context = "Website";
51 }
52
53 public String getContext() {
54 return this.context;
55 }
56
57 public void setContext(String context) {
58 this.context = context;
59 }
60
61 public void setActpage() {
62 this.actpage = (Content) this.request.getAttribute("actpage");
63 }
64
65 public Content getActpage() {
66 return this.actpage;
67 }
68
69 public void setRequest(HttpServletRequest request) {
70 this.request = request;
71 setActpage();
72 }
73
74 public HttpServletRequest getRequest() {
75 return this.request;
76 }
77
78 public void setLevel(int level) {
79 this.level = level;
80 }
81
82 public int getLevel() {
83 return this.level;
84 }
85
86 public void setMaxLevel(int maxLevel) {
87 this.maxLevel = maxLevel;
88 }
89
90 public int getMaxLevel() {
91 return this.maxLevel;
92 }
93
94 public String getHtml() {
95 this.html = "";
96 /*
97 The path defines what part of the tree should be expanded.
98 We show root, all siblings of every level withing the path
99 plus the children of the path node.
100 Example: if the path is /hello/world/cms, we show root (level 0),
101 all siblings of level 1 (hello), level 2 (world), level 3 (cms)
102 and the children of cms.
103 */
104
105 Content root = null;
106 if (this.context.equals("Website")) {
107 try {
108 root = actpage.getAncestor(0);
109 this.path = request.getParameter("path");
110 if (this.path == null) path = "/";
111 // getting rid of the first slash to prevent labels[0] = ""
112 if (this.path.charAt(0) == '/') this.path = this.path.substring(1);
113 }
114 catch (Exception e) {}
115 }
116 if (this.context.equals("Users")) {
117 try {
118 HierarchyManager hm = new HierarchyManager(this.request);
119 Ticket t = ConfigLoader.usersRepository.connect(new PasswordCredentials(Authenticator.getUserId(this.request),Authenticator.getPassword(this.request)));
120 Node rootNode = t.getRootNode();
121 hm.setStartPage(rootNode);
122 root = hm.getPage("/");
123 }
124 catch (Exception e) {}
125 }
126 if (this.context.equals("Roles")) {
127 try {
128 HierarchyManager hm = new HierarchyManager(this.request);
129 Ticket t = ConfigLoader.userRolesRepository.connect(new PasswordCredentials(Authenticator.getUserId(this.request),Authenticator.getPassword(this.request)));
130 Node rootNode = t.getRootNode();
131 hm.setStartPage(rootNode);
132 root = hm.getPage("/");
133 }
134 catch (Exception e) {}
135 }
136
137 String[] labels = this.path.split("/");
138
139 int levels = labels.length;
140 setMaxLevel(levels);
141
142 if (this.path.equals("")) setMaxLevel(0);
143 boolean[] isLast = new boolean[levels+1];
144 String action;
145 action = this.request.getParameter("action");
146 if (action == null) action = "detailview";
147
148 this.currentPath = this.request.getParameter("currentPath");
149 if (this.currentPath == null) this.currentPath = "/";
150
151 showChildren(root,0,labels,isLast,action);
152
153 return this.html;
154 }
155
156 public void setHtml(String html) {
157 this.html = html;
158 }
159
160
161 private String getIcon(String action, String handle) {
162 String icon = "";
163 if (action.equals("detailview")) {
164 String imgName = "";
165 if (handle.equals(this.currentPath)) icon = "<img src=\"/admindocroot/images/folderIconSelected.gif\" width=\"16\" height=\"16\" border=\"0\" name=\""+handle+"\" id=\""+handle+"\" onmousedown=\"changeHighlightedImage(this)\" />";
166 else icon = "<img src=\"/admindocroot/images/folderIcon.gif\" width=\"16\" height=\"16\" border=\"0\" name=\""+handle+"\" id=\""+handle+"\" onmousedown=\"changeHighlightedImage(this)\" />";
167 }
168 else if (action.equals("movepages") || action.equals("copypages") || action.equals("linkbrowse")) icon = "<img src=\"/admindocroot/images/docIcon.gif\" border=\"0\" />";
169 return icon;
170 }
171
172
173 private String getIconHref(String action, String handle) {
174 String link = "";
175 if (action.equals("detailview")) link = "<a href=\"javascript:detailViewLink('"+handle+"')\">";
176 else if (action.equals("movepages") || action.equals("copypages")) link = "<a href=\"javascript:setPathForMoveOrCopy('"+handle+"')\">";
177 else if (action.equals("linkbrowse")) link = "<a href=\"javascript:setPathForLinkBrowse('"+handle+"')\">";
178 return link;
179 }
180
181
182 private String getTreeLineIcon(String sign, Content page, boolean isLast) {
183 String img = "";
184 try {
185 if (isLast && page.hasChildren()) img += "<img src=\"/admindocroot/images/treeLineCorner"+sign+".gif\" border=\"0\" />";
186 else if (isLast && !page.hasChildren()) img += "<img src=\"/admindocroot/images/treeLineCorner.gif\" />";
187 else if (!isLast && page.hasChildren()) img += "<img src=\"/admindocroot/images/treeLineCrossing"+sign+".gif\" border=\"0\" />";
188 else if (!isLast && !page.hasChildren()) img += "<img src=\"/admindocroot/images/treeLineCrossing.gif\" />";
189 }
190 catch (Exception e) {img = e.getMessage();}
191 return img;
192 }
193
194
195 private String getTreeLineIconHref(int level, String[] label, Content page, String actHandle, String action, boolean isLast) {
196 String link = "";
197 String icon = "";
198 String path = "";
199 try {
200 if ((level != this.maxLevel) && page.getName().equals(label[level])) {
201 if (page.hasChildren()) {
202 path = page.getParent().getHandle();
203 link += ("<a href=\"javascript:collapseTree('"+actHandle+".html','"+path+"','"+action+"');\">");
204 icon = getTreeLineIcon("Minus",page,isLast);
205 link += icon + "</a>";
206 }
207 else {
208 icon = getTreeLineIcon("Minus",page,isLast);
209 link += icon;
210 }
211 }
212 else {
213 if (page.hasChildren()) {
214 path = page.getHandle();
215 link += ("<a href=\"javascript:expandTree('"+actHandle+".html','"+path+"','"+action+"');\">");
216 icon = getTreeLineIcon("Plus",page,isLast);
217 link += icon + "</a>";
218 }
219 else {
220 icon = getTreeLineIcon("Plus",page,isLast);
221 link += icon;
222 }
223 }
224 }
225 catch (Exception e) {}
226 return link;
227 }
228
229
230 private void showChildren(Content page, int level, String[] labels, boolean[] isLast, String action) {
231 if (level > this.maxLevel) return;
232 String childName = null;
233 String childTemplate = null;
234 String childTitle = null;
235 String docIconHref = null;
236 String treeLineIconHref = "";
237 if (level == 0) {
238 try {
239 docIconHref = getIconHref(action,page.getHandle());
240 this.html += ("<tr>");
241 this.html += ("<td width=\"1%\">"+docIconHref+getIcon(action,page.getHandle())+"</a></td>");
242 this.html += ("<td width=\"100%\" colspan="+(this.maxLevel + 2)+">"+this.context+"</td>");
243 this.html += ("</tr>");
244 }
245 catch (Exception e) {}
246 }
247
248 Collection children = page.getChildren();
249 Iterator childrenIterator = children.iterator();
250 while (childrenIterator.hasNext()) {
251 Content child;
252 child = (Content) childrenIterator.next();
253 if (!childrenIterator.hasNext()) isLast[level] = true;
254 try {
255 childName = child.getName();
256 if (childName.equals(".CMSadmin")) continue;
257 childTemplate = child.getAtom("template").getString();
258 childTitle = child.getAtom("title").getString();
259 child.getAtom("title").getString();
260 // limit users to one hierarchy
261 // later we should get rid of this by removing this condition (see also further down);
262 //if (!this.context.equals("Website")) docIconHref = "";
263 //else docIconHref = getIconHref(action,child.getHandle());
264 docIconHref = getIconHref(action,child.getHandle());
265 this.html += ("<tr>");
266 for (int i = 0; i < level; i++) {
267 if (isLast[i]) this.html += ("<td width=\"1%\"><img src=\"/admindocroot/0.gif\" height=\"1\" width=\"16\"/></td>");
268 else this.html += ("<td width=\"1%\"><img src=\"/admindocroot/images/treeLineDown.gif\" /></td>");
269 }
270 this.html += "<td width=\"1%\">";
271 treeLineIconHref = getTreeLineIconHref(level, labels, child, this.actpage.getHandle(), action, isLast[level]);
272 this.html += treeLineIconHref;
273 this.html += "</td>";
274 this.html += ("<td width=\"1%\">");
275 this.html += docIconHref;
276 this.html += getIcon(action,child.getHandle());
277 //this.html += ("<img src=\"/admindocroot/"+getIcon(action)+"\" border=\"0\" alt=\"Template: "+childTemplate+", Path: "+child.getHandle()+"\" />");
278 // limit users to one hierarchy
279 // later we should get rid of this by removing this condition;
280 if (this.context.equals("Website")) {
281 this.html += ("</a>");
282 }
283 this.html += ("</td>");
284 this.html += ("<td width=\"100%\" colspan=\""+(this.maxLevel+3-(level+2))+"\"><nobr>"+childTitle+"</nobr></td>");
285 this.html += ("</tr>");
286 }
287 catch (Exception e) {this.html += "hm";}
288 if ((level != this.maxLevel) && childName.equals(labels[level])) {
289 showChildren(child,++level,labels,isLast,action);
290 level--;
291 }
292 }
293 }
294
295
296 }