KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > jsp > CmsJspNavElement


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/jsp/CmsJspNavElement.java,v $
3  * Date : $Date: 2005/06/28 13:30:16 $
4  * Version: $Revision: 1.15 $
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  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.jsp;
33
34 import org.opencms.file.CmsPropertyDefinition;
35 import org.opencms.file.CmsResource;
36 import org.opencms.i18n.CmsMessages;
37
38 import java.util.Map JavaDoc;
39
40 /**
41  * Bean to collect navigation information from a resource in the OpenCms VFS.<p>
42  *
43  * Each nav element contains a number of information about a VFS resource,
44  * obtained either from the resources properties or attributes.
45  * You can use this information to generate a HTML navigation for
46  * files in the VFS in your template.<p>
47  *
48  * @author Alexander Kandzior
49  *
50  * @version $Revision: 1.15 $
51  *
52  * @since 6.0.0
53  *
54  * @see org.opencms.jsp.CmsJspNavBuilder
55  */

56 public class CmsJspNavElement implements Comparable JavaDoc {
57
58     private String JavaDoc m_fileName;
59     private Boolean JavaDoc m_hasNav;
60     private int m_navTreeLevel = Integer.MIN_VALUE;
61     private float m_position;
62     private Map JavaDoc m_properties;
63     private String JavaDoc m_resource;
64     private String JavaDoc m_text;
65
66     /**
67      * Empty constructor required for every JavaBean, does nothing.<p>
68      *
69      * Call one of the init methods afer you have created an instance
70      * of the bean. Instead of using the constructor you should use
71      * the static factory methods provided by this class to create
72      * navigation beans that are properly initialized with current
73      * OpenCms context.
74      *
75      * @see CmsJspNavBuilder#getNavigationForResource()
76      * @see CmsJspNavBuilder#getNavigationForFolder()
77      * @see CmsJspNavBuilder#getNavigationTreeForFolder(int, int)
78      */

79     public CmsJspNavElement() {
80
81         // empty
82
}
83
84     /**
85      * Create a new instance of the bean and calls the init method
86      * with the provided parametes.<p>
87      *
88      * @param resource will be passed to <code>init</code>
89      * @param properties will be passed to <code>init</code>
90      *
91      * @see #init(String, Map)
92      */

93     public CmsJspNavElement(String JavaDoc resource, Map JavaDoc properties) {
94
95         init(resource, properties, -1);
96     }
97
98     /**
99      * Create a new instance of the bean and calls the init method
100      * with the provided parametes.<p>
101      *
102      * @param resource will be passed to <code>init</code>
103      * @param properties will be passed to <code>init</code>
104      * @param navTreeLevel will be passed to <code>init</code>
105      *
106      * @see #init(String, Map, int)
107      */

108     public CmsJspNavElement(String JavaDoc resource, Map JavaDoc properties, int navTreeLevel) {
109
110         init(resource, properties, navTreeLevel);
111     }
112
113     /**
114      * @see java.lang.Comparable#compareTo(Object)
115      */

116     public int compareTo(Object JavaDoc obj) {
117
118         if (obj == this) {
119             return 0;
120         }
121         if (obj instanceof CmsJspNavElement) {
122             float pos = ((CmsJspNavElement)obj).getNavPosition();
123             // please note: can't just substract and cast to int here because of float precision loss
124
if (m_position == pos) {
125                 return 0;
126             }
127             return (m_position < pos) ? -1 : 1;
128         }
129         return 0;
130     }
131
132     /**
133      * @see java.lang.Object#equals(Object)
134      */

135     public boolean equals(Object JavaDoc obj) {
136
137         if (obj == this) {
138             return true;
139         }
140         if (obj instanceof CmsJspNavElement) {
141             return ((CmsJspNavElement)obj).m_resource.equals(m_resource);
142         }
143         return false;
144     }
145
146     /**
147      * Returns the value of the property PROPERTY_DESCRIPTION of this nav element,
148      * or <code>null</code> if this property is not set.<p>
149      *
150      * @return the value of the property PROPERTY_DESCRIPTION of this nav element
151      * or <code>null</code> if this property is not set
152      */

153     public String JavaDoc getDescription() {
154
155         return (String JavaDoc)m_properties.get(CmsPropertyDefinition.PROPERTY_DESCRIPTION);
156     }
157
158     /**
159      * Returns the filename of the nav element, i.e.
160      * the name of the nav resource without any path information.<p>
161      *
162      * @return the filename of the nav element, i.e.
163      * the name of the nav resource without any path information
164      */

165     public String JavaDoc getFileName() {
166
167         if (m_fileName == null) {
168             // use "lazy initialiting"
169
if (!m_resource.endsWith("/")) {
170                 m_fileName = m_resource.substring(m_resource.lastIndexOf("/") + 1, m_resource.length());
171             } else {
172                 m_fileName = m_resource.substring(
173                     m_resource.substring(0, m_resource.length() - 1).lastIndexOf("/") + 1,
174                     m_resource.length());
175             }
176         }
177         return m_fileName;
178     }
179
180     /**
181      * Returns the value of the property <code>{@link CmsPropertyDefinition#PROPERTY_NAVINFO}</code> of this nav element,
182      * or <code>null</code> if this property is not set.<p>
183      *
184      * @return the value of the property or <code>null</code> if this property is not set
185      */

186     public String JavaDoc getInfo() {
187
188         return (String JavaDoc)m_properties.get(CmsPropertyDefinition.PROPERTY_NAVINFO);
189     }
190
191     /**
192      * Returns the value of the property <code>{@link CmsPropertyDefinition#PROPERTY_LOCALE}</code> of this nav element,
193      * or <code>null</code> if this property is not set.<p>
194      *
195      * @return the value of the property or <code>null</code> if this property is not set
196      */

197     public String JavaDoc getLocale() {
198
199         return (String JavaDoc)m_properties.get(CmsPropertyDefinition.PROPERTY_LOCALE);
200     }
201
202     /**
203      * Returns the value of the property <code>{@link CmsPropertyDefinition#PROPERTY_NAVIMAGE}</code> of this nav element,
204      * or <code>null</code> if this property is not set.<p>
205      *
206      * @return the value of the property or <code>null</code> if this property is not set
207      */

208     public String JavaDoc getNavImage() {
209
210         return (String JavaDoc)m_properties.get(CmsPropertyDefinition.PROPERTY_NAVIMAGE);
211     }
212
213     /**
214      * Returns the value of the property C_PROPERTY_NAVPOS converted to a <code>float</code>,
215      * or a value of <code>Float.MAX_VALUE</code> if the nav position property is not
216      * set (or not a valid number) for this resource.<p>
217      *
218      * @return float the value of the property C_PROPERTY_NAVPOS converted to a <code>float</code>,
219      * or a value of <code>Float.MAX_VALUE</code> if the nav position property is not
220      * set (or not a valid number) for this resource
221      */

222     public float getNavPosition() {
223
224         return m_position;
225     }
226
227     /**
228      * Returns the value of the property PROPERTY_NAVTEXT of this nav element,
229      * or a warning message if this property is not set
230      * (this method will never return <code>null</code>).<p>
231      *
232      * @return the value of the property PROPERTY_NAVTEXT of this nav element,
233      * or a warning message if this property is not set
234      * (this method will never return <code>null</code>)
235      */

236     public String JavaDoc getNavText() {
237
238         if (m_text == null) {
239             // use "lazy initialiting"
240
m_text = (String JavaDoc)m_properties.get(CmsPropertyDefinition.PROPERTY_NAVTEXT);
241             if (m_text == null) {
242                 m_text = CmsMessages.formatUnknownKey(CmsPropertyDefinition.PROPERTY_NAVTEXT);
243             }
244         }
245         return m_text;
246     }
247
248     /**
249      * Returns the nav tree level of this resource.<p>
250      *
251      * @return int the nav tree level of this resource
252      */

253     public int getNavTreeLevel() {
254
255         if (m_navTreeLevel < 0) {
256             // use "lazy initialiting"
257
m_navTreeLevel = CmsResource.getPathLevel(m_resource);
258         }
259         return m_navTreeLevel;
260     }
261
262     /**
263      * Returns the name of the parent folder of the resource of this nav element.<p>
264      *
265      * @return the name of the parent folder of the resource of this nav element
266      */

267     public String JavaDoc getParentFolderName() {
268
269         return CmsResource.getParentFolder(m_resource);
270     }
271
272     /**
273      * Returns the original Hashtable of all file properties of the resource that
274      * the nav element belongs to.<p>
275      *
276      * Please note that the original reference is returned, so be careful when making
277      * changes to the Hashtable.<p>
278      *
279      * @return the original Hashtable of all file properties of the resource that
280      * the nav element belongs to
281      */

282     public Map JavaDoc getProperties() {
283
284         return m_properties;
285     }
286
287     /**
288      * Returns the value of the selected property from this nav element.<p>
289      *
290      * The nav element contains a hash of all file properties of the resource that
291      * the nav element belongs to.<p>
292      *
293      * @param key the property name to look up
294      * @return the value of the selected property
295      */

296     public String JavaDoc getProperty(String JavaDoc key) {
297
298         return (String JavaDoc)m_properties.get(key);
299     }
300
301     /**
302      * Returns the resource name this nav element was initalized with.<p>
303      *
304      * @return the resource name this nav element was initalized with
305      */

306     public String JavaDoc getResourceName() {
307
308         return m_resource;
309     }
310
311     /**
312      * Returns the value of the property PROPERTY_TITLE of this nav element,
313      * or <code>null</code> if this property is not set.<p>
314      *
315      * @return the value of the property PROPERTY_TITLE of this nav element
316      * or <code>null</code> if this property is not set
317      */

318     public String JavaDoc getTitle() {
319
320         return (String JavaDoc)m_properties.get(CmsPropertyDefinition.PROPERTY_TITLE);
321     }
322
323     /**
324      * @see java.lang.Object#hashCode()
325      */

326     public int hashCode() {
327
328         return super.hashCode();
329     }
330
331     /**
332      * Same as calling {@link #init(String, Map, int)
333      * init(String, Hashtable, -1)}.<p>
334      *
335      * @param resource the name of the resource to extract the navigation
336      * information from
337      * @param properties the properties of the resource read from the vfs
338      */

339     public void init(String JavaDoc resource, Map JavaDoc properties) {
340
341         init(resource, properties, -1);
342     }
343
344     /**
345      * Initialized the member variables of this bean with the values
346      * provided.<p>
347      *
348      * A resource will be in the nav if at least one of the two properties
349      * <code>I_CmsConstants.PROPERTY_NAVTEXT</code> or
350      * <code>I_CmsConstants.PROPERTY_NAVPOS</code> is set. Otherwise
351      * it will be ignored.
352      *
353      * This bean does provides static methods to create a new instance
354      * from the context of a current CmsObject. Call these static methods
355      * in order to get a properly initialized bean.
356      *
357      * @param resource the name of the resource to extract the navigation
358      * information from
359      * @param properties the properties of the resource read from the vfs
360      * @param navTreeLevel tree level of this resource, for building
361      * navigation trees
362      *
363      * @see CmsJspNavBuilder#getNavigationForResource()
364      */

365     public void init(String JavaDoc resource, Map JavaDoc properties, int navTreeLevel) {
366
367         m_resource = resource;
368         m_properties = properties;
369         m_navTreeLevel = navTreeLevel;
370         // init the position value
371
m_position = Float.MAX_VALUE;
372         try {
373             m_position = Float.parseFloat((String JavaDoc)m_properties.get(CmsPropertyDefinition.PROPERTY_NAVPOS));
374         } catch (Exception JavaDoc e) {
375             // m_position will have Float.MAX_VALUE, so nevigation element will
376
// appear last in navigation
377
}
378     }
379
380     /**
381      * Returns <code>true</code> if this nav element describes a folder, <code>false</code>
382      * otherwise.<p>
383      *
384      * @return <code>true</code> if this nav element describes a folder, <code>false</code>
385      * otherwise.<p>
386      */

387     public boolean isFolderLink() {
388
389         return m_resource.endsWith("/");
390     }
391
392     /**
393      * Returns <code>true</code> if this nav element is in the navigation, <code>false</code>
394      * otherwise.<p>
395      *
396      * A resource is considered to be in the navigation, if <ol>
397      * <li>it has the property PROPERTY_NAVTEXT set
398      * <li><em>or</em> it has the property PROPERTY_NAVPOS set
399      * <li><em>and</em> it is not a temporary file that contains a '~' in it's filename.</ol>
400      *
401      * @return <code>true</code> if this nav element is in the navigation, <code>false</code>
402      * otherwise
403      */

404     public boolean isInNavigation() {
405
406         if (m_hasNav == null) {
407             // use "lazy initialiting"
408
Object JavaDoc o1 = m_properties.get(CmsPropertyDefinition.PROPERTY_NAVTEXT);
409             Object JavaDoc o2 = m_properties.get(CmsPropertyDefinition.PROPERTY_NAVPOS);
410             m_hasNav = new Boolean JavaDoc(((o1 != null) || (o2 != null)) && (m_resource.indexOf('~') < 0));
411         }
412         return m_hasNav.booleanValue();
413     }
414
415     /**
416      * Sets the value that will be returned by the {@link #getNavPosition()}
417      * method of this class.<p>
418      *
419      * @param value the value to set
420      */

421     public void setNavPosition(float value) {
422
423         m_position = value;
424     }
425 }
Popular Tags