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.

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
« May 2008 »
SunMonTueWedThuFriSat
    
2
5
7
16
20
22
25
28
       
Today