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

org.xlcloud.console.menu.MenuItem 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.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.xlcloud.service.Entitlement;
import org.xlcloud.service.HttpAction;

/**
 * Represents single menu item
 * 
 * @author Tomek Adamczewski, AMG.net
 */
public class MenuItem implements Serializable, Comparable {

    private static final long serialVersionUID = 5650171494923626329L;
    
    private final int index;

    private final String actionLink;

    private final String label;
    
    private final String bundle;

    private final MenuGroupLabel groupLabel;

    List entitlements;

    private final String activateOnPathRegexp;

    /**
     * Menu item constructor
     * 
     * @param index
     *            position in the group
     * @param actionLink
     *            menu item action link
     * @param label
     *            menu item label
     * @param groupLabel
     *            enum that indicates to which group menu item belongs to
     * @param httpAction
     *            menu item http action
     * @param entitlementPath
     *            menu item entitlement path
     * @param activateOnPathRegexp
     *            regexp that indicates on which action menu item is active
     */
    public MenuItem( int index, String actionLink, String label, String bundle, MenuGroupLabel groupLabel, HttpAction httpAction, String entitlementPath,
            String activateOnPathRegexp ) {
        this.index = index;
        this.actionLink = actionLink;
        this.label = label;
        this.bundle = bundle;
        this.groupLabel = groupLabel;
        this.activateOnPathRegexp = activateOnPathRegexp;

        Entitlement entitlement = new Entitlement();
        entitlement.getAction().add(httpAction);
        entitlement.setResource(entitlementPath);
        entitlements = new ArrayList();
        entitlements.add(entitlement);
    }

    public MenuItem( int index, String actionLink, String label, String bundle, MenuGroupLabel groupLabel, String activateOnPathRegexp,
            Entitlement... entitlements ) {
        this.index = index;
        this.actionLink = actionLink;
        this.label = label;
        this.bundle = bundle;
        this.groupLabel = groupLabel;
        this.activateOnPathRegexp = activateOnPathRegexp;
        this.entitlements = new ArrayList();
        this.entitlements.addAll(Arrays.asList(entitlements));
    }

    /**
     * @return the actionLink
     */
    public String getActionLink() {
        return actionLink;
    }

    /**
     * @return the label
     */
    public String getLabel() {
        return label;
    }
    
    public String getBundle() {
        return bundle;
    }

    /**
     * @return the groupLabel
     */
    public MenuGroupLabel getGroupLabel() {
        return groupLabel;
    }

    /**
     * Gets the value of {@link #activateOnPathRegexp}.
     * 
     * @return value of {@link #activateOnPathRegexp}
     */
    public String getActivateOnPath() {
        return activateOnPathRegexp;
    }

    /**
     * @param path
     *            current path
     * @return {code @true} if the given path matches to the
     *         {@link #activateOnPathRegexp} assigned to this menu item, it
     *         returns: {@code false} otherwise
     */
    public boolean isActiveMenuItem(String path) {
        return path.matches(activateOnPathRegexp);
    }

    /**
     * Gets the value of {@link #entitlements}.
     * 
     * @return value of {@link #entitlements}
     */
    public List getEntitlements() {
        return entitlements;
    }

    /**
     * Sets the value of {@link #entitlements}.
     * 
     * @param entitlements
     *            - value
     */
    public void setEntitlements(List entitlements) {
        this.entitlements = entitlements;
    }

    /**
     * Gets the value of {@link #index}.
     * @return value of {@link #index}
     */
    public int getIndex() {
        return index;
    }

    /** {@inheritDoc} */
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((actionLink == null) ? 0 : actionLink.hashCode());
        result = prime * result + ((activateOnPathRegexp == null) ? 0 : activateOnPathRegexp.hashCode());
        result = prime * result + ((entitlements == null) ? 0 : entitlements.hashCode());
        result = prime * result + ((groupLabel == null) ? 0 : groupLabel.hashCode());
        result = prime * result + index;
        result = prime * result + ((label == null) ? 0 : label.hashCode());
        return result;
    }

    /** {@inheritDoc} */
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        MenuItem other = (MenuItem) obj;
        if (actionLink == null) {
            if (other.actionLink != null)
                return false;
        } else if (!actionLink.equals(other.actionLink))
            return false;
        if (activateOnPathRegexp == null) {
            if (other.activateOnPathRegexp != null)
                return false;
        } else if (!activateOnPathRegexp.equals(other.activateOnPathRegexp))
            return false;
        if (entitlements == null) {
            if (other.entitlements != null)
                return false;
        } else if (!entitlements.equals(other.entitlements))
            return false;
        if (groupLabel == null) {
            if (other.groupLabel != null)
                return false;
        } else if (!groupLabel.equals(other.groupLabel))
            return false;
        if (index != other.index)
            return false;
        if (label == null) {
            if (other.label != null)
                return false;
        } else if (!label.equals(other.label))
            return false;
        return true;
    }

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy