X

Geertjan's Blog

  • July 3, 2013

New File Without Project

Geertjan Wielenga
Product Manager

The upcoming NetBeans Platform 7.4 @TemplateRegistration attribute requireProject is handy when you want to display a file template in the New File dialog when you have no project that relates to it. However, what if you not only have no related project but no interest in the NetBeans project system at all? Then you have a problem because you need the NetBeans project APIs if you want to have the New File dialog because the New File dialog classes are found in the modules that define the NetBeans project system. Lots of menus and toolbar buttons are added to your application for free, superfluously, and you spend time hiding them while at the same time the user gets a bunch of JARs that are not needed at all. Though the coupledness between projects and files makes complete sense from the perspective of NetBeans IDE, it would be nice if the New File dialog were to be in its own separate module from the perspective of non-project oriented NetBeans Platform applications.

A simple workaround is to forget the New File dialog completely, because of above stated dependencies on the project system, and create your own. Even simpler is to create a bunch of simple actions that create new files for you, which means that the whole project infrastructure isn't needed at all.

Here's an example, with thanks to the NetBeans Ribbon Bar project, provided by Chris Bohme from the awesome Maltego team.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;
import org.netbeans.api.actions.Openable;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileSystem;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataFolder;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle.Messages;
@ActionID(
category = "File",
id = "org.squibbly.file.odp.NewFile")
@ActionRegistration(
displayName = "#CTL_NewFile")
@ActionReference(
path = "Menu/File",
position = 0)
@Messages("CTL_NewFile=New File")
public final class NewFileAction implements ActionListener {
private static AtomicInteger _integer = new AtomicInteger(0);
@Override
public void actionPerformed(ActionEvent e) {
try {
DataObject gdo = getDataObject();
Openable openable = gdo.getLookup().lookup(Openable.class);
openable.open();
} catch (DataObjectNotFoundException ex) {
Exceptions.printStackTrace(ex);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}
protected DataObject getDataObject() throws DataObjectNotFoundException, IOException {
String templateName = getTemplate();
FileObject fo = FileUtil.getConfigRoot().getFileObject(templateName);
DataObject template = DataObject.find(fo);
FileSystem memFS = FileUtil.createMemoryFileSystem();
FileObject root = memFS.getRoot();
DataFolder dataFolder = DataFolder.findFolder(root);
DataObject gdo = template.createFromTemplate(
dataFolder,
"New Document" + " (" + getNextCount() + ")");
return gdo;
}
protected String getTemplate() {
return "Templates/Other/file";
}
private static int getNextCount() {
return _integer.incrementAndGet();
}
}

You'd create one of the above for each type of document you'd like the user to be able to create. Note that "getTemplate" above, which points to the location in the filesystem where you've registered your template, probably thanks to the @TemplateRegistration annotation.

Join the discussion

Comments ( 3 )
  • Michael Bishop Friday, July 5, 2013

    Currently this code appears to write the contents of a template to memory, then open them in the associated editor the file type. I'm assuming when the editor is modified and the "Save" button is pressed, it prompts the user for a file location to save to.

    How would this be modified if we know where we want to save the file on the file system? I want the template to be written to an actual file location before opening it for the user to edit.

    Excellent post, thanks!


  • Andreas Hauffe Sunday, September 29, 2013

    How do I realize when the editor is modified and the "Save" button is pressed, it prompts the user for a file location to save to? I tried and it does not work automatically.

    Regards

    Andreas


  • Chris Wednesday, December 7, 2016

    Hey Andreas,

    unfortunately it is not possible at all, Geertjan and me had a very long discussion about this feature. I created a Plugin called NbScratchFile (http://plugins.netbeans.org/plugin/58020/nbscratchfile) where you can create a file in Memory and it will open in the editor immediately. But I can't hit ctrl + s to save the file to the File System, it will only save the file in memory. Big Editors does this like Sublime Text, Atom and Visual Studio, if you hit ctrl + s it will prompt you the save dialog, this is not possible in NetBeans. As a workaround, I created a shortcut for save as (Ctrl + Alt + S; Ctrl + Alt + S). This is only a workaround, but it works. But nice to see that another user has such problem ;).


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.