import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class SaveRestoreDemo extends JFrame implements ActionListener, Serializable{

    private final String[] ICON_NAME = {"smug", "playful", "cheshire", "wedgie", "animarrow"};
    private final String[] TYPE_TEXT = {"default", "rollover", "pressed", "selected", "disabled"};
    private final String SAVE_FILE = "SaveRestoreDemo.save";
    private final int N_ICONS = 5;  // Number of icons.
    private final int N_BUTTONS = 4;  // Number of buttons.
    private ImageIcon[] icons = new ImageIcon[N_ICONS];
    private String[] butText = new String[N_BUTTONS];
    private JButton[] buttons = new JButton[N_BUTTONS];
    private JComboBox iconSelector, buttonSelector, typeSelector;
    private JPanel butPanel, ctrlPanel;
    
    private void makemenus() {
        JMenuBar mb = new JMenuBar();
        this.setJMenuBar(mb);
        JMenu m = new JMenu("File");
        mb.add(m);
        JMenuItem mi = new JMenuItem("Save");
        mi.addActionListener(this);
        m.add(mi);
        mi = new JMenuItem("Restore");
        mi.addActionListener(this);
        m.add(mi);
        mi = new JMenuItem("Quit");
        mi.addActionListener(this);
        m.add(mi);
    }

    public SaveRestoreDemo() {
        Container cp = getContentPane();
        butPanel = new JPanel();
        ctrlPanel = new JPanel();
        cp.setLayout(new GridLayout(2,0));
        cp.add(butPanel);
        cp.add(ctrlPanel);
        
        butPanel.setLayout(new BoxLayout(butPanel,BoxLayout.X_AXIS));
        ctrlPanel.setLayout(new BoxLayout(ctrlPanel,BoxLayout.X_AXIS));
        for (int i=0; i<N_BUTTONS; i++) {
            butText[i] = ""+(i+1);
            buttons[i] = (new JButton(butText[i]));
        }
        for (int i=0; i<N_ICONS; i++) {
            icons[i] = (new ImageIcon("images/"+ ICON_NAME[i]+".gif"));
        }
        buttonSelector = new JComboBox((Object[])butText);
        buttonSelector.addActionListener(this);
        typeSelector = new JComboBox((Object[])TYPE_TEXT);
        typeSelector.addActionListener(this);
        iconSelector = new JComboBox((Object[])icons);
        iconSelector.addActionListener(this);
        ctrlPanel.add(buttonSelector);
        ctrlPanel.add(typeSelector);
        ctrlPanel.add(iconSelector);
        for (int i=0; i<N_BUTTONS; i++) {
            butPanel.add(buttons[i]);
        }
        makemenus();
        // Standard JFrame code:
        addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }});
        pack();
        setVisible(true);
    }
    
    static public void main(String[] args) {
        SaveRestoreDemo f = new SaveRestoreDemo();
    }
    
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == iconSelector) {
            ImageIcon ic = (ImageIcon)iconSelector.getSelectedItem();
            JButton but = buttons[buttonSelector.getSelectedIndex()];
            if (((String)typeSelector.getSelectedItem()).equals("default")) 
                but.setIcon(ic);	
            else if (((String)typeSelector.getSelectedItem()).equals("rollover"))
                but.setRolloverIcon(ic);
            else if (((String)typeSelector.getSelectedItem()).equals("pressed"))
                but.setPressedIcon(ic);
            else if (((String)typeSelector.getSelectedItem()).equals("selected"))
                but.setSelectedIcon(ic);
            else if (((String)typeSelector.getSelectedItem()).equals("disabled"))
                but.setDisabledIcon(ic);
            pack();
        } 
        if (e.getActionCommand().equals("Quit"))
            System.exit(0);
        else if (e.getActionCommand().equals("Save")) {
            try {
                FileOutputStream out = new FileOutputStream(SAVE_FILE);
                ObjectOutputStream s = new ObjectOutputStream(out);
                s.writeObject(butPanel);
                s.flush();
            } catch (Exception ex) {System.out.println("Save exception." + ex);}
        } else if (e.getActionCommand().equals("Restore")) {
            try {
                FileInputStream in = new FileInputStream(SAVE_FILE);
                ObjectInputStream s = new ObjectInputStream(in);
                butPanel = (JPanel)s.readObject();
                Container cp = getContentPane();
                cp.remove(0); // Pulls old butPanel out.
                cp.add(butPanel,0);  // This places the restored butPanel,
                      // complete with restored buttons, in the window.
                // The next loop makes the internal representation agree
                // with the display.  The array buttons[] is what is
                // updated by the ComboBoxes.  We must ensure that the
                // buttons in this array are the restored ones (which
                // we have already displayed with the last command).
                for (int i=0; i< N_BUTTONS; i++) {
                    buttons[i] = (JButton)(butPanel.getComponent(i));
                    // System.out.println("Restored button " + i);
                }
                pack();
                butPanel.repaint();
            } catch (Exception ex) {System.out.println("Restore exception."+ex);}
        }
        
    }

}




