menu.java

来自「开源项目CRM之OpenCustomer」· Java 代码 · 共 265 行

JAVA
265
字号
/*******************************************************************************
 * ***** BEGIN LICENSE BLOCK Version: MPL 1.1
 * 
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * 
 * The Original Code is the OpenCustomer CRM.
 * 
 * The Initial Developer of the Original Code is Thomas Bader (Bader & Jene
 * Software-Ingenieurb黵o). Portions created by the Initial Developer are
 * Copyright (C) 2005 the Initial Developer. All Rights Reserved.
 * 
 * Contributor(s): Thomas Bader <thomas.bader@bader-jene.de>
 * 
 * ***** END LICENSE BLOCK *****
 */

package org.opencustomer.util.menu;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.log4j.Logger;
import org.apache.struts.config.ActionConfig;
import org.apache.struts.config.ModuleConfig;
import org.opencustomer.application.auth.Authenticator;
import org.opencustomer.application.auth.Right;

public abstract class Menu
{
    private static final Logger log = Logger.getLogger(Menu.class);

    private ModuleConfig moduleConfig;

    private List<MenuItem> items = new ArrayList<MenuItem>();

    private Set<String> pathElements = new HashSet<String>();

    public Menu(ModuleConfig moduleConfig)
    {
        this.moduleConfig = moduleConfig;

        init();
    }

    protected abstract void init();

    protected void infoSubItems(MenuItem item, int level)
    {
        final String indent = "   ";
        Iterator<MenuItem> it = item.getItems().iterator();
        while (it.hasNext())
        {
            MenuItem subItem = it.next();
            StringBuffer indentation = new StringBuffer();
            for (int i = 0; i < level; i++)
                indentation.append(indent);
            log.info(indentation.toString() + subItem.getMessageKey() + " " + subItem.getPath());
            infoSubItems(subItem, level + 1);
        }
    }

    public void customize(Authenticator auth)
    {
        if (log.isDebugEnabled())
            log.debug("customize menu");
        customize(items, auth);
    }

    private int customize(List<MenuItem> items, Authenticator auth)
    {
        int jspItems = 0;

        Iterator<MenuItem> it = items.iterator();
        while (it.hasNext())
        {
            MenuItem item = it.next();
            if (item.isJspInclude())
            {
                if (log.isDebugEnabled())
                    log.debug("use jsp include: " + item);
                jspItems++;
            }
            else if (item.getItems().isEmpty())
            {
                if (!auth.isValid(item.getRights()))
                {
                    if (log.isDebugEnabled())
                        log.debug("remove item with invalid rights: " + item);

                    it.remove();
                }
            }
            else
            {
                int jsps = customize(item.getItems(), auth);
                if (item.getItems().isEmpty())
                {
                    if (log.isDebugEnabled())
                        log.debug("remove item with no subItems:" + item);

                    it.remove();
                }
                else if (item.getItems().size() == jsps)
                {
                    if (log.isDebugEnabled())
                        log.debug("remove item due to only jsp items found on next level");

                    it.remove();
                }
                else
                {
                    item.setPath(getPath(item));
                    if (log.isDebugEnabled())
                        log.debug("set path for non leaf: " + item.getPath());
                }
            }
        }

        registerPathElements(items);
        if (log.isDebugEnabled())
            log.debug("registered " + pathElements.size() + " path element(s)");

        return jspItems;
    }

    private void registerPathElements(List<MenuItem> items)
    {
        Iterator<MenuItem> it = items.iterator();
        while (it.hasNext())
        {
            MenuItem item = it.next();
            if (item.getItems().isEmpty())
            {
                if (log.isDebugEnabled())
                    log.debug("add path element: " + item.getPath());
                pathElements.add(item.getPath());
            }
            else
                registerPathElements(item.getItems());
        }
    }

    public void activate(String path)
    {
        if (log.isDebugEnabled())
            log.debug("active path: " + path);

        boolean containsPath = pathElements.contains(path);

        if (log.isDebugEnabled())
            log.debug("path found in menu: " + containsPath);

        if (containsPath)
            activatePath(items, path);
    }

    private boolean activatePath(List<MenuItem> items, String path)
    {
        boolean activated = false;

        Iterator<MenuItem> it = items.iterator();
        while (it.hasNext())
        {
            MenuItem item = it.next();
            if (item.getItems().isEmpty())
                item.setActive(item.getPath().equals(path));
            else
                item.setActive(activatePath(item.getItems(), path));

            if (item.isActive())
                activated = true;

            if (log.isDebugEnabled())
                log.debug("set (" + activated + "): " + item);
        }

        return activated;
    }

    private String getPath(MenuItem item)
    {
        String action = null;

        if (item.getPath() != null)
            action = item.getPath();
        else if (!item.getItems().isEmpty())
            action = getPath(item.getItems().get(0));
        else
            throw new IllegalArgumentException("configuration invalid ... missing action or subItems");

        return action;
    }

    protected void addSubItem(MenuItem item, String action, String messageKey)
    {
        ActionConfig config = moduleConfig.findActionConfig(action);

        MenuItem subItem = new MenuItem(action, messageKey, toRightArray(config.getRoleNames()));

        addSubItem(item, subItem);
    }

    protected void addSubItem(MenuItem item, MenuItem subItem)
    {
        item.getItems().add(subItem);
    }

    private Right[] toRightArray(String[] roles)
    {
        List<Right> rights = new ArrayList<Right>();

        for (int i = 0; i < roles.length; i++)
        {
            try
            {
                rights.add(Right.parseRight(roles[i]));
            }
            catch (IllegalArgumentException e)
            {
                log.error("found invalid right", e);
            }
        }

        return rights.toArray(new Right[rights.size()]);
    }

    public String getFirstPath()
    {
        if (items.isEmpty())
            return null;
        else
            return getPath(items.get(0));
    }

    public List<MenuItem> getItems()
    {
        return items;
    }

    public void setItems(List<MenuItem> items)
    {
        this.items = items;
    }

    public String toString()
    {
        ToStringBuilder builder = new ToStringBuilder(this);

        builder.append("items=" + items);

        return builder.toString();
    }

}

⌨️ 快捷键说明

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