Over a million developers have joined DZone.

New File From Template Without Project

· Java Zone

Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code! Brought to you in partnership with ZeroTurnaround.

The NetBeans Platform provides support to create new projects, and new files within those projects. You have a wizard to choose which type of file you want, you can provide a wizard to setup your new file, give the user inline help, and so on. However, some time ago I needed to open and create new files, but without having projects  for the development of SweetHome3D on NetBeans. This led me to research on the subject, and answered my own question.

Here is how I solved it:

 Open:

Open file

Here I use auxilliary functions, but you can use yours as well. What I want to show you is not affected.

First, create your action, set up your names, icons, menues and all you need. Then implement the  actionPerformed function as this:

    @Override
public void actionPerformed(ActionEvent e) {
ContentManager contentManager = SweetHome3D.getInstance().
getContentManager();
//Show a Open dialog with type sh3d
String homeName = contentManager.showOpenDialog(
null,
bundle.getString("HomePane.openHomeDialog.title"),
ContentManager.ContentType.SWEET_HOME_3D);
if (homeName != null) {
open(homeName);
}
}

/**
* Opens a given <code>homeName</code>home.
* @param homeName
*/
public void open(final String homeName) {
DataObject home = null;
try {
//Find the File's DataObject
home = DataObject.find(FileUtil.toFileObject(new File(homeName)));
} catch (DataObjectNotFoundException ex) {
Exceptions.printStackTrace(ex);
return;
}
try {
//get the open cookie and use it
home.getCookie(OpenCookie.class).open();
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
return;
}
}

Then, the New Action. First you have to get the Template from the layer; then create a new file from this template, asking the user to save the template. Then just open it as you did in Open:

 

 

    @Override
public void actionPerformed(ActionEvent e) {
ContentManager contentManager = SweetHome3D.getInstance().getContentManager();
//The template path
FileObject configFO = FileUtil.getConfigFile("/Templates/eTeks/sH3DFile");
DataObject doTemplate = null;
DataObject home = null;
//Show a Save dialog with type sh3d
String filename = contentManager.showSaveDialog(
null,
bundle.getString("HomePane.saveHomeDialog.title"),
ContentManager.ContentType.SWEET_HOME_3D,
configFO.getName());
try {
doTemplate = DataObject.find(configFO);
} catch (DataObjectNotFoundException ex) {
Exceptions.printStackTrace(ex);
return;
}
if (filename != null) {
File newHome = new File(filename);
if (newHome.exists()) {
//delete the file. createFromTemplate fails
//if the file already exists
try {
newHome.delete();
} catch (Exception ex) {
return;
}
}
try {
FileUtil.createData(newHome.getParentFile());
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
return;
}
try {
home = doTemplate.createFromTemplate(
DataFolder.findFolder(
FileUtil.toFileObject(newHome.getParentFile())),
newHome.getName());
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
return;
}
//From now on, this is like Open
try {
home.getCookie(OpenCookie.class).open();
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
return;
}
}
}

There are improvements you can add, such as allow to select among the available templates or registered MIMEtypes, executing the asociated wizard, etc., but that's already done in the Project UI. Unfortunatelly, that's only for Projects, which is why I started this post in the first place.

The Java Zone is brought to you in partnership with ZeroTurnaround. Check out this 8-step guide to see how you can increase your productivity by skipping slow application redeploys and by implementing application profiling, as you code!

Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}