/Users/petercappello/NetBeansProjects/56-2014/56-2014-FileIO/src/TextFileProcessor.java
import java.awt.Component;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.zip.DataFormatException;
import javax.swing.JFileChooser;

/**
 * A text file read/write utility that 
 * reads lines processed by a Processor and
 * writes lines produced by a Processor. 
 * @author Peter Cappello
 */
public class TextFileProcessor
{        
    private final Component parent;
    
    /**
     * Constructs a TextFileProcessor.
     * @param parent The parent argument determines 2 things: 
     * the frame on which the open dialog depends and the component whose 
     * position the look and feel should consider when placing the dialog. 
     * If the parent is a Frame object (such as a JFrame) then the dialog 
     * depends on the frame and the look and feel positions the dialog relative 
     * to the frame (for example, centered over the frame). 
     * If the parent is a component, then the dialog depends on the frame 
     * containing the component, and is positioned relative to the component 
     * (e.g., centered over the component). 
     * If the parent is null, then the dialog depends on no visible window, and 
     * it's placed in a look-and-feel-dependent position such as the center of 
     * the screen.
     */
    public TextFileProcessor( Component parent ){ this.parent = parent; }
    
    /**
     * Read a text file, passing each line to a Processor.
     * @param processor processes the file, 1 line at a time.
     * @throws IOException
     * @throws DataFormatException 
     */
    public void readFile( Processor processor ) throws IOException, DataFormatException
    {
        JFileChooser fileChooser = new JFileChooser();
        int returnValue = fileChooser.showOpenDialog( parent );
        if ( returnValue == JFileChooser.APPROVE_OPTION )
        {
            File readFile = fileChooser.getSelectedFile();
            try ( BufferedReader reader = new BufferedReader( new FileReader( readFile.getCanonicalPath() ) ) )
            {
                for ( String line; ( line = reader.readLine() ) != null; ) 
                {
                    processor.processInputLine( line );
                }
            }
        }
        // else user cancelled
    }
    
    /**
     * Write a text file from lines produced by a Processor.
     * @param processor constructs the file to be written, 1 line at a time.
     * @throws java.io.IOException
     */
    public void writeFile( Processor processor ) throws IOException
    {
        JFileChooser fileChooser = new JFileChooser();
        int returnValue = fileChooser.showSaveDialog( parent );
        if ( returnValue == JFileChooser.APPROVE_OPTION )
        {
            File writeFile = fileChooser.getSelectedFile();
            try ( Writer writer = new BufferedWriter( new FileWriter( writeFile.getCanonicalPath() ) ) )
            {
                processor.processOutputLines( writer );
            }
        }
        // else user cancelled
    }
}