KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > help > CmsHelpNavigationListView


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/help/CmsHelpNavigationListView.java,v $
3  * Date : $Date: 2005/07/17 13:34:50 $
4  * Version: $Revision: 1.3 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * For further information about Alkacon Software GmbH, please see the
21  * company website: http://www.alkacon.com
22  *
23  * For further information about OpenCms, please see the
24  * project website: http://www.opencms.org
25  *
26  * You should have received a copy of the GNU Lesser General Public
27  * License along with this library; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29  */

30
31 package org.opencms.workplace.help;
32
33 import org.opencms.file.CmsResource;
34 import org.opencms.jsp.CmsJspActionElement;
35 import org.opencms.jsp.CmsJspNavElement;
36 import org.opencms.main.CmsIllegalArgumentException;
37
38 import java.util.List JavaDoc;
39 import java.util.StringTokenizer JavaDoc;
40
41 import javax.servlet.http.HttpServletRequest JavaDoc;
42 import javax.servlet.http.HttpServletResponse JavaDoc;
43 import javax.servlet.jsp.PageContext JavaDoc;
44
45 /**
46  * Generates a simple TOC - list by using a navigation model
47  * obtained from a <code>{@link org.opencms.jsp.CmsJspNavBuilder}</code>. <p>
48  *
49  * This is a simpler facade to a fixed html - list based layout.
50  * Only a navigation root path and a desired depth have to be set.
51  * It is not specific to the online help.<p>
52  *
53  * @author Achim Westermann
54  *
55  * @version $Revision: 1.3 $
56  *
57  * @since 6.0.0
58  */

59 public final class CmsHelpNavigationListView {
60
61     /** The depth (in levels) of the navigation. **/
62     private int m_depth;
63
64     /** The CmsJspActionElement to use. **/
65     private CmsJspActionElement m_jsp;
66
67     /** The root path where the navigation will start. **/
68     private String JavaDoc m_navRootPath;
69
70     /**
71      * Creates a <code>CmsNaviationListView</code> which uses the given
72      * <code>CmsJspActionElement</code> for accessing the underlying
73      * navigation API. <p>
74      *
75      * @param jsp the <code>CmsJspActionElement</code> to use
76      */

77     public CmsHelpNavigationListView(CmsJspActionElement jsp) {
78
79         m_jsp = jsp;
80         m_navRootPath = m_jsp.getCmsObject().getRequestContext().getUri();
81
82     }
83
84     /**
85      * Creates a <code>CmsNaviationListView</code> which creates a
86      * <code>CmsJspActionElement</code> for accessing the underlying
87      * navigation API with the given arguments . <p>
88      *
89      * @param context the <code>PageContext</code> to use
90      * @param request the <code>HttpServletRequest</code> to use
91      * @param response the <code>HttpServletResponse</code> to use
92      *
93      */

94     public CmsHelpNavigationListView(PageContext JavaDoc context, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
95
96         this(new CmsJspActionElement(context, request, response));
97     }
98
99     private static String JavaDoc getSpaces(int n) {
100
101         // avoid negative NegativeArraySizeException in case uri is missing
102
n = Math.max(n, 0);
103         StringBuffer JavaDoc result = new StringBuffer JavaDoc(n);
104         for (; n > 0; n--) {
105             result.append(' ');
106         }
107         return result.toString();
108     }
109
110     /**
111      * Returns a string containing the navigation created by using the internal members.<p>
112      *
113      * The navigation is a nested html list. <p>
114      *
115      * @return a string containing the navigation created by using the internal members
116      */

117     public String JavaDoc createNavigation() {
118
119         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(2048);
120         int endlevel = calculateEndLevel();
121         String JavaDoc spaces = getSpaces((endlevel - m_depth) * 2);
122         if (m_navRootPath != null) {
123             buffer.append("\n").append(spaces).append("<p>\n");
124             buffer.append(spaces).append(" <ul>\n");
125             List JavaDoc navElements = m_jsp.getNavigation().getSiteNavigation(m_navRootPath, endlevel);
126             if (navElements.size() > 0) {
127                 createNavigationInternal(buffer, navElements);
128             }
129             buffer.append(spaces).append(" </ul>\n");
130             buffer.append(spaces).append("</p>");
131             return buffer.toString();
132         } else {
133             CmsIllegalArgumentException ex = new CmsIllegalArgumentException(Messages.get().container(
134                 Messages.GUI_HELP_ERR_SITEMAP_MISSING_PARAM_1,
135                 "navRootPath"));
136             throw ex;
137         }
138     }
139
140     /**
141      * Returns the depth in levels of the navigation. <p>
142      *
143      * @return the depth in levels of the navigation.
144      */

145     public int getDepth() {
146
147         return m_depth;
148     }
149
150     /**
151      * Returns the navigation root path of the navigation to generate a view for. <p>
152      *
153      * @return the navigation root path of the navigation to generate a view for.
154      */

155     public String JavaDoc getSiteRootPath() {
156
157         return m_navRootPath;
158     }
159
160     /**
161      * Set the depth in level of the navigation to generate a view for. <p>
162      *
163      * @param depth the depth in level of the navigation to generate a view for to set
164      */

165     public void setDepth(int depth) {
166
167         m_depth = depth;
168     }
169
170     /**
171      * Set the navigation root path of the navigation to generate a view for. <p>
172      *
173      * The navigation will start there. <p>
174      *
175      * @param navRootPath the navigation root path of the navigation to generate a view for to set
176      */

177     public void setNavigationRootPath(String JavaDoc navRootPath) {
178
179         m_navRootPath = navRootPath;
180     }
181
182     private int calculateEndLevel() {
183
184         int result = 0;
185         if (m_navRootPath != null) {
186             // where are we? (start level)
187

188             StringTokenizer JavaDoc counter = new StringTokenizer JavaDoc(m_navRootPath, "/", false);
189             // one less as level 0 nav elements accepted is one level (depth 1).
190
result = counter.countTokens() - 1;
191             if (!CmsResource.isFolder(m_navRootPath)) {
192                 // cut stuff like system/workpalce/locale/de/help/index.html
193
result--;
194             }
195             result += m_depth;
196         }
197         if (result < 0) {
198             result = 0;
199         }
200         return result;
201     }
202
203     private void createNavigationInternal(StringBuffer JavaDoc buffer, List JavaDoc navElements) {
204
205         // take the element to render.
206
CmsJspNavElement element = (CmsJspNavElement)navElements.remove(0);
207         int elementLevel = element.getNavTreeLevel();
208         String JavaDoc spacing = getSpaces(elementLevel * 2);
209         // render element:
210
buffer.append(spacing).append("<li>\n");
211         buffer.append(spacing).append(" <a HREF=\"");
212         buffer.append(m_jsp.link(element.getResourceName()));
213         buffer.append("\" title=\"");
214         buffer.append(element.getNavText());
215         buffer.append("\"");
216         if (elementLevel == 1) {
217             buffer.append(" class=\"bold\"");
218         }
219         buffer.append(">");
220         buffer.append(element.getNavText());
221         buffer.append("</a>\n");
222
223         // peek at the next (list is depth - first by contract)
224
if (!navElements.isEmpty()) {
225             CmsJspNavElement child = (CmsJspNavElement)navElements.get(0);
226             int childLevel = child.getNavTreeLevel();
227             if (elementLevel < childLevel) {
228                 // next one goes down a level: it is a child by tree means
229
buffer.append(spacing).append(" <ul>\n");
230             } else if (elementLevel == childLevel) {
231                 // it is a sibling: close our list item, no recursion
232
buffer.append(spacing).append("</li>\n");
233             } else {
234                 // next element gets up one layer
235
// this has to happen because of the depth-first contract!
236
buffer.append(spacing).append(" </li>\n").append(spacing).append("</ul>\n");
237             }
238             createNavigationInternal(buffer, navElements);
239         } else {
240             // no more next elements: get back and close all lists (by using the recursion we are in)
241
buffer.append(spacing).append(" </li>\n").append(spacing).append("</ul>\n");
242         }
243
244     }
245 }
246
Popular Tags