Adding files to an existing project
By John 'Jb' Brock-Oracle on Oct 28, 2011
I've run into this question a few times now in the Jdeveloper forums and thought it would be a good topic for this blog.
There are multiple ways that you can add a file to a project in JDeveloper, but only one that will really get the file properly setup in the IDE framework.
NOTE: The code shown below will work in both 11gR1 and 11gR2.
Let's start by showing the entire code block and then breaking it down.
The first thing we have to do, is get the current project, so that we know where to write this new file to. I'm running this chunk of code from the controller class of a Context Menu item that I created to show up in the Navigator. This provides me with a context to work with and I can get the project with a simple getProject() call.
The most important thing to note in all of the file manipulation, is that we are NOT using the standard Java.io methods. We are using URLFileSystem and URLFactory to do all of the work. This makes sure that the file that we create is being integrated into the IDE framework properly.
A quick lesson in Navigator view and Directory structures
The Navigator window in the IDE, is not a representation of what is on the filesystem. This is really important to understand. That Navigator is a representation of what is in your Application (.jws) and Project (.jpr) files. There are many things in the filesystem that do not show up in the Navigator. One thing in particular is an empty directory is filtered out and not shown at all.
The Navigator also does it's own grouping of source files into a folder called Application Sources and other files located in non-source-path directories are grouped under a folder called Resources.
By default any files under the /src directory will be shown in the Application Sources folder of the Navigator window.
Here is a quick look at the CreateStructure sample project and how things look on the filesystem and in the navigator window.
Notice how the directories that I created earlier (/src/test/name) do not show in the navigator window?
By default the navigator shows the directories under /src as the package id. You can adjust how this is displayed by adjusting the package level value in the navigator display settings
Writing the file
Ok, back to the actual code now. You will see that I created a URL variable called path that points to the directory that I want to place the new file in. I used the URLFileSystem method mkdirs() to make sure the directory is created before I try and place the file in it. The nice thing about mkdir*s* versus mkdir is that it will create the entire path, including parent directories, if they don't already exist.
In the sample, I've just hardcoded the file name. You can get this from a dialog or whatever means you want. I then created a new URL using both the path and filename variables.
I then created a new TextNode to represent our new file, and I called a method that I wrote called writeDefaultText(). This just adds a comment to the top of our new file so that it's not empty. The editor handles files with a little something in them better. I believe most folks that take the time to write an extension that adds a file, is going to seed that file with some pre-generated code anyway.
Here is a look at the writeDefaultText() method
After we have some content in the file we do sav of the file in it's own try / catch block.
Updating the Navigator
Finally we need to update the Navigator window so that it displays our new file as part of the project.
While you can use the standard java.io classes to create the file on the filesystem, it's best to use the URLFileSystem methods provided by the IDE to do this. It's really a pretty easy thing to do once you know the proper methods to use.
As always, don't hesitate to add your comments and experiences to the comments section so others can learn from your coding experiences as well.