X

Geertjan's Blog

  • July 4, 2013

Print File Templates to Output Window

Geertjan Wielenga
Product Manager

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;
}
}
}

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.