[opencms-dev] Re: how to show 2 level navigation in one page

Shi Yusen shiys at langhua.cn
Mon Apr 17 14:47:24 CEST 2006


Hi Sean,

I think you can use readResources(String resourcename, CmsResourceFilter
filter, boolean readTree) in org.opencms.file.CmsObject.

The following is an example on how to use this method to build a selector in
html. Please note channel is a resource type extended from folder, you can
use folder to substitute it.
    /**
     * Get channel select html form for channels under folder. Includes all
channels in folder if search is true,
     * or only direct channels in folder if search is false.
     * 
     * @param folder the parent folder where to get channels
     * @param search if true, including all sub channels; if false,
including direct sub channels.
     * @param selected the selected folder
     * 
     * @return the html select form
     * @author Shi Yusen, shiys at langhua.cn
     */
    public String getChannelSelectForm(String folder, boolean search, String
selected) {
        
        StringBuffer result = new StringBuffer(128);
        result.append("<select name=\"resource\">");
        CmsObject cms = getCmsObject();
        CmsResourceFilter filter = CmsResourceFilter.DEFAULT;
        List channels = new ArrayList();
        try {
            filter =
filter.addRequireType(OpenCms.getResourceManager().getResourceType("channel"
).getTypeId());
            channels = cms.readResources(folder, filter, search);
        } catch (CmsLoaderException e) {
            // do nothing
        } catch (CmsException e) {
            // do nothing
        }
        
        int base = folder.split("/").length;
        if (base == 0) base = 1;
        
        // append the parent folder as the 1st option
        String title = property("Title", folder);
        if (CmsStringUtil.isEmptyOrWhitespaceOnly(title))
            title = property("NavText", folder);
        if (CmsStringUtil.isEmptyOrWhitespaceOnly(title))
            title = folder;
        result.append("\n<option value=\"");
        result.append(folder);
        result.append("\">");
        result.append(title);
        result.append("</option>");

        for (int i=0; i<channels.size(); i++) {
            CmsResource resource = (CmsResource) channels.get(i);
            String resourceName =
getRequestContext().removeSiteRoot(resource.getRootPath());
            title = property("Title", resourceName);
            if (CmsStringUtil.isEmptyOrWhitespaceOnly(title))
                title = property("NavText", resourceName);
            if (CmsStringUtil.isEmptyOrWhitespaceOnly(title))
                title = resourceName;
            int slashes = resourceName.split("/").length;
            for (int j=base; j<slashes; j++) {
                title = "  " + title;
            }
            result.append("\n<option value=\"");
            result.append(resourceName + "\"");
            if (CmsStringUtil.isNotEmpty(selected) &&
resourceName.equals(selected))
                result.append(" selected");
            result.append(">");
            result.append(title);
            result.append("</option>");
        }
        
        result.append("\n</select>");
        return result.toString();
    }

You can change this program according to your requirement.

Hope it's helpful to you. BTW, I would suggest you to download OpenCMS
source code from the cvs server. I'm sure you'll find it's a great treasure.

Regards,

Shi Yusen/Beijing Langhua Ltd.

-----邮件原件-----
发件人: opencms-dev-bounces at opencms.org [mailto:opencms-dev-bounces at opencms.
org] 代表 xiaowei.x.wang at accenture.com
发送时间: 2006年4月17日 9:37
收件人: opencms-dev at opencms.org
主题: [opencms-dev] RE: how to show 2 level navigation in one page

 Actually, I want to build a directory navigation in the page, to show all
the information in one page. I can't find an API like this. Do I have to
call "getFolder list", and implement it by myself? Thank you for any help.
Sean




More information about the opencms-dev mailing list