Thursday May 26, 2005
Baking a First Cookie for a NetBeans Module's Data Object
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!
May 26 2005, 09:38:23 AM PDT Permalink




