package store;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.util.*;

public class StoreGui {

  //½çÃæµÄÖ÷Òª´°Ìå×é¼þ
  protected JFrame frame;
  protected Container contentPane;
  protected CardLayout card=new CardLayout();
  protected JPanel cardPan=new JPanel();

  //°üº¬¸÷ÖÖ°´Å¥µÄÑ¡ÔñÃæ°åÉÏµÄ×é¼þ
  protected JPanel selPan=new JPanel();
  protected JButton custBt=new JButton("¿Í»§ÏêÏ¸ÐÅÏ¢");
  protected JButton allCustBt=new JButton("ËùÓÐ¿Í»§Çåµ¥");

  //ÏÔÊ¾µ¥¸ö¿Í»§µÄÃæ°åÉÏµÄ×é¼þ
  protected JPanel custPan=new JPanel();
  protected JLabel nameLb=new JLabel("¿Í»§ÐÕÃû");
  protected JLabel idLb=new JLabel("ID");
  protected JLabel addrLb=new JLabel("µØÖ·");
  protected JLabel ageLb=new JLabel("ÄêÁä");

  protected JTextField nameTf=new JTextField(25);
  protected JTextField idTf=new JTextField(25);
  protected JTextField addrTf=new JTextField(25);
  protected JTextField ageTf=new JTextField(25);
  protected JButton getBt=new JButton("²éÑ¯¿Í»§");
  protected JButton updBt=new JButton("¸üÐÂ¿Í»§");
  protected JButton addBt=new JButton("Ìí¼Ó¿Í»§");
  protected JButton delBt=new JButton("É¾³ý¿Í»§");

  //ÁÐ¾ÙËùÓÐ¿Í»§µÄÃæ°åÉÏµÄ×é¼þ
  protected JPanel allCustPan=new JPanel();
  protected JLabel allCustLb=new JLabel("ËùÓÐ¿Í»§Çåµ¥",SwingConstants.CENTER);
  protected JTextArea allCustTa=new JTextArea();
  protected JScrollPane allCustSp=new JScrollPane(allCustTa);

  String[] tableHeaders={"ID","ÐÕÃû","µØÖ·","ÄêÁä"};
  JTable table;
  JScrollPane tablePane;
  DefaultTableModel tableModel;

  //ÈÕÖ¾Ãæ°åÉÏµÄ×é¼þ
  protected JPanel logPan=new JPanel();
  protected JLabel logLb=new JLabel("²Ù×÷ÈÕÖ¾",SwingConstants.CENTER);

  protected JTextArea logTa=new JTextArea(9,50);
  protected JScrollPane logSp=new JScrollPane(logTa);
  
  /** ÏÔÊ¾²¢Ë¢ÐÂµ¥¸ö¿Í»§Ãæ°å custPan */
  public void refreshCustPane(Customer cust){
    showCard("customer");

    if(cust==null || cust.getId()==-1){
      idTf.setText(null);
      nameTf.setText(null);
      addrTf.setText(null);
      ageTf.setText(null);
      return;
    }
    idTf.setText(new Long(cust.getId()).toString());
    nameTf.setText(cust.getName().trim());
    addrTf.setText(cust.getAddr().trim());
    ageTf.setText(new Integer(cust.getAge()).toString());
  }
  
  /** ÏÔÊ¾²¢Ë¢ÐÂËùÓÐ¿Í»§Ãæ°å allCustPan */
  public void refreshAllCustPan(Set<Customer> custs){
    showCard("allcustomers");
    String newData[][];
    newData=new String[custs.size()][4];
    Iterator<Customer> it=custs.iterator();
    int i=0; 
    while(it.hasNext()){
      Customer cust=it.next();
      newData[i][0]=new Long(cust.getId()).toString();
      newData[i][1]=cust.getName();
      newData[i][2]=cust.getAddr();
      newData[i][3]=new Integer(cust.getAge()).toString();
      i++; 
    }

    tableModel.setDataVector(newData,tableHeaders);
  }
  
  /** ÔÚÈÕÖ¾Ãæ°ålogPanÖÐÌí¼ÓÈÕÖ¾ÐÅÏ¢ */
  public void updateLog(String msg){
    logTa.append(msg+"\n");
  }

  /** »ñµÃ¿Í»§Ãæ°åcustPanÉÏÓÃ»§ÊäÈëµÄID */
  public long getCustIdOnCustPan(){
     try{
       return Long.parseLong(idTf.getText().trim());
     }catch(Exception e){
       updateLog(e.getMessage());
       return -1;
     }
  }
  
  /** »ñµÃµ¥¸ö¿Í»§Ãæ°åcustPanÉÏÓÃ»§ÊäÈëµÄ¿Í»§ÐÅÏ¢ */
  public Customer getCustomerOnCustPan(){
    try{
      return new Customer(Long.parseLong(idTf.getText().trim()),
        nameTf.getText().trim(),addrTf.getText().trim(),
        Integer.parseInt(ageTf.getText().trim()));
    }catch(Exception e){
      updateLog(e.getMessage());
      return null; 
    }
  }
  
  /** ÏÔÊ¾µ¥¸ö¿Í»§Ãæ°åcustPan»òÕßËùÓÐ¿Í»§Ãæ°åallCustPan */
  private void showCard(String cardStr){
    card.show(cardPan,cardStr);
  }
  
  /** ¹¹Ôì·½·¨ */
  public StoreGui(){
    buildDisplay();
  }
  
  /** ´´½¨Í¼ÐÎ½çÃæ */
  private void buildDisplay(){
   frame=new JFrame("ÉÌµêµÄ¿Í»§¹ÜÀíÏµÍ³");
   buildSelectionPanel();
   buildCustPanel();
   buildAllCustPanel();
   buildLogPanel();
   
   /** carPan²ÉÓÃCardLayout²¼¾Ö¹ÜÀíÆ÷£¬°üÀ¨custPanºÍallCustPanÁ½ÕÅ¿¨Æ¬ */
   cardPan.setLayout(card);
   cardPan.add(custPan,"customer");
   cardPan.add(allCustPan,"allcustomers");

   //ÏòÖ÷´°ÌåÖÐ¼ÓÈë¸÷ÖÖÃæ°å
   contentPane=frame.getContentPane();
   contentPane.setLayout(new BorderLayout());
   contentPane.add(cardPan,BorderLayout.CENTER);
   contentPane.add(selPan,BorderLayout.NORTH);
   contentPane.add(logPan,BorderLayout.SOUTH);

   frame.pack();
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   frame.setVisible(true);

  }
 
  /** ´´½¨Ñ¡ÔñÃæ°åselPan */ 
  private void buildSelectionPanel(){
    selPan.setLayout(new GridLayout(1,2));
    selPan.add(custBt);
    selPan.add(allCustBt);
  }

  /** ÎªÑ¡ÔñÃæ°åselPanÖÐµÄ2¸ö°´Å¥×¢²á¼àÌýÆ÷ */
  public void addSelectionPanelListeners(ActionListener a[]){
   int len=a.length;
   if(len!=2){ return;}

   custBt.addActionListener(a[0]);
   allCustBt.addActionListener(a[1]);
 }
  
  /**¡¡´´½¨µ¥¸ö¿Í»§custPanÃæ°å */
  private void buildCustPanel(){
   custPan.setLayout(new GridLayout(6,2));
   custPan.add(idLb);
   custPan.add(idTf);
   custPan.add(nameLb);
   custPan.add(nameTf);
   custPan.add(addrLb);
   custPan.add(addrTf);
   custPan.add(ageLb);
   custPan.add(ageTf);

   custPan.add(getBt);
   custPan.add(updBt);
   custPan.add(addBt);
   custPan.add(delBt);

  }
  
  /** Îªµ¥¸ö¿Í»§Ãæ°åcustPanÖÐµÄ4¸ö°´Å¥×¢²á¼àÌýÆ÷ */
  public void addCustPanelListeners(ActionListener a[]){
    int len=a.length;
    if(len!=4){ return;}

   getBt.addActionListener(a[0]);
   addBt.addActionListener(a[1]);
   delBt.addActionListener(a[2]);
   updBt.addActionListener(a[3]);
  }
  
  /** ´´½¨ËùÓÐ¿Í»§allCustPanÃæ°å */
  private void buildAllCustPanel(){
    allCustPan.setLayout(new BorderLayout());
    allCustPan.add(allCustLb,BorderLayout.NORTH);
    allCustTa.setText("all customer display");

    tableModel=new DefaultTableModel(tableHeaders,10);
    table=new JTable(tableModel);
    tablePane=new JScrollPane(table);

    allCustPan.add(tablePane,BorderLayout.CENTER);

    Dimension dim=new Dimension(500,150);
    table.setPreferredScrollableViewportSize(dim);
  }
  
  /** ´´½¨ÈÕÖ¾Ãæ°å*/
  private void buildLogPanel(){
   logPan.setLayout(new BorderLayout());
   logPan.add(logLb,BorderLayout.NORTH);
   logPan.add(logSp,BorderLayout.CENTER);
  }
}


/****************************************************
 * ×÷Õß£ºËïÎÀÇÙ                                     *
 * À´Ô´£º<<JavaÍøÂç±à³Ì¾«½â>>                       *
 * ¼¼ÊõÖ§³ÖÍøÖ·£ºwww.javathinker.org                *
 ***************************************************/
