package org.minimalj.frontend.action;

import java.text.MessageFormat;

import org.minimalj.util.StringUtils;
import org.minimalj.util.resources.Resources;

/**
 * Action is used for at several points. But not everywhere all of its
 * behavior is supported. See the javadoc of the setter for more information.
 *
 */
public abstract class Action {

	private final String name;
	private String description;
	private boolean enabled = true;
	private ActionChangeListener changeListener;

	/**
	 * The name and description of this action will be defined by the
	 * class name.
	 */
	protected Action() {
		String resourceName = Resources.getResourceName(getClass());
		Object[] nameArguments = getNameArguments();
		this.name = nameArguments != null ?  MessageFormat.format(Resources.getString(resourceName), nameArguments) : Resources.getString(resourceName);
		
		String descriptionResourceName = resourceName + ".description";
		if (Resources.isAvailable(descriptionResourceName)) {
			this.description = Resources.getString(descriptionResourceName);
		}
	}
	
	protected Object[] getNameArguments() {
		return null;
	}
	
	protected Action(String name) {
		this.name = name;
	}
	
	/**
	 * 
	 * @return the text normally displayed in the frontend
	 */
	public final String getName() {
		return name;
	}
	
	/**
	 * 
	 * @return this could be used by a frontend for a additional information about this action. For example a tooltip.
	 */
	public String getDescription() {
		return description;
	}
	
	public boolean isEnabled() {
		return enabled;
	}
	
	/**
	 * @param enabled Note: this is not supported if the action was used to create
	 * a label by the Frontend
	 */
	public void setEnabled(boolean enabled) {
		if (this.enabled != enabled) {
			this.enabled = enabled;
			fireChange();
		}
	}

	/**
	 * Note: this is only supported for actions used in an Editor
	 * 
	 * @param description New description (tooltip) of this action
	 */
	public void setDescription(String description) {
		if (!StringUtils.equals(this.description, description)) {
			this.description = description;
			fireChange();
		}
	}

	protected void fireChange() {
		if (changeListener != null) {
			changeListener.change();
		}
	}

	public abstract void action();

	public void setChangeListener(ActionChangeListener changeListener) {
		this.changeListener = changeListener;
	}

	public interface ActionChangeListener {
		public void change();
	}
}

@@ -351,22 +351,16 @@
 private 
-final 
 String n
@@ -362,16 +362,29 @@
 ing name
+, description
 ;%0A%09priva
@@ -386,22 +386,23 @@
 private 
-String
+Boolean
  descrip
@@ -397,32 +397,41 @@
 lean description
+Available
 ;%0A%09private boole
@@ -616,873 +616,1140 @@
  %7B%0A%09
-%09String resourceName = Resources.getResourceName(getClass());%0A%09%09Object%5B%5D nameArguments = getNameArguments();%0A%09%09this.name = nameArguments != null ?  MessageFormat.format(Resources.getString(resourceName), nameArguments) : Resources.getString(resourceName);%0A%09%09%0A%09%09String descriptionResourceName = resourceName + %22.description%22;%0A%09%09if (Resources.isAvailable(descriptionResourceName)) %7B%0A%09%09%09this.description = Resources.getString(descriptionResourceName);%0A%09%09%7D%0A%09%7D%0A%09%0A%09protected Object%5B%5D getNameArguments() %7B%0A%09%09return null;%0A%09%7D%0A%09%0A%09protected Action(String name) %7B%0A%09%09this.name = name;%0A%09%7D%0A%09%0A%09/**%0A%09 * %0A%09 * @return the text normally displayed in the frontend%0A%09 */%0A%09public final String getName() %7B%0A%09%09return name;%0A%09%7D%0A%09%0A%09/**%0A%09 * %0A%09 * @return this could be used by a frontend for a additional information about this action. For example a tooltip.%0A%09 */%0A%09public String getDescription() %7B
+%7D%0A%09%0A%09protected String getResourceName() %7B%0A%09%09return Resources.getResourceName(getClass());%0A%09%7D%0A%09%0A%09protected Object%5B%5D getNameArguments() %7B%0A%09%09return null;%0A%09%7D%0A%09%0A%09protected Action(String name) %7B%0A%09%09this.name = name;%0A%09%7D%0A%09%0A%09/**%0A%09 * %0A%09 * @return the text normally displayed in the frontend%0A%09 */%0A%09public final String getName() %7B%0A%09%09if (name == null) %7B%0A%09%09%09String resourceName = getResourceName();%0A%09%09%09Object%5B%5D nameArguments = getNameArguments();%0A%09%09%09name = nameArguments != null ? MessageFormat.format(Resources.getString(resourceName), nameArguments) : Resources.getString(resourceName);%0A%09%09%7D%0A%09%09return name;%0A%09%7D%0A%09%0A%09/**%0A%09 * %0A%09 * @return this could be used by a frontend for a additional information about this action. For example a tooltip.%0A%09 */%0A%09public String getDescription() %7B%0A%09%09if (description == null && !Boolean.FALSE.equals(descriptionAvailable)) %7B%0A%09%09%09String resourceName = getResourceName();%0A%09%09%09String descriptionResourceName = resourceName + %22.description%22;%0A%09%09%09descriptionAvailable = Resources.isAvailable(descriptionResourceName);%0A%09%09%09if (descriptionAvailable) %7B%0A%09%09%09%09this.description = Resources.getString(descriptionResourceName);%0A%09%09%09%7D%0A%09%09%7D
 %0A%09%09r

make name/description more configurable