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.actions;

import org.openide.ErrorManager;

import java.lang.reflect.Method;
import java.util.*;

import org.openide.cookies.ExecCookie;

import org.openide.loaders.DataObject;
import org.openide.nodes.Node;
import org.openide.util.HelpCtx;
import org.openide.util.Lookup;
import org.openide.util.Mutex;
import org.openide.util.NbBundle;
import org.openide.util.RequestProcessor;
import org.openide.util.actions.CookieAction;
import org.openide.windows.WindowManager;
import org.openide.windows.Workspace;

/** Execute a class.
* Is enabled if the only selected node implements
* {@link ExecCookie}.
* @see org.openide.execution
*
* @author   Ian Formanek, Jaroslav Tulach, Jan Jancura
*/
public class ExecuteAction extends CookieAction {

    private static String workspace = "None"; // NOI18N

    /** should we run compilation before execution */
    private static boolean runCompilation;

    /** Switches to running workspace */
    private static void changeWorkspace () {
        WindowManager dp = WindowManager.getDefault();
        Workspace d = dp.findWorkspace (workspace);
        if (d != null) d.activate ();
    }

    /**
    * Get the name of the workspace in which execution is performed.
    * By default, execution is performed in the "running" workspace.
    * @return the workspace name
    */
    public static String getWorkspace () {
        return workspace;
    }

    /**
    * Set the name of the workspace in which execution is performed.
    * @param workspace the workspace name
    */
    public static void setWorkspace (String workspace) {
        ExecuteAction.workspace = workspace;
    }

    /** Set whether files should be compiled before execution.
    * @param run true if they should
     * @deprecated Only works if the org.openide.compiler module is enabled.
    */
    public static void setRunCompilation (boolean run) {
        runCompilation = run;
    }

    /** Test whether files will be compiled before execution.
    * By default they will.
    * @return true if they will be
     * @deprecated Only works if the org.openide.compiler module is enabled.
    */
    public static boolean getRunCompilation () {
        return runCompilation;
    }

    // init ..........................................................................................

    protected Class[] cookieClasses() {
        return new Class[] { ExecCookie.class };
    }

    protected void performAction (final Node[] activatedNodes) {
        // Running ExecCookie should be fast. But running compilation before
        // may take a long time. To be safe, do it asynch.
        RequestProcessor.getDefault().post(new Runnable() {
            public void run() {
                execute(activatedNodes, runCompilation, true);
            }
        });
    }
    
    protected boolean asynchronous() {
        return false;
    }

    protected int mode () {
        return MODE_ANY;
    }

    public String getName() {
        return NbBundle.getMessage(ExecuteAction.class, "Execute");
    }

    public HelpCtx getHelpCtx() {
        return new HelpCtx (ExecuteAction.class);
    }

    protected String iconResource () {
        return "org/openide/resources/actions/execute.gif"; // NOI18N
    }

    // utility methods

    /** Execute a list of items by cookie.
    *
    * @param execCookies list of {@link ExecCookie}s (any may be null)
    */
    public static void execute(Iterator execCookies) {
        while (execCookies.hasNext()) {
            ExecCookie cookie = (ExecCookie) execCookies.next();
            if (cookie != null) {
                cookie.start();
            }
        }
    }

    /** Execute some data objects.
    *
    * @param dataObjects the data objects (should have {@link ExecCookie} on them if they are to be used)
    * @param compileBefore true to compile before executing
    * @return true if compilation succeeded or was not performed, false if compilation failed
    */
    public static boolean execute(DataObject[] dataObjects, boolean compileBefore) {
        // search all DataObjects with unique ExecCookies/StartCookies -
        // - it is possible, that multiple activated nodes have the same exec cookie and
        // we have to prevent running it multiple times
        HashSet execute = new HashSet ();

        for (int i = 0; i < dataObjects.length; i++) {
            ExecCookie exec = (ExecCookie) dataObjects[i].getCookie(ExecCookie.class);
            if (exec != null) {
                execute.add(exec);
            }
        }
        // compile
        if (compileBefore && !compile("compileDataObjects", dataObjects)) { // NOI18N
            return false;
        }

        // execute
        execute(execute.iterator());
        return true;
    }

    /** Execute some nodes.
    *
    * @param nodes the nodes (should have {@link ExecCookie} on them if they are to be used)
    * @param compileBefore true to compile before executing
    * @return true if compilation succeeded or was not performed, false if compilation failed
    */
    public static boolean execute(Node[] nodes, boolean compileBefore) {
        return execute(nodes, compileBefore, false);
    }

    /** Execute some nodes.
    *
    * @param nodes the nodes (should have {@link ExecCookie} on them if they are to be used)
    * @param compileBefore true to compile before executing
    * @param switchWorkspace true to switch workspace before executing
    * @return true if compilation succeeded or was not performed, false if compilation failed
    */
    private static boolean execute(Node[] nodes, boolean compileBefore, boolean switchWorkspace) {
        // find all activatedNodes with unique ExecCookies/StartCookies -
        // - it is possible, that multiple activated nodes have the same exec cookie and
        // we have to prevent running it multiple times
        HashSet execute = new HashSet ();

        for (int i = 0; i < nodes.length; i++) {
            ExecCookie exec = (ExecCookie) nodes[i].getCookie(ExecCookie.class);
            if (exec != null) {
                execute.add(exec);
            }
        }

        // compile
        if (compileBefore && !compile("compileNodes", nodes)) { // NOI18N
            return false;
        }

        if (switchWorkspace) {
            // May be running from RP; need to do winsys ops in EQ.
            Mutex.EVENT.readAccess (new Mutex.Action () {
                    public Object run() {
                        changeWorkspace ();
                        return null;
                    }
                });
        }
        // execute
        execute(execute.iterator());
        return true;
    }
    
    private static boolean compile(String methodName, Object[] args) {
        try {
            Class c = ((ClassLoader)Lookup.getDefault().lookup(ClassLoader.class)).loadClass("org.openide.actions.AbstractCompileAction"); // NOI18N
            Method m = c.getDeclaredMethod(methodName, new Class[] {args.getClass()});
            return ((Boolean)m.invoke(null, new Object[] {args})).booleanValue();
        } catch (ClassNotFoundException e) {
            // Failed, but not implausible.
            return false;
        } catch (Exception e) {
            // Something else wrong.
            ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
            return false;
        }
    }
    
}
... 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.