Thursday Jul 04, 2013

Print File Templates to Output Window

Here's how to show a dialog listing the folders and files of the Templates folder.

When OK is clicked in the dialog, all the templates in the selected folder are printed to the Output window.

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.Enumeration;
import javax.swing.JPanel;
import org.openide.DialogDescriptor;
import org.openide.DialogDisplayer;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.view.BeanTreeView;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
import org.openide.loaders.DataObjectNotFoundException;
import org.openide.nodes.FilterNode;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.NbBundle.Messages;
import org.openide.windows.IOProvider;
import org.openide.windows.InputOutput;
import org.openide.windows.OutputWriter;

@ActionID(
        category = "File",
        id = "org.netbeans.printtemplates.PrintTemplatesAction")
@ActionRegistration(
        displayName = "#CTL_PrintTemplatesAction")
@ActionReference(path = "Menu/File", position = 0)
@Messages("CTL_PrintTemplatesAction=Print Templates")
public final class PrintTemplatesAction implements ActionListener {

    private ExplorerManager em = new ExplorerManager();

    @Override
    public void actionPerformed(ActionEvent e) {
        DialogDescriptor dd = new DialogDescriptor(
                new SelectorPanel(), "Select Category", true, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == DialogDescriptor.OK_OPTION) {
                    OutputWriter writer;
                    InputOutput io = IOProvider.getDefault().getIO("Selected Templates", true);
                    io.select();
                    writer = io.getOut();
                    Node[] selectedNodes = em.getSelectedNodes();
                    for (Node node : selectedNodes) {
                        Enumeration children =
                                node.getLookup().lookup(FileObject.class).getChildren(true);
                        for (FileObject fileObject : Collections.list(children)) {
                            if (!fileObject.isFolder()) {
                                try {
                                    writer.println(DataObject.find(
                                      fileObject).getNodeDelegate().getDisplayName());
                                } catch (DataObjectNotFoundException ex) {
                                    Exceptions.printStackTrace(ex);
                                }
                            }
                        }
                    }
                }
            }
        });
        DialogDisplayer.getDefault().createDialog(dd).setVisible(true);
    }

    private class SelectorPanel extends JPanel implements ExplorerManager.Provider {

        public SelectorPanel() {
            setLayout(new BorderLayout());
            setPreferredSize(new Dimension(400, 400));
            add(new BeanTreeView(), BorderLayout.CENTER);
            try {
                em.setRootContext(
                        new FilterNode(DataObject.find(
                        FileUtil.getConfigFile("Templates")).getNodeDelegate()));
            } catch (DataObjectNotFoundException ex) {
                Exceptions.printStackTrace(ex);
            }
        }

        @Override
        public ExplorerManager getExplorerManager() {
            return em;
        }

    }

}
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
« July 2013 »
SunMonTueWedThuFriSat
 
5
10
14
21
27
28
29
30
31
   
       
Today