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

package org.netbeans.modules.struts.actions;

import org.netbeans.modules.struts.nodes.JDOMElementCookie;
import org.openide.nodes.Node;
import org.openide.util.HelpCtx;
import org.openide.util.NbBundle;
import org.openide.util.actions.CookieAction;
import org.openide.filesystems.*;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.cookies.OpenCookie;
import org.openide.*;

/** Action sensitive to some cookie that does something useful.
 *
 * @author  mk115033
 */
public class GoToSourceAction extends CookieAction {
    
    protected Class[] cookieClasses() {
        return new Class[] { /* SourceCookie.class */ };
    }
    
    protected int mode() {
        return MODE_EXACTLY_ONE;
        // return MODE_ALL;
    }
    
    protected void performAction(Node[] nodes) {
        JDOMElementCookie cookie = (JDOMElementCookie) nodes[0].getCookie (JDOMElementCookie.class);
        String source = cookie.getSourceElement().getAttribute("type").getValue();
        Repository rep = org.openide.TopManager.getDefault().getRepository();
        try {
            FileObject fo = rep.findResource(source.replace('.','/')+".java");
            if (fo!=null) {
                DataObject dObj = DataObject.find(fo);
                OpenCookie openCookie = (OpenCookie)dObj.getCookie(OpenCookie.class);
                if (openCookie!=null) openCookie.open();
            } else {
                TopManager.getDefault().notify(new NotifyDescriptor.Message(
                    NbBundle.getMessage(GoToSourceAction.class, "ERR_cannotFindSource", new Object[]{source}),NotifyDescriptor.ERROR_MESSAGE));
            }
        } catch(DataObjectNotFoundException ex) {System.out.println("ex="+ex);}
        
        // do work based on the current node selection, e.g.:
        // SourceCookie cookie = (SourceCookie) nodes[0].getCookie (SourceCookie.class);
        // etc.
    }
    
    public String getName() {
        return NbBundle.getMessage(GoToSourceAction.class, "LBL_GoToSourceAction");
    }
    
    protected String iconResource() {
        return "org/netbeans/modules/struts/actions/GoToSourceActionIcon.gif";
    }
    
    public HelpCtx getHelpCtx() {
        return HelpCtx.DEFAULT_HELP;
        // If you will provide context help then use:
        // return new HelpCtx (GoToSourceAction.class);
    }
    
    /** Perform special enablement check in addition to the normal one.
    */
    protected boolean enable (Node[] nodes) {
        JDOMElementCookie cookie = (JDOMElementCookie) nodes[0].getCookie (JDOMElementCookie.class);
        if (cookie!=null && cookie.getSourceElement().getAttribute("type")!=null) return true;
        else return false;
        //if (...) ...;
    }
    
    
    /** Perform extra initialization of this action's singleton.
     * PLEASE do not use constructors for this purpose!
     * protected void initialize () {
     * super.initialize ();
     * putProperty (Action.SHORT_DESCRIPTION, NbBundle.getMessage (GoToSourceAction.class, "HINT_Action"));
     * }
     */
    
}
... 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.