/**
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * Copyright © 2009 Mindoo GmbH
 *
 * @author Tammo Riedinger
 *
 */
package com.mindoo.ec09.framework.actions;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.Icon;

import com.mindoo.ec09.framework.views.View;

public class ActionDelegate extends AbstractAction {
	private static final long serialVersionUID = -5499587780873055821L;
	private View m_view;
	public static final String DELEGATE_NAME="DelegateName";
	
	public ActionDelegate(String name, String delegateName) {
		super(name);
		
		putValue(DELEGATE_NAME, delegateName);
	}
	
	public ActionDelegate(String name, String delegateName, Icon icon) {
		super(name, icon);
		
		putValue(DELEGATE_NAME, delegateName);
	}

	public ActionDelegate(String name, Icon icon) {
		super(name, icon);
		
		putValue(DELEGATE_NAME, name);
	}
	
	public ActionDelegate(String name) {
		super(name);
		
		putValue(DELEGATE_NAME, name);
	}

	public void actionPerformed(ActionEvent evt) {
		Action a=null;
		
		if (m_view!=null) {
			a=m_view.getAction((String)this.getValue(DELEGATE_NAME));
		}
		
		if (a!=null && a.isEnabled())
			a.actionPerformed(evt);
	}
	
	public void setView(View v) {
		m_view=v;
		
		updateEnabledState();
	}
	
	private void updateEnabledState() {
		if (m_view!=null) {
			Action a=m_view.getAction((String)this.getValue(DELEGATE_NAME));

			setEnabled((a!=null) ? a.isEnabled() : false);
		}
		else
			setEnabled(false);
	}
}