package howto;

import java.awt.*;
import howto.*;
import java.awt.event.*;
import java.io.*;
import java.util.Vector;

public class MainEx extends howto.Main{
    Shape selShape;
    static List list;
    Panel p;
	public void init()
   {
      setBackground(Color.white);

      setLayout(new BorderLayout());

      list=new List(1);
	  list.add("red");
	  list.add("green");
      list.add("blue");

      _sm = new ShapeMgr();

      add("Center", _sm);

      p = new Panel();

      p.add(new Button("Save"));
      p.add(new Button("Load"));
      p.add(list);
      p.add(new Button("Line"));
      p.add(new Button("Rectangle"));
      p.add(new Button("Oval"));

      add("South", p);
      list.addItemListener(new ItemListener(){
		  public void itemStateChanged(ItemEvent e) {
		        if(e.getStateChange()==e.SELECTED) {
		          if (selShape!=null) {
		           selShape.setColor(Color.getColor(list.getSelectedItem()));
		           _sm.repaint();
		          }
	             }}
      }
    );
   }

   public void save(File f){
	   try{ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(f));
	       out.writeObject(_sm);
	       out.close();
	       out.flush();}
	   catch(IOException e){System.out.println("Error saving file");}
	   }

   public void load(File f){
	   ObjectInputStream in = null;
	     try {
	      in = new ObjectInputStream(new FileInputStream(f));
	      ShapeMgr insm = (ShapeMgr)in.readObject();
	      in.close();
	      _sm._hs = insm._hs;
	      _sm._hsm = insm._hsm;
	      _sm._vec = insm._vec;
	      for (int i=0; i<insm._vec.size(); i++) {
	          Shape sh = (Shape)insm._vec.elementAt(i);
	          Vector vec = sh.getHotSpots();
	          for(int j=0; j<vec.size(); j++) {
	             ((HotSpot)vec.elementAt(j)).addObserver(sh);
	          }
	      }
	      _sm.repaint();
	      }
	      catch (IOException ioe) {
	       System.out.println("Error reading file");
	      }
	      catch (ClassNotFoundException cnfe) {
	       System.out.println("Class not found");
       }
	   }

   public boolean action(Event e, Object o)
      {
		 if(o.equals("Save")){
			 FileDialog d = new FileDialog(new Frame("Save"),"Save",FileDialog.SAVE);
             d.show();
             String filename = new String();
             if(d.getFile()!=null){File file = new File(d.getDirectory()+d.getFile());
             try{
             file.createNewFile();
                }
             catch(IOException ioe2){System.out.println("Error creating file");}
             save(file);}
		 }else
		 if(o.equals("Load")) {
             FileDialog d = new FileDialog(new Frame("Load"),"Load",FileDialog.LOAD);
             d.show();
             if(d.getFile()!=null){
             File file = new File(d.getDirectory()+d.getFile());
             if(file.exists()){load(file);}
             }
	     }
         else
         if (o.equals("Line"))
         {
            selShape=new LineShapeEx(10,10,110,110,Color.getColor(list.getSelectedItem()));
            _sm.manage(selShape);
         }
         else if (o.equals("Rectangle"))
         {
            _sm.manage(new RectangleShape(10, 10, 100, 100));
         }
         else if (o.equals("Oval"))
         {
            _sm.manage(new OvalShape(10, 10, 100, 100));
         }

         else
         {
            return false;
         }
         return true;
   }
   public static void main(String [] args)
      {
         Frame f = new Frame("Hot Spot");

         MainEx m = new MainEx();

         m.init();
         FileDialog d=new FileDialog(f);

         f.add("Center", m);

         f.pack();
         f.show();
         Frame info=new Frame(list.getSelectedItem());
         info.show();
   }
}

