Wednesday January 31, 2007
JavaMail Client on the NetBeans Platform (Step 2)
So, let's move our simple JavaMail client to the NetBeans Platform. At the end of this procedure, NetBeans IDE (or any application on the NetBeans Platform) will have a new "Mail Window", as shown in the "editor" area of the screenshot below:
The IMAP server, in this case, is accessed, the e-mails are retrieved, and displayed in their folders. Some can also be opened for reading, but as pointed out yesterday, there's some problem with that part of it in the original application. For a larger screenshot, click here.
When porting (i.e., migrating your Swing application to the NetBeans Platform), there are three really important principles. Only three? Yes. Only three. Here they are:
- Decide whether to reproduce or leverage. Ask yourself this question: Do you want to replicate your existing user interface and functionality or do you want to leverage the NetBeans Platform's idioms and infrastructure? This is an important question. A few blog entries ago, when I wrote about porting a movie player to the NetBeans Platform, I simply dumped the JFileChooser, because the NetBeans Platform's Favorites window provides the same (in fact, better) functionality... for free. No programming required. However, in this case, where the application is quite a lot more complex, let's begin by simply replicating (i.e., keeping as much of the original functionality as possible) the functionality, since that requires less re-work than the leveraging (i.e., where possible, replacing the original functionality with better functionality provided by the NetBeans Platform) approach. Also, I don't really know the simple JavaMail client so well, and I don't know JavaMail very well either, therefore I'd rather not mess around with the internals of the application. However, the downside of this approach is that my application will not integrate very well with the NetBeans Platform. Ideally, each folder and e-mail would be represented by a node provided by the NetBeans Nodes API. But, since I'm not going to go down that route, I'll not have nodes. This means that it will be much more difficult to extend the module once I've created it. I mean, I could extend the module itself, but I won't be able to plug new functionality into the ported mail client, because I will have no extension points at my disposal. However, doing it the simplest way is probably the best way to go initially. Once everything is working okay, I can begin replacing the pieces with NetBeans API equivalents, such as a NetBeans API TreeTableView, for example.
However, as you can see from this, porting is not a trivial task. It basically means, in many cases, if you want to do it properly, rewriting your application. And I can't imagine a wizard that would be helpful in automating the process. Not every JFrame should become a TopComponent, for example. (And you may as well use the Window Component wizard instead, anyway.) Despite the fact that porting isn't trivial, the further you port (i.e., the more you integrate) the greater will be the benefits that you gain, especially in the sense that you'll be able to expand your functionality in a modular, sustainable, scalable way -- even after distributing the module that contains the original functionality, without needing to redistribute the module.
- Get to know the entry point. Even if you don't know the application you're going to port very well, you can at least find its entry point. That is the hook to your application. You need to create some NetBeans API class that will access your application's entry point. That's really all you need, at the simplest level. In the case of the simple JavaMail client, the SimpleClient class is the starting point for everything else. Its constructor sets everything up, builds a JSplitPane from various other GUI elements, and then adds it to a JFrame. However, we're not going to use a JFrame. We'll use a TopComponent instead. So, what I've done is I've rewritten the constructor to return a JSplitPane (see my rewritten SimpleClient.java). In my TopComponent, I have a simple JButton (not shown in the screenshots above) that does the following in the actionPerformed():
//Find simple.mailcap (needed //by activation.jar for finding viewers), //which is a workaround because the module //couldn't find it by itself: String mailCap = null; JFileChooser fc = new JFileChooser(); int returnVal = fc.showOpenDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { mailCap = fc.getSelectedFile().toString(); } //Hard code the URL to the server, //which needs to be replaced by an Options window extension: String argsv[]; argsv = new String[2]; argsv[0] = "-L"; argsv[1] = "imap://gw12345:password@my-mail-server.com"; //Create new instance of SimpleClient, passing the //arguments defined above, //and return the JSplitPane, //which you add to a JPanel, //which you drag-and-dropped on the TopComponent: SimpleClient client = new SimpleClient(); JSplitPane jSplitPane1 = client.main(argsv, mailCap); jPanel1.add(jSplitPane1, java.awt.BorderLayout.CENTER);By the way, maybe, instead of under the JButton, the above code would fit better in the action that opens the TopComponent.
- Start loving TopComponents. If you are going to port a Swing application to the NetBeans Platform, and you have some user interface that you want to migrate, you better start loving the TopComponent. This NetBeans API class is going to be your best buddy. It is simply a component that integrates into NetBeans Platform applications, providing the window you need. The JButton described above is in a TopComponent. (For non-GUI oriented applications, see the upcoming NetBeans Platform book for an excellent chapter by Jens Trapp, about porting a command-line-driven application to the NetBeans Platform.) So, just run the Window Component wizard, add a JButton to the TopComponent, stick in the code above, and then copy all the simple JavaMail client's classes into your module. Remember to replace the SimpleClient.java with my rewrite. There will be one or two minor tweaks in other files, because you are using a TopComponent instead of a JFrame. For example, the simple authenticator now applies to the TopComponent instead of the JFrame. Next, create a module suite project to which you add this module. Also add a library wrapper module containing the JavaMail JARs, as well as the Activation JAR. In your functionality module, set a dependency on the library wrapper, so that the JavaMail JARs (and Activation JAR) can be found by your code.
That's it. You're done. The Projects window should now show this (below I have selected only the additional files, i.e., those needed in addition to those provided by the original simple JavaMail client, to show how little needed to be added):
Note that all the additional files were created by wizards and required no coding for this porting procedure to work, other than the simple JButton described above.
By the way, in general, it is better to have a bug free and stable application, prior to porting it to the NetBeans Platform. Probably obvious, but worth pointing out. That way, you'll know that all your bugs relate to the ported version and not to the original.
Even though the ported simple JavaMail client has a lot of defects, described in item 1 above, it is already more useful than the original. Why? Well, I don't need to start up a separate process to read my e-mails... Also, now that the mail client is integrated in a TopComponent, it is part of a window system. As a result, it can be moved around and minimized, for example, just like any other window in the NetBeans IDE (or whatever application you've added it to). Importantly, this movement and minimization is a free gift that the mail client receives, for being in a TopComponent.
An easy next step is to create an Options Window extension where the user can set the username, password, protocol, and mail server. After that, one probably needs to take a deep breath and then... integrate the NetBeans Nodes API and replace the JSplitPane with a TreeTableView. Using the Nodes API, you would add nodes for the e-mail folders to a new explorer window. Then you would add menu items on the folders, for opening them in a TopComponent. How to do that will be described in a future installment in this series.
In other news. Have you made your NetBeans predictions for 2007 yet? No? Do so here.
Jan 31 2007, 12:03:32 AM PST Permalink
JavaMail Client on the NetBeans Platform (Step 1)
Let's create the simple JavaMail client, first mentioned in yesterday's blog entry. Once we have it, we'll be able to analyze it and see how best to port it to the NetBeans Platform. The simple client comes from the JavaMail download, so we'll just get it from there and then get it up and running.
- Download JavaMail here. Next, download the Java Activation Framework here.
- Use the Java Project with Existing Sources template, to create an application from the sources at demo/client in the unzipped JavaMail download. Refactor the default package, where all the sources end up, to a package name like "client". Move simple.mailcap to the project root (use the Files window to do this, putting the simple.mailcap file in the same place where build.xml is found).
- Use the Library Manager to create a library where you add the JAR files for JavaMail and the Java Activation Framework. Attach that library to your application's Libraries node.
You should now see this in the Projects window:
You can only see simple.mailcap if you look in the Files window.
- Right-click the application in the Projects window, choose Properties and add something like this in the Run tab's Arguments field:
-L imap://gw12345:password@my-mail-server.com
Doesn't have to be imap. See JavaMail docs for other supported protocols.
- Run the project. If the Output window shows error messages about 'login methods not being found' (which is a rather obscure message), see this tip:
How do I connect to an IMAP server over an SSL connection?
Once the application is successfully deployed, you'll be able to view your e-mails in your simple client. However, I encountered the same NPE as someone else (here). It starts like this:
at MessageViewer.getBodyComponent(MessageViewer.java:199) at MessageViewer.setMessage(MessageViewer.java:104)
This happens when I want to open a message in the lower part of the client. However, for other types of messages, there's no problem. Need to investigate this. Anyway, for the successfully opened messages, the client looks as follows:

Now that we have a simple client, we are able to examine its content and see how best to port it to the NetBeans Platform, described in part 2.
Jan 30 2007, 02:26:40 AM PST Permalink
Simple JavaMail Client
I spent some time trying to get the Simple JavaMail Client (which comes with the JavaMail API download) to work. I wanted to have a simple Swing application which would connect to my work e-mail account, which is found on an IMAP server. The problem is that it requires SSL authentication, which I discovered after a lot of google sleuthing, at the end of which I ended up here:
How do I connect to an IMAP server over an SSL connection?
That tip provides great information if you stumbled across the authentication issues that I had. Anyway, at the end of it all, I have almost exactly what I wanted (still a few bugs, but at least I can get onto the server now). Below, you see part of my list of folders in the left tree and the e-mails within the folder on the right, below all that you see the headers (which should also display the mail content ultimately).

"But why on earth would you want to create a new mail client?! Don't you already have one?! What a waste of time!" Well, thank you for asking. My ultimate aim, of course, is to port this JavaMail Swing client to the NetBeans Platform. I imagine it will end up being something like Josh's Postfachinspektor.
So, watch this space, because I will explain everything I did/will do and then you can follow along and create your own mail client in NetBeans IDE (or for your own NetBeans Platform application).
Jan 29 2007, 12:55:48 PM PST Permalink
Flash in NetBeans IDE
Technical writer Talley Mulligan made a brand new series of flash movies as a High-level Introduction to NetBeans IDE and the Community. It's pretty cool to watch and Talley's voice is really soothing. However, if you're a semi-savvy module developer, why not integrate something like JFlashPlayer... and then you'll be able to watch/hear Talley's movie right inside the IDE:

Maybe in future we'll be able to distribute an integrated Flash Player with the IDE, together with flash movies as tutorials? That would be cool. Pity JFlashPlayer isn't free though. But they have temporary licenses available and maybe there's an open source alternative. Here's all the code I needed, all under the "Browse" button (everything else was generated by the Window Component wizard, with the addition of me drag-and-dropping a JPanel on the TopComponent):
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String flashMovie = fc.getSelectedFile().toString();
FlashPanel welcomePanel = new FlashPanel(new File(flashMovie));
jPanel1.add(welcomePanel, BorderLayout.CENTER);
}
So all that happens is that the FlashPanel gets added to the JPanel. And that's it. That's how you integrate Talley into NetBeans IDE.
Jan 26 2007, 09:22:04 AM PST Permalink
How to Create a Movie Player (Step 3: Creating a Movie Player on the NetBeans Platform)
In the final step of this three part series, we create a completely independent application, with its own launcher, that looks like this:

The special features of this movie player are its ability to show multiple movies simultaneously, together with the fact that you can detach each movie window from the application, and that it is based on the Visual Library, so that you can zoom and pan within a movie. Ultimately, since it uses the Visual Library, maybe you'd add functionality for cropping images from running movies, which is something I'd like to explore.
Everything done in this application in relation to branding has been discussed elsewhere in this blog. For example, the title bar has a distinct name ("NetBeans Movie Player"), the icon in the title bar is changed, the TopComponent tabs do not have borders, a splash screen has been added, all the superfluous IDE modules have been removed so that only those that are relevant to this application remain, and the Favorites window has been branded so that "Movies" instead of "Favorites" appears everywhere in this window. Also, the "Create ZIP Distribution" menu item has created a launcher (such as "movieplayer.exe", under the Windows operating system).
Here's a closer look at the top left corner, with some context around it (must be some way of removing "Editor" from the title bars of the movie windows, but don't know how to do that yet):

Most of this is done by creating a "Module Suite Project" in NetBeans IDE, where you can select to exclude all the IDE modules, so that you can create a stripped-down version of NetBeans IDE. Then you install the additional modules you need (in this case, the module that provides the JMF functionality). And that's it. You now have a rich-client application based on the NetBeans Platform. Your only actual contribution to the application is the JMF functionality discussed in previous parts of this series. All the rest is provided by the NetBeans Platform. Makes sense, because that's what it is for -- to offer a framework where you can hang your modules, or (more poetically) like a concert hall where you place your instruments. And who are the musicians? Well, the end users, of course...
Earlier parts of this series:
- How to Create a Movie Player (Step 1: Setting Up and Testing JMF)
- How to Create a Movie Player (Step 2: Adding the Movie Player to NetBeans IDE)
Jan 24 2007, 10:37:43 AM PST Permalink
How to Create a Movie Player (Step 2: Adding the Movie Player to NetBeans IDE)
At the end of my recent blog entry "Watch Movies in NetBeans", there are some interesting comments, specifically from Stefan Flemming and Oliver Rettig. Both have very interesting scenarios in which JMF on the NetBeans Platform might be useful to explore. Stefan writes:
"Have you thought about video streaming? Is it possible? It would be so great if i could stream my camera view of the scientific instrument directly into my Netbeans RCP control suite."
And Oliver's use case is:
"I am working with motion anlysis in my docoral research in the gaitlab Heidelberg. I have included my modelling and data analysis sofware in Netbeans and it will be great if can also look at the row patient video also inside netbeans."
The case of streaming, suggested by Stefan, should be possible, since JMF seems to support that (click here). I'd like to experiment with it, but don't know exactly what I would need to have or set up in order to try it out. I mean, what would be a scenario that I could play with easily? In Oliver's scenario, if I follow it correctly, one would have a database with videos, which one would like to see in one's application on the NetBeans Platform. That's completely doable, depending on whether JMF supports the video's format (i.e., file type). And, if it doesn't (such as in the case of the movie format that I make use of), you'd need to get some extra library (like Fobs or FFmpeg).
In both cases, I highly recommend following the instructions in How to Create a Movie Player (Step 1: Setting Up and Testing JMF). In that blog entry, there are two sets of instructions. In the first, you install and configure all the libraries you need. You then open a movie (or whatever you want to play) in the JMF application "JM Studio". Once your application works in there, you build a very simple Java application that results in a Swing movie player. Once your application is playing correctly in that simple application, you're making very good progress. You will possibly have a lot less hair at that stage, or the hair that you had at the start will have gone grey, because of frustrations with JMF, but... if you get that application to work, you're very far along in your journey to movie integration with the NetBeans Platform.
At this point, having overcome various obstacles, you run the application, see a file chooser, select a movie, and then the movie begins in the movie player:

Now click the small button on the right bottom corner of the player. You now see the "Media Properties" application, thanks to JMF. In the "Plug-in Settings" tab, click "PlugIn Viewer". Now you see this:

The above tells you (a) whether both audio and visual are working and (b) which decoders (registered either automatically by the JMF installer or manually by you, when you configured your own additions to JMF, such as Fobs or FFmpeg) are being used. For me, the strange thing is that I see different decoders in use at different times, which tells me that I have several decoders available for the same movie format, and that for whatever reason, sometimes one and sometimes another of my decoders is chosen. So, we need to investigate in the JMF Registry. When I do that, I notice that the decoder that I registered was right at the bottom, so I move it to the top:

Tip: Always click "Commit", otherwise your changes will not be saved!
And now look at the quality of the visual component, much better than before (which tells me that in this instance, you can't provide everything to your end users via NetBeans modules in an update center; quite a few things need to be defined by the end users themselves, on their own system):

And, yes, my self-installed decoder is used instead of the default provided by JMF:

However, now that our Swing application is running so nicely, you want to go one step further -- integrate the functionality into NetBeans IDE. This is really simple. But one principle is important (in general, when porting an application to the NetBeans Platform or any other platform). Do you want to replicate you existing application or do you want to leverage the platform? For example, currently our application consists of a file chooser and a JPanel. When we move the application to a NetBeans module and install it into the IDE, there is no need for a file chooser, because the IDE's Favorites window serves the same purpose. We'll need to make sure that our file type is recognized by NetBeans IDE though, which simply means working through a wizard that generates the necessary code. So, we forget the file chooser, because we're going to leverage the Favorites window. Secondly, we have a JPanel. To integrate it with the NetBeans IDE (i.e., NetBeans Platform), we'll need to create a TopComponent instead (click here for details on TopComponents). This is simply an embeddable visual component for display in NetBeans Platform applications, such as the IDE. There's a wizard for that. Once we have the TopComponent, we'll simply transfer our code from the Swing application's JPanel to the TopComponent.
So, let's port our Swing application to a module that we will install in NetBeans IDE:
- Creat a module project called MoviePlayerModule.
- Use the New File Type wizard to generate code that will let the IDE recognize files with the extension used by your movies (e.g., AVI, or MOV, or FOB, or all three). Choose an icon in the wizard, so that you'll be able to see that the IDE distinguishes your file type from all others.
- Use the New Window Component wizard to generate a TopComponent.
- Now install your module. In the Favorites window (Ctrl-3), notice that you can right-click and browse to a folder of your choice. If you browse to a folder containing your movies, with the file type you specified in the New File Type wizard, you should see that the movie files have the icon that you specified in the wizard:

You see? There's no need to create a file chooser in our module, because we already have one in NetBeans IDE (it is part of the NetBeans Platform so is available to any application built on top of it).
- Next, we want our files to open inside our TopComponent. If you look in your file type's DataObject code, you'll see this line in the constructor:
cookies.add((Node.Cookie) DataEditorSupport.create(this, getPrimaryEntry(), cookies));
As a result of this line, the file will try to open in a very simple text editor (provided by DataEditorSupport). This is not good news, because we have binary data. Let's tell the constructor that we will create a new class that will handle the opening of our files. So replace the above line with this one:
getCookieSet().add(new MyOpenSupport(getPrimaryEntry()));
You'll get a red underline because you haven't created that MyOpenSupport yet. Create it now and implement it as follows:
public class MyOpenSupport extends OpenSupport implements OpenCookie, CloseCookie{ public MyOpenSupport(MyDataObject.Entry entry) { super(entry); } protected CloneableTopComponent createCloneableTopComponent() { MyDataObject dobj = (MyDataObject)entry.getDataObject(); MyTopComponent tc = new MyTopComponent(dobj); tc.setDisplayName(dobj.getName()); return tc; } } - Now you need to make your TopComponent public, change the constructor to public, receive the data object in the constructor, and change the TopComponent's extension class to CloneableTopComponent. (This will let us open multiple movies, while simultaneously meeting the expectation of OpenSupport.createCloneableTopComponent().)
Now there are several methods that you need to comment out for now: getDefault(), findInstance(), as well as the writeReplace() and ResolvableHelper at the end. You can just delete the content of the action's performAction(), because we don't need the entire menu item anyway.
- Install the module again. Now go to the Favorites window and notice that when you right-click a movie file and choose Open... a new window opens, with the name of the file placed in the tab. Hurray! We're now almost there. All we now need to do is transfer the code from our Swing application to the TopComponent.
- Take all the code we added to the MediaPanel previously and stick it into the TopComponent's constructor, after the last line that is there already. (That code could be more complex, but let's leave that for another time. This is not a JMF tutorial, after all. Go here for JMF documentation.)
However, add this within the try/catch block, to replace some of the work done by the file chooser in the Swing application:
URL mediaURL = FileUtil.toFile(dobj.getPrimaryFile()).toURI().toURL();
Again, if you have the JMF JARs and related libraries installed on your system, you don't need to provide them with your module. If you want to provide them with your module, create a module suite project, add a library wrapper module project, and declare a dependency on the library wrapper, using the module's Libraries panel.
And that's it! Now install the module again. Open the movie file and notice that it now displays within the TopComponent. You probably want it to display in a JPanel instead. You can tweak the code in the TopComponent's constructor accordingly. In addition, you probably want to make sure that the player closes when the TopComponent closes (Player.close() in TopComponent.componentClosed()).
And now you've ported your Swing application to a NetBeans module and installed it in the IDE. Hurray!
An advantage of having your own module that runs movies is that you can extend it. (Unlike all other movie players out there. Is there even one movie player in the world that allows you to add completely new functionality? I'm willing to bet that there isn't.) For example, let's say you want to see movie reviews while watching your movie. You can create a completely new module that provides a "Read Review" action, which opens the related movie page from the Internet Movie Database. Here the related review opens in the Swing browser, but it could also be an external brower or Rich Unger's JDIC browser:

Of course, none of this functionality need be limited to NetBeans IDE. You can take the Favorites window, "Read Review" functionality, and movie player...

...and then you can reuse these features in any other application based on the NetBeans Platform, such as the JFugue Music Player, for example. Or... you could make an entire application dedicated to playing movies. That's what the final part of this series will discuss.
Postscript. If you're considering working with JMF, I highly recommend that you read JMF 2.1.1 - Known Issues. Better to know beforehand where you're likely to get burnt. For example, this is issue 18:
- 18. JMF may run out of memory in the following situations:
- The close method is not being called on Players or Processors when they are no longer being used. Without calling close, the Players or Processors will not release the memory and other resources (e.g. live threads).
- There are still some external references to an unused Player or Processor such that garbage-collection is not taking place. References may come in many different ways. One of the less obvious ones is the player's visual and control component not being removed from their container components.
- Some movies might require more memory to play than is available from the Java VM. In this case, you'll need to allocate more memory for JVM to run. For example, by issuing the command: "java -mx32m".
- JVM might temporarily run out of memory after playing a few movies in succession. This is usually temporary and the VM can recover after garbage-collection. If you reload the movie a second time, sometimes it will work.
Jan 23 2007, 06:56:12 AM PST Permalink
Interview with Author of "Pro NetBeans"
Adam Myatt, experienced author and NetBeans user, is the writer of Pro NetBeans IDE 5.5 Enterprise Edition. It is set to be released by Apress towards the end of March 2007. You can already pre-order it from Amazon (click here). At 475 pages in length, it seems like a book worth getting! Here's what the front cover looks like:

The book's matching web sites, for which Adam has written several articles, are http://www.ProNetBeans.com or http://www.ProNetBeansIDE.com.
I interviewed Adam, about his NetBeans background, his experiences writing about it, and the opinions he formed while working on it.
- Hi Adam, tell us something about yourself in general. Who are you and so on?
- You've recently written a book about NetBeans. Wow. Great. Why and how did that come about?
- How did you get to be a writer anyway? And say I am an expert in some technology area and want to write a book about it. What would you advise me to do?
- So what's the book "Pro NetBeans IDE 5.5 Enterprise Edition" about, generally?
- Had you read the NetBeans IDE Field Guide at the time of writing your book (or since)? How does your book compare? What are its differences? Should I get it if I already have the NetBeans IDE Field Guide?
- Who is your target audience?
- What were your biggest surprises about NetBeans IDE while writing about it?
- What do you think of NetBeans, compared to other IDEs you've worked with?
- What could be improved? What is missing, in your opinion? Try complete this sentence: "NetBeans would be even better if..."
- How was it to write the book? What were the highlights and "lowlights"?
- If you were to start again from scratch, what would you do differently?
- What are the main things you've learned about writing while writing about NetBeans?
- When is the book coming out? Where can I get it from?
I'm your typical technology enthusiast. I love everything about technology, particularly programming and software tools. I work as the Principal Technologist of Software Development at General Electric's Global Research Center in Niskayuna, New York. It's an amazing place to work and it helps me constantly improve my technical skills - especially Java.
I was originally introduced to Java at college and have been with it ever since. I got my Computer Science degree from the State University of New York at Potsdam. It's a great small-town school and a phenomenal Computer Science department. At college I realized I not only loved technology, but also teaching others about it. To this day I continue with that passion as I'm always going off on rants about the newest open-source project or cool API, sponsoring college interns at work, or writing articles and books to educate others!
One of my favorite things to play with are Java IDEs. I'm constantly amazed at how advanced they are, the powerful features, the variety of plug-ins they offer, the productivity improvements, and so on. I was a fan of Eclipse for quite a while, but continued to have numerous frustrations with it. As of version 5.0 I decided that NetBeans was ready to migrate to and made the switch. I haven't regretted it since.
The book itself came about as I was already speaking with the wonderful folks from Apress about a few possible book projects. Eventually, the subject of NetBeans came up. After some discussion on the best approach we decided to move forward on Pro NetBeans IDE 5.5 Enterprise Edition.
I grew up writing short stories, but never had anything published. I always wanted to write a book, but never had a good enough topic or any contacts to actually make it happen. Once I entered the technology field I discovered that there were plenty of topics you could learn well enough to write about. You just have to make up your mind to do it. As for finding a publisher, there are many book publishers such as Apress that have a "Write For Us" section on their web page. Just draft a few paragraphs about your idea and send it in. If they like it there's a good chance you'll hear back from them. If you don't, be persistent and keep generating new ideas.
As the name suggests, it is really centered on the Java Enterprise technologies. It primarily focuses on Java Web Applications & Struts, the Visual Web Pack + JSF + AJAX, UML modeling features, EJBs and Java Persistence, and the general area of web services, SOA, and BPEL. There are also chapters on features like refactoring, the Ant project structure, JUnit integration, code coverage, database tools, and collaboration.
I actually got a copy of it at NetBeans Day 2006 in San Francisco before JavaOne. I read parts of it on the long plane ride home. Good book; it mostly focuses on NetBeans 5.0. My book targets the newer NetBeans 5.5 features such as the Visual Web Pack, JSF and AJAX components, UML modeling, EJB 3.0, Java Persistence, XML tools, SOA, BPEL, etc.
Java developers who are seeking an IDE that is intuitive to use and has an amazing feature set. Whether you're a Java amateur, professional, or student - this book (and NetBeans) are for you.
I'd have to say how well everything worked together. The stability of the platform is impressive. I was particularly surprised at how well the Visual Web Pack worked, even though it was only recently ported into NetBeans. I'm also impressed with how easy NetBeans makes it to work with the myriad of Java EE technologies.
For Java developers, absolutely the best. Hats off to Sun's NetBeans team. The competing tools are good, but not nearly as easy to use as NetBeans. I'm not just saying this because I wrote a book about it. I've held the same positive opinion as of day one of using NetBeans. The usefulness and stability of the system exceed my expectations.
...it had support for and was tightly coupled with several continuous integration servers. I'd also love to see new features in the Collaboration tools and have them better integrated with the rest of the NetBeans features. It would also be great to see the Visual Web Pack's JSF Page Navigation tool able to be used for Struts.
To be honest, it was a lot of work. It takes a lot of personal time at night and on weekends to put it all together. This past year was quite busy for me in that I took on a new time-consuming role at work, bought a house, got a rambunctious cat, went through several home renovation projects, and wrote the book. It didn't leave a lot of time for sleep. However, it was great when all the chapters started to come together and I saw final layouts for them. I also enjoyed testing out all the little nooks and crannies of NetBeans. I thought I was well-versed with it previously, but quickly realized I had only used a portion of what NetBeans has to offer.
I would love to have written more content on the Visual Web Pack and JSF. The book contains a reasonably lengthy chapter on it, but there are so many great parts to the Visual Web pack. I would love to have written an entire book on it.
First and foremost, probably how to prioritize my time better and not multi-task as much! A lot of people are guilty of taking on too much at once and I'm no exception. I also learned how difficult it is to communicate written concepts in a meaningful way. You don't realize how hard it is sometimes to do that. It's easy to talk about a technology and explain it to someone in person, but a whole different story when you try to write about it. The book writing process definitely helps you organize your thoughts about a subject. It's a lot of work, but amazingly rewarding to see your name on a book.
Apress is releasing the book around March 26, 2007. I know it's on Amazon.com, Barnes & Noble's site (bn.com), and a host of other online book sites. I'm hoping you'll soon be able to walk into your local Borders or Barnes&Noble to find it!
Here's Adam with Duke at JavaOne:
(Duke is the one with the big red nose and pointy head. Adam is the one next to him, smaller nose and less pointy head.)
Jan 22 2007, 02:14:41 AM PST Permalink
How to Create a Movie Player (Step 1: Setting Up and Testing JMF)
This is the first of a 3 part series of blog entries in which I intend to show you, as exactly as possible, how to create the movie player shown in yesterday's blog entry. At the end of this series, you should have something that looks a lot like the movie player shown in yesterday's blog entry.
The three part series will consist of the following:
- Part 1: Setting Up and Testing JMF
- Part 2: Adding the Movie Player to NetBeans IDE
- Part 3: Creating a Movie Player on the NetBeans Platform
So, this is part 1, the most complex part, in which we set up Java Media Framework API (JMF). Everything I know about JMF I learned from blogs and discussion threads, because I trust bloggers more than technical writers. (And I am a technical writer, so go figure.) But, seriously, bloggers tend to find the pain points of a product, while the official documentation tends to describe the best-case scenario, with some low-threshold troubleshooting tips near the end. I'm adding this disclaimer up front, because the procedure that follows is my own ad-hoc JMF setup procedure, and hasn't been tried and tested, and has worked for me only on Windows (although the same procedure should work on other operating systems, with the variations specified below).
But first, what is JMF? Here is the definition according to the website: "The Java Media Framework API (JMF) enables audio, video and other time-based media to be added to applications and applets built on Java technology. This optional package, which can capture, playback, stream, and transcode multiple media formats, extends the Java 2 Platform, Standard Edition (J2SE) for multimedia developers by providing a powerful toolkit to develop scalable, cross-platform technology."
Here's how I personally recommend getting JMF up and running:
- Click this link. You now find yourself in the JMF Diagnostics Applet. It tells you whether you have JMF set up or not. At the start of this procedure, I'm assuming you have nothing set up. Here is what I see in the text area in the JMF Diagnostics Applet:
JMF Diagnostics: Java 1.1 compliant browser.....Maybe JMF classes.....Not Found
Later we will go this JMF Diagnostics Applet again, and we will notice that the message we see here is different.
- Next, go to a command prompt and type the following:
java JMStudio
You should now see the following:
Exception in thread "main" java.lang.NoClassDefFoundError: JMStudio
This tells you that JMF's most important tool, JMStudio, is not available. It will be available after you install JMF. Let's do that and come back to the command prompt afterwards.
- Click here to go to the JMF download page. Click the Download button on that page. Click the Accept button. Next, select the JMF download applicable to your platform. (I don't know what the Java all-platform download is for, but somehow I haven't been able to download it thus far.)
- At the end of the wizard that installs JMF, you will be prompted to restart your computer. Do so. Then, go back to the JMF Diagnostics Applet. Depending on your operating system, you should now see this in the text area:
JMF Version... 2.1.1e Win32 Build Native Libraries Found
- Next, start up the JMStudio application, which should now start up, because it is part of your JMF installation and because the right environment variables have been set by the installer. So type this in the command prompt:
java JMStudio
Now JMStudio starts up. Go to File | Open File. Browse to a movie (or music). Depending on the type of file you want to view, possibly you will now see a JMStudio Error dialog with this message:
Controller Error: Failed to realize: input media not supported: XVID video, mpgelayer3 audio
Now here is an important rule to all of this stuff: If something does not play in JMStudio, it will not play in your own application either.
So, we need to extend JMF. Let's do that in the next step.
- Click here to get to the Fobs download page. What is Fobs and why do you need it? This is what the Fobs website says about itself: "FOBS (Ffmpeg OBjectS) is a set of object oriented APIs to deal with media. It relies on the ffmpeg library, but provides developers with a much simpler programming interface."
So, download Fobs. I use the Windows ZIP file. After you have unzipped it, double-click runjmstudio.bat. Now JMStudio starts up, with the Fobs JAR files registered. Choose File | Open File again and browse to your file. The movie should start. And the command prompt should now show you this:
Fobs4JMF - Native shared library found 7283.99First Position: 0, 41 Duration: 7283992 Frame Rate: 23.976 Opening Thread[JMF thread: com.sun.media.PlaybackEngine@111a3ac[ com.sun.media.P laybackEngine@111a3ac ] ( configureThread),9,system] Fobs Java2DRenderer: setInputFormat Fobs Java2DRenderer: setInputFormat Fobs Java2DRenderer: start
- Meanwhile, your movie is now playing in JMStudio. At the bottom of JMStudio, you see a slider for increasing/decreasing the volume. On the right side of the slider, in the far right corner, you see two buttons, one for toggling the sound on/off and another for calling up information on JMStudio. Click that button. In "Media Properties", click the "Plug-in Settings" tab. Then click the "Plugin Viewer" button. The Plugin Viewer dialog appears, showing you a diagram that should show you that both the video and audio are functioning. This is good news.
- Now type the following in the command prompt:
java JMFRegistry
You now end up in the JMF Registry Editor. (You could also get here by choosing File | Preferences in JMStudio.) Now look in the very useful Configuration of Fobs4JMF document. Specifically, look in step 5 of the "Manual Configuration" section. Check that everything has been registered correctly, which happened in the previous step, when you clicked the BAT file (or whatever file relevant to your operating system). For example, look in Plugins | Codec and check that the three Fobs Codecs have been added to the end of the list. If something has not been added automatically for some reason, do so manually, as described in step 5 of the "Manual Instructions" section.
As far as I can tell, you have now completed the set up of JMF, and extended it with Fobs. You need Fobs for a lot of file types that are not covered by JMF by default. Without Fobs, I have not been able to watch a single movie in JMStudio (and, therefore, not in my own application either). There's also an MP3 Plugin for JMF (here), which only works under Windows.
Next, we are going to create a simple Swing application which will test our JMF set up. Once we have that, we are ready for the next stage (part 2 of this series), where we port our Swing application to a NetBeans module which we integrate in the IDE. Here are the steps for creating our simple swing application:
- Start up NetBeans IDE 5.5 or a 6.0 build. (Or start up 5.0, but why are you still using 5.0 anyway?)
- Create a new Java application called "MoviePlayer".
- Create a new JPanel called "MediaPanel". Rewrite the default constructor as follows:
public MediaPanel( URL mediaURL ) { initComponents(); setLayout( new BorderLayout() ); // use a BorderLayout try { // Create a JMF player to play the media specified in the URL: Player mediaPlayer = Manager.createRealizedPlayer( new MediaLocator(mediaURL) ); // Get the components for the video and the playback controls: Component video = mediaPlayer.getVisualComponent(); Component controls = mediaPlayer.getControlPanelComponent(); if ( video != null ) add( video, BorderLayout.CENTER ); // add video component if ( controls != null ) add( controls, BorderLayout.SOUTH ); // add controls // Start the JMF player: mediaPlayer.start(); // start playing the media clip } // end try catch ( NoPlayerException noPlayerException ) { System.err.println( "No media player found" ); } // end catch catch (CannotRealizeException ex) { ex.printStackTrace(); } // end catch catch ( IOException iOException ) { System.err.println( "Error reading from the source" ); } // end catch } // end MediaPanel constructorNotice that the constructor receives a URL. This URL is the URL to your movie. (We will use the IDE-generated Main.java class, in step 5 below, which will pass the URL, after the user has selected a movie via a file chooser.) Next, we create a JMF player object. We also create two components, a visual component and a controller component. The controller is the sliding bar at the bottom of a JMF application, for controlling the volume, as well as the additional buttons for toggling the sound, and so on. In other words, the controller is something you get for free when you use JMF. Finally, after adding the visual component and controller component, we start the player. And that's it! JMF in a nutshell.
- However, assuming that you want to wrap the JARs with the application (instead of using the installed JMF libraries that, thanks to the installer, must be on your classpath at this stage), we haven't put the JMF JAR on the application's classpath. Let's do that now. Right-click the Libraries node, choose "Add JAR/Folder" and browse to the two JARs provided by the Fobs download. In other words, you should have fobs4jmf.jar and jmf.jar added to the Libraries node. Also include the native library there. For example, create a Java library and put fobs4jmf.dll in its default package. That's all I need to do and it works. (You could also add the native library to the PATH environment variable, or its equivalent on your operating system.) Now the red underlines, if any, in the MediaPanel code should disappear.
- Next, we use the Main.java class that the IDE generated for us to pass the movie's URL to the MediaPanel. Here's the constructor:
public static void main( String args[] ) { // Create a File Chooser: JFileChooser fileChooser = new JFileChooser(); // Show Open File dialog: int result = fileChooser.showOpenDialog( null ); if ( result == JFileChooser.APPROVE_OPTION ) // User chose a file { URL mediaURL = null; try { // Get the file as URL: mediaURL = fileChooser.getSelectedFile().toURI.toURL(); } // end try catch ( MalformedURLException malformedURLException ) { System.err.println( "Could not create URL for the file" ); } // end catch if ( mediaURL != null ) // Only display if there is a valid URL { JFrame mediaTest = new JFrame( "Movie Player" ); mediaTest.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); MediaPanel mediaPanel = new MediaPanel( mediaURL ); mediaTest.add( mediaPanel ); mediaTest.setSize( 300, 300 ); mediaTest.setVisible( true ); } // end inner if } // end outer if } // end mainSo here we have a simple file chooser. We convert the chosen file to a URL and send it to the MediaPanel. That's all.
- Now run the application. The file chooser appears. Select a movie. You should see this, after a moment, in the Output window:
init: deps-jar: compile: run: Fobs4JMF - Native shared library found 4820First Position: 0, 40 Duration: 4820000 Frame Rate: 25 Opening Thread[JMF thread: com.sun.media.PlaybackEngine@eb7859[ com.sun.media.PlaybackEngine@eb7859 ] ( configureThread),9,system] Fobs Java2DRenderer: setInputFormat Fobs Java2DRenderer: setInputFormat Fobs Java2DRenderer: start BUILD SUCCESSFUL (total time: 45 seconds)
And now your movie starts up. Hurray, you've successfully set up and tested JMF.
I hope the above steps all make sense and, if they don't, especially for non-Windows operating systems, please leave a note at the end of this blog entry. Plus, note that many/all of the instructions could be wrong, since they're all based on my personal flounderings in JMF land, during the past few days.
In the next entry in this 3 part series, we will port our Swing application to a NetBeans module, which we will install in the IDE. This will give us a movie player inside our IDE.
Jan 21 2007, 08:02:28 AM PST Permalink
Watch Movies in NetBeans
My long-cherished dream has come true at last... I can watch movies in NetBeans IDE. And, thanks to the powerful windowing system, I can watch multiple movies at once:

Plus, it is very easy to remove all the IDE's modules... and then you're left with a separate non-IDE application that contains only the parts needed for the movie player. Here the application is running on NetBeans 6, so that the windows can be undocked:

To make the above application yourself, you need only have a very minimal knowledge of the NetBeans Platform, which is the framework on which the movie player runs. To see the user interface of the original Swing application, look at the screenshot below (which, though it has "NetBeans" in the titlebar, has nothing to do with NetBeans, other than that I used NetBeans IDE to create it, but I could have created it in any IDE or no IDE at all):

Here you see a simple Swing application that runs movies, using the JMF framework as its basis. Clearly, it is not a fully fledged application. But, the second screenshot above shows a complete, fully-functioning application (though it needs to be cleaned up a little bit, like the icons and so on). If you compare the third screenshot with the second one, you'll immediately see the benefit of the NetBeans Platform. I.e., a windowing system, explorer view, and menus. I did no coding whatsoever for these valuable application features. In fact, porting the application required no additional coding on my part, with the exception of creating the class that opens the movie in the new window, via the NetBeans APIs. All the rest was either given to me for free as part of the NetBeans Platform or provided via templates that come with NetBeans IDE.
The only real work needed for this application is setting up JMF correctly -- you also need some additional stuff, such as Fobs. Then you need to register the additional plugins correctly in the JMF Registry. This part is the most difficult and when things are not working correctly it is almost impossible to know what piece of the puzzle is missing. (This JMF diagnostics tool is very useful.) And each platform has different requirements. All not very nice, but the end result is really cool. High quality resolution, great sound, and the possibility of watching multiple movies simultaneously while having the option of extending the functionality if needed. Hurray for JMF and NetBeans.
Jan 20 2007, 05:03:47 AM PST Permalink
UI Fidelity and Elliotte Rusty Harold
Way back on Wednesday August 03, 2005, in this blog, I praised Elliotte Rusty Harold's great Processing XML with Java. I've learnt a lot from it (and I make use of it at least once or twice a month), all from the online version of the book, though I've seen the hardcopy of the book lying around in various places here at Sun in Prague. Then today I saw in Alex's blog that Elliotte Rusty Harold has had some nice things to say about NetBeans and Swing. So I clicked the link and read the nice comments. I think the comments are so nice (and come from such a highly respected member of the Java community) that it deserves to be repeated in bold. First Elliotte starts out with some misgivings. Then he says, and here it is in bold:
...comparing UI to UI, NetBeans is now way ahead of Eclipse. The claim that SWT provides a more native looking interface than Swing is demonstrably false. In fact, exactly the opposite is true. I'm sure it doesn't hurt that the NetBeans team has a significant portion of Mac users, and the Eclipse team has none (or so close to none that I can't tell the difference).
Will this be the final word on the eternal "UI fidelity" debate? Probably not, because there are still people out there who like keeping the discussion alive as if the jury is still out (a miniature version of the jury supposedly still being out on global warming and on the harm that nicotine causes, but only according to those in whose interest it is to claim that those discussions are still alive). However, for the rest of us -- lets stop the "UI fidelity" debate in the SWT/Swing discussion. Sure there are differences, and sure there are things that are better in one over the other, but "UI fidelity" is no longer one of them.
Anyway, thanks Elliotte, I hope you'll end up enjoying NetBeans (whether or not you decide to switch) as much as I've enjoyed your great book.
Jan 19 2007, 03:18:25 AM PST Permalink
Continuing Problems with Native Library, Probably
Well, I tried various things, and despite the tips received in yesterday's blog entry (thanks Tom, Sven, and Anonymous, also thanks Antonio, I'll investigate your suggestion), I still can't get my NetBeans Platform application to work correctly, with the same error message as yesterday. I'm sure the reason is what Tom and Sven suggest, the DLL not being found. But I put it in various places (like in release/modules/lib within my library wrapper, which seemed, from mailing lists and so on, the right place to put it), and ended up with the same result. Any advice would be appreciated.
However, thanks to this thread, I discovered that I needed JDK 6 instead of 5, for the JavaSoundRenderer to work correctly. And now, magically, the sound is working and the speed is normal as well, so I have a cool (if small) movie player in Swing now, which works perfectly:

How cool would it be if I had the above on the NetBeans Platform? Very cool. I already have a lot of the ui set up, with a data object for AVI files, opening into a TopComponent via OpenSupport. I really hope someone can help here.
By the way, small competition for movie buffs -- the above is a moment in one of my favorite movies 'Donnie Brasco'. Question: Which of the following things has Paulie (James Russo) just said in the clip above?
- "Sonny Black ain't happy. And you know what that means."
- "Sonny Red's dead meat. Forget about it."
- "We better start earning or somebody's gonna get clipped."
- "Forget about it. Donnie ain't no rat."
- "A priest, a duck, and an eskimo walk into a bar..."
Anyone who gets it right wins a copy of my Swing Movie Player. (But then you'd need to get your own codec libraries and probably various other platform-specific stuff if you're not using Windows, because those are the only libraries that this application provides.) If you know the answer (or if you think you do), send it to geertjan DOT wielenga AT sun DOT com, with "Paulie said..." in the subject line.
Jan 18 2007, 09:43:49 AM PST Permalink
Hello JMF
Well, I gotta say that working with JMF is about as fun as something that isn't much fun at all. But the results came sooner than I'd feared. Here's my first movie player:

Currently it is a simple Swing application. However, no matter how much I tried, I couldn't get it to work on the NetBeans Platform. I know it must have something to do with Fobs, which "is a set of object oriented APIs to deal with media. It relies in the ffmpeg library, but provides developers with a much simpler programming interface." It works fine when I bundle it together with the other JMF libraries (although, I have no sound and the visual movement is slow). But, when I migrate the same application to the NetBeans Platform, using the same libraries, I get this error message, which is what I got before porting my application to the NetBeans Platform, prior to including the Fobs library:
Unable to handle format: XVID, 624x352, FrameRate=23.9, Length=1317888 0 extra bytes Unable to handle format: mpeglayer3, 48000.0 Hz, 0-bit, Stereo, Unsigned, 18153.0 frame rate, FrameSize=9216 bits
If anyone knows the solution to this (and also how to get the sound working in my normal Swing application), that would be great.
Jan 17 2007, 11:57:37 AM PST Permalink
Toolbar Configurations
If you define XML files that conform to the http://www.netbeans.org/dtds/toolbar.dtd, you can set up toolbar configurations and dynamically show or hide your toolbars. Everything I know about this I read in the FAQ: How do I hide and show toolbars the way the debugger does?. Handily, the IDE comes with two of its own toolbar configuration files, called Coding.xml and Debugging.xml, as you'll find when you expand the Toolbars folders within "<this layer in context>". For example, this is what Coding.xml looks like:
<!DOCTYPE Configuration PUBLIC "-//NetBeans IDE//DTD toolbar//EN"
"http://www.netbeans.org/dtds/toolbar.dtd">
<Configuration>
<Row>
<Toolbar name="File" />
<Toolbar name="Edit" />
<Toolbar name="Build" />
<Toolbar name="Debug" visible="false" />
<Toolbar name="Memory" visible="false" />
<Toolbar name="Versioning" visible="false" />
</Row>
</Configuration>
Notice that the Debug toolbar and the Memory toolbar are both set to not be visible. In Debugging.xml, both of these are visible.
So all you need to do, after registering the XML files in the module layer file as described in the FAQ referenced above (which you do not need to do for Coding.xml and Debugging.xml, because these are part of the distribution), is say: "Now I want to use Coding.xml" (or Debugging.xml, depending which one you want). And you do that by simply coding this line:
ToolbarPool.getDefault().setConfiguration("Coding");
You'd use that line of code whenever you want a certain toolbar configuration to be displayed, such as when you open a window component group, for example. Here's a very simple example -- a new toolbar where I can click a radiobutton to show or hide a configuration:

Jan 16 2007, 10:04:20 AM PST Permalink
Table of Contents for NetBeans Platform Book
We're wrapping up the oft-mentioned NetBeans Platform book ("Rich Client Programming - Plugging into the NetBeans Platform") right now. Our deadline for handing off to the publisher is today. And we're definitely going to make it (although this is where I'm supposed to knock on wood to prevent all our files evaporating into thin air). Here, after much discussion, is the "final" table of contents, exactly as we're going to communicate it to the publisher:
Preface
1. Getting Started with the NetBeans Platform
2. The Benefits of Modular Programming
3. Modular Architecture
4. Loosely Coupled Communication
5. Lookup
6. Filesystems
7. Threading, Listener Patterns, and MIME Lookup
8. The Window System
9. Graphical User Interfaces
10. Nodes, Explorer Views, Actions, and Presenters
11. MultiView Editors
12. Syntax Highlighting
13. Code Completion
14. Component Palettes
15. Hyperlinks
16. Annotations
17. Options Windows
18. Web Frameworks
19. Web Services
20. JavaHelp Documentation
21. Update Centers
Three Users Look at NetBeans
- User A
- User B
- User C
Appendices
Appendix A: Advanced Module System Techniques
Appendix B: Common Idioms and Code Patterns in NetBeans
Appendix C: Performance
And... if all works out as planned, there'll be a cool foreword by a very interesting person (more details to be revealed at a later stage).
Jan 15 2007, 12:48:14 PM PST Permalink
New Books on NetBeans
One place where NetBeans is massive is China. Every couple of weeks I see internal e-mails from China with reports about thousands of people having attended presentations or trainings in NetBeans. Today I heard that SDN China worked with partners to publish two new books on Java and NetBeans:
Mastering NetBeans -- Developing Desktop, Web, and Enterprise Applications
Author: Ya Feng Wu, Xin Lei Wang
Publisher: Posts & Telecom Press, China
ISBN: 978-7-115-13837-8
Pages: 587
This book is a detailed tutorial on the NetBeans IDE, with a lot of examples on how to develop various kinds of applications with the IDE. The version used in this book is NetBeans 5.0, and the CD bundled with the book provides the NetBeans installation binaries.
Part I of the book gives an overall introduction to the NetBeans IDE and various components. Part II of the book focuses on SWING/JFC development with NetBeans. Part III of the book focuses on web application development with NetBeans and Tomcat. Part IV of the book focuses on developing Java EE applications with NetBeans. Part V of the book focuses on Ant, JUnit, Profiler, and migrating from Eclipse to NetBeans.
Mastering Java ME -- Best Practice for Java ME Core Technologies
Author: Jiang Fei Zhan
Publisher: Publishing House of Electronics Industry, China
ISBN: 978-7-121-03303-8
Pages: 525
This book is a detailed tutorial on Java ME technology, with a lot of examples on how to develop, debug and deploy various MIDlet functions with NetBeans IDE 5.0 and Mobility Pack 5.0. Based on JTWI 1.0, the book systematically introduces the MIDlet application model, GUI, RMS, networking, wireless, and mobile multimedia API's. Following the book, the reader can develop several highly attractive full-featured Java ME games.
Hurray for China! And take a look at the list of NetBeans books here.
Jan 12 2007, 02:24:15 AM PST Permalink


