org.openide.filesystems.FileUtil.findBrother

The org.openide.filesystems.FileUtil.findBrother method is typically used by the Matisse GUI Builder so that if a file named "xyz.java" is in the same folder as a file named "xyz.frm", the two are merged together and opened as one in the editor. Specifically, FileUtil.findBrother takes a FileObject and a file extension, which is the file extension of the "brother" file.

There are several other conceivable scenarios where this functionality might be handy, such as Wicket, which typically has "xyz.java" in the same folder as "xyz.html". The latter provides the markup for the former. Below you can see my point, the Java files that have a matching HTML file (i.e., the same name) have a Wicket icon merged with their Java icon:

So, first create a new DataObject for text/x-java—and make sure it will install before the standard Java DataObject. Then, use the DataLoader to branch the DataObject, depending on whether a "brother" is found:

@Override
protected MultiDataObject createMultiObject(FileObject primaryFile) throws DataObjectExistsException, IOException {
    FileObject bro = FileUtil.findBrother(primaryFile, "html");
    if (null != bro) {
        //Return our own data object:
        return new WicketDataObject(primaryFile, this);
    }
    //Return the standard data object:
    return new JavaDataObject(primaryFile, this);
}

Finally, in the Node, use org.openide.util.Utilities.mergeImages to merge a small image on top of the standard Java icon:

private static final String WICKET_ICON_BASE = 
         "org/netbeans/findbrotherdemo/wicket_8x8.png";
private static final String JAVA_ICON_BASE =   
         "org/netbeans/modules/java/resources/class.gif";
 
@Override
public Image getIcon(int arg0) {
    Image wicket = Utilities.loadImage(WICKET_ICON_BASE);
    Image java = Utilities.loadImage(JAVA_ICON_BASE);
    Image result = Utilities.mergeImages(java, wicket, 8, 8);
    return result;
}

Install the module and then Java files with HTML brothers will have our Wicket icon merged with their Java icon.

Comments:

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
« April 2014
SunMonTueWedThuFriSat
  
12
13
14
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today