In the last part of this tutorial we have added a configuration wizard to the schema2beanssupport. So now we can set the parameters via a GUI. I had planned to add some info on how to copy the files and configure the target project for the fourth part, but I changed my mind ’cause I received an email by Jesse Glick today. He told me how I could get hold of the projects source dir and the available package names and I had to try this immediately. Here is what Jesse wrote:
> ClassPath.getClassPath(d.getPrimaryFile(), ClassPath.SOURCE)
> will give you the root of the source folder (if applicable); from there you can use FileUtil.getRelativePath or whatever, after some robustness checks.
This could remedy the problem that until now the generated classes are placed in the directory where the schema is. I wrote my first tests and wanted to import the module. So I typed “ClassPath” in the library manager and found some libraries. The first in the list was the Java Support API. That’s the one I was looking for. But I also found the Java Project Support API and the description told me, that the wizard for the new files uses this for package management. I tried to find some documentation, but wasn’t too lucky so I decided to browse the sources. The first Intersting thing I found is PackageView. It has a createListView(SourceGroup group) method which returns a ComboBoxModel. It is used to create the PackageChooserComboBox of the JavaTargetChooserPanelGUI. Yet I had no idea how to get hold of a SourceGroup, but after some “findUsages” I found this:
Sources sources = ProjectUtils.getSources(project);
SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
Nice, so all we need is a project, and we get the package view for free. Bring up your schema2beans action class and add this method:
private Project getProject(FileObject file){
Project p = FileOwnerQuery.getOwner(file);
return p;
}
This returns the project. In the actionperformed change the WizardDescriptor generation like this:
Sources sources = ProjectUtils.getSources(getProject(dataObject.getPrimaryFile()));
SourceGroup[] groups = sources.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
WizardDescriptor wizardDescriptor = new WizardDescriptor(getPanels(groups));
Now change the start of the getPanels() method to this:
private WizardDescriptor.Panel[] getPanels(SourceGroup [] sourceGroups) {
if (panels == null) {
panels = new WizardDescriptor.Panel[] {
new Schema2BeansWizardPanel1(sourceGroups)
}
...
Add a constructors to Schema2BeansWizardPanel1:
SourceGroup [] sourceGroups;
public Schema2BeansWizardPanel1 (SourceGroup [] sourceGroups){
super();
this.sourceGroups = sourceGroups;
}
and change the getComponent method:
public Component getComponent() {
if (component == null) {
component = new Schema2BeansVisualPanel1(sourceGroups);
}
return component;
}
Now change the contructor of Schema2BeansVisualPanel1:
private ComboBoxModel comboBoxModel;
/** Creates new form Schema2BeansVisualPanel1 */
public Schema2BeansVisualPanel1(SourceGroup [] sourceGroups) {
comboBoxModel = PackageView.createListView(sourceGroups[0]);
initComponents();
}
add a line to the getParameters method:
String parameters = "";
parameters+=" -p "+packageComboBox.getEditor().getItem().toString(); //<-add this line
...
Go to the visual editor, select the combobox and switch to the “code” tab in the properties panel, add some post-initialization code:
packageComboBox.setRenderer(PackageView.listRenderer());
packageComboBox.setModel(comboBoxModel);
That part also adds a nice icon to the dropdown list. So now we can also add a package via a GUI component.

Now we still have the problem, that the dir variabledoesn’t hold the source rootfolder, so you need to copy the generated classes there manually. You can fix this by adding a parameter to Schema2BeansVisualPanel1
SourceGroup sourceGroup;
/** Creates new form Schema2BeansVisualPanel1 */
public Schema2BeansVisualPanel1(SourceGroup [] sourceGroups) {
sourceGroup = sourceGroups[0];
comboBoxModel = PackageView.createListView(sourceGroups[0]);
initComponents();
}
and another line to the getParameters method:
parameters+=" -r\""+sourceGroup.getRootFolder().getPath()+"\""; // path must be last parameter due to the way it's split
return parameters.trim();
Now you can throw out the default parameters in your Actions actionPerformed method and use this instead:
String [] parts = ((String)wizardDescriptor.getProperty(Schema2BeansWizardPanel1.PROP_PARAM)).split("\"");
String dir = parts[1];
System.out.println("dir "+dir);
String [] userargs = parts[0].split("\\s+");
String [] args = new String [userargs.length+3];
System.arraycopy(userargs, 0,args,0,userargs.length );
args[args.length-3]=dir;
args[args.length-2]="-f";
args[args.length-1]=path;
GenBeans.main(args);
I learned a lot about the API doing this. Maybe I will reuse some other parts to create a wizard for the “New File” section instead of just a context menu action. Well, in the next part of this blog series I will probably be showing how to configure target projects :).