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

net.sf.gluebooster.java.booster.basic.events.DefaultActions Maven / Gradle / Ivy

package net.sf.gluebooster.java.booster.basic.events;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import net.sf.gluebooster.java.booster.essentials.eventsCommands.CallableAbstraction;
import net.sf.gluebooster.java.booster.essentials.utils.Constants;
import net.sf.gluebooster.java.booster.essentials.utils.ThrowableBoostUtils;

/**
 * Default actions that can happen on components or other pojos.
 * 
 * @author cbauer
 *
 */
public class DefaultActions extends CallableAbstraction implements ActionListener {

	/**
	 * The type to hide a component
	 */
	private static final String TYPE_SET_INVISIBLE = "set invisible";

	/**
	 * The type of the action.
	 */
	private String type;

	/**
	 * The target of the action
	 */
	private Object target;

	/**
	 * Create a listener that sets a component to invisible when invoked.
	 * 
	 * @param targetToSetInvisible
	 *            the component to modify
	 * @return the created action listener
	 */
	public static DefaultActions createSetInvisibleAction(Component targetToSetInvisible) {
		return new DefaultActions(TYPE_SET_INVISIBLE, targetToSetInvisible);
	}

	private DefaultActions(String type) {
		this.type = type;
	}

	private DefaultActions(String type, Object target) {
		this(type);
		setTarget(target);
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		try {
			call(type);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw ThrowableBoostUtils.toRuntimeException(e);
		}
		// switch (type) {
		// case TYPE_SET_INVISIBLE:
		//
		// ((Component) target).setVisible(false);
		// break;
		// default:
		// throw new IllegalStateException("type not supported: " + type);
		// }
		//
	}

	@Override
	public String getType() {
		return type;
	}

	private void setType(String type) {
		this.type = type;
	}

	public Object getTarget() {
		return target;
	}

	public void setTarget(Object target) {
		this.target = target;
	}

	/**
	 * @return TRUE if successful
	 */
	@Override
	protected Boolean callImpl(Object... parameters) throws Exception {
		Object command = parameters[0];
		if (TYPE_SET_INVISIBLE.equals(command)) {
			((Component) target).setVisible(false);
		} else if (Constants.ACTIONPERFORMED.equals(command)) {
			return callImpl(type);
		} else {
			throw new IllegalStateException("command not supported: " + command);

		}
		return Boolean.TRUE;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy