package frameeditor;
import saveopen.presentation.*;
import saveopen.translation.*;
import util.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.File;

/*
 * Sample code to illustrate the overall framework for the
 * FrameEditor homework assignment.
 */

public class FrameEditor extends ActionFrame {
    
    private AbstractButton theComponent;  // most recently selected editable component.
    private JFrame theComponentFrame;
    private JMenuItem quitMenuItem;
    private static final String IMAGE_DIR = "images" + File.separator + "starters" + File.separator;

    /*
     * newMI creates a new MenuItem with given Text string,
     * adds it to the given Menu, registers the FrameEditor
     * as ActionListener for the MenuItem, and returns the new
     * MenuItem (in case further customization is desired).
     */
    
    protected JMenuItem newMI(JMenu m, String newMenuItemText) {
        JMenuItem mi = new JMenuItem(newMenuItemText);
        mi.addActionListener(this);
        m.add(mi);
        return mi;
    }
    
    /*
     * A slight enhancement to the previous newMI.
     */

    protected JMenuItem newMI(JMenu m, String newMenuItemText, String iconFileName) {
        JMenuItem mi = newMI(m,newMenuItemText);
        mi.setIcon(new ImageIcon(iconFileName));
        return mi;
    }

    /*
     * Creates and configures the MenuBar.
     * Called by the Frame constructor.
     * Note the use of this method and the newMI method
     * to reduce clutter in the source code.
     */

    protected void menuSetUp() {
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        
        JMenu menu = new JMenu("Configure");
        menuBar.add(menu);

        JMenu iconMenu = new JMenu("Icon");
        menu.add(iconMenu);
       
        newMI(iconMenu,"icon1.gif",IMAGE_DIR + "icon1.gif");
        newMI(iconMenu,"icon2.gif",IMAGE_DIR + "icon2.gif");
        newMI(iconMenu,"icon3.gif",IMAGE_DIR + "icon3.gif");
        newMI(iconMenu,"icon4.gif",IMAGE_DIR + "icon4.gif");

        newMI(menu,"Text").setAccelerator(KeyStroke.getKeyStroke(                                                 KeyEvent.VK_T, ActionEvent.ALT_MASK));

        JMenu bgMenu = new JMenu("Background");
        menu.add(bgMenu);
        
        newMI(bgMenu,"Color.blue").setBackground(Color.blue);
        newMI(bgMenu,"Color.blue").setBackground(Color.white);
        
        quitMenuItem = newMI(menu,"Quit");
        quitMenuItem.setAccelerator(KeyStroke.getKeyStroke(
                                     KeyEvent.VK_Q, ActionEvent.ALT_MASK));
    }

    public FrameEditor(ActionFrame targetFrame) {
        menuSetUp();
        targetFrame.addActionListener(this);
        theComponentFrame = targetFrame;
        setTitle("Editor Window");
        getContentPane().add(new JLabel("Add your edit tools here"));
        
        SaveOpenDemoFrame saveFrame = new SaveOpenDemoFrame();
        saveFrame.setTitle("Save/Open Window");
        SaveOpenDemoMenuHandler saveHandler = new SaveOpenDemoMenuHandler(saveFrame);
        saveFrame.setSize(300,200);
        saveFrame.setVisible(true);
        targetFrame.addActionListener(saveHandler);
    }
    
    /*
     * Handle ActionEvents generated by the MenuItems established
     * by menuSetUp(), and also any events generated by targetFrame.
     * All such events are assumed to be generated by AbstractButtons,
     * and are handled by making the event-generating component the
     * next to be configured.
     */

    public void addActionListener(ActionListener listener) {
        quitMenuItem.addActionListener(listener);
    }
    
    public void actionPerformed(ActionEvent e) {
        String s = e.getActionCommand();
        if (s != null) {
            if (s.equals("Text")) {
                String newText = JOptionPane.showInputDialog(this,"Enter new text for selected component.");
                if (newText != null) {
                    theComponent.setText(newText);
                    theComponentFrame.pack(); // make display reflect possible size change.
                }
            }
            else if (s.equals("Icon")) {
            }
            else if (s.equals("Component")) {}
            else if (s.equals("Quit")){}  // Ignore Quit events.
            else if (s.startsWith("icon")) {
                theComponent.setIcon(((JMenuItem)e.getSource()).getIcon());
                theComponentFrame.pack(); // make display reflect possible size change.
            }
            else if (s.startsWith("Color")) {
                theComponent.setBackground(((JMenuItem)e.getSource()).getBackground());
            }
            else // Not one of our menu items.  Must be from targetPanel.
                {
                    theComponent = (AbstractButton)e.getSource();
                }
        }
        else  // No ActionCommand.  Must be from targetPanel.
            {
                theComponent = (AbstractButton)e.getSource();
            }
    }
}


