com.sfs.beans.ActionsBean.java Source code

Java tutorial

Introduction

Here is the source code for com.sfs.beans.ActionsBean.java

Source

/*******************************************************************************
 * Copyright (c) 2009 David Harrison.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl-3.0.html
 *
 * Contributors:
 *     David Harrison - initial API and implementation
 ******************************************************************************/
package com.sfs.beans;

import java.util.ArrayList;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;

/**
 * The Class ActionsBean.
 *
 * @author David Harrison
 */
public class ActionsBean extends Object implements Serializable {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 1L;

    /** The actions. */
    private ArrayList<ActionBean> actions = new ArrayList<ActionBean>();

    /**
     * Adds an action to the ActionBean with the supplied parameters.
     *
     * @param name the name
     * @param url the url
     * @param onClick the on click
     * @param shortcutKey the shortcut key
     * @param id the id
     * @param tool tip the toolTip
     *
     * @throws Exception the exception
     */
    public final void addAction(final String name, final String url, final String onClick, final String shortcutKey,
            final String id, final String toolTip) throws Exception {

        ActionBean action = newAction(name, url, onClick, shortcutKey, id, toolTip);

        this.actions.add(action);
    }

    /**
     * Adds the action.
     *
     * @param name the name
     * @param url the url
     * @param onclick the onclick
     * @param shortcutKey the shortcut key
     *
     * @throws Exception the exception
     */
    public final void addAction(final String name, final String url, final String onclick, final String shortcutKey)
            throws Exception {
        // No id or tooltip value supplied
        addAction(name, url, onclick, shortcutKey, "", "");
    }

    /**
     * Adds the action.
     *
     * @param name the name
     * @param subActions the sub actions
     */
    public final void addAction(final String name, final ActionsBean subActions) {
        ActionBean action = new ActionBean();
        action.setName(name);
        action.setSubActions(subActions);

        this.actions.add(action);
    }

    /**
     * Gets the action.
     *
     * @param index the index
     *
     * @return the action
     *
     * @throws Exception the exception
     */
    public final ActionBean getAction(final int index) throws Exception {
        if (index > this.actions.size()) {
            throw new Exception("Sorry the requested action is out of bounds");
        }
        return this.actions.get(index);
    }

    /**
     * Size.
     *
     * @return the int
     */
    public final int size() {
        return this.actions.size();
    }

    /**
     * Create a new action bean.
     *
     * @param name the name
     * @param url the url
     * @param onClick the on click
     * @param shortcutKey the shortcut key
     * @param id the id
     * @param toolTip the tool tip
     * @return the action bean
     * @throws Exception the exception
     */
    private ActionBean newAction(final String name, final String url, final String onClick,
            final String shortcutKey, final String id, final String toolTip) throws Exception {

        if (StringUtils.isBlank(name)) {
            throw new Exception("The action name cannot be an empty string");
        }

        if (StringUtils.isBlank(url) && StringUtils.isBlank(onClick)) {
            throw new Exception("No url or onclick event has been set for the action: " + name);
        }

        ActionBean action = new ActionBean();

        action.setId(id);
        action.setName(name);
        action.setUrl(url);
        action.setOnClick(onClick);
        action.setShortcutKey(shortcutKey);
        action.setToolTip(toolTip);

        return action;
    }
}