KickJava   Java API By Example, From Geeks To Geeks.

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


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 ListMenu extends AbstractTag {
34   public static final String JavaDoc ITEMS_ALL = "all";
35   public static final String JavaDoc ITEMS_ON_PATH = "onpath";
36   public static final String JavaDoc ITEMS_FIRST_LEVEL = "firstlevel";
37   public static final String JavaDoc ITEMS_LAST_LEVEL = "lastlevel";
38   public static final String JavaDoc ITEMS_CHILDREN = "children";
39   public static final String JavaDoc ITEMS_INTERMEDIATE_LEVELS = "intermediatelevels";
40   public static final String JavaDoc TEXT = "text";
41   public static final String JavaDoc LINK = "link";
42
43   private String JavaDoc indentation = " ";
44   private String JavaDoc indentBuffer = "";
45
46   private String JavaDoc path;
47   private String JavaDoc items;
48   private String JavaDoc style;
49   private String JavaDoc current;
50   private String JavaDoc currentStyle;
51   private String JavaDoc currentPathStyle;
52   private String JavaDoc allowHiding;
53
54   boolean itemsAll;
55   boolean itemsOnPath;
56   boolean itemsFirstLevel;
57   boolean itemsLastLevel;
58   boolean itemsChildren;
59   boolean itemsIntermediateLevels;
60
61   public void writeTag() throws IOException {
62     if (items == null) {
63       itemsAll = itemsIntermediateLevels = false;
64       itemsOnPath = itemsFirstLevel = itemsLastLevel = itemsChildren = true;
65     } else {
66       StringTokenizer st = new StringTokenizer(items.toLowerCase(), ",;: ");
67
68       while (st.hasMoreTokens()) {
69         String JavaDoc token = st.nextToken();
70
71         if (token.equals(ITEMS_ALL)) {
72           itemsAll = true;
73         } else if (token.equals(ITEMS_CHILDREN)) {
74           itemsChildren = true;
75         } else if (token.equals(ITEMS_FIRST_LEVEL)) {
76           itemsFirstLevel = true;
77         } else if (token.equals(ITEMS_INTERMEDIATE_LEVELS)) {
78           itemsIntermediateLevels = true;
79         } else if (token.equals(ITEMS_LAST_LEVEL)) {
80           itemsLastLevel = true;
81         } else if (token.equals(ITEMS_ON_PATH)) {
82           itemsOnPath = true;
83         }
84       }
85     }
86
87     boolean linkCurrent = current != null && current.equalsIgnoreCase(LINK);
88
89     SiteMap siteMap = webSite.getSiteMap();
90     SiteInfo siteInfo = webSite.getSiteInfo();
91     Path rootPath = (path == null) ? siteInfo.getThemeRoot(pagePath) : new Path(path);
92     Path pathInMenu = webSite.getSiteMap().getPathInMenu(pagePath);
93     int baseLevel = rootPath.getElementCount() + 1;
94     Writer outWriter = getOut();
95     int lastLevel = rootPath.getElementCount();
96     SiteMapIterator iter = new SiteMapIterator(webSite, rootPath);
97     iter.setSkipHiddenSubPages(Utils.isTrue(allowHiding));
98     boolean styleNotApplied = !Utils.isNullOrEmpty(style);
99     int showLastLevel = -1;
100
101     while (iter.hasNext()) {
102       PageInfo current = (PageInfo) iter.next();
103       Path currentPath = current.getPath();
104       int level = Math.max(baseLevel, current.getLevel());
105
106       if ( current.getLevel() <= showLastLevel )
107         showLastLevel = -1;
108
109       if ( siteInfo.getHideSubmenu(currentPath) && showLastLevel == -1 )
110         showLastLevel = current.getLevel();
111
112       boolean add = false;
113
114       if (itemsAll) {
115         add = true;
116       } else {
117         Path parentPath = currentPath.getParent();
118
119         if (parentPath.isRelative() || pathInMenu.isContainedIn(parentPath)) {
120           if (itemsOnPath && pathInMenu.isContainedIn(currentPath)) {
121             add = true;
122           } else if (level <= baseLevel) {
123             add = itemsFirstLevel;
124           } else if (currentPath.getElementCount() == pathInMenu.getElementCount()) {
125             add = itemsLastLevel;
126           } else if (currentPath.getElementCount() > pathInMenu.getElementCount()) {
127             add = itemsChildren;
128           } else {
129             add = itemsIntermediateLevels;
130           }
131         }
132       }
133
134       if (add) {
135       // Close off any pending lower levels
136
for (int i = lastLevel - 1; i >= level; i--) {
137               writeIndented(outWriter, "</li>", i + 1);
138               writeIndented(outWriter, "</ul>", i);
139       }
140
141       // If we're a level deeper - then create new sub ul+li
142
for (int i = lastLevel; i < level; i++) {
143         if (styleNotApplied) {
144           writeIndented(outWriter, "<ul class=\"" + style + "\">", i);
145           styleNotApplied = false;
146         } else {
147           writeIndented(outWriter, "<ul>", i);
148         }
149         writeIndented(outWriter, "<li>", i + 1);
150       }
151
152       // If we do not have an open li at the right level - then create one
153
if (level <= lastLevel) {
154           writeIndented(outWriter, "</li>", level);
155           writeIndented(outWriter, "<li>", level);
156       }
157
158       if ( current.getLevel() > 0 && pathInMenu.isContainedIn(currentPath)
159               && ! current.getPath().equals(pathInMenu)
160               && ! Utils.isNullOrEmpty(currentPathStyle)) {
161           outWriter.write("<a HREF=\"" + cp + webSite.getLink(current) +
162                   "\" class='" + currentPathStyle + "'>" +
163                   siteInfo.getPageTitle(current) + "</a>");
164       } else if (current.getPath().equals(pathInMenu)) {
165         if (isEdit || linkCurrent) {
166           if (! Utils.isNullOrEmpty(currentStyle) || ! Utils.isNullOrEmpty(currentPathStyle)) {
167               outWriter.write("<a HREF=\"" + cp + webSite.getLink(current) +
168                       "\" class='" + (currentPathStyle+" "+currentStyle).trim() + "'>" +
169                       siteInfo.getPageTitle(current) + "</a>");
170           } else {
171               outWriter.write("<a HREF=\"" + cp + webSite.getLink(current) + "\">" +
172                       siteInfo.getPageTitle(current) + "</a>");
173           }
174         } else {
175           if (Utils.isNullOrEmpty(currentStyle)) {
176             outWriter.write(siteInfo.getPageTitle(current));
177           } else {
178             outWriter.write("<span class='" + currentStyle + "'>" +
179               siteInfo.getPageTitle(current) + "</span>");
180           }
181         }
182       } else {
183         outWriter.write("<a HREF=\"" + cp + webSite.getLink(current) + "\">" +
184           siteInfo.getPageTitle(current) + "</a>");
185       }
186
187       lastLevel = level;
188       }
189     }
190
191     for (int i = lastLevel - 1; i >= rootPath.getElementCount(); i--) {
192       writeIndented(outWriter, "</li>", i + 1);
193       writeIndented(outWriter, "</ul>", i);
194     }
195   }
196
197   private void writeIndented(Writer w, String JavaDoc s, int level) throws IOException {
198     while (indentBuffer.length() < indentation.length() * level) {
199       indentBuffer += indentation;
200     }
201
202     w.write('\n');
203     w.write(indentBuffer, 0, indentation.length() * level);
204     w.write(s);
205   }
206
207   public String JavaDoc getPath() {
208     return path;
209   }
210
211   public void setPath(String JavaDoc path) {
212     this.path = path;
213   }
214
215   public String JavaDoc getItems() {
216     return items;
217   }
218
219   public void setItems(String JavaDoc items) {
220     this.items = items;
221   }
222
223   public String JavaDoc getStyle() {
224     return style;
225   }
226
227   public void setStyle(String JavaDoc style) {
228     this.style = style;
229   }
230
231   public String JavaDoc getCurrent() {
232     return current;
233   }
234
235   public void setCurrent(String JavaDoc current) {
236     this.current = current;
237   }
238
239   public String JavaDoc getCurrentStyle() {
240     return currentStyle;
241   }
242
243   public void setCurrentStyle(String JavaDoc currentStyle) {
244     this.currentStyle = currentStyle;
245   }
246
247   public String JavaDoc getCurrentPathStyle() {
248     return currentPathStyle;
249   }
250
251   public void setCurrentPathStyle(String JavaDoc currentPathStyle) {
252     this.currentPathStyle = currentPathStyle;
253   }
254   public String JavaDoc getAllowHiding() {
255     return allowHiding;
256   }
257
258   public void setAllowHiding(String JavaDoc allowHiding) {
259     this.allowHiding = allowHiding;
260   }
261 }
262
Popular Tags