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

package org.openide.actions;

import javax.swing.JEditorPane;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;
import java.util.Locale;

import org.openide.util.HelpCtx;
import org.openide.debugger.Debugger;
import org.openide.debugger.Breakpoint;
import org.openide.debugger.DebuggerNotFoundException;
import org.openide.TopManager;
import org.openide.cookies.DebuggerCookie;
import org.openide.cookies.EditorCookie;
import org.openide.util.NbBundle;
import org.openide.nodes.Node;
import org.openide.text.NbDocument;
import org.openide.text.Line;


/** Go to the cursor.
* @author   Jan Jancura (checked [PENDING HelpCtx])
*/
public class GoToCursorAction extends GoAction {
    /** generated Serialized Version UID */
    static final long serialVersionUID = -1407195105130821880L;

    private boolean enabled = true;

    /* @return the action's icon */
    public String getName() {
        return NbBundle.getBundle("org.openide.deprecated.Bundle", Locale.getDefault(), StepOutAction.class.getClassLoader()).getString("GoToCursor");
    }

    /* @return the action's help context */
    public HelpCtx getHelpCtx() {
        return new HelpCtx (GoToCursorAction.class);
    }

    /* Icon resource.
    * @return name of resource for icon
    */
    protected String iconResource () {
        return "org/openide/deprecated/goToCursor.gif"; // NOI18N
    }

    /** Set whether the debugger action is enabled in general.
    * @param e true if so
    */
    public void changeEnabled (boolean e) {
        enabled = e;
        setEnabled (enable (getActivatedNodes ()));
    }

    /* Enables goToCursor action when only one data object which supports
    * debugging (isDebuggingAllowed () == true) is selected.
    *
    * @param activatedNodes Currently activated nodes.
    */
    protected boolean enable (final Node[] activatedNodes) {
        if (!enabled) return false;
        try {
            EditorCookie edCookie;
            if ((activatedNodes == null) || (activatedNodes.length != 1)) return false;
            if ((activatedNodes[0].getCookie(DebuggerCookie.class)==null) ||
                    ((edCookie=(EditorCookie)activatedNodes[0].getCookie(EditorCookie.class))==null))
                return false;
            JEditorPane [] panes = edCookie.getOpenedPanes();
            if (panes == null) return false;
            int state = TopManager.getDefault ().getDebugger ().getState ();
            if (state != Debugger.DEBUGGER_NOT_RUNNING) return state == Debugger.DEBUGGER_STOPPED;
            return true;
        } catch (DebuggerNotFoundException e) {
            return false;
        }
    }

    /* This performer starts the debugger (if isn't started yet),
    * or calls the goToCursor method of debugger in the other case.
    *
    * @param activatedNodes Currently activated nodes.
    */
    protected void performAction (final Node[] activatedNodes) {
        if ((activatedNodes == null) || (activatedNodes.length == 0))
            return;
        EditorCookie edCookie = (EditorCookie)activatedNodes[0].getCookie (EditorCookie.class);
        if (edCookie == null)
            return;
        JEditorPane [] panes = edCookie.getOpenedPanes();
        int index = -1;
        if (panes == null) return;
        for (int x = 0; x < panes.length; x++)
            if (panes[x].isManagingFocus()) {
                index = x;
                break;
            }
        if (index == -1) return;
        int l = NbDocument.findLineNumber (
                    edCookie.getDocument (),
                    panes[index].getCaret ().getDot ()
                );
        Line ll = edCookie.getLineSet ().getCurrent (l);
        if (ll == null) return;

        try {
            final Debugger debugger = TopManager.getDefault ().getDebugger ();
            final Breakpoint b = debugger.createBreakpoint (ll, true);
            PropertyChangeListener pcl;
            debugger.addPropertyChangeListener (pcl = new PropertyChangeListener () {
                public void propertyChange (PropertyChangeEvent ev) {
                    if (ev.getPropertyName ().equals (debugger.PROP_STATE)) {
                        if ((((Integer)ev.getNewValue ()).intValue () == debugger.DEBUGGER_STOPPED) ||
                                (((Integer)ev.getNewValue ()).intValue () == debugger.DEBUGGER_NOT_RUNNING)) {
                            b.remove();
                            debugger.removePropertyChangeListener(this);
                        }
                    }
                }
            });
            DebuggerPerformer.getDefault ().setDebuggerRunning (true);
            int state = debugger.getState ();
            if (state == Debugger.DEBUGGER_NOT_RUNNING) {
                // start in different thread
                DebuggerPerformer.StartDebugThread dt = DebuggerPerformer.
                    getDefault ().new StartDebugThread (activatedNodes, false);
                dt.storeGoToCursorInfo (pcl, b);
                dt.start ();
            }
            else
                if (state == Debugger.DEBUGGER_STOPPED)
                    try {
                        TopManager.getDefault ().getDebugger ().go ();
                    } catch (org.openide.debugger.DebuggerException e) {
                        DebuggerPerformer.getDefault ().
                            notifyDebuggerException (e);
                    }
        } catch (DebuggerNotFoundException e) {
        }
    }

}
... 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.