Open File Action

Let's create a simple NetBeans Platform application for opening files. We assume we need an "Open File" action, rather than using the Favorites window to open files, which would work just as well, if not better.

  1. Use the NetBeans Platform Application template in the New Project dialog to create a skeleton NetBeans Platform application.

  2. Add a new module and set dependencies on Datasystems API, File System API, Lookup API, Nodes API, UI Utilities API, and Utilities API.

  3. In the new module, define the class below:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.File;
    import org.openide.awt.ActionID;
    import org.openide.awt.ActionReference;
    import org.openide.awt.ActionRegistration;
    import org.openide.cookies.OpenCookie;
    import org.openide.filesystems.FileChooserBuilder;
    import org.openide.filesystems.FileUtil;
    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.mycore.OpenFileAction")
    @ActionRegistration(displayName = "#CTL_OpenFileAction")
    @ActionReference(path = "Menu/File", position = 10)
    @Messages("CTL_OpenFileAction=Open File")
    public final class OpenFileAction implements ActionListener {
    
        @Override
        public void actionPerformed(ActionEvent e) {
            //The default dir to use if no value is stored
            File home = new File(System.getProperty("user.home"));
            //Now build a file chooser and invoke the dialog in one line of code
            //"user-dir" is our unique key
            File toAdd = new FileChooserBuilder("user-dir").setTitle("Open File").
                    setDefaultWorkingDirectory(home).setApproveText("Open").showOpenDialog();
            //Result will be null if the user clicked cancel or closed the dialog w/o OK
            if (toAdd != null) {
                try {
                    DataObject.find(FileUtil.toFileObject(toAdd)).
                           getLookup().lookup(OpenCookie.class).open();
                } catch (DataObjectNotFoundException ex) {
                    Exceptions.printStackTrace(ex);
                }
            }
        }
        
    }
  4. Add the "image" module, which is in the "ide" cluster, in the Libraries tab of the application's Project Properties dialog.

Run the application. Choose File | Open File and then browse on disk to the files of your choice. Depending on whether you have support for the related file type, e.g., you're able to open image files because of the "image" module added above, the file will open in the application's editor mode.

Read this related blog entry for a different approach!

Comments:

How do you combine this with your own FileType (e.g. AbcFileType) so that the file selected is opened in an appropriate editor, your own?

Håkan

Posted by guest on April 02, 2013 at 12:38 PM PDT #

Hi Hakan, the file should automatically open in your own editor. Nothing needs to be done for that. Go to the New File dialog, select your file on disk, and then it will be opened in the editor you created for it.

Posted by Geertjan on April 15, 2013 at 02:09 PM PDT #

Hi!, thank for your post
please, I have a problem loading xml-file "java.lang.IllegalArgumentException: We expect CloneableEditorSupport in org.openide.nodes.FilterNode$FilterLookup@106d267"

Posted by Javier on May 22, 2014 at 03:45 PM PDT #

Hi Javier,

I just got the same error message.

The solution is to add the "XML Multiview Editor" module to your platform app project.

right click on your platform app project -> properties -> libraries -> ide -> check XML Multiview Editor -> clean and build

Nico

Posted by guest on May 30, 2014 at 02:52 PM PDT #

BTW: this class is not necessary at all.

Add the User Utilities module in the ide section and you have an image viewer without a single line of code.

Nico

Posted by Nico on May 30, 2014 at 02:57 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« July 2015
SunMonTueWedThuFriSat
   
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 
       
Today