// Decompiled by Jad v1.5.7. Copyright 1997-99 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   JActionButton.java

package com.symantec.itools.javax.swing.actions;

import com.symantec.itools.javax.swing.icons.ImageIcon;
import java.awt.Component;
import java.awt.Container;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.GrayFilter;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.SwingConstants;

public class JActionButton extends JButton
{
    protected class ActionChangedListener
        implements PropertyChangeListener
    {

        public void propertyChange(PropertyChangeEvent propertychangeevent)
        {
            String s = propertychangeevent.getPropertyName();
            if(s.equals("Name"))
            {
                String s1 = (String)propertychangeevent.getNewValue();
                setText(s1);
                return;
            }
            if(s.equals("enabled"))
            {
                Boolean boolean1 = (Boolean)propertychangeevent.getNewValue();
                setEnabled(boolean1.booleanValue());
                return;
            }
            if(s.equals("SmallIcon"))
            {
                Icon icon = (Icon)propertychangeevent.getNewValue();
                setIcon(icon);
                invalidate();
                repaint();
            }
        }

        protected ActionChangedListener()
        {
        }
    }


    public JActionButton()
    {
        m_Display = 3;
        setHorizontalTextPosition(4);
        setVerticalTextPosition(0);
    }

    public JActionButton(Action action)
    {
        this();
        setAction(action);
    }

    public Action getAction()
    {
        return m_Action;
    }

    public void setAction(Action action)
    {
        if(action == null)
            throw new IllegalArgumentException();
        if(action == m_Action)
            return;
        if(m_Action != null)
        {
            removeActionListener(m_Action);
            m_Action.removePropertyChangeListener(m_ActionPropertyChangeListener);
        }
        m_Action = action;
        addActionListener(m_Action);
        m_ActionPropertyChangeListener = createActionChangeListener();
        m_Action.addPropertyChangeListener(m_ActionPropertyChangeListener);
        updateMenuItem();
    }

    public int getDisplay()
    {
        return m_Display;
    }

    public void setDisplay(int i)
    {
        if(m_Display != i)
        {
            m_Display = i;
            updateMenuItem();
        }
    }

    protected PropertyChangeListener createActionChangeListener()
    {
        return new ActionChangedListener();
    }

    public void setIcon(Icon icon)
    {
        if(icon == null)
        {
            super.setIcon(null);
            setDisabledIcon(null);
            return;
        }
        super.setIcon(icon);
        if(!(icon instanceof javax.swing.ImageIcon) && (icon instanceof ImageIcon))
            setDisabledIcon(new javax.swing.ImageIcon(GrayFilter.createDisabledImage(((ImageIcon)icon).getImage())));
    }

    protected void updateMenuItem()
    {
        if(m_Action == null)
        {
            setText("");
            setIcon(null);
            setEnabled(true);
        }
        else
        {
            if(m_Display == 3 || m_Display == 2)
                setText((String)m_Action.getValue("Name"));
            else
                setText("");
            if(m_Display == 3 || m_Display == 1)
                setIcon((Icon)m_Action.getValue("SmallIcon"));
            else
                setIcon(null);
            setEnabled(m_Action.isEnabled());
        }
        invalidate();
        repaint();
    }

    public static final int DISPLAY_ACTION_ICON = 1;
    public static final int DISPLAY_ACTION_NAME = 2;
    public static final int DISPLAY_ACTION_ICON_AND_NAME = 3;
    protected Action m_Action;
    protected PropertyChangeListener m_ActionPropertyChangeListener;
    protected int m_Display;
}
