Teaching Oracle JDeveloper to Make Coffee
Getting started with JDeveloper extension development
Ask any serious Java developer what are the tools in his arsenal and coffee will sure be one of them. Coffee is what keeps the developer's eyes open after hours of staring at his code trying to figure out why it doesn't work. I was surprised to discover that JDeveloper - although being a comprehensive Java IDE - is missing support for making coffee cups straight out of the IDE. I figured out that this could be a helpful feature that will attract many new developers to use JDeveloper. So I decided to go ahead and add coffee making capabilities to JDeveloper.
Getting New Features into JDeveloper
Oracle JDeveloper is built around a core IDE with an extension SDK. The core IDE takes care of windowing, menus, structure window, etc. The extensions are everything from the UML modeler to the code profilers. All these extensions are built using the JDeveloper extension SDK.
One of the great things about JDeveloper is that it exposes the same extension SDK that is used by the Oracle JDeveloper developers to the public. This means that anyone can build new functionality and features into JDeveloper.
You can integrate your solution into JDeveloper in several levels. The very basic integration is to use the external tools menu option. This allows you to invoke command lines from inside JDeveloper and pass parameters to them, such as the current file name.
So, if your tool can be invoked from a command line - you can create this basic integration and even add options to the main menu and a context sensitive menu.
To provide a more friendly experience to the JDeveloper user, you might want to actually integrate the interface of your extension into the IDE. This way the end-user doesn't have to use two separate tools. If your tool's interface is Java based, this is quite a simple thing to do. This article will show you the basics. You can follow along with the project code.
Coffee Extension Recipe
So we decided that we want to teach JDeveloper to make coffee. We need to accomplish two tasks:
1. Create an application in Java that makes coffee.
2. Integrate this application into JDeveloper.
Making Coffee in Java
This is the basic end user interface:
It lets you choose your preferred coffee type, and then by pressing the "make coffee" button it prepares the drink for you. It's a basic Swing JPanel with the needed Swing components. Here is the code for the implementation.
Note: To keep things short here I didn't include the complete code behind the coffee-making button, just the last line of code that notifies that the coffee is ready.
Integrating with JDeveloper
There are different ways to integrate your application into the user interface. For example you can create a wizard that will guide the user through your application. Since I already have a user interface for my application, all I want to do is show it as a dockable window in JDeveloper.
To make this simpler, the Extension SDK comes with a set of code samples, and one of them - appropriately called DockableWindow - has the code for creating a dockable add-in to JDeveloper. If you'll run this sample you'll see that it shows a very simple dockable window with a button. In my project all I need to do is replace this button with my coffee machine UI. I created java files in my project that corresponded to the four files that come with that demo (In my project they are called: MyDockable, MyDockableFactory, MyController, MyExtension*). Then I copied code from the sample source into my project's files, changing package locations and names a little to fit my project structure. I also went into the project settings and added the "JDeveloper Extension SDK" library to my project. This allowed me to compile all the files without any errors.
Next I needed to replace the current dockable implementation with my own coffee making code. This is quite simple, just open the MyDockable file and change line 28 in the code
_ui = new JButton("Hello");
to: _ui = new coffeePanel();
Another good thing to do now is go over the source and look for strings that dictate the text that will appear in the menu and window's title and replace it to a more appropriate name. These strings were defined in the MyControler and MyDockable files.
The last step is to add a jdev-ext.xml file that is used to describe my extension. Simply create a new file in the project and place it in a src\meta-inf directory.
Here is the content of the file. As you can see it is self-explanatory.
Next I create a simple JAR deployment for my project. I direct it to be saved in the [jdev-root]\jdev\lib\ext directory. (This is where all the extensions are placed).
All I need to do now, is run the file called MyExtension. This actually starts an instance of JDeveloper. In the view menu option I now have my new coffee machine option.
Although teaching JDeveloper to make coffee might not be practical to everyone; you can use the steps above to create more realistic extensions. For more information on the Extension Exchange make sure to also check the online help for Oracle JDeveloper 10g.
* In my implementation the class MyExtension implements Extension and not an Addin as in the original sample. Both ways work, but Oracle recommends using the Extension interface when building extensions. The benefit of the Extension interface is that it let you control what happens when an extension is unloaded.
The JDeveloper online help has a complete chapter dedicated to building extensions; this is a great source for more information on the subject.
Have questions about this article? Discuss it here.