alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.openide.util.actions;

import java.beans.*;

import org.openide.TopManager;
import org.openide.nodes.*;
import org.openide.util.WeakListener;

/** Base class for all project sensitive actions. Attaches listener
* to changes of project desktop (TopManager.PROP_PLACES) and also to 
* cookie changes on current project desktop. If either one is changed,
* the enable method is called to enable/disable the action. When 
* enabled and invoked the method performAction (Node) is called.
*
* @author Jaroslav Tulach
*/
public abstract class ProjectSensitiveAction extends CallableSystemAction {
    /** the listener for this action */
    private static final String PROP_LISTENER = "listener"; // NOI18N

    /* Initialize the listener.
    */
    static final long serialVersionUID =1813729754448097488L;
    protected void initialize () {
        super.initialize ();
        NodesL l = new NodesL (getClass ());
        putProperty(PROP_LISTENER, l);
    }

    /* Adds listener to registry.
    */
    protected void addNotify () {
        super.addNotify();
        // initializes the listener
        NodesL l = (NodesL)getProperty (PROP_LISTENER);
        l.setActive (true);
    }

    /* Removes listener to changes of activated nodes */
    protected void removeNotify () {
        NodesL l = (NodesL)getProperty (PROP_LISTENER);
        l.setActive (false);
        super.removeNotify();
    }

    /** Test for enablement based on {@link #enable}.
    * You probably ought not override this, except possibly
    * to call the super method and add an additional check.
    * @return true to enable
    */
    public boolean isEnabled () {
        NodesL l = (NodesL)getProperty (PROP_LISTENER);
        if (!l.isActive ()) {
            l.checkEnabled (this);
        }
        return super.isEnabled ();
    }

    /* Implementation of method of javax.swing.Action interface.
    * Checks if the source of event is node and if so, it executes
    * performAction (thatNode). Otherwise simply calls
    * performAction ().
    *
    * @param ev event where to find source
    */
    public void actionPerformed (java.awt.event.ActionEvent ev) {
        Object s = ev == null ? null : ev.getSource ();
        if (s instanceof Node) {
            performAction ((Node)s);
        } else {
            performAction (projectDesktop ());
        }
    }

    /** Performs the action.
    * In the default implementation, calls {@link #performAction(Node)}.
    * In general you need not override this.
    */
    public void performAction() {
        performAction (projectDesktop ());
    }

    /** Performs the action on the current project desktop node.
    *
    * @param project desktop node
    */
    protected abstract void performAction (Node project);

    /** Performs the action on the current project desktop node.
    *
    * @param project desktop node
    * @return true to be enabled, false to be disabled
    */
    protected abstract boolean enable (Node project);


    /** Project desktop
    */
    static Node projectDesktop () {
        return TopManager.getDefault ().getPlaces ().nodes ().projectDesktop ();
    }

    /** Node listener to check whether the action is enabled or not
    */
    private static final class NodesL extends NodeAdapter {
        /** the class we work with */
        private Class clazz;
        /** listener */
        private NodeListener listener;

        /** Constructor that checks the current state
        */
        public NodesL (Class clazz) {
            this.clazz = clazz;
        }

        /** Activates/passivates the listener.
        */
        void setActive (boolean active) {
            if (listener == null && active) {
                listener = org.openide.nodes.NodeOp.weakNodeListener (this, TopManager.getDefault ());
                TopManager.getDefault ().addPropertyChangeListener (listener);
                projectDesktop ().addNodeListener (listener);
                checkEnabled (action ());
                return;
            }

            if (listener != null && !active) {
                TopManager.getDefault ().removePropertyChangeListener (listener);
                projectDesktop ().removeNodeListener (listener);
                listener = null;
            }
        }

        /** Is the listener active?
        */
        boolean isActive () {
            return listener != null;
        }

        /** Property change listener.
        */
        public void propertyChange (PropertyChangeEvent ev) {
            if (TopManager.PROP_PLACES.equals (ev.getPropertyName ())) {
                // changed project => release listeners
                setActive (false);
                // attach listeners
                setActive (true);
                return;
            }

            if (Node.PROP_COOKIE.equals (ev.getPropertyName ())) {
                checkEnabled (action ());
                return;
            }
        }

        /** Obtains the action.
        * @return the node action we belong to or null if there is none
        */
        private ProjectSensitiveAction action () {
            return (ProjectSensitiveAction)findObject (clazz);
        }

        /** Checks the state of the action.
        */
        void checkEnabled (ProjectSensitiveAction a) {
            // no action no work
            if (a == null) return;

            a.setEnabled (a.enable (projectDesktop ()));
        }
    }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.