Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

New File From Template Without Project

DZone's Guide to

New File From Template Without Project

· Java Zone
Free Resource

Try Okta to add social login, MFA, and OpenID Connect support to your Java app in minutes. Create a free developer account today and never build auth again.

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.

Build and launch faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}