/*
 * @(#)SimpleFunctionPaneWithIO.java  2.5.0  2 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 edu.neu.ccs.parser.*;

import java.awt.*;
import java.awt.font.*;
import javax.swing.*;
import java.util.*;

/**
 * <p>Class <code>SimpleFunctionPaneWithIO</code>
 * extends <code>SimpleFunctionPane</code>
 * in order to add the facility
 * to save simple function definitions to a file
 * and to read back such definitions from a file.</p>
 * 
 * <p>The file extension used is "fcn".</p>
 * 
 * @author Richard Rasala
 * @version 2.5.0
 */
public class SimpleFunctionPaneWithIO
    extends SimpleFunctionPane
{
    /**
     * The text file IO tool initialized with
     * the file extension "fcn".
     */
    protected TextFileIO fileIO =
        new TextFileIO("fcn");
    
    /** The action to save definitions. */
    protected SimpleAction save =
        new SimpleAction("Save Definitions") {
            public void perform() { saveDefinitions(); }
    };
    
    /** The action to read definitions. */
    protected SimpleAction read =
        new SimpleAction("Read Definitions") {
            public void perform() { readDefinitions(); }
    };
    
    
    /** The button to save definitions. */
    protected JButton saveButton =
        new JButton(save);
    
    /** The button to read definitions. */
    protected JButton readButton =
        new JButton(read);
    
    // Set the font for the buttons
    {
        saveButton.setFont(buttonFont);
        readButton.setFont(buttonFont);
    }
    
    
    /** The array with the 2 buttons. */
    protected Object[] IOButtons =
        { saveButton, readButton };
    
    /** The table with the 2 buttons. */
    protected HTable IOButtonsTable =
        new HTable(IOButtons, gap, gap, CENTER);
    
    
    /**
     * The constructor for a
     * simple function definition pane with IO.
     */
    public SimpleFunctionPaneWithIO() {
        mainTable.addObject(IOButtonsTable, 2);
    }
    
    
    /** The method to save definitions. */
    protected void saveDefinitions() {
        String defs = parser.simpleFunctionsToString();
        fileIO.saveDataToFile(defs);
    }
    
    
    /** The method to read definitions. */
    protected void readDefinitions() {
        try {
            String defs = fileIO.readDataFromFile();
            parser.stringToSimpleFunctions(defs);
        }
        catch (Throwable ex) {
            String message =
                "Error reading simple function definitions\n"
                + ex.getMessage();
            
            GeneralDialog.showOKDialog
                (message, "Simple Function Error");
        }
        finally {
            populateFcnsList();
        }
    }
    
    
    /**
     * <p>The main method launches
     * a simple function definition pane with IO
     * in its own GUI frame.</p>
     * 
     * <p>Use the call:</p>
     * 
     * <pre>  SimpleFunctionPaneWithIO.main(null)</pre>
     * 
     * @param args ignored and may be <code>null</code>
     */
    public static void main(String[] args) {
        new SimpleFunctionPaneWithIO().
            frame("Simple Function Pane With IO");
    }
   
}

