/**
 * @(#)StringableFileIO.java  2.5.0  1 September 2006
 *
 * Copyright 2006
 * College of Computer and Information Science
 * Northeastern University
 * Boston, MA  02115
 *
 * The Java Power Tools software may be used for educational
 * purposes as long as this copyright notice is retained intact
 * at the top of all source files.
 *
 * To discuss possible commercial use of this software, 
 * contact Richard Rasala at Northeastern University, 
 * College of Computer and Information Science,
 * 617-373-2462 or rasala@ccs.neu.edu.
 *
 * The Java Power Tools software has been designed and built
 * in collaboration with Viera Proulx and Jeff Raab.
 *
 * Should this software be modified, the words "Modified from 
 * Original" must be included as a comment below this notice.
 *
 * All publication rights are retained.  This software or its 
 * documentation may not be published in any media either
 * in whole or in part without explicit permission.
 *
 * This software was created with support from Northeastern 
 * University and from NSF grant DUE-9950829.
 */

package edu.neu.ccs.gui;

import edu.neu.ccs.*;

import java.io.*;
import java.text.ParseException;

/**
 * <p>Class <code>StringableFileIO</code> contains
 * methods to load or save the state of a
 * <code>Stringable</code> model object
 * using text file IO.</p>
 * 
 * <p>The basic methods use file dialogs.  The most
 * complex methods assume a File object has been
 * defined and associated with a text file and then
 * work with that object.</p>
 * 
 * <p>An instance retains information about the
 * <code>Stringable</code> model object as well as
 * the file extension, the file filter, and the most
 * recent directory visited by its file chooser
 * internal object.</p>
 * 
 * <p>Utilizes the class <code>TextFileIO</code> in
 * its implementation.</p>
 * 
 * @author Richard Rasala
 * @version 2.5.0
 */
public class StringableFileIO
{
    /** The stringable model to read or save via this class. */
    protected Stringable model = null;
    
    /** The text file IO tool. */
    protected TextFileIO textFileIO = null;
    
    
    /**
     * <p>The constructor that provides the Stringable model
     * to read or save via this class and the file extension
     * for its files.</p>
     * 
     * <p>Throws <code>NullPointerException</code> if the model
     * is <code>null</code>.</p>
     * 
     * <p>Uses no file extension if the given extension is
     * <code>null</code>.</p>
     * 
     * @param model the Stringable model requiring file IO
     * @param extension the file extension to use for model files
     * @throws NullPointerException
     */
    public StringableFileIO(Stringable model, String extension) {
        if (model == null)
            throw new NullPointerException
                ("Null model passed to StringableFileIO constructor");
        
        this.model = model;
        this.textFileIO = new TextFileIO(extension);
    }
    
    
    /** Returns the Stringable model object. */
    public Stringable getModel() { return model; }
    
    
    /**
     * <p>Opens a file dialog to get the file name of a text file
     * with model data and then reads the data into the model.<p>
     *
     * <p>Displays error dialogs.</p>
     * 
     * <p>Does nothing to the current data in the model if errors
     * occur.</p>
     *
     * @return true if the operation succeeds
     */
    public boolean readDataFromFile()
    {
        return readDataFromFile(true);
    }
    
    
    /**
     * <p>Opens a file dialog to get the file name of a text file
     * with model data and then reads the data into the model.<p>
     *
     * <p>Displays error dialogs if the parameter is set to true.</p>
     * 
     * <p>Does nothing to the current data in the model if errors
     * occur.</p>
     *
     * @return true if the operation succeeds
     */
    public boolean readDataFromFile(boolean displayErrorDialogs)
    {
        String data =
            textFileIO.readDataFromFile
                (displayErrorDialogs);
        
        return setModel(data, displayErrorDialogs);
    }
    
    
    /**
     * <p>Reads the model data from the given text file.<p>
     *
     * <p>Does nothing to the current data if errors occur.</p>
     *
     * @param source the data source
     * @param displayErrorDialogs if true display error dialogs
     *
     * @return true if the operation succeeds
     */
    public boolean readDataFromFile
        (File source, boolean displayErrorDialogs)
    {
        String data =
            textFileIO.readDataFromFile
                (source, displayErrorDialogs);
        
        return setModel(data, displayErrorDialogs);
    }
    
    
    protected boolean setModel
        (String data, boolean displayErrorDialogs)
    {
        if (data == null)
            return false;
        
        try {
            model.fromStringData(data);
        }
        catch (ParseException ex) {
            if (displayErrorDialogs) {
                String message =
                    "Error in setting model from file data\n"
                    + ex.getMessage() + "\n";
                
                GeneralDialog.showOKDialog
                    (message, "File Read Error");
            }
            
            return false;
        }
        
        return true;
    }
    
    
    /**
     * <p>Opens a file dialog to get the file name of a model file
     * and then saves the model data to the file.<p>
     *
     * <p>Displays error dialogs.</p>
     * 
     * <p>Does no save if any errors occur.</p>
     * 
     * @return true if the operation succeeds
     */
    public boolean saveDataToFile()
    {
        return saveDataToFile(true);
    }
    
    
    /**
     * <p>Opens a file dialog to get the file name of a model file
     * and then saves the model data to the file.<p>
     *
     * <p>Displays error dialogs if the parameter is set to true.</p>
     * 
     * <p>Does no save if any errors occur.</p>
     * 
     * @return true if the operation succeeds
     */
    public boolean saveDataToFile(boolean displayErrorDialogs)
    {
        String data = model.toStringData();
        
        return textFileIO.saveDataToFile
            (data, displayErrorDialogs);
    }
    
    
    /**
     * <p>Saves the model data to the given model file.<p>
     *
     * <p>Displays error dialogs if the last parameter is
     * set to true.</p>
     * 
     * <p>Does no save if any errors occur.</p>
     * 
     * @param target the data target
     * @param displayErrorDialogs if true display error dialogs
     * @return true if the operation succeeds
     */
    public boolean saveDataToFile
        (File target, boolean displayErrorDialogs)
    {
        String data = model.toStringData();
        
        return textFileIO.saveDataToFile
            (target, data, displayErrorDialogs);
    }
    
}

