⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmschnav.java

📁 cms是开源的框架
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * File   : $Source: /usr/local/cvs/opencms/src/org/opencms/workplace/commons/CmsChnav.java,v $
 * Date   : $Date: 2006/03/27 14:52:18 $
 * Version: $Revision: 1.23 $
 *
 * This library is part of OpenCms -
 * the Open Source Content Mananagement System
 *
 * Copyright (c) 2005 Alkacon Software GmbH (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 GmbH, 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 org.opencms.workplace.commons;

import org.opencms.file.CmsObject;
import org.opencms.file.CmsProperty;
import org.opencms.file.CmsPropertyDefinition;
import org.opencms.file.CmsResource;
import org.opencms.i18n.CmsEncoder;
import org.opencms.i18n.CmsMessages;
import org.opencms.jsp.CmsJspActionElement;
import org.opencms.jsp.CmsJspNavBuilder;
import org.opencms.jsp.CmsJspNavElement;
import org.opencms.main.CmsException;
import org.opencms.main.CmsLog;
import org.opencms.main.OpenCms;
import org.opencms.security.CmsPermissionSet;
import org.opencms.util.CmsStringUtil;
import org.opencms.workplace.CmsDialog;
import org.opencms.workplace.CmsWorkplace;
import org.opencms.workplace.CmsWorkplaceSettings;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import org.apache.commons.logging.Log;

/**
 * Provides methods for the change navigation dialog.<p> 
 * 
 * The following files use this class:
 * <ul>
 * <li>/commons/chnav.jsp
 * </ul>
 * <p>
 *
 * @author  Andreas Zahner 
 * 
 * @version $Revision: 1.23 $ 
 * 
 * @since 6.0.0 
 */
public class CmsChnav extends CmsDialog {

    /** Value for the action: change the navigation. */
    public static final int ACTION_CHNAV = 100;

    /** The dialog type. */
    public static final String DIALOG_TYPE = "chnav";
    
    /** Request parameter name for the navigation position. */
    public static final String PARAM_NAVPOS = "navpos";

    /** Request parameter name for the navigation text. */
    public static final String PARAM_NAVTEXT = "navtext";

    /** The log object for this class. */
    private static final Log LOG = CmsLog.getLog(CmsChnav.class);
    
    private String m_paramNavpos;

    private String m_paramNavtext;

    /**
     * Public constructor.<p>
     * 
     * @param jsp an initialized JSP action element
     */
    public CmsChnav(CmsJspActionElement jsp) {

        super(jsp);
    }

    /**
     * Public constructor with JSP variables.<p>
     * 
     * @param context the JSP page context
     * @param req the JSP request
     * @param res the JSP response
     */
    public CmsChnav(PageContext context, HttpServletRequest req, HttpServletResponse res) {

        this(new CmsJspActionElement(context, req, res));
    }

    /**
     * Builds the HTML for the select box of the navigation position.<p>
     * 
     * @param cms the CmsObject
     * @param filename the current file
     * @param attributes optional attributes for the &lt;select&gt; tag, do not add the "name" atribute!
     * @param messages the localized workplace messages
     * 
     * @return the HTML for a navigation position select box
     */
    public static String buildNavPosSelector(CmsObject cms, String filename, String attributes, CmsMessages messages) {

        List navList = new ArrayList();
        List options = new ArrayList();
        List values = new ArrayList();

        // get current file navigation element
        CmsJspNavElement curNav = CmsJspNavBuilder.getNavigationForResource(cms, filename);

        // get the parent folder of the current file
        filename = CmsResource.getParentFolder(filename);

        // get navigation of the current folder
        navList = CmsJspNavBuilder.getNavigationForFolder(cms, filename);
        float maxValue = 0;
        float nextPos = 0;

        // calculate value for the first navigation position
        float firstValue = 1;
        if (navList.size() > 0) {
            try {
                CmsJspNavElement ne = (CmsJspNavElement)navList.get(0);
                maxValue = ne.getNavPosition();
            } catch (Exception e) {
                // should usually never happen
                LOG.error(e.getLocalizedMessage());
            }
        }

        if (maxValue != 0) {
            firstValue = maxValue / 2;
        }

        // add the first entry: before first element
        options.add(messages.key(Messages.GUI_CHNAV_POS_FIRST_0));
        values.add(firstValue + "");

        // show all present navigation elements in box
        for (int i = 0; i < navList.size(); i++) {
            CmsJspNavElement ne = (CmsJspNavElement)navList.get(i);
            String navText = ne.getNavText();
            float navPos = ne.getNavPosition();
            // get position of next nav element
            nextPos = navPos + 2;
            if ((i + 1) < navList.size()) {
                nextPos = ((CmsJspNavElement)navList.get(i + 1)).getNavPosition();
            }
            // calculate new position of current nav element
            float newPos = (navPos + nextPos) / 2;

            // check new maxValue of positions and increase it
            if (navPos > maxValue) {
                maxValue = navPos;
            }

            // if the element is the current file, mark it in selectbox
            if (curNav.getNavText().equals(navText) && curNav.getNavPosition() == navPos) {
                options.add(CmsEncoder.escapeHtml(messages.key(Messages.GUI_CHNAV_POS_CURRENT_1, new Object[] {ne.getFileName()})));
                values.add("-1");
            } else {
                options.add(CmsEncoder.escapeHtml(navText + " [" + ne.getFileName() + "]"));
                values.add(newPos + "");
            }
        }

        // add the entry: at the last position
        options.add(messages.key(Messages.GUI_CHNAV_POS_LAST_0));
        values.add((maxValue + 1) + "");

        // add the entry: no change
        options.add(messages.key(Messages.GUI_CHNAV_NO_CHANGE_0));
        if (curNav.getNavPosition() == Float.MAX_VALUE) {
            // current resource has no valid position, use "last position"
            values.add((maxValue + 1) + "");
        } else {
            // current resource has valid position, use "-1" for no change
            values.add("-1");
        }

        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(attributes)) {
            attributes = " " + attributes;
        } else {
            attributes = "";
        }
        return CmsWorkplace.buildSelect(
            "name=\"" + PARAM_NAVPOS + "\"" + attributes,
            options,
            values,
            values.size() - 1,
            true);
    }

    /**
     * Performs the navigation change.<p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -