Package research.toolAction

Source Code of research.toolAction.ToolAction

package research.toolAction;

import research.tool.Tool;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

/**
* author: zhangwei
* Date: 2003-5-10
* Time: 22:17:55
*/
public class ToolAction extends AbstractAction {

    public final static String TOOL_CONTROLLER = "TOOL_CONTROLLER";
    public final static String TOOL = "TOOL";

    protected PropertyChangeListener myPropertyChangeListener;

    protected Tool tool = null;

    protected boolean selected = false;
    protected boolean defaulted = false;

    public ToolAction() {
        super();
        myPropertyChangeListener = createPropertyChangeListener();
        addPropertyChangeListener(myPropertyChangeListener);
    }

    public void setDefault(boolean value){
        defaulted = value;
    }

    public boolean isDefault(){
        return defaulted;
    }

    public void setSelected(boolean selected) {
        boolean oldValue = isSelected();
        this.selected = selected;
        boolean newValue = isSelected();

        if (oldValue != newValue) {
            if (newValue) {
                activate();
            } else {
                deactivate();
            }

            firePropertyChange("selected", Boolean.valueOf(oldValue), Boolean.valueOf(newValue));
        }
    }

    public boolean isSelected() {
        return selected;
    }

    public void setEnabled(boolean enabled) {
        boolean oldValue = isEnabled();
        this.enabled = enabled;
        boolean newValue = isEnabled();
        if (oldValue != newValue) {
            firePropertyChange("enabled", Boolean.valueOf(oldValue), Boolean.valueOf(newValue));
        }
    }

    protected PropertyChangeListener createPropertyChangeListener() {
        return new InerPropertyChangeListener();
    }

    public void actionPerformed(ActionEvent e) {
        if (!isEnabled()) return;

        if(defaulted && isSelected()){
            Object source = e.getSource();
            if(JToggleButton.class.isInstance(source)){
                JToggleButton tb = (JToggleButton) source;
                tb.setSelected(!tb.isSelected());
            }
            return;
        }

        setSelected(!isSelected());
    }

    public void activate(){

    }

    public void deactivate(){

    }

    protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
        if (changeSupport == null || oldValue == newValue) {
            return;
        }

        if (oldValue != null && newValue != null && oldValue.equals(newValue)) {
            return;
        }

        changeSupport.firePropertyChange(propertyName, oldValue, newValue);

    }

    protected class InerPropertyChangeListener implements PropertyChangeListener {
        public void propertyChange(PropertyChangeEvent e) {
            if (ToolAction.TOOL.equals(e.getPropertyName())) {
                Tool oldTool = tool;
                tool = (Tool) e.getNewValue();
            }
        }
    }
}
TOP

Related Classes of research.toolAction.ToolAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.