Download NetBeans!

20080513 Tuesday May 13, 2008

Identifying the PNG Files in the JDK

Let's identify all the PNG files in the JDK's Boot ClassPath. Create a new NetBeans module with an action that has a performAction defined as folows:

OutputWriter writer;

@Override
public void performAction() {
    InputOutput io = IOProvider.getDefault().getIO("PNG Images", false);
    try {
        writer = io.getOut();
        io.select();
        io.getOut().reset();
        Set cps = GlobalPathRegistry.getDefault().getPaths(ClassPath.BOOT);
        for (ClassPath cp : cps) {
            List entries = cp.entries();
            for (ClassPath.Entry entry : entries) {
                ClassPath entryCp = entry.getDefiningClassPath();
                FileObject[] fos = entryCp.getRoots();
                for (FileObject fo : fos) {
                    cycleThroughKids(fo);
                }
            }
        }
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
}

private void cycleThroughKids(final FileObject fo) {
    final Thread thread = new Thread() {

        @Override
        public void run() {
            if (fo.getChildren().length > 0) {
                for (FileObject oneKid : fo.getChildren()) {
                    if (oneKid.getMIMEType().equals("image/png")) { {
                        try {
                            writer.println(oneKid.getPath(), new PngOutPutListener(oneKid));
                        } catch (IOException ex) {
                            Exceptions.printStackTrace(ex);
                        }
                    }
                    cycleThroughKids(oneKid);
                }
            }
        }
    };
    thread.start();
}

The line in bold above has as its final argument the creation of a NetBeans API OutputListener class, which magically returns a hyperlink for each line added to the Output window, as defined below:

class PngOutPutListener implements OutputListener {

    FileObject oneKid;

    PngOutPutListener(FileObject oneKid) {
        this.oneKid = oneKid;
    }

    @Override
    public void outputLineAction(OutputEvent arg0) {
        try {
            DataObject dObj = DataObject.find(oneKid);
            OpenCookie open = (OpenCookie) dObj.getCookie(OpenCookie.class);
            if (open != null) {
                open.open();
            }
        } catch (DataObjectNotFoundException ex) {
            Exceptions.printStackTrace(ex);
        }
    }

    @Override
    public void outputLineSelected(OutputEvent arg0) {}

    @Override
    public void outputLineCleared(OutputEvent arg0) {}
    
}

When a link is clicked, which is an event that a hyperlink implies should be performed, the file is opened in the editor. This is how the hyperlinks look in the Output window:

Upon being clicked, the related PNG file opens in the editor. Not useful in any way, though.

May 13 2008, 10:50:53 AM PDT Permalink

Trackback URL: http://blogs.sun.com/geertjan/entry/identifying_the_png_files_in
Comments:

GlobalPathRegistry.getDefault().getPaths(ClassPath.BOOT) will only work if there are some (Java) projects open. You might prefer to use JavaPlatformManager directly to get the default JDK (or prompt the user for which JDK to list).

Posted by Jesse Glick on May 13, 2008 at 02:19 PM PDT #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed