Thursday, February 19, 2009

Open a file in a JDeveloper Extension.

Since a few months I now run Linux.
And I am using the PMD plugin for JDeveloper.
After the upgrade of JDeveloper to the latest version 11.1.1.0.1 the preferences are always gone.
So I wanted to import my settings again, but the file selector did not come up!

So looking into the log I saw the following stacktrace:
Exception occurred during event dispatching:
java.lang.ClassCastException: sun.awt.NullComponentPeer cannot be cast to sun.awt.X11.XChoicePeer
at sun.awt.X11.XFileDialogPeer.init(XFileDialogPeer.java:264)
at sun.awt.X11.XFileDialogPeer.show(XFileDialogPeer.java:726)
with the following code to open the dialog
final FileDialog fdlg = new FileDialog(new Frame(), "Import", FileDialog.LOAD);
fdlg.setVisible(true);
Looking throug a lot of (blog) post I could not get a fix for this.
So the thing that intrigued by the fact that the JDeveloper File chooser was working.
But that was not the Java default file chooser.

Then the question arises how can I use the JDeveloper file chooser?

A quest was up to get through the knowledge where a description is on the JDeveloper File Chooser.

I finally found it in an extension I also did some work on, the search extension.

It is needed to use the following code:
    public String getPath(String windowText, String currPath)
{
String returnPath = "";

URL url = URLFactory.newDirURL( Ide.getSystemDirectory() );
URLChooser chooser = DialogUtil.newURLChooser( url );
chooser.setSelectionMode( URLChooser.SINGLE_SELECTION );
chooser.setSelectionMode( URLChooser.FILES_ONLY);
int ret = chooser.showOpenDialog( Ide.getMainWindow(), windowText);
if ( ret == URLChooser.APPROVE_OPTION )
{
URL selectedURL = chooser.getSelectedURL();
returnPath = URLFileSystem.getPlatformPathName( selectedURL ) ;
}
return returnPath;
}
With this also on Linux it will work to import or export files into the PMD plugin.