package jp.ac.kyutech.mns.ist.util;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JToggleButton;

public class MyAction extends AbstractAction {
	boolean isToggle;
	private boolean isSelected;
	private static final long serialVersionUID = -2253928255968178345L;
	/**
	 * isToggleを省略すると，falseになる
	 * @param text
	 * @param icon
	 * @param desc
	 * @param mnemonic
	 */
	ArrayList<ActionListener> actionListenerAry;
	ArrayList<ItemListener> itemListenerAry;
	
	public MyAction(String text, String icon, String desc) {
		this(text,icon,desc,false);
	}

	public MyAction(String text, String icon,
                      String desc, boolean isToggle) {
        super(text, QuickMenuActionManager.createNavigationIcon(icon+"24"));
        putValue(Action.SMALL_ICON, QuickMenuActionManager.createNavigationIcon(icon+"16"));
        putValue(Action.LARGE_ICON_KEY, QuickMenuActionManager.createNavigationIcon(icon+"24"));
        actionListenerAry = new ArrayList<ActionListener>();
        itemListenerAry = new ArrayList<ItemListener>();
        
        putValue(SHORT_DESCRIPTION, desc);
//        putValue(MNEMONIC_KEY, mnemonic);
        if (isToggle) {
        	putValue("toggle","true");
        	this.isToggle = isToggle;
        }
    }
	public void addActionListenerToAry(ActionListener al){
		actionListenerAry.add(al);
	}
	public void addItemListenerToAry(ItemListener il){
		itemListenerAry.add(il);
	}
    
	public void actionPerformed(ActionEvent e) {
//    	System.out.println("super.action");
		e.setSource(this);
    	for(ActionListener al: actionListenerAry){
    		al.actionPerformed(e);
    	}
    	if (isToggle){
    		setSelected(!isSelected);
//    		if (e.getSource() instanceof JCheckBoxMenuItem){
//    			JCheckBoxMenuItem jmi = (JCheckBoxMenuItem) e.getSource();
//    			setSelected(jmi.isSelected());
//    		}
//    		if (e.getSource() instanceof JToggleButton){
//    			JToggleButton jtb = (JToggleButton) e.getSource();
//    			setSelected(jtb.isSelected());		
//    		}
    	}    		
    }
	@SuppressWarnings("unchecked")
    public void setSelected(boolean b){
    	isSelected = b;
    	
    	JToggleButton jtb = (JToggleButton) getValue("togglebutton");
		if (jtb != null) jtb.setSelected(isSelected);
		
		JCheckBoxMenuItem jmi = (JCheckBoxMenuItem) getValue("togglemenu");
		if (jmi != null) jmi.setSelected(isSelected);
		
//		 更新対象がリストの場合
		ArrayList<JToggleButton> jtbal = (ArrayList<JToggleButton>) getValue("togglebuttonList");
		if (jtbal != null) for(JToggleButton jtb0: jtbal) if (jtb0!=null) jtb0.setSelected(isSelected);
		ArrayList<JCheckBoxMenuItem> jmial = (ArrayList<JCheckBoxMenuItem>) getValue("togglemenuList");
		if (jmial != null) for(JCheckBoxMenuItem jmi0: jmial) if(jmi0!=null) jmi0.setSelected(isSelected);
    }
    /**
     * 未使用
     * @param e
     */
    public void itemStateChanged(ItemEvent e){
    	for(ItemListener il: itemListenerAry){
    		il.itemStateChanged(e);
    	}
    }
	public boolean isSelected() {		
		return isSelected;
	}

	public void doClick() {
		actionPerformed(new ActionEvent(this, 1, "doClick"));
	}
    
}
