The usual way of creating a Java project is by showing a wizard that guides the user through the different configuration options. In case you want to customize the existing wizard (add a new page, for example), you specify the org.eclipse.ui.newWizards extension point in your plugin.xml and specify a new wizard section with project attribute set to true. But what if you want to create a new Java project in a purely programmatical way without showing any wizard dialog?
The published API to configure the Java project is the org.eclipse.jdt.ui.wizards.JavaCapabilityConfigurationPage. It has two public utility methods, createProject and configureJavaProject that do most of the heavy lifting. The first problem is that the second method is not static (which means that you need an instance of this page to call it), and the second problem is that the implementation of these methods is calling into internal classes. Fortunately, all the APIs that are required to create a new custom Java project are published. Let’s take a look at the sequence of steps.
First, call ResourcesPlugin.getWorkspace().getRoot().getProject(projectName) to get the IProject handle. Calling exists() will let you know whether the project named projectName already exists. All the following steps assume that this project does not yet exist.
Next, call project.create and project.open. This will create and open the new project.
Now it’s time to create the source folders for the project. Suppose you want to create a source folder named custom. Start by creating a new IPath instance with IPath customPath = new Path(“custom“). Next, call IFolder customFolder = project.getFolder(customPath) and finally customFolder.create(true, true, null). If you have more than one source folder, repeat the steps above for each one of them. A source folder can be a link to a directory on your hard disk. To mark the source folder as link, call IFolder.createLink API, passing the path to the hard disk location. The second parameter can be IResource.ALLOW_MISSING_LOCAL to allow the creation of the link source folder even if the linked target folder does not (yet) exist.
Next step is configuring the classpath entries for the new project. The classpath entries include:
- your source folders
- your custom jars
- the JRE container
To create an IClasspathEntry instance that corresponds to your source folder, call JavaCore.newSourceEntry(project.getFullPath().append(customPath)) where customPath is the IPath instance created above.
The simplest way to create the classpath entries for the JRE container is to use the PreferenceConstants.getDefaultJRELibrary() method. This will return the classpath entries for the default JRE of the workspace. If you want to use another JRE, you would need to iterate through the VM install types returned by the JavaRuntime.getVMInstallTypes(), and iterate through the VM installs for each one of them. The IVMInstall does not expose an API to check its Java compliance. Cast it to AbstractVMInstall and check its getJavaVersion() to the specific compliance spec string (such as JavaCore.VERSION_1_6 for Java 6.0, for example). Once you find a matching JRE, you can get its classpath entries with the following:
- Get the JRE container path with IPath containerPath=new Path(JavaRuntime.JRE_CONTAINER)
- Assuming that you have found a matching AbstractVMInstall, create its path by IPath vmPath = containerPath.append(vmInstall.getVMInstallType().getId()).append(vmInstall.getName())
- Now, the classpath entry of that JRE is simply JavaCore.newContainerEntry(vmPath)
To create a classpath entry for a custom jar (with optional javadoc and source attachments), use JavaCore.newLibraryEntry API. The first parameter is the path to the jar file that can be constructed with the various FileLocator APIs. The sources can be specified via the second and the third parameters. The javadoc attachment is passed in the classpath attribute array (fifth parameter). To construct an IClasspathAttribute entry, use JavaCore.newClasspathAttribute API with IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME name and “file:filePath” value where the filePath is the URL of your javadoc file.
Next step is to create a IJavaProject by using IJavaProject javaProject=JavaCore.create(project).
Next step is to add the Java nature to the new project. This is done by getting the IProjectDescription of the project with the IProject.getDescription, creating a new String[] array to hold an additional entry, copying all the existing nature IDs (from IProjectDescription.getNatureIds() API), adding the JavaCore.NATURE_ID entry and setting back the description.
Next step is to create the default output folder for the project. The name of the default output folder can be taken from the PreferenceConstants.getPreferenceStore().getString(PreferenceConstants.SRCBIN_NAME) and the folder itself is created with the same IProject.getFolder and IFolder.create APIs as for the source folders. The only difference is in that the output folder needs to be marked as derived. In order to do this, pass IResource.FORCE | IResource.DERIVED as the first parameter to the IFolder.create method and call IFolder.setDerived(true) afterward.
At this point, the project is ready to be refreshed (before the actual classpath is set). Call project.refreshLocal with IResource.DEPTH_INFINITE).
Next step is to set the classpath on the java project. Call javaProject.setRawClasspath passing the array of all the classpath entries created before.
The next optional step involves setting custom natures and moving the Java builder in the builder sequence. If your project is going to have a custom nature with custom builders, you first need to add the custom nature(s) to the project description. This is done in exactly the same way as with the java nature above. After setting the nature, you might want to move the default Java builder down in the sequence. This is relevant if the Java builder (the one that compiles the source Java classes) needs to be invoked after your custom builder(s) – the ones that generate additional Java classes, perhaps. To do so, get the build spec sequence from IProjectDescription.getBuildSpec array and shuffle the commands as necessary.
The final step is to set the Java compliance settings on the project. This is done to enforce the different Java rules. Get the current settings from IJavaProject.getJavaOptions. Next, set the following entries in the map:
- JavaCore.COMPILER_COMPLIANCE to the matching JavaCore constant, such as JavaCore.VERSION_1_6.
- JavaCore.COMPILER_SOURCE to the matching JavaCore constant, such as JavaCore.VERSION_1_6.
- JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM to the matching JavaCore constant, such as JavaCore.VERSION_1_6.
- JavaCore.COMPILER_PB_ASSERT_IDENTIFIER to JavaCore.ERROR to prevent using assert as a valid identifier.
- JavaCore.COMPILER_PB_ENUM_IDENTIFIER to JavaCore.ERROR to prevent using enum as a valid identifier.
After setting these entries, store the options back with IJavaProject.setOptions
Congratulations – now you have a new Java project with custom source folders (some of them linked), custom jars, an output folder and custom builders set. And all of this without the user having to click through the wizard screens.
Here are some Swing links that you might have missed during the last week:
- Ken Orr explores an interesting usability side of Swing buttons with popup menus. The solution involves using an unpublished “doNotCancelPopup” client property supported by the current implementation of popup handling in core Swing. This may be different in other JVM implementations, and may even break in the future Swing versions (although the later is quite unlikely). Thanks to this post i have also updated the Flamingo command buttons to dismiss popup menu / panel on the second click. The implementation does not use the above client property, since Flamingo has its own code to manage popups.
- Gregg Bolinger has kicked off a series of posts on blueprints for well-written Swing applications (part 1, part 2, part 3 so far). Project Maddie has been created to provide the collaboration grounds for interested developers, and it will be interesting to see how this pans out after the initial excitement is gone. I have talked about the lack of Swing blueprints in May 2006 (just after JavaOne 2006), and unfortunately nothing has changed as far as the proper platform documentation goes.
- Jeremy Wood delves into the intricacies of using existing alpha composites to do cross fades between two translucent images. After finding out that the existing alpha composites are not up to the job, he tries doing direct manipulations on the underlying buffer. As this turns off the hardware acceleration, the time and memory performance were disappointing.
- Gabriele Carcassi provides a solution for registering JavaScript functions to respond to Swing events on applets.
- Following his experimentations in the SwingX incubator, Matt Nathan has decided to create a separate project for scalable icons.
- Geertjan Wielenga writes about the JSyntaxPane project, mentioning the supported editor kits, find / replace dialog, code completion configuration, color styles and more.
- Gregg Bolinger vents his frustration over the opaque and very slow bug fixing process of core Swing bugs. Working on drag-and-drop between tables and lists, he has hit a bug reported in 2003 and not fixed since then. In the next post he presents (a rather poorly formatted) a solution that he found on JavaRanch came up with.
- Wolfgang Zitzelsberger has announced release 2.8 of Synthetica look-and-feel. New in this release are the SyntheticaSimple2D theme, improved translucency / border support for comboBox / spinner / text components, transparent window support for 6u10, improved RTL component orientation support.
- Jean Francois Poilpret has posted the slides to his Jazoon 08 presentation on the JSR 296 (AppFramework).
- Greg Brown shows two examples of building Pivot applications that are wired with JavaScript and Groovy.
- Jeremy Wood talks about his implementation of Aqua-like preference panel thats supports both single-row and multi-row layouts, along with cross fading between the components.
- And finally, Collin Fagan kicks off the new series on exploring layout prototyping. The first part documents his attempts to apply XML to the UI layouts. Layout managers surveyed in this part are absolute, table, grid bag and Mig. It certainly looks like the desktop heavyweights (Adobe and Microsoft) have adopted XML as the lingua franca to enable easier collaboration between designer and developer tools. And while the hardcore developers still prefer writing and tweaking those XML files by hand, both sides of the respective toolchains provide tools that are familiar to the target audience.
What, why, when and how – these are the questions that shape the communication between the product developers and their users. Different teams choose different levels of opacity regarding these four questions, and making the answers more opaque over the time leads to a building frustration among the community members. This has been one of the major reasons behind my post on the current state of core Swing, and allow me to further explore this area.
Open-sourcing Java during the JavaOne 2006 and the announcement of OpenJDK project was hailed by Sun as the new era for Java, where everybody can lend their hand in shaping the future of the Java platform. Specifically in the client area, the summer of 2006 has supplied Swing developers with such gems as Chris Campbell’s entry on soft clipping, Chet Haase talking about Java on Vista, official birth of JSR 295 and JSR 296, as well as lively discussions on the SwingX painters and layers. Those have been exciting times for me personally, as they showed a renewed and well backed interest in the Java client side development.
Unfortunately, the last eighteen months have been quite disappointing. The level of openness (or transparency, if you will) set in the summer of 2006 was not a genuine and lasting commitment to the community, which has been effectively shut not only from participating in the decision making process, but in following it as well. And this brings me back to the four questions – what, why, when and how.
The how is perhaps the easiest one, and Java has always been shipped with the matching source archives. This has allowed the users to understand not only how the specific piece works, but also how to work around the specific bugs in order to achieve the required functionality. Even if the internal technical design documents (if any are present) are not available to the community, the full source code is an excellent reference guide to a deeper understanding of the platform and becoming a better Java developer in the process.
The answer to when allows bigger projects to estimate timelines for the different technologies, both existing and emerging. In this area, Java has not been a great success over the last three years. The new 18-month release cycle for major versions (announced at JavaOne 2005) which would have Java 6 ship in summer 2006 and Java 7 to ship in early 2008 has failed, without any explanation or apology. While the main reason for “slipping” Java 7 is the great work that has gone into Java 6 update 10, we still don’t even have the umbrella JSR for Java 7.
The answer to what allows estimating the functionality for the different technologies in coming to decide which one to use. Will we have a cross-platform Swing web browser component in Java 7? Will we have a public Swing API for translucent and shaped windows in Java 7? Will we have a public Swing API for playing, manipulating and producing video streams in Java 7? Will we have binding layer for Swing applications in Java 7? Will we see modern Swing components such as pivot table or date picker in Java 7? Withholding answers to these questions has been a normal practice for the past eighteen months, and this opacity has resulted in quite a few disappointed advocates of the toolkit.
The answer to why defines the level of trust between you and your community. You can be extremely opaque, not only making your decisions behind closed doors, but also presenting them as the only right way. You can be somewhat translucent and write about the reasons behind the decisions, even when the decisions themselves are made without any input from the community. Or, you can be extremely transparent and have the community participate equally in shaping and moving the platform forward.
Different projects will differ in their level of opacity. One-man open source projects are different from commercial companies that are frequently judged by revenue stream and monetization. You have the extremely opaque Apple and somewhat translucent Microsoft. On the other hand, you have an incredibly open Mozilla that already has 61 posts on its aggregator since the beginning of this week. And in between you have projects such as Qt Labs that are engaging in early feeback cycle about emerging functionality in their toolkit.
The last two examples (Mozilla and Qt Labs) show a continuous level of commitment to the two-way communication with their users. Is this coming from the developers? Is this coming from the management? Does it matter at all to your developer users?
If you’re starting a blog about the product and then abandon it after a few weeks, why start it in the first place? If your project has the word “open” in it and then you want to suppress the discussion about the planned features, why bother pretending? Do you really want to hold all your cards close to your chest if you’re trying to compete against a formidable opponent, especially when many of your pre-announced features are already in his well established offerings? Can you afford keeping the potential developers out of the loop instead of using the power of dev-to-dev blogs to build up the excitement? Is it beneficial to your project to hide behind the “open source you can help us instead of whining” mantra, when my chances as a potential outside contributor to add significant new functionality (as opposed to fixing a bug) are almost non existant?
And lastly, can you justify being “heads down” in development / testing to create a void to be filled with rumors, conjectures, interpretations and wild guesses?
The latest addition in the Flamingo component suite is support for pluggable resizing policies on the ribbon tasks and ribbon bands. This has been one of the items on the roadmap for version 4.0 (code named Fainnear), and is now available in the latest 4.0dev drop of Flamingo core and 5.1dev drop of Substance Flamingo plugin.
The existing support has been in place for more than two years, but it was quite flaky. It would often happen that making the ribbon progressively smaller resulted in a jarring resizing behavior of individual bands, and recently a bug report was filed on inconsistent layouts that sometimes enter into an infinite re-layout loop. In addition, the resizing decisions were rigid and not configurable by the application code.
This has finally been addressed in the latest 4.0dev drop of the core Flamingo library. The entire layout / resizing layer has been completely revisited, making the code much simpler, more modular, and, more importantly, configurable by the application code. I will talk about the relevant APIs at the end of this entry, but first let me show a few screenshots that illustrate the out-of-the-box resize policies.
The first screenshot shows the progressive collapse of different ribbon bands under the core CollapseFromLast resize sequencing policy. Under this policy, the ribbon bands are being collapsed from right to left. When the currently collapsing band has reached the last step (iconified), the band to its left becomes the next one to be collapsed.

The next screenshot shows the progressive collapse of different ribbon bands under the core RoundRobin resize sequencing policy. Under this policy, the ribbon bands are being collapsed in a cyclic fashion, distributing the collapsed pixels between the different bands. Under this resize sequencing policy, when the ribbon gets shrinked it is still possible for a specific band to have more width – see the transition from step 5 to step 6 below.

When a ribbon band is fully collapsed (iconified), its contents can be shown by clicking on its collapse button. Unlike before, the display state of the popup band contents is controlled by the most permissive resize policy installed on that band. Here is a screenshot of a popup ribbon band under the core Restrictive resize policy that makes the three trailing buttons to be displayed in MEDIUM display state (unlike before where they were shown in BIG state):

As before, the popups can be multi level (a drop-down gallery shown from a collapsed ribbon band). The command button menus work as well:

Command buttons in popup ribbon bands show their rich tooltips:

Note that all screenshots are taken under Ubuntu and the native GTK look-and-feel. This illustrates how the core Flamingo components adapt the currently set look-and-feel.
If you are interested in testing the new resizing policies in your applications, here is a quick introduction (until the formal documentation is ready). All the relevant code is in the org.jvnet.flamingo.ribbon.resize package.
There are two main concepts – resize policy and resize sequencing policy. The resize policy defines a single visual state of the given ribbon band. For every control in the specific ribbon band (command button, gallery etc), the resize policy defines what is its display state. The resize sequencing policy defines which ribbon band will be chosen next when the ribbon is shrinked / expanded.
The base interface for the resize policies is defined in the RibbonBandResizePolicy interface.The new JRibbonBand.setResizePolicies API can be used to install a custom set of resize policies on the specific ribbon band. The CoreRibbonResizePolicies factory provides two core resize policies list – permissive and restrictive. The default permissive list starts with a resize policy that shows all command buttons and ribbon galleries in the BIG display state, fully utilizing the available screen space. The restrictive list starts with a resize policy that respects the associated ribbon element priority set on the specific component – this is what is used in the last three screenshots (where the popup band shows one BIG button and three MEDIUM buttons).
The base interface for the resize sequencing policies is defined in the RibbonBandResizeSequencingPolicy interface. The new RibbonTask.setResizeSequencingPolicy API can be used to install a custom resize sequencing policy on the specific ribbon task. The CoreRibbonResizeSequencingPolicies factory provides two core policies – round robin and collapse from last. Under the default round robin policy, the ribbon bands are being collapsed in a cyclic fashion, distributing the collapsed pixels between the different bands. Under the collapse from last policy, the ribbon bands are being collapsed from right to left.
If your application needs more control over the resizing of the specific ribbon task or ribbon band, you can implement one (or both) of these interfaces and install them with the APIs mentioned above. To see the default round robin resize sequencing policy and permissive resize policy in action, click the WebStart launch button below and play with the application width:

As always, you are more than welcome to leave comments and report bugs on the project issue tracker, mailing lists or forums.