X

Geertjan's Blog

  • November 10, 2005

Extending the NetBeans Projects Window

Geertjan Wielenga
Product Manager
Kind of a fun thing—I've added a menu item to the Projects window's pop-up menu. When I right-click in the Projects window (not on any of the nodes in the window, but in the window itself), I now see this addition:

That could be a pretty useful thing for adding new actions that are applicable to projects. (Maybe an action that interacts with Roman's Project Packager, for example.)

So, this is how I added the action (click to enlarge):


But, the best part is that I discovered how to do this by just hanging out in the System Filesystem Browser which you get when you create a module project. It lists all the layers made available by all the available modules. So, that's how I found out about the existence of the ProjectsTabActions folder... And then I just used the Action wizard to create the action and tweaked the layer.xml file so that the action was registered in the ProjectsTabActions folder instead of somewhere else.

So, whenever you create a module project, you get the System Filesystem Browser, which is an extremely educational tool, allowing you to discover folders that you never knew existed. Here, for the uninitiated, is part of my System Filesystem Browser (click to enlarge):


What's also cool about the System Filesystem Browser is that you can drag and drop items within the folder, which results in tags being added to the layer.xml file for sorting the item within the folder. In fact, I didn't type anything in the layer.xml file shown in the second screenshot above (apart from the name of the ProjectsTabActions folder). It was all done for me by the IDE. Something else that's cool is that everything that the current module is going to add to the module universe is printed in bold in the System Filesystem Browser. Really handy. Go ahead—enlarge the screenshot above and notice the bold folders and then enlarge the second screenshot above and notice that the bold folders are the ones that are in my module's layer.xml file. Handy, right?

Join the discussion

Comments ( 14 )
  • Patrick Wright Friday, January 20, 2006
    Hi Geertjan--
    I'm really enjoying your blog, have been learning lots by reading it--you aren't just promoting, you are educating, so kudos to you.
    I saw this blog entry and was wondering--how do I add a menu item to a free-form project? The docs leave something out. I have a custom target in my build file, say, "do-foo". I can map this to an existing IDE action in project.xml (like compile.file), but if I add a new ide-action, like nb.do-foo, I can't figure out how to trigger it. Since NB support is built-in, it would be nice to add context-menu actions that trigger Ant targets.
    Any advice is welcome!
    Patrick
  • Geertjan Friday, January 20, 2006
    Look in the free-form project's Project Properties dialog box. There's a panel called "Build and Run" (I think), where you can map Ant scripts to the free-form project's contextual menu. Read this 4.1 doc for details. Please leave a comment to say whether this helped you or not.
  • Patrick Wright Friday, January 20, 2006
    Thanks for the reply, Geertjan--but that does something different. I'm trying to do something very much like the instructions for "Advanced Freeform Project Configuration" at http://www.netbeans.org/kb/41/freeform-config.html. In the entries for "run single file" or "debug single file", they have a custom Ant target that must be executed by NetBeans passing in the currently selected files. If you just map a regular Ant target to the project's menu, then it won't be passed anything by NetBeans. So, in my case, I added the entries under ide-actions as described in documentation, which uses a "context" element to describe the set of files passed into the target. The question is--how do I execute that target? All I could figure out was to use an existing, pre-defined NetBeans IDE task (like "compile.file", mapped to F9).
    Thoughts?
    Thanks,
    Patrick
  • Geertjan Friday, January 20, 2006
    Sorry for not understanding, still; so, could you give the exact use case? I mean, what are you trying to do exactly? I'm really happy to help (and happy to find out who can help you if I don't know the answer), but I don't fully understand the question. What functionality are you trying to add? Or what are you hoping to achieve?
  • Patrick Wright Friday, January 20, 2006

    Hi Geertjan

    Sorry, some tags got clobbered on that last post. Here's a correction: ...

    So, to be specific, in project.xml, I have a new <action> element under my <ide-actions>. Now I need to figure out how to add a context-menu item for the project that triggers that action. In my case, I set the action to <action name="compile.single"> so I can trigger it from the Build/Compile File menu.

    TIA

    Patrick

  • Patrick Wright Saturday, January 21, 2006

    Hi Geertjan

    I checked back to see if there was a reply, and found my posting, in two parts, was cropped so only one part (the important one) got posted.

    Here's my situation/use case: I have XDoclet tags that currently process a whole directory tree. There is one global flag to check if XDoclet needs to run, but if it does need to run, it runs against the whole tree. This takes several minutes, so when I just modify 1 file there's a long delay to see the new generated output.

    I want to pass in a file name, or multiple file names, into my Ant script, so that the XDoclet tag processes just those files (or all files if I pass nothing in). I have modified the XDoclet script in Ant to take one or more files, have tested it. Now I want to use that Ant target from NetBeans, passing in the current file or selected files in the Project view.

    This is about the same as "Debug Single File". The issue is, I can create an <ide-actions> entry for my special new target, and I can trigger it, say, with "Compile Single File". That is, NB already knows about the compile.single or debug.single targets and when I map those to an Ant task, it knows how to run them. How can I map a menu item to a custom <ide-actions> entry, such as "Run XDoclet on Current File"?

    That's it! The script works, everything works, I just can't trigger the script from within NB without a hack--changing the meaning of an existing tag like "Compile Single File" to mean something else.

    Thanks, Patrick

  • Geertjan Sunday, January 22, 2006
    Hi Patrick. Am I wrong, or do you want to see this in a free-form project's contextual menu?:

    <img src="http://blogs.sun.com/roller/resources/geertjan/runxdocletoncurrentfile.png">

  • Patrick Wright Sunday, January 22, 2006
    Hi Geertjan--yes, this is what I want to see. I may have more than one (since there are different XDoclet targets), so I want to map each one to a custom menu name on the project context menu. Is this possible? Thanks, Patrick
  • Geertjan Sunday, January 22, 2006

    <img src="http://blogs.sun.com/roller/resources/geertjan/runtothehills2.png">

    <img src="http://blogs.sun.com/roller/resources/geertjan/runtothehills1.png">

  • Patrick Wright Sunday, January 22, 2006

    Hi Geertjan--I am aware of that option, but it does something different. You can take a target in the Ant file and map it to a GUI menu command. When you do that, the Ant target is executed (pretty much) as if it had been run from the command line.

    What I am looking for is something a little different. If you look at the documentation on Advanced Free-Form Project Configuration, see section http://www.netbeans.org/kb/41/freeform-config.html#runsingle. There, they set up an Ant target that expects to be called from NetBeans, and passed a variable called ${runclass}. If that variable is not given, the target will fail. I'm doing the same thing, except I'm calling an XDoclet target with a variable called ${files}. If I map the way you did, on Project Properties/Build and Run, the Ant target will be called but with no special variables. However, it is possible to do, the question is whether it will work if you step outside of run.single, debug.single, etc. as the examples are limited to those situations.

    Try adding this to your Ant build file:

    <target name="do-single-file" depends="" if="netbeans.home" description="For a Single File">
    <fail unless="files">Must set property 'files'</fail>
    <echo>Targeting file(s): ${files}</echo>
    <echo>DONE</echo>
    </target>

    Then add this to your project.xml file:

    <action name="run-do-single-file">
    <target>do-single-file</target>
    <context>
    <property>files</property>
    <folder>source/java</folder>
    <pattern>\\.java$</pattern>
    <format>relative-path</format>
    <arity>
    <separated-files>,</separated-files>
    </arity>
    </context>
    </action>

    to your ide-actions section. No, how do you execute "run-do-single-file" from within NetBeans?

    Thanks,

    Patrick

  • Geertjan Monday, January 23, 2006
    Thanks for your patience in explaining your question! I will look at this more closely, but maybe, in the meantime, this might be of help -- the Ant input handler. Ive described it here and here . Maybe you can adopt that approach?
  • Patrick Wright Tuesday, January 24, 2006
    Hi Geertjan--my post isn't showing up--to answer our question from Jan 23--no, Ant input handlers don't help. But maybe the answer to my question is simply "can't be done"--I just noticed this sentence in th Advanced Free-Form Project Configuration page, "The ide-actions element holds the mappings for IDE commands. You enter an action element with the name for any of the standard IDE actions and define the script and target to which you want to map the command." If I want to map a non-standard IDE action, I may be out of luck. Patrick
  • Jamie Wednesday, April 5, 2006
    That's really useful but... how do you create an action on a particular project's node? i.e., not a general action but one specific to the currently selected project?
  • Geertjan Wednesday, April 5, 2006
    I don't think projects can be extended. I don't think there's an API for that. (You could do so via Ant scripts in free-form projects, but that only impacts the current application, not the project type, and that would have nothing to do with modules.) I recommend writing to dev@openide.netbeans.org (and joining it if you haven't already) with this question.
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.