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-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.openide.actions;

import java.awt.*;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.text.MessageFormat;
import java.util.HashSet;
import java.util.Collections;
import java.util.Locale;
import javax.swing.SwingUtilities;
import javax.swing.border.*;

import org.openide.ErrorManager;
import org.openide.NotifyDescriptor;
import org.openide.TopManager;
import org.openide.cookies.DebuggerCookie;
import org.openide.cookies.CompilerCookie;
import org.openide.debugger.*;
import org.openide.util.actions.SystemAction;
import org.openide.util.actions.ActionPerformer;
import org.openide.nodes.Node;
import org.openide.util.NbBundle;
import org.openide.windows.Workspace;
import org.openide.windows.WindowManager;


/**
* Performer for debugger actions.
*
* @author Jan Jancura
*/
class DebuggerPerformer extends Object implements PropertyChangeListener {


    // static ..........................................................................

    private static DebuggerPerformer  defaultPerformer;

    /** Debugger exception notification system */
    static void notifyDebuggerException (DebuggerException e) {
        ErrorManager em = ErrorManager.getDefault();
        Throwable toNotify = e.getTargetException() == null ? e : e.getTargetException();
        em.annotate(toNotify, MessageFormat.format(
            NbBundle.getBundle("org.openide.deprecated.Bundle",
                               Locale.getDefault(),
                               StepOutAction.class.getClassLoader()).
            getString("FMT_EXC_Debugger"),
            new Object[] {e.getMessage()}));
        em.notify(toNotify);
    }

    static DebuggerPerformer getDefault () {
        if (defaultPerformer == null) new DebuggerPerformer ();
        return defaultPerformer;
    }

    private boolean installed = false;

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

    DebuggerPerformer () {
        if (defaultPerformer == null) defaultPerformer = this;
        TopManager.getDefault ().addPropertyChangeListener (this);
        update ();
    }


    // main methods ....................................................................

    public void propertyChange (PropertyChangeEvent e) {
        if (e.getPropertyName ().equals (TopManager.PROP_DEBUGGER)) {
            update ();
            return;
        }
        if (!e.getPropertyName ().equals (Debugger.PROP_STATE)) return;
        try {
            switch (TopManager.getDefault ().getDebugger ().getState ()) {
            case Debugger.DEBUGGER_NOT_RUNNING:
                ((TraceOverAction) SystemAction.get (TraceOverAction.class)).setActionPerformer (null);
                ((StepOutAction) SystemAction.get (StepOutAction.class)).setActionPerformer (null);
                ((StartDebuggerAction) SystemAction.get (StartDebuggerAction.class)).changeEnabled (true);
                ((GoAction) SystemAction.get (GoAction.class)).changeEnabled (false);
                ((TraceIntoAction) SystemAction.get (TraceIntoAction.class)).changeEnabled (true);
                ((FinishDebuggerAction) SystemAction.get (FinishDebuggerAction.class)).setActionPerformer (null);
                ((GoToCursorAction) SystemAction.get (GoToCursorAction.class)).changeEnabled (true);
                break;
            case Debugger.DEBUGGER_STARTING:
                break;
            case Debugger.DEBUGGER_RUNNING:
                ((TraceOverAction) SystemAction.get (TraceOverAction.class)).setActionPerformer (null);
                ((StepOutAction) SystemAction.get (StepOutAction.class)).setActionPerformer (null);
                ((GoAction) SystemAction.get (GoAction.class)).changeEnabled (false);
                ((TraceIntoAction) SystemAction.get (TraceIntoAction.class)).changeEnabled (false);
                ((FinishDebuggerAction) SystemAction.get (FinishDebuggerAction.class)).setActionPerformer (new FinishDebuggerPerformer ());
                ((GoToCursorAction) SystemAction.get (GoToCursorAction.class)).changeEnabled (false);
                break;
            case Debugger.DEBUGGER_STOPPED:
                ((TraceOverAction) SystemAction.get (TraceOverAction.class)).setActionPerformer (new TraceOverPerformer ());
                ((StepOutAction) SystemAction.get (StepOutAction.class)).setActionPerformer (new StepOutPerformer ());
                ((GoAction) SystemAction.get (GoAction.class)).changeEnabled (true);
                ((TraceIntoAction) SystemAction.get (TraceIntoAction.class)).changeEnabled (true);
                ((GoToCursorAction) SystemAction.get (GoToCursorAction.class)).changeEnabled (true);
                break;
            }
        } catch (DebuggerNotFoundException ex) {
        }
    }

    void update () {
        try {
            Debugger debugger = TopManager.getDefault ().getDebugger ();
            if (installed) return;
            installed = true;
            debugger.addPropertyChangeListener (this);
            return;
        } catch (DebuggerNotFoundException ex) {
            ex.printStackTrace();
        }
        if (!installed) return;
        installed = false;
        ((AddWatchAction) SystemAction.get (AddWatchAction.class)).setActionPerformer (null);
    }

    void setDebuggerRunning (boolean b) {
        ((StartDebuggerAction) SystemAction.get (StartDebuggerAction.class)).changeEnabled (!b);
        ((GoAction) SystemAction.get (GoAction.class)).changeEnabled (!b);
        ((GoToCursorAction) SystemAction.get (GoToCursorAction.class)).changeEnabled (!b);
        ((TraceIntoAction) SystemAction.get (TraceIntoAction.class)).changeEnabled (!b);
        ((TraceOverAction) SystemAction.get (TraceOverAction.class)).setActionPerformer (null);
    }

    static void init () {
        final Debugger debugger;
        try {
            debugger = TopManager.getDefault ().getDebugger ();
        } catch (DebuggerException ex) {
            notifyDebuggerException(ex);
            return;
        }
    }


    // innerclasses ................................................................

    /**
    * Performer for FinishDebugger action.
    * This class is final only for performance reasons,
    * can be happily unfinaled if desired.
    */
    static final class FinishDebuggerPerformer implements ActionPerformer {

        public void performAction (SystemAction action) {
            try {
                TopManager.getDefault().getDebugger().finishDebugger();
            } catch (DebuggerException e) {
                DebuggerPerformer.notifyDebuggerException (e);
            }
        }
    } // FinishDebuggerPerformer

    /** Performer for StepOut action.
    * This class is final only for performance reasons,
    * can be happily unfinaled if desired.
    */
    static final class StepOutPerformer implements ActionPerformer {

        public void performAction(final org.openide.util.actions.SystemAction p1) {
            DebuggerPerformer.getDefault ().setDebuggerRunning (true);
            try {
                TopManager.getDefault().getDebugger().stepOut();
            } catch (DebuggerException e) {
                DebuggerPerformer.notifyDebuggerException (e);
            }
        }
    } // StepOutPerformer

    /**
    * Performer for TraceOver action.
    * This class is final only for performance reasons,
    * can be happily unfinaled if desired.
    */
    static final class TraceOverPerformer implements ActionPerformer {
        public void performAction(final org.openide.util.actions.SystemAction p1) {
            DebuggerPerformer.getDefault ().setDebuggerRunning (true);
            try {
                TopManager.getDefault().getDebugger().traceOver ();
            } catch (DebuggerException e) {
                DebuggerPerformer.notifyDebuggerException (e);
            }
        }
    } // TraceOverPerformer

    /** Switches to running workspace */
    static void changeWorkspace () {
        WindowManager dp = WindowManager.getDefault();
        final Workspace d = dp.findWorkspace (StartDebuggerAction.getWorkspace());
        if (d != null)
            SwingUtilities.invokeLater (new Runnable () {
                                        public void run () {
                                            d.activate ();
                                        }
                                    });
    }


    /** This class performs debugger starting */
    public class StartDebugThread extends Thread {
        /** Currently activated nodes array */
        private Node[] activatedNodes;
        private boolean stopOnMain;

        private PropertyChangeListener goToCursor_pcl = null;
        private Breakpoint goToCursor_breakpoint = null;

        public StartDebugThread (final Node[] activatedNodes, boolean stopOnMain) {
            super();
            this.activatedNodes = activatedNodes;
            this.stopOnMain = stopOnMain;
        }

        void storeGoToCursorInfo (PropertyChangeListener pcl, Breakpoint b) {
            goToCursor_pcl = pcl;
            goToCursor_breakpoint = b;
        }

        public void run () {


            if (StartDebuggerAction.getRunCompilation()) {
                // compile cookies
                HashSet compile = new HashSet ();
                for (int i = 0; i < activatedNodes.length; i++) {
                    CompilerCookie comp = (CompilerCookie) activatedNodes[i].getCookie(CompilerCookie.Compile.class);
                    if (comp != null) {
                        compile.add(comp);
                    }
                }
                // do compile
                if (! AbstractCompileAction.compile(Collections.enumeration(compile),
                                                    AbstractCompileAction.findName(activatedNodes))) {
                    setDebuggerRunning (false);
                    try {
                        if (goToCursor_pcl != null) {
                            TopManager.getDefault ().getDebugger ().removePropertyChangeListener (goToCursor_pcl);
                            goToCursor_pcl = null;
                            goToCursor_breakpoint.remove ();
                        }
                        return;
                    }
                    catch (org.openide.debugger.DebuggerException e) {
                        notifyDebuggerException (e);
                        return;
                    }
                }
            }

            DebuggerCookie cookie = (DebuggerCookie) activatedNodes[0].getCookie (DebuggerCookie.class);
            if (cookie == null) {
                TopManager.getDefault().notify(new NotifyDescriptor.Message(
                    MessageFormat.format(NbBundle.getBundle("org.openide.deprecated.Bundle",
                                                            Locale.getDefault(),
                                                            StepOutAction.class.getClassLoader()).
                                         getString("FMT_MSG_CannotDebug"),
                                         new Object[] {
                                             activatedNodes[0].getDisplayName()
                                         }),
                            NotifyDescriptor.WARNING_MESSAGE
                ));
                return;
            }

            changeWorkspace();

            try {
                cookie.debug (stopOnMain);
                if (TopManager.getDefault ().getDebugger ().getState () == Debugger.DEBUGGER_NOT_RUNNING) {
                    setDebuggerRunning (false);
                    if (goToCursor_pcl != null) {
                        TopManager.getDefault ().getDebugger ().removePropertyChangeListener (goToCursor_pcl);
                        goToCursor_pcl = null;
                        goToCursor_breakpoint.remove ();
                    }
                }
                else
                    goToCursor_pcl = null;
            } catch (org.openide.debugger.DebuggerException e) {
                notifyDebuggerException (e);
            }
        }

    } // end of StartDebugThread
}
... 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.