/*
 * @(#)SaveOpenDemoMenuHandler.java
 *
 * Copyright 2001 Thomas P. Hayes, All Rights Reserved.
 *
 * This software is the proprietary information of Thomas P. Hayes.
 * Use outside the CSPP 535 course taught by the author is subject
 * to license agreement.
 */

package saveopen.translation;
import javax.swing.*;
import java.awt.event.*;
import saveopen.presentation.*;
import saveopen.application.*;

public class SaveOpenDemoMenuHandler implements ActionListener {
    
    /**
     * This is the Frame from which we listen accept "Save", "Open" and
     * "Quit" commands.  Commands from other windows are heard but not
     * obeyed.
     */
    protected SaveOpenDemoFrame theSource;

    /**
     * This is the most recent Frame from which an ActionEvent has
     * been heard, excluding theSource.
     */
    protected JFrame theTarget;

    /** 
     * Creates an instance of <code>SaveOpenDemoMenuHandler</code>, 
     * and registers it to receive <code>ActionEvents</code>
     * from <code>source</code>.
     */
    public SaveOpenDemoMenuHandler(SaveOpenDemoFrame source) {
        source.addActionListener(this);
        theSource = source;
    }
    
    /**
     * Our implementation of the ActionListener interface.
     */
    public void actionPerformed(ActionEvent e) {
        String ac = e.getActionCommand();
        if (ac.equals("SODFQuit")) {
            System.out.println("Quit");
            System.exit(0);
        }
        else if (ac.equals("SODFSave")) { 
            System.out.println("Save");
            FrameBlinker blinker = new FrameBlinker(theTarget);
            blinker.start();
            try {
                JFileChooser chooser = new JFileChooser(".");
                chooser.setDialogTitle("Save selected frame");
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                int returnVal = chooser.showSaveDialog(theSource);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    System.out.println("You chose to save to this file: " +
                                       chooser.getSelectedFile().getCanonicalPath());
                    blinker.stop();
                    Saver.save(theTarget,chooser.getSelectedFile().getCanonicalPath());
                }
            } catch (Exception ex) {
                System.out.println("Save exception: "+ex);
            }
            blinker.stop();
        }
        else if (ac.equals("SODFOpen")) {
            System.out.println("Open");
            try {
                JFileChooser chooser = new JFileChooser(".");
                chooser.setDialogTitle("Open a saved frame");
                chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                int returnVal = chooser.showOpenDialog(theSource);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    System.out.println("You chose to open this file: " +
                                       chooser.getSelectedFile().getCanonicalPath());
                    JFrame restoredFrame = (JFrame)Saver.openAndRun(chooser.getSelectedFile().getCanonicalPath());
                    restoredFrame.pack();
                    restoredFrame.setVisible(true);
                }
            } catch (Exception ex) {
                System.out.println("Open exception: "+ex);
            }
        }
        else {
            theTarget = (JFrame)((JComponent)e.getSource()).getTopLevelAncestor();
            System.out.println("theTarget = " + theTarget);
        }
    }
}
    





