开发者

Best way to handle and display files without file extensions within the Netbeans platform?

开发者 https://www.devze.com 2023-04-10 14:40 出处:网络
I have a project that i have to tackle for the company I\'m working for and it basically means I have to handle and display files without any file extension. I\'m not able to change the way they produ

I have a project that i have to tackle for the company I'm working for and it basically means I have to handle and display files without any file extension. I'm not able to change the way they produce these files so I need some advice on the best way to deal with these kinds of files. The DataSystems API seems to only take care of files with extensions, so does that mean I have to make use of the FileSystems API only? If so I would appreciate some samples of Node and Children classes using the FileObject as what they actually represent.

Thanks a mil, really appreciate any help.

After spending some time reading through the material available from the Netbeans Platform website, I'm left with a few questions regarding my goal as mentioned above.

To clarify my objective: 1. I have a folder (outside of the application installation path) which contains files without extensions. Each file represent a point of interest, containing space-separated text data that will later be used to draw graphs by various graphing softwares. 2. I want to display these files as nodes in one module with perhaps a separate module for updating of these files. Updating will encompass a button that will cause the downloading of a compressed file, uncompressing this file, read its contents and interpret that contents to determine how the text data files will be updated. 3. Perhaps have content change listeners on the FileObjects represented by the nodes in order to display/indicate (perhaps via icon change?) the change of file contents. 4. I also want to be able to display the text data (read-only) contained in the FileObject represented by the clicked node in a text editor, but that is all I need to be able to do with these FileObjects and the nodes that represent them. 4. Have another module displaying all the compressed files downloaded to date (assuming that again I will want to display all the files in a 'download' folder) with the help of the Nodes and ExplorerView API. Here I might want to add 'semantic meaning' to these files since they are not average zip/Jar files, thus perhaps using the DataSystems API for this module.

The most dominant question in my mind at this point is what the correct way would be t开发者_运维百科o represent all the files in this folder as nodes, and what this node's subclass and the corresponding Children class would look like. In the examples the "APIObject" is used, I want to set about replacing that object with FileObject if it is not necessary to use the DataSystems API in the first module?

Again I want to thank you for any help, it really is appreciated.


Ok, read your update. So, I'd say that definitely at some point you need DataObjects. The key points:

  1. To "link" DataObjects to FileObjetcs you need a DataLoader. It's a factory of DataObjects, that gets activated only for some kind of files. Basically:

    DataObject dataObject = DataObject.find(fileObject);
    

    See http://wiki.netbeans.org/DevFaqDataLoader

  2. While it's common for a DataLoader to use a file extension to ack a file type, it's not the only way to go. Indeed, DataLoaders use MIME types and a MIMEResolver class can be used to assign a MIME type to a given file. Again, the easiest thing is to look at the extension, but your code can also look at contents to guess (http://wiki.netbeans.org/DevFaqFileRecognition). Basically you have to implement your MIMEResolver with a single method:

    public String findMIMEType (FileObject fileObject);
    

that can do whatever it wants, then you register the MIMEResolver into the system (see http://bits.netbeans.org/dev/javadoc/org-openide-filesystems/org/openide/filesystems/doc-files/HOWTO-MIME.html for details).

At this point, DataObject.find() will be able to instantiate your own subclass of DataObject.

  1. DataObject has got a createNodeDelegate() which can be used to create a Node to put into an view for rendering. It will automaticaly render with the name of the DataObject, which by default is the name of the FileObject. If I recall correctly (but at the moment I'm unsure), the Platform on its own should be able to create the proper DataObject for a directory, which would automatically create a Node with children for each contained file.

  2. For looking at contents, you might have a look at the Editor API. Unfortunately I'm not acquainted with it, but generically speaking it should be activatable again by using MIME types, that its associating the MIME type you've previously defined to a "plain text" style. Look here http://bits.netbeans.org/dev/javadoc/org-openide-text/org/openide/text/doc-files/api.html and don't be worried by the lenght of the document, as it also explains stuff such as changing the contents, that you're not interested to.

  3. Once you are able to render a tree of files, by using FilterNode to hide some children from view (see http://wiki.netbeans.org/DevFaqNodesDecorating).


First, some basic points:

  1. Files are represented by the Filesystems API, so you'll be using it for sure. It's not a problem that FileObject manages an extension, as it will be a "" string in case your files don't have an extension.

  2. DataObjects are another piece. You use them to represent a model entity which is backed by a file (a FileObject, to be precise) and has got some semantics (that you usually implement by subclassing DataObject and adding methods). With "some semantics" I mean something more than merely having a bunch of data such as a sequence of bytes, but data that are interpreted. I mean: if your application is going to provide the typical functions of a file system explorer, that is copying, moving, deleting files etc..., the FileSystems API is enough: you don't need to interpret data. If you e.g. want to implement a photo manipulation application, which also offers functions such as filtering etc..., it's likely that you need to have a PhotoDataObject subclassing DataObject where you put the extra semantics. This implies having methods for reading the data from the file and creating e.g. a BufferedImage, or another representation. This is just a rough way to introduce things, but we can refine to details later. DataObjects are of course and additional complexity over FileObjects, and you should use them only if you need them.

  3. Your question about Nodes and Children is related to displaying stuff, rather than manipulating it (for which DataObject and FileObject are enough). The Nodes API is a generic one for creating a presentation model, that is a data structure that is rendered on the screen. It can be anything you want, of course including a FileSystem or DataObject. I don't see any specific problem with files without extension, as if they don't have an extension they will be just rendered without extension. In any case, subclassing Node you can control the way stuff is rendered on the screen as you want.

To provide some meaningful code example requires that you specify more what you need, in the perspective that I've just described in points 1, 2 and 3.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号