Friday November 30, 2007
Guide for Porting Swing Applications to the NetBeans Platform
Observe the zebra, in a place where it never thought it would be. Imagine all the advantages. Now that it knows how to get there, to the top of the giraffe, with an amazing view right over the savannah, it can pass on this ability to its offspring. Its offspring, in turn, will then have the same ability and the zebra will evolve from a weak and helpless victim to a versatile, interesting, and awe inspiring member of the animal kingdom.
Now, look at your humble Swing application. Imagine where it could be... on the NetBeans Platform, of course. There, your helpless little application would gain a robustness it had never dreamt of attaining. When predators come prowling through the wilderness, when competition comes lurking round the corner, your application will be ready. The giraffe can see much further than any other animal, which is why when a giraffe starts running, all the other animals start running too, because they know danger is approaching. Similarly, the NetBeans Platform has a set of features that a typical Swing application simply does not have, such as an out-of-the-box state-of-the-art docking system and a pluggable modular architecture, to name just two. But... how to make the journey? How to evolve as quickly and smoothly as possible? How to get to the top of the giraffe? Here, for the first time, we present:
NetBeans Platform Porting Tutorial
Try it. It works. Look at that zebra. Then look at your app. Imagine that zebra had had a ladder. Lucky you, you do. You don't need to get to the highest point right from the start. You can take small steps. Over a long period of time. Gradually, as you become more and more familiar with the terrain, as you discover all the benefits you are accruing on your journey, your desire to get further will increase. And once you are up there, why would you ever want to get down again? The giraffe will do all your walking for you. You will acquire new habits, sure, it will take some time to adapt, especially because the giraffe is a bit awkward in places. Initially it'll be tough to get used to that long neck. In the final analysis, though, you'll be far better off than you were at the start. You'll find that it is exactly that tricky long neck that's made the journey worth the while and, as you breathe deeply and survey the horizon, you'll be thankful for all that it's giving you.
So, read the above document, feedback welcome. Let's get your zebra up that giraffe.
Nov 30 2007, 01:03:14 PM PST Permalink
1.5 Version of Google Translation Service Integrator
I incremented the NetBeans Google Translator Integrator to 1.5. One can now translate text in any kind of file (Editors/Popup folder, instead of Editors/mime-type/Popup), not just HTML or Java. Go here to get the plugin:
http://plugins.netbeans.org/PluginPortal/faces/PluginDetailPage.jsp?pluginid=4189
You'll get a ZIP file with two NBMs. When you're in the Plugin Manager, you'll see this:
After installation, you can translate to your heart's content. Select the from/to languages in the Translate toolbar, then select your text to be translated, and then right-click and choose Translate:
For example, here's something I pasted into the text editor, something I got from the Arabic CNN site:
Then, I chose the Translate menu item and got this:
Many scenarios where this feature might be useful can be imagined. Remember that you're interacting with the on-line Google translation service, under the hood, so make sure to be on-line. If there's an error for any reason, the status bar will tell you so.
Here's a funny Dutch/English translation:
By the way, does someone know how to get the selected index displayed in a JList? By default, the first value in the list is shown in the JList, which is Arabic/Arabic. However, I'd like it to scroll to the selected index by default, at startup, which is English/French. Can someone tell me how to do that?
Nov 29 2007, 10:11:45 AM PST Permalink
Caption Competition Winner
Roumen recruited me (unasked and without any form of payment being offered, so far, even in liquid form, which would be acceptable too, hint hint) to judge his ad-hoc caption competition. The photo requiring a caption was this one from NetBeans Day Cairo:
The comments at the end of his blog entry indicate a lot of enthusiasm and creativity among Roumen's readership. I have been given no guidance as to the criteria for judging the winner, so I guess I will make up my own. For a start, the captions entered must make some kind of sense, at least, they must make some kind of sense to me, as the judge of this illustrious competion. So, that excludes several right from the start. Also, a certain sense of humor is important for caption competitions. Humor is, again, subjective. However, based on these highly subjective criteria, and bearing in mind that multiple entries per person were allowed, the shortlist of possible winners is cut down to the following:
- "Can I order pizza for me and my friends?" (Michael Hüttermann)
- "Okay guys, who took my wallet?" (Keith Blackwell)
- "Back to the future, part 6: 'Yes, they all use NetBeans 6. Very good. And now back to '07.'" (Michael Hüttermann)
- "Damn. I wish someone had been more specific about those 77 virgins." (Robert)
- "I didn't think so many would respond to my 'Swap your girlfriend for a NetBeans CD' campaign." (Jack)
- "Cool party! But I wonder which one of them is 'Lonely nerdy Arab guy, in an experimental phase, looking for a good time'." (Harry Peterson)
- "I feel like a NetBeans Plugin!" (David)
- "java.lang.OutOfMemoryException!" (NetPaladin)
The above... is the shortlist. I must say I at least smiled a tiny bit at each of them. Some of them border on the inappropriate, but that's precisely what makes them funny. :-) And.. the final winner is (and remember, yada yada yada, there are no losers here, yada yada yada, we are all winners, yada yada yada, it's not the winning, but the taking part that matters, yada yada yada)... drumroll...
"java.lang.OutOfMemoryException!" by NetPaladin! Hurray! That one combines geeky humor with how Roumen probably felt standing there amidst all those happy faces, flushed with the excitement of just having heard one of his cool presentations. NetPaladin, you win a NetBeans 6.0 t-shirt. Please send your details to roman DOT strobl AT sun DOT com and you will get your t-shirt sent to you in the coming days. Thanks all for participating!
Nov 28 2007, 05:11:59 AM PST Permalink
Where's the AST View? And the Tokens View?
If you, inspired by Heinrich Schliemann's gift for languages, are bringing that gift to NetBeans IDE, you will by now know about the AST view and Token view that the IDE provides to help you. Many have cited these views as just about the most useful thing provided by the Schliemann project. Put a different way, if you are using Schliemann without the AST view and Token view... your life must suck. :-) At least, it sucks a lot more than it should, because you're driving your car with your eyes closed. These views show you the impact of your grammar rules and tokens on a file of the type for which you're creating the editor. Here is an example of the AST view in action:
The power of the AST view in debugging your grammar rules is discussed in detail here.
However, these two views are potentially confusing for end users (i.e., for people who are not module developers or, even, those who are module developers but do not need to write a Schliemann editor). Hence, in the RC releases, certainly in RC2, these views are not easy to find. We don't want the unwary user to stumble across them and get confused or worried or something similar. So, now, if you want to see these views... you must have an NBS file. You'd probably begin creating your Schliemann editor via the Language Support wizard in the New File wizard. So, now you have an NBS file (i.e., "NetBeans Scripting", which defines your tokens, grammar, and feature mappings). Now you can right-click this file and choose "Tokens View" or "AST View". And then these views open and now... they show you the AST and tokens of your NBS file.
But you don't want that! You want to see the AST and tokens of your own file. No worries. Just leave the window open, now that you've opened it. Leave it open! Then... select your own file. Thanks to selection management in NetBeans IDE, the views show the AST and tokens of the currently selected file. So now you can go ahead and select your own file and the windows will be populated with the correct AST and tokens. Drag the window somewhere else if that will make working with it more comfortable for you. You can even undock it (Alt-Shift-D) and then it will be freed from the NetBeans IDE frame.
Yes, maybe slightly cumbersome (but a lot less than this blog entry makes it seem). However, since these views are intended for a microscopically tiny fraction of developers, when compared to the entire NetBeans user base, it is provided in such a way that only those specifically needing them can can get to them. An alternative might have been to only show the views in the Window menu if an NBS file is created, but that would be contrary to all design principles. And the problems that this solution would cause would make the solution totally meaningless. Hence, under the circumstances, it all makes complete sense. However, it is good to know what happened and... now you know too. I will add this blog entry to our fantastic NetBeans Developer FAQ for future reference.
Nov 27 2007, 08:27:56 AM PST Permalink
Project-Level License Settings in NetBeans IDE 6.0
In my earlier series on FreeMarker support (especially this one), I discussed the many flexible ways in which templating support in NetBeans IDE 6.0 can be of benefit to you. Importantly, the flexibility can be made use of either by the developer providing the templates or by the user of those templates. (That series discusses this, and the differences, in some detail.) One point that I hadn't discussed yet relates to licensing. The short story is: in NetBeans IDE 6.0 you can define a license for your project and then all files within that project will be created with that license embedded in the code. OK, that's the short story.
The long story makes sense in the context of a step-by-step procedure:
- Go to the Tools menu. Choose Templates. Open the Java|Java Class template in the editor:
- In general, the template above, and the ramifications of defining it in FreeMarker, have been discussed in the earlier blog entries (starting here). However, lets look (this time) at the first four lines:
<#assign licenseFirst = "/*"> <#assign licensePrefix = " * "> <#assign licenseLast = " */"> <#include "../Licenses/license-${project.license}.txt">What do these lines mean? They all have to do with licensing. The last line determines the license that will be used, per project. The first three determine the characters in front of and behind each line in the license. Above are the four lines for Java source files. Here's the same set of definitions you'll find for the Properties file template:
<#assign licensePrefix = "# "> <#include "../Licenses/license-${project.license}.txt">What's the difference? Nothing in the second line (which we'll look at later). The first line, however, tells us that each line in the license will be prefixed with a "# ", instead of with a " * ", which is the prefix for Java source files (with "/*" for the first line and " */" for the last line). To verify this, create a Java source file and then create a Properties file. You'll see a license in both cases. However, the characters prefixing and postfixing each line is different. Now you know why.
- Next, let's look at the license itself. Notice this line in the templates above:
<#include "../Licenses/license-${project.license}.txt">In particular, notice this bit:
${project.license}Put that, as a key, in your application's nbproject/project.properties file. Now add a value. For example:
project.license=apache
Now look in the Template Manager again, in the Licenses folder. You see some templates there. Create a new one called "license-apache.txt". For now, you can just copy an existing one and paste it in the same category in the Template Manager. Then, next time that you create a file that is defined by a FreeMarker template that includes this line:
<#include "../Licenses/license-${project.license}.txt">...you will have the specified license embedded within the newly created file.
What's the purpose of all this? Simple: NetBeans IDE 6.0 lets you define, per project, the license that each of its files should display. Plus, imagine if you need to create a new project with a different license. Assuming you have all your licenses defined in the Template Manager, using a new license is as simple as adding that one key/value pair to the nbproject/project.properties file. That was not possible before but, thanks to FreeMarker support in NetBeans IDE 6.0, is possible now.
Nov 26 2007, 09:01:54 AM PST Permalink
Masters Thesis on the NetBeans Platform
Visualization of Program Dependence Graphs, by Thomas Wurthinger, from the Johannes Kepler University in Linz, Austria, is a paper dealing with the Java HotSpot VM server compiler. It was successfully submitted in partial fulfilment of the requirements of the degree of Master of Science at the abovementioned university.
Below follows a brief interview with Thomas, about his thesis (which is well worth browsing through, in fact, I highly recommend it):
What was your thesis about?
My thesis was about the visualization of the internal graph data structure of the server compiler of the Java HotSpot virtual machine. I developed an application for displaying, filtering, and analyzing the program dependence graph of the compiler. The tool helps to debug the server compiler and understand its optimizations.
Why did you decide to use the NetBeans Platform for your thesis?
Because it fitted best for the purpose of creating a modular GUI application without having to think about the window system or module management. Additionally, the NetBeans Visual Library provides an easy-to-use API for displaying graphs, which is perfectly suitable for my application.
How did you first hear about the NetBeans Platform?
First time I stumbled over NetBeans was when I was porting a C++ application to Java and I was looking for a Java GUI framework. I found the NetBeans Platform homepage and created a prototype application.
How was it to use the NetBeans Platform? Advantages/disadvantages?
The disadvantage of using the NetBeans Platform was the time needed at the beginning of the project to get familiar with the platform. As the application got more and more complex, the advantages of using modularization and the frameworks became clear.
Did it help you in some way in relation to your thesis?
Building my application on top of the NetBeans Platform meant that during development I could focus on the application logic. Plus, the modularization concepts of the NetBeans Platform forced me to give the application a clean modular structure.
How has your thesis been received? What kind of results did you get?
The tool developed as part of the thesis is currently tested by the HotSpot compiler team at Sun Microsystems. Based on the results, I will further improve the application and then publish it as an open source project. The hierarchical layout algorithm implemented for the tool will be integrated into the NetBeans Visual Library.
What are you going to do now that you have graduated?
Currently, I'm doing my civil service. Afterwards, I would like to continue working as a researcher and maybe do a PhD thesis in the area of compiler construction.
And will you continue to use the NetBeans Platform in some way?
Of course I will continue using the NetBeans Platform for developing GUI Java applications. Now that I'm familiar with the concepts of the platform, there's an even greater advantage to using it.

Nov 25 2007, 12:41:25 AM PST Permalink
Citra FX Photo Filter 2.0
What do the two screenshots below have in common?
They both come from Citra FX Photo Filter 2.0, which is a filter effects studio for digital photos. The latest release was announced about a week ago. And... the application is created on top of NetBeans Platform 6. Tonny Kohar and his team at Kiyut in Indonesia are behind this cool software. Here, in an interview conducted over the past few weeks, he spends some time talking especially about the classes they used from the NetBeans Platform 6 APIs.
Nov 24 2007, 01:24:41 AM PST Permalink
Big Milestone for Groovy and Grails in NetBeans IDE
The biggest development of Groovy and Grails in relation to NetBeans IDE happened yesterday: "Groovy and Grails support in NetBeans has been enabled on Development Update Center." I mean, the most concrete contribution of Groovy and Grails support and the clearest sign that NetBeans loves Groovy (and Grails). The work done by Martin Adamek and Matthias Schmidt is now, for the first time, available in the update center for NetBeans Development Builds (i.e., for post-6.0 builds, hence, not in RC 2). They will not be part of the standard NetBeans IDE 6.0 distribution, because of timing and planning and so on, but anyone with a development build (obtainable from netbeans.org download page) can go to the Plugin Manager and look for Groovy:
Partly, this is what I referred to a few days ago in this blog: "Interestingly, I've talked to lots of people internally in NetBeans and Sun recently about Groovy, even more so than before. Watch this space for some interesting announcements coming up in the next months (or maybe even sooner)."
After installation of the plugin, one has new project templates:
You need to register Groovy and Grails in the Options window, before you can use them. I don't like this. But I can see how it is preferable. It means a smaller NBM distribution and no need for licensing issues, because the user has to register Groovy and Grails themselves. That also allows the user to set a different Groovy or Grails version, if so desired. You could set Groovy 1.0 and then later upgrade to Groovy 1.1, without needing to wait for a related NBM to be distributed. Still, I'm lazy and I liked the approach that I took, where everything was handled internally.
After the above registration, you can generate Grails projects and deploy them. You can also run Groovy scripts:
And how does this relate to my plugin in the Plugin Portal? It doesn't really. It is based on much firmer footing than my plugin. It makes use of the General Scripting Framework, which Tor Norbye is spinning out of his JRuby support. (So, yes, this implies Groovy support in NetBeans IDE will be similar/same as what Tor has been doing for JRuby.) It makes use of the NetBeans Lexer API. It really is the right approach to take, while I took the simplified Schliemann approach. I did that because I wanted a quick result; I wanted to start working with Groovy immediately and Schliemann afforded that opportunity. I got "good enough" results, was able to run Groovy scripts (as well as Java/Groovy combinations, which the Martin/Matthias plugin doesn't yet support). Plus, part of the motivation for my plugin was to say to the Groovy/Grails community: "Hey guys, there are plenty of people in Sun who love your work and, look, one of them has even produced a NetBeans plugin." Both these use cases are now becoming obsolete, because the new plugin caters to both of them. So, sooner or later, I will remove the plugin from the Plugin Portal and point to a tutorial that describes how to use the official plugin in NetBeans. But, since the official plugin will only be official after 6.0, that could still take some time. You're welcome to try either plugin, though the future is clearly with the other one.
So, if interested in Groovy/Grails, download a development build, install the plugin, play around with it, and then file bugs and enhancements in the 'groovy' category in Issuezilla. Information and tasks for this plugin can be found here. The Groovy plugin is built three times a day as part of the development build and committers (you could be one of them, if you're interested) need to be careful because they could even break the build. That's how committed NetBeans is to Groovy and Grails and is a sign of how solid the plugin already is: we are even willing to have the NetBeans builds break for Groovy and Grails.
In other news. Have a look at Wouter van Reeven's article on Seam, Maven, NetBeans IDE, and GlassFish, here.
Nov 23 2007, 02:28:13 AM PST Permalink
Direct vs. Precompiled Mode in Groovy
Groovy can be used in two modes, Dierk König et al, in "Groovy In Action" tell us, on page 19:
- Direct mode. Code is "directly executed, without producing any executable files".
- Precompiled mode. The second way, which involves taking the code and "compiling it to Java bytecode and running it as regular Java application code within a Java Virtual Machine (JVM)".
Importantly, notes the book: "Both ways execute Groovy inside a JVM eventually, and both ways compile the Groovy code to Java bytecode. The major difference is when that compilation occurs and whether the resulting classes are used in memory or stored on disk".
I'm stating the above things so explicitly because of the implications this has for the Groovy plugin on the Plugin Portal. If, for example, I run Groovy script that looks as follows, there is no need to compile the code explicitly. It simply works, because here we are making use of direct mode:
Here (i.e., in the pic below), however, the situation is different. Here we are dealing with a closure. When we run this by simply right-clicking in the editor and choose Run, as done above, nothing appears in the Output window, because we are using direct mode, while we should be using precompiled mode:
To use precompiled mode, we basically need to set up a Groovy harness within our Java application. Parts of this are already supported by the Groovy plugin. Parts are not and need to be done manually. I've been doing them manually all along, but will automate the following steps in the next version of the plugin. Here I'm just writing them down so you know what you need to do to run scripts such as the above. (I'm also doing this as a reminder to myself of what I need to do to automate this next time I work on the plugin.)
- We need to use groovyc to compile the script. (Joint compiler is something I haven't even begun to look at yet.) However, everything you need for groovyc is already provided by the plugin. Start by creating a new application after installing the plugin. (Only Java applications supported currently, though possible in others, not explicitly supported.)
- Now, right-click the Libraries node and choose Add Library. Choose Groovy and then click Add Library and then (a snapshot build of) Groovy 1.1 RC 2 is added to the Libraries node. That library includes groovyc. Now we need an Ant script that will call it.
- Right-click the application's project node and choose Properties. Note there is a new panel, called "Groovy", provided by the plugin. In it, click the checkbox that says "Ant Task for Compiling Groovy". Then click OK.
- Now switch to the Files window. In the nbproject folder, you will find an Ant script called groovy-compile-impl.xml. Open it. Notice that it will compile to a folder called "groovy". Create that folder in the Files window. (That will automatically be done for you in the next version of this plugin.) Notice also that the Ant script refers to a library (the snapshot library that you added to the project earlier). The script says that the library is in dist/lib. Currently it isn't there.
- Build the project. Now, back in the Files window, you have dist/lib containing the Groovy snapshot build.
- The earlier step where we generated the Ant script did more than just generating the Ant script. It also hooked it up in such a way that, when you right-click the build.xml, you can choose the "groovyc" target. (The next version of the plugin will hook that target to the project's Compile target, so that those two work together. Or the joint compiler will somehow be included.) You can now choose that "groovyc" target. Since we have no Groovy scripts, nothing is compiled, but at least you can see that it is now possible.
- Now go back to the Projects window. Right-click the Libraries node and add the "groovy" folder to the Libraries node. We need to do this to let the (currently non-existent) compiled Groovy classes be available to code completion in our Java source files.
- Now add a Groovy script, but in a Java class format, i.e., with a package declaration, class declaration, and method declaration:
package javaaplication36; class newScript { public static void main(String[] args) { def x = 1 3.times { println x++ } } } - Next, run the groovyc target. The groovy folder, visible in your Libraries node, now shows the compiled class, as well as a class for the closure:
- And now you can call the main method above from your Java class:
newScript.main(args);
It has to be done this way, because currently one can't run the Groovy script (except in direct mode) and there is no right-click Run action on compiled Java classes. (That would be a very nice thing to have.)
- When you run the application, you now get the expected results, i.e., via the closure in the Groovy script (or, in fact, via the compiled Java class):
The moral of this story is that if you try to run a Groovy script using the plugin, and nothing appears in the Output window (or wherever it should appear), the reason probably is that you need to be making use of precompiled mode. The above is the current way of setting that up. It might seem slightly cumbersome, and currently it is unnecessarily cumbersome, because the plugin could take care of more things than it does right now (i.e., creating the 'groovy' folder, adding it to the Libraries node, and hooking the Ant groovyc target to the general build process), none of which would be hard to do. Also, I will include samples in the next version, i.e., samples that have the correct folder structure and so on already defined, so that you can look at how it should be done. Anyway, until those parts are automated, now at least you know what you need to do in these cases.
Note, again, that the above procedure only applies if you are using Groovy's precompiled mode. Otherwise, simply right-click in the editor and choose "Run". The Groovy script is then directly executed without producing executable files.
Nov 22 2007, 04:52:47 AM PST Permalink
Groovy Plugin Updated to NetBeans IDE 6.0 RC 2
In celebration of the release of NetBeans IDE 6.0 RC 2, I updated the Groovy plugin on the Plugin Portal. Where before you would download an archive (containing 5 NBMs) called "groovynbms-4.zip", this time you'll get "groovynbms-5.zip". What are the changes? Nothing other than that the plugin now works in NetBeans IDE 6.0 RC 2 and that Groovy 1.1 RC 2 is supported (instead of a snapshot build).
So, if you liked it before, you'll like it now. If you didn't, you still won't. The update replaces the older one, hence you will only be able to use the plugin in NetBeans IDE 6.0 RC 2 and nowhere else. (At least, it might work elsewhere but I will not help you out if you install it somewhere else and then encounter some problem or other). It's pretty cool that the current download number for that plugin is 661. Probably about 20 of those are me, but not much more than that. So, I hope those who downloaded it weren't too disappointed by its modest functionality (though, you shouldn't have been had you read the description on the plugin's page). Others have written to say they like it. Well, whatever works works.
All the functionality in this releaselet is identical to what it was before, with small templates, some basic syntax coloring and code completion, and "run script" support, as shown here:
Go here to get it:
http://plugins.netbeans.org/PluginPortal/faces/PluginDetailPage.jsp?pluginid=3914
Interestingly, I've talked to lots of people internally in NetBeans and Sun recently about Groovy, even more so than before. Watch this space for some interesting announcements coming up in the next months (or maybe even sooner).
In other news. Have a look at Wouter van Reeven's article Combining Hibernate and Facelets with Maven, NetBeans, and GlassFish.
Nov 21 2007, 01:35:25 PM PST Permalink
To Out... Or To Err... That Is The Question
I've been told that some NetBeans users, and some potential NetBeans users, don't like the Ant output being written to the Output window, together with the application's actual output. One can see the point. Below, it's not immediately clear which bits come from Ant and which come from the application:
However, maybe the solution is simpler than it seems. Use System.err instead of System.out, and violin, as they say in French, the distinction between the application's output and the Ant output is very clear:
Still, it would be nice if there was a way to exclude the Ant output, via an option setting. And switching to "Quiet" mode in the Ant settings doesn't help. Believe me, I've tried.
Nov 20 2007, 02:21:53 PM PST Permalink
SaveCookie (Part 2)
SaveCookie (Part 1) worked but wasn't very efficient, because we were passing a field around, i.e., a boolean that was set whenever a text field was modified. Plus, we could have used getCookieSet().assign to assign our implementation of SaveCookie to the cookie set. So, here's the new and improved save functionality for a text field in a TopComponent, with help from Jarda to create it. As before, we begin by creating a node in the TopComponent.
private class DummyNode extends AbstractNode {
SaveCookieImpl impl;
public DummyNode() {
super(Children.LEAF);
impl = new SaveCookieImpl();
}
public void fire(boolean modified) {
if (modified) {
//If the text is modified,
//we implement SaveCookie,
//and add the implementation to the cookieset:
getCookieSet().assign(SaveCookie.class, impl);
} else {
//Otherwise, we make no assignment
//and the SaveCookie is not made available:
getCookieSet().assign(SaveCookie.class);
}
}
private class SaveCookieImpl implements SaveCookie {
public void save() throws IOException {
Confirmation msg = new NotifyDescriptor.Confirmation("Do you want to save \"" +
guessedWord.getText() + "\"?", NotifyDescriptor.OK_CANCEL_OPTION,
NotifyDescriptor.QUESTION_MESSAGE);
Object result = DialogDisplayer.getDefault().notify(msg);
//When user clicks "Yes", indicating they really want to save,
//we need to disable the Save button and Save menu item,
//so that it will only be usable when the next change is made
//to the text field:
if (NotifyDescriptor.YES_OPTION.equals(result)) {
fire(false);
//Implement your save functionality here.
}
}
}
}
We'll need to refer to this node in other places, so let's create a class variable:
private DummyNode dummyNode;
Here's how the node is added to the TopComponent, i.e., in the constructor:
setActivatedNodes(new Node[]{dummyNode = new DummyNode()});
And then we notify the node whenever changes are made to the text field:
myTextField.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent arg0) {
dummyNode.fire(true);
}
public void removeUpdate(DocumentEvent arg0) {
dummyNode.fire(true);
}
public void changedUpdate(DocumentEvent arg0) {
dummyNode.fire(true);
}
});
Because we pass true to the node, our implementation of SaveCookie is added to the cookie set, which enables the Save button (if you have one, which you don't by default), as well as the Save menu item under the File menu. If you want a Save button in the toolbar, add this to the layer:
<folder name="Toolbars">
<folder name="File">
<file name="org-openide-actions-SaveAction.instance"/>
</folder>
</folder>
In other news. Have a look at this pretty brilliant clip on YouTube: The future is in the past!
Nov 19 2007, 04:33:29 AM PST Permalink
Analyzing JURPE
I wanted to understand the relationships between the classes of the JURPE demo's wizard package. So, I installed the UML plugin into 6.0 RC 1. I thought that maybe because the demo is an Ant project, the UML support would not apply to the project. But I was wrong. Two clicks later, I had the complete dependency graph generated for me:
Hurray for UML in NetBeans IDE.
Nov 18 2007, 03:30:42 PM PST Permalink
Austria in Prague
Around 50 students from various universities in Austria came to see us at Sun in Prague on Thursday. The students are visiting a range of companies (e.g., Siemens and Skoda too) all over the place, it seems. We did presentations on Sun, OpenSolaris, NetBeans IDE, and GlassFish. Here's a pic that shows the general setting, with Petr Jiricka opening the day with a presentation about Sun:
I had the honor of introducing NetBeans IDE and here is my presentation. I really stressed the Java angle (since we had been told the students wanted to hear about Java and Sun), i.e., that Sun supports Java development by providing tools and how the NetBeans team creates all those tools and delivers them in the form of NetBeans IDE. It was pretty cool—during my presentation I developed an application which the GlassFish guys deployed during theirs, showing the seamless integration between development and deployment via the IDE. The questions and discussion at the end were interesting, about UML, Eclipse, plugins, the NetBeans Java editor, NetBeans Platform, and future developments of NetBeans IDE.
Nov 17 2007, 03:50:09 AM PST Permalink
SaveCookie (Part 1)
Let's switch on the Save button (and the related Save menu item under File menu) for a text field. First, we need to listen to changes in the text field and then set a boolean on the change:
private boolean change = false;
myTextField.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent arg0) {
change = true;
}
public void removeUpdate(DocumentEvent arg0) {
change = true;
}
public void changedUpdate(DocumentEvent arg0) {
change = true;
}
});
Now we are listening on the text field and whenever something changes within it, a boolean is set to true. Next, we create a node in the TopComponent that holds the text field. A TopComponent has an activated node, which we set in the TopComponent's constructor:
setActivatedNodes(new Node[]{dummyNode = new DummyNode()});
And declare the node at the top of the class:
private DummyNode dummyNode;
And here is the node itself. We are dealing here with cookies, which are capabilities. If the capability to save is available (which is true by default for TopComponents) and our boolean is set to true, the save button (and menu item under File menu) will be enabled. Whenever we want to tell the node that we have made a change, we call fire(), which fires a change event on the node. So, below we call fire() after the button is enabled, at which point we set the boolean to false, so that our fire() will result in the button being disabled, thus toggling on/off when clicked.
private class DummyNode extends AbstractNode {
public DummyNode() {
super(Children.LEAF);
}
@Override
public Node.Cookie getCookie(Class type) {
if (type == SaveCookie.class && change == true) {
return new SaveCookie() {
public void save() throws IOException {
change = false;
fire();
Confirmation msg = new NotifyDescriptor.Confirmation(
"Do you want to save \"" + guessedWord.getText() + "\"?",
NotifyDescriptor.OK_CANCEL_OPTION,
NotifyDescriptor.QUESTION_MESSAGE);
DialogDisplayer.getDefault().notify(msg);
//Implement your save functionality here.
}
};
} else {
return super.getCookie(type);
}
}
public void fire() {
fireCookieChange();
}
}
Now we need to also fire change events in the document listener. Below I've highlighted the code I added:
myTextField.getDocument().addDocumentListener(new DocumentListener() {
public void insertUpdate(DocumentEvent arg0) {
change = true;
dummyNode.fire();
}
public void removeUpdate(DocumentEvent arg0) {
change = true;
dummyNode.fire();
}
public void changedUpdate(DocumentEvent arg0) {
change = true;
dummyNode.fire();
}
});
And that's it. Now the Save button (and menu item under File menu) is enabled whenever a change is made in the text field. At that point, a small dialog appears asking whether you want to save. When the user says OK, something should happen, maybe the content of the text field ends up stored in a file. But that's something you need to decide on and implement yourself. Here the focus is purely on one way of integrating your text field with the NetBeans Platform, i.e., by hooking it up to the NetBeans Platform's Save functionality.
Nov 16 2007, 12:31:15 AM PST Permalink



