KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > taglib > AlibMenu


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.taglib;
24
25 import java.io.*;
26 import java.util.*;
27 import org.meshcms.core.*;
28 import org.meshcms.util.*;
29
30 /**
31  * Creates a navigation menu, using a unnumbered list.
32  */

33 public final class AlibMenu extends AbstractTag {
34   public static final String JavaDoc PART_HEAD = "head";
35   public static final String JavaDoc PART_BODY = "body";
36   public static final String JavaDoc HORIZONTAL = "horizontal";
37   public static final String JavaDoc VERTICAL = "vertical";
38
39   private String JavaDoc indentation = " ";
40   private String JavaDoc indentBuffer = "";
41
42   private String JavaDoc orientation;
43   private String JavaDoc part;
44   private String JavaDoc path;
45   private String JavaDoc current;
46   private String JavaDoc currentPathStyle = "selected";
47   private String JavaDoc allowHiding = "true";
48
49   public void writeTag() throws IOException {
50     Writer outWriter = getOut();
51     boolean horizontal = orientation != null && orientation.equals(HORIZONTAL);
52
53     if (part != null && part.equals(PART_HEAD)) {
54       String JavaDoc sp = request.getContextPath() +
55           webSite.getAdminScriptsPath().getAsLink() + "/alib";
56       String JavaDoc menuType = horizontal ? HORIZONTAL : VERTICAL;
57       outWriter.write("<script SRC='" + sp +
58           "/alib.common/script.js' type='text/javascript'></script>\n");
59       outWriter.write("<script SRC='" + sp +
60           "/menu." + menuType + "/script.js' type='text/javascript'></script>\n");
61       outWriter.write("<link type='text/css' HREF='" +
62           WebUtils.getFullThemeFolder(request) + "/alib.css' rel='stylesheet' />\n");
63     } else {
64       SiteMap siteMap = webSite.getSiteMap();
65       SiteInfo siteInfo = webSite.getSiteInfo();
66       Path rootPath = (path == null) ? siteInfo.getThemeRoot(pagePath) : new Path(path);
67       Path pathInMenu = webSite.getSiteMap().getPathInMenu(pagePath);
68       int baseLevel = rootPath.getElementCount() + 1;
69       int lastLevel = rootPath.getElementCount();
70       SiteMapIterator iter = new SiteMapIterator(webSite, rootPath);
71       iter.setSkipHiddenSubPages(Utils.isTrue(allowHiding));
72       boolean liUsed = false;
73       boolean firstUl = true;
74
75       while (iter.hasNext()) {
76         PageInfo current = (PageInfo) iter.next();
77         Path currentPath = current.getPath();
78         int level = Math.max(baseLevel, current.getLevel());
79
80         for (int i = lastLevel; i < level; i++) {
81           if (firstUl) {
82             writeIndented(outWriter, "<ul class=\"" +
83                 (horizontal ? "hmenu" : "vmenu") + "\">", i);
84             firstUl = false;
85           } else {
86             writeIndented(outWriter, "<ul>", i);
87           }
88
89           writeIndented(outWriter, "<li>", i + 1);
90           liUsed = false;
91         }
92
93         for (int i = lastLevel - 1; i >= level; i--) {
94           if (liUsed) {
95             outWriter.write("</li>");
96             liUsed = false;
97           } else {
98             writeIndented(outWriter, "</li>", i + 1);
99           }
100
101           writeIndented(outWriter, "</ul>", i);
102         }
103
104         if (liUsed) {
105           outWriter.write("</li>");
106           writeIndented(outWriter, "<li>", level);
107         }
108
109         for ( int i = lastLevel - 1; i >= level; i--) {
110             writeIndented(outWriter, "</li>", i);
111             writeIndented(outWriter, "<li>", i);
112         }
113
114         if ( ! Utils.isNullOrEmpty(currentPathStyle)
115                         && ( current.getLevel() >= baseLevel
116                                && pathInMenu.isContainedIn(currentPath)
117                      || current.getPath().equals(pathInMenu)
118                    ) ) {
119           outWriter.write("<a HREF=\"" + cp + webSite.getLink(current) +
120             "\" class='" + currentPathStyle + "'>" +
121             siteInfo.getPageTitle(current) + "</a>");
122         } else {
123           outWriter.write("<a HREF=\"" + cp + webSite.getLink(current) +"\">" +
124             siteInfo.getPageTitle(current) + "</a>");
125         }
126
127         liUsed = true;
128         lastLevel = level;
129       }
130
131       for (int i = lastLevel - 1; i >= rootPath.getElementCount(); i--) {
132         writeIndented(outWriter, "</li>", i + 1);
133         writeIndented(outWriter, "</ul>", i);
134       }
135     }
136   }
137
138   private void writeIndented(Writer w, String JavaDoc s, int level) throws IOException {
139     while (indentBuffer.length() < indentation.length() * level) {
140       indentBuffer += indentation;
141     }
142
143     w.write('\n');
144     w.write(indentBuffer, 0, indentation.length() * level);
145     w.write(s);
146   }
147
148   public String JavaDoc getPath() {
149     return path;
150   }
151
152   public void setPath(String JavaDoc path) {
153     this.path = path;
154   }
155
156   public String JavaDoc getCurrent() {
157     return current;
158   }
159
160   public void setCurrent(String JavaDoc current) {
161     this.current = current;
162   }
163
164   public String JavaDoc getOrientation() {
165     return orientation;
166   }
167
168   public void setOrientation(String JavaDoc orientation) {
169     this.orientation = orientation;
170   }
171
172   public String JavaDoc getPart() {
173     return part;
174   }
175
176   public void setPart(String JavaDoc part) {
177     this.part = part;
178   }
179
180   public String JavaDoc getCurrentPathStyle() {
181     return currentPathStyle;
182   }
183
184   public void setCurrentPathStyle(String JavaDoc currentPathStyle) {
185     this.currentPathStyle = currentPathStyle;
186   }
187
188   public String JavaDoc getAllowHiding() {
189     return allowHiding;
190   }
191
192   public void setAllowHiding(String JavaDoc allowHiding) {
193     this.allowHiding = allowHiding;
194   }
195 }
196
Popular Tags