Geertjan's Blog

  • May 26, 2005

Baking a First Cookie for a NetBeans Module's Data Object

Geertjan Wielenga
Product Manager
In Adding an Item to a File's Pop-Up Menu in NetBeans IDE 4.1 I learnt, well, exactly what's described in the title of that blog entry. However, what I didn't mention is that the Open item, the View item, and the Edit item were disabled. So, while I was able to use the Cut and the Paste items, I wasn't able to open the HTML file, to view it, or to edit it. Cutting and pasting are standard actions -- there's only one conceivable reason why you'd choose those items; this is not the case for Open, Edit, and View. There are different ways of viewing and different editors in which you'd maybe like to open and edit a file. Hence, you need to tell the IDE (or the Platform) exactly what should happen when these items are chosen. For this reason, they're disabled by default (click the enlarge):

To enable them, you need to create a cookie. In the context of NetBeans Modules, a cookie has nothing to do with the message sent from a web server to a web browser. It's a completely different concept. (I don't know why it's the same word, but there are probably entirely logical historical reasons for it.) Reading and browsing through various documents, I've come across the following handy definition of what a cookie is: "Cookies are design patterns that you attach to a data object or a node to add functionality to the data object or the node. Cookies provide information to the the IDE or Platform about the types of operations that data objects or nodes are capable of supporting."

There are various ways to create cookies. The simplest (and hence the one that I'm most drawn to!) is to modify the data object by providing an ad-hoc implementation of a cookie interface (such as OpenCookie, ViewCookie, or EditCookie) and adding it to the CookieSet instance. So, it's best to show this in a screenshot:

As can be seen in the above screenshot, the OpenCookie interface has been implemented and added to the CookieSet instance. As a result (after rebuilding and reloading the module) the Open item is now enabled. Nothing can be done with it yet (it currently provides a JoptionPane with the text "Open Sesame!") but at least the item is now enabled. And... I've finally managed to bake my first cookie!

Join the discussion

Comments ( 6 )
  • CharlesS Friday, May 27, 2005
    Great BLOG!
    I have successfully done this
    "Adding an Item to a File's Pop-Up Menu in NetBeans IDE 4.1"

    When I create a JSP Project I can right click on the FileSystem and see new actions "Validate All Pages" and "Export War File"...

    How can I hook into that Menu to add an action that says "Insert New Visual Template File."?

    Clicking that Action should create a new file and open our TopComponent Editor to edit it.

    I have tried your "Playing with File Types in NetBeans IDE 4.1" but I don't want to hide all options that someone else put in. I simply want to add to them.

    I am at a loss can you provde insight?

    Thank you so much.
  • CharlesS Friday, May 27, 2005
    I should probably mention I have since now been trying only on 3.6
    does that make a difference?
  • CharlesS Friday, May 27, 2005
    I should probably mention I have since now been trying only on 3.6
    does that make a difference?
  • Geertjan Saturday, May 28, 2005
    Hi Charles, thanks for your question. What you want to do is create a template. That's something I'd like to blog about soon. However, what you also want to do is add a project-level menu item. I don't know how to do that in 3.6, not sure if it's even possible. I'd recommen moving to 4.1 (many new and cool features there anyway). By the way, when you've created a module to add a template (like I said, something I'll cover sometime soon) you wouldn't create the template via a project-level menu, but via the New File wizard, which is where the template would be installed. Hope this is clear and if not please leave another message! Thanks for liking the blog. (It's really written for 4.x (i.e., 4.0 or 4.1) so if anything works under 3.6, that's a bonus but unintentional.
  • CharlesS Wednesday, June 1, 2005
    Dear Sir,

    Your answer is great and thank you so much for your blog entitled "Create File Templates for NetBeans Module Development." I have a working sample now, in 4.1, much better!

    Your solution is good but I would like to clarify our case:

    What we want to do is have the user right click on a menu, somewhere, and insert a new file with our extension. The new File Wizard does the trick and works well. Here is our problem:
    Our file (.vector extension) is simply a text file that we parse which eventually produces an image. So we need to then insert some code to our jsp file to manipulate the .vector file to create an image.

    This will require us to insert text (StyledDocument.insertString) into a JSP page, something I would LOVE you to blog about.

    Now, if we just click new file on the folder level two problems exist:

    If there is no jsp file in the folder the .vector file is useless, and if there are two jsp files which one gets the injected code?

    So for us right clicking on the jsp file solves the two problems?

    I have thought about adding to the Tools menu but don't know how?

    Lastly, I do have a few issues maybe you can shed some light on:

    1) When I right click on the sample.vector file I get two options: Open and Save, because in my DataObject I add two objects to getCookieSet()
    The first one (Open) is an object which implements OpenCookie. When I get called with the open method I create a TopComponent, make it visible, and pass it the contents of the .vector file. Thus I can visually change the contents of the image using my SWING program.
    I can't figure out how to make the '\*' appear that indicates that the file is dirty. Like when you open any file and start typing, immediately the user knows the file is dirty because of the '\*'

    Any ideas on how to achive the dirty mark?

    2) As our netbeans module has some dependencies I need to figure out how to package them in the nbm file and have them extracted to the correct folder later? Any good links?

    3) And finally, some of those same dependencies are used at runtime so when I create a new .vector file I will need to add them to the Projects->Libaries node so they will be there to compile.

    You have been most helpful and I thank you for your time.

    Keep Blogging!

  • Ken Friday, March 31, 2006
    This blog is cool! Cheers!
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.