NetBeans Platform: How to register hidden file types

289 views Asked by At

I'm writing a NetBeans plugin and would like to register a file type. The file type is a hidden file (e.g. ".something") with mime-type text/plain and a settled name (".something"). The registration looks like this:

@MIMEResolver.ExtensionRegistration(
        displayName = "#Label",
        mimeType = "text/plain+something",
        extension = {"something"}
)
@DataObject.Registration(
    mimeType = "text/plain+something",
    iconBase = "com/welovecoding/netbeans/plugin/logo.png",
    displayName = "#Label",
    position = 300
)
public class SomethingDataObject extends MultiDataObject {

  public SomethingDataObject(FileObject pf, MultiFileLoader loader) throws DataObjectExistsException, IOException {
    super(pf, loader);
    registerEditor("text/plain", true);
  }
  //...
}

The problem with this is NetBeans will only recognize the filetype if the name of the file has a name, a point and an extension (e.g. "name.something"). Just a point and an extension (e.g. ".something") is not recognized properly. Is there a solution for this kind of problem?

1

There are 1 answers

0
Yser On BEST ANSWER

I solved the problem by implementing a custom non-declarative MIMEResolver. Here's the code:

@ServiceProvider(service = MIMEResolver.class, position = 3214328)
public class FilenameResolver extends MIMEResolver {

  private static final String mimetype = "text/plain+something";

  public FilenameResolver() {
    super(mimetype);
  }

  @Override
  public String findMIMEType(FileObject fo) {
    String nameExt = fo.getNameExt();
    if (".something".equalsIgnoreCase(nameExt)) {
      return mimetype;
    }
    return null;
  }

}

There's a declarative MIMEResolver too. Note that the declarative way seems to be preferred by NetBeans-Devs.