📄 cmsjspnavbuilder.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/com/opencms/flex/jsp/CmsJspNavBuilder.java,v $
* Date : $Date: 2003/06/16 09:22:04 $
* Version: $Revision: 1.5.2.2 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2002 - 2003 Alkacon Software (http://www.alkacon.com)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about Alkacon Software, please see the
* company website: http://www.alkacon.com
*
* For further information about OpenCms, please see the
* project website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.opencms.flex.jsp;
import com.opencms.file.CmsFile;
import com.opencms.file.CmsObject;
import com.opencms.file.CmsResource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;
/**
* Bean to provide a convenient way to build navigation structures based on
* {@link com.opencms.flex.jsp.CmsJspNavElement}.<p>
*
* @author Alexander Kandzior (a.kandzior@alkacon.com)
* @version $Revision: 1.5.2.2 $
*
* @see com.opencms.flex.jsp.CmsJspNavElement
*
* @since 5.0
*/
public class CmsJspNavBuilder {
// Member variables
private CmsObject m_cms = null;
private String m_requestUri = null;
private String m_requestUriFolder = null;
/**
* Empty constructor, so that this bean can be initialized from a JSP.<p>
*
* @see java.lang.Object#Object()
*/
public CmsJspNavBuilder() {}
/**
* Default constructor.<p>
*
* @param cms context provider for the current request
*/
public CmsJspNavBuilder(CmsObject cms) {
init(cms);
}
/**
* Initiliazes this bean.<p>
*
* @param cms context provider for the current request
*/
public void init(CmsObject cms) {
m_cms = cms;
m_requestUri = m_cms.getRequestContext().getUri();
m_requestUriFolder = CmsResource.getPath(m_requestUri);
}
/**
* Returns a CmsJspNavElement for the resource of the current request URI.<p>
*
* @return CmsJspNavElement a CmsJspNavElement for the resource of the current request URI
*/
public CmsJspNavElement getNavigationForResource() {
return getNavigationForResource(m_cms, m_requestUri);
}
/**
* Returns a CmsJspNavElement for the named resource.<p>
*
* @param resource the resource name to get the nav information for,
* must be a full path name, e.g. "/docs/index.html".
* @return CmsJspNavElement a CmsJspNavElement for the given resource
*/
public CmsJspNavElement getNavigationForResource(String resource) {
return getNavigationForResource(m_cms, resource);
}
/**
* Returns a CmsJspNavElement for the named resource.<p>
*
* @param cms context provider for the current request
* @param resource the resource name to get the nav information for,
* must be a full path name, e.g. "/docs/index.html".
* @return a CmsJspNavElement for the given resource
*/
public static CmsJspNavElement getNavigationForResource(CmsObject cms, String resource) {
Map properties;
try {
properties = cms.readProperties(resource);
} catch (Exception e) {
return null;
}
int level = CmsResource.getPathLevel(resource);
if (resource.endsWith("/")) level--;
return new CmsJspNavElement(resource, properties, level);
}
/**
* Collect all navigation elements from the files of the folder of the current request URI,
* navigation elements are of class CmsJspNavElement.<p>
*
* @param cms context provider for the current request
* @param folder the selected folder
* @return A sorted (ascending to nav position) ArrayList of navigation elements.
*/
public ArrayList getNavigationForFolder() {
return getNavigationForFolder(m_cms, m_requestUriFolder);
}
/**
* Collect all navigation elements from the files in the given folder,
* navigation elements are of class CmsJspNavElement.<p>
*
* @param folder the selected folder
* @return A sorted (ascending to nav position) ArrayList of navigation elements.
*/
public ArrayList getNavigationForFolder(String folder) {
return getNavigationForFolder(m_cms, folder);
}
/**
* Collect all navigation elements from the files in the given folder,
* navigation elements are of class CmsJspNavElement.<p>
*
* @param cms context provider for the current request
* @param folder the selected folder
* @return A sorted (ascending to nav position) ArrayList of navigation elements.
*/
public static ArrayList getNavigationForFolder(CmsObject cms, String folder) {
folder = CmsFile.getPath(folder);
ArrayList list = new ArrayList();
Vector v = null, dir = null;
try {
// v = cms.getResourcesInFolder(folder);
v = cms.getFilesInFolder(folder);
dir = cms.getSubFolders(folder);
} catch (Exception e) {
return new ArrayList(0);
}
v.addAll(dir);
Iterator i = v.iterator();
while (i.hasNext()) {
CmsResource r = (CmsResource)i.next();
if (r.getState() != CmsResource.C_STATE_DELETED) {
CmsJspNavElement element = getNavigationForResource(cms, r.getAbsolutePath());
if ((element != null) && element.isInNavigation()) {
list.add(element);
}
}
}
Collections.sort(list);
return list;
}
/**
* Build a navigation for the folder that is either minus levels up
* from of the folder of the current request URI, or that is plus levels down from the
* root folder towards the current request URI.<p>
*
* If level is set to zero the root folder is used by convention.<p>
*
* @param level if negative, walk this many levels up, if positive, walk this many
* levels down from root folder
*/
public ArrayList getNavigationForFolder(int level) {
return getNavigationForFolder(m_cms, m_requestUriFolder, level);
}
/**
* Build a navigation for the folder that is either minus levels up
* from the given folder, or that is plus levels down from the
* root folder towards the given folder.<p>
*
* If level is set to zero the root folder is used by convention.<p>
*
* @param folder the selected folder
* @param level if negative, walk this many levels up, if positive, walk this many
* levels down from root folder
*/
public ArrayList getNavigationForFolder(String folder, int level) {
return getNavigationForFolder(m_cms, folder, level);
}
/**
* Build a navigation for the folder that is either minus levels up
* from the given folder, or that is plus levels down from the
* root folder towards the given folder.<p>
*
* If level is set to zero the root folder is used by convention.<p>
*
* @param cms context provider for the current request
* @param folder the selected folder
* @param level if negative, walk this many levels up, if positive, walk this many
* levels down from root folder
*/
public static ArrayList getNavigationForFolder(CmsObject cms, String folder, int level) {
folder = CmsFile.getPath(folder);
// If level is one just use root folder
if (level == 0) return getNavigationForFolder(cms, "/");
String navfolder = CmsResource.getPathPart(folder, level);
// If navfolder found use it to build navigation
if (navfolder != null) return getNavigationForFolder(cms, navfolder);
// Nothing found, return empty list
return new ArrayList(0);
}
/**
* @see #getNavigationTreeForFolder(CmsObject, String, int, int)
*/
public ArrayList getNavigationTreeForFolder(int startlevel, int endlevel) {
return getNavigationTreeForFolder(m_cms, m_requestUriFolder, startlevel, endlevel);
}
/**
* @see #getNavigationTreeForFolder(CmsObject, String, int, int)
*/
public ArrayList getNavigationTreeForFolder(String folder, int startlevel, int endlevel) {
return getNavigationTreeForFolder(m_cms, folder, startlevel, endlevel);
}
/**
* Builds a tree navigation for the folders between the provided start and end level.<p>
*
* A tree navigation includes all nav elements that are required to display a tree structure.
* However, the data structure is a simple list.
* Each of the nav elements in the list has the {@link CmsJspNavElement#getNavTreeLevel()} set
* to the level it belongs to. Use this information to distinguish between the nav levels.<p>
*
* @param cms context provider for the current request
* @param folder the selected folder
* @param startlevel the start level
* @param endlevel the end level
* @return a sorted list of nav elements with the nav tree level property set
*/
public static ArrayList getNavigationTreeForFolder(CmsObject cms, String folder, int startlevel, int endlevel) {
folder = CmsFile.getPath(folder);
// Make sure start and end level make sense
if (endlevel < startlevel) return new ArrayList(0);
int currentlevel = CmsResource.getPathLevel(folder);
if (currentlevel < endlevel) endlevel = currentlevel;
if (startlevel == endlevel) return getNavigationForFolder(cms, CmsResource.getPathPart(folder, startlevel), startlevel);
ArrayList result = new ArrayList(0);
Iterator it = null;
float parentcount = 0;
for (int i=startlevel; i<=endlevel; i++) {
String currentfolder = CmsResource.getPathPart(folder, i);
ArrayList entries = getNavigationForFolder(cms, currentfolder);
// Check for parent folder
if (parentcount > 0) {
it = entries.iterator();
while (it.hasNext()) {
CmsJspNavElement e = (CmsJspNavElement)it.next();
e.setNavPosition(e.getNavPosition() + parentcount);
}
}
// Add new entries to result
result.addAll(entries);
Collections.sort(result);
// Finally spread the values of the nav items so that there is enough room for further items.
float pos = 0;
int count = 0;
it = result.iterator();
String nextfolder = CmsResource.getPathPart(folder, i+1);
parentcount = 0;
while (it.hasNext()) {
pos = 10000 * (++count);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -