All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xlcloud.console.menu.MenuGroup Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2012 AMG.lab, a Bull Group Company
 * 
 * Licensed under the Apache License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 *
 */
package org.xlcloud.console.menu;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

import org.apache.commons.collections.CollectionUtils;
import org.xlcloud.console.context.EntitlementEngine;

/**
 * Represents the menu group
 * @author Tomek Adamczewski, AMG.net
 */
public class MenuGroup implements Comparable {

	private final SortedSet items;
	
	private final int index;
	
	private final String name;
	
	private  String bundle;
	
	public MenuGroup(MenuGroupLabel groupLabel) {
	    this(groupLabel.getPosition(), groupLabel.getLabel());
	}
	
	public MenuGroup(int index, String name) {
	    this.index = index;
		this.name = name;
		this.items = new TreeSet();
	}

    /**
     * The returned list is a defensive copy. To add items use addItem(MenuItem
     * item).
     * 
     * @return the items
     */
	public List getItems() {
		return new ArrayList<>(items);
	}
	
	/**
	 * Adds menu item to the group.
	 * 
	 * @param menuItem
	 */
	public void addItem(MenuItem menuItem) {
	    items.add(menuItem);
	}
	
    /**
     * Gets the value of {@link #index}.
     * @return value of {@link #index}
     */
    public int getIndex() {
        return index;
    }

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

    /**
     * @param path
     * @return
     */
    public MenuItem getActiveMenuItem(String path) {
        for(MenuItem menuItem : items) {
            if(menuItem.isActiveMenuItem(path)) {
                return menuItem;
            }
        }
        
        return null;
    }

    /**
     * @param entitlementEngine 
     * @return
     */
    public boolean hasAnyAllowedMenuItem(EntitlementEngine entitlementEngine) {
        return !getFilteredMenuItems(entitlementEngine).isEmpty();
    }

    /**
     * @param entitlementEngine
     * @return
     */
    public List getFilteredMenuItems(EntitlementEngine entitlementEngine) {
        List filteredMenuItems = new ArrayList();
        CollectionUtils.select(items, new MenuItemAllowancePredicate(entitlementEngine), filteredMenuItems);
        
        return filteredMenuItems;
    }

    /** {@inheritDoc} */
    @Override
    public int compareTo(MenuGroup menuGroup) {
        return index - menuGroup.getIndex();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy