import java.awt.*;
import java.awt.event.*;
import java.io.*; 
import java.util.*;
import javax.xml.bind.*;
import org.businesscard.*;

public class BCedit2 extends Frame implements ActionListener {
  // declare the user interface
  Button ok = new Button("ok");
  Button delete = new Button("delete");
  Button clear = new Button("clear");
  Button save = new Button("save");
  Button quit = new Button("quit");
  TextField name = new TextField(20);
  TextField title = new TextField(20);
  TextField email = new TextField(20);
  TextField phone = new TextField(20);
  TextField logo = new TextField(20);
  Panel cardpanel = new Panel(new GridLayout(0,1));

  String cardfile;
  java.util.List cardlist;
  CardlistType cl;
  int current = -1;
  JAXBContext jc;
  ObjectFactory objFactory = new ObjectFactory();

  public static void main(String[] args) { new BCedit2(args[0]); }

  void addCards() {
    cardpanel.removeAll();
    Iterator i = cardlist.iterator();
    int j = 0;
    while (i.hasNext()) {
      CardType c = (CardType)i.next();
      Button b = new Button(c.getName());
      b.setActionCommand(String.valueOf(j++));
      b.addActionListener(this);
      cardpanel.add(b);
    }
    pack();
  }

  public BCedit2(String cardfile) {
    super("BCedit2");
    this.cardfile = cardfile;
    try {
      jc = JAXBContext.newInstance("org.businesscard");
      Unmarshaller u = jc.createUnmarshaller();  
      cl = (CardlistType)((JAXBElement)u.unmarshal(new FileInputStream(cardfile))).getValue();
      cardlist = cl.getCard();
    } catch (Exception e) { 
      e.printStackTrace(); 
      System.exit(-1);
    }
    // initialize the user interface
    setLayout(new BorderLayout());
    ScrollPane s = new ScrollPane();
    s.setSize(200, 0);
    s.add(cardpanel);
    add(s,BorderLayout.WEST);
    Panel l = new Panel(new GridLayout(5, 1));
    l.add(new Label("Name"));                  
    l.add(new Label("Title"));                  
    l.add(new Label("Email"));                  
    l.add(new Label("Phone"));                  
    l.add(new Label("Logo"));                  
    add(l, BorderLayout.CENTER);
    Panel f = new Panel(new GridLayout(5, 1));
    f.add(name);    
    f.add(title);    
    f.add(email);    
    f.add(phone);    
    f.add(logo);    
    add(f, BorderLayout.EAST);
    Panel p = new Panel();
    ok.addActionListener(this);
    p.add(ok);
    delete.addActionListener(this);
    p.add(delete);
    clear.addActionListener(this);
    p.add(clear);
    save.addActionListener(this);
    p.add(save);
    quit.addActionListener(this);
    p.add(quit);
    add(p,BorderLayout.SOUTH);
    addCards();
    setVisible(true);
  }

  public void actionPerformed(ActionEvent event) {
     CardType c = null;
     String command = event.getActionCommand();
     if (command.equals("ok")) {
       try {
         c = objFactory.createCardType();
         c.setName(name.getText());
         c.setTitle(title.getText());
         c.setEmail(email.getText());
         c.setPhone(phone.getText());
         LogoType l = objFactory.createLogoType();
         l.setUri(logo.getText());
         c.setLogo(l);
       } catch (Exception e) { 
         System.err.println(e); 
       }
       if (current==-1) 
	 cardlist.add(objFactory.createCard(c));
       else
         cardlist.set(current, c);
       addCards();
     } else if (command.equals("delete")) {
        if (current!=-1) {
          cardlist.remove(current);
          current = -1;
          addCards();
        }
     } else if (command.equals("clear")) {
        current = -1;
        name.setText("");
        title.setText("");
        email.setText("");
        phone.setText("");
        logo.setText("");
     } else if (command.equals("save")) {
        try {
          Marshaller m = jc.createMarshaller();
          m.marshal(objFactory.createCardlist(cl),new FileOutputStream(cardfile));
        } catch (Exception e) { 
          System.err.println(e); 
        }
     } else if (command.equals("quit")) {
        System.exit(0);
     } else {
        current = Integer.parseInt(command);
        c = (CardType)cardlist.get(current);
        name.setText(c.getName());
        title.setText(c.getTitle());
        email.setText(c.getEmail());
        phone.setText(c.getPhone());
        LogoType lt = c.getLogo();
        if (lt!=null) 
          logo.setText(lt.getUri());
        else 
          logo.setText("");
     }
  }
}
