
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
import java.awt.event.*;
import javax.swing.event.*;

public class Controller extends JFrame {
    ViewGraph viewGraph = null;
    ViewText viewText = null;
    Model model = null;
    
    JScrollPane jScrollPane1 = new JScrollPane();
    JTextField jTextField1 = new JTextField();
    JTextField jTextField2 = new JTextField();
    JLabel jLabel1 = new JLabel();
    JLabel jLabel2 = new JLabel();
    JLabel jLabel3 = new JLabel();

    public Controller() {
        try {
            init();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void init() throws Exception {

        Data[] data = new Data[2];
        data[0] = new Data("张三", 20);
        data[1] = new Data("李四", 14);

        model = new Model(data);
        viewText = new ViewText(model);
        viewGraph = new ViewGraph(model);

       // 注意下面两行：向模型中登记它的观察者View1和View2.
        model.registerObserver(viewText);
        model.registerObserver(viewGraph);
        this.getContentPane().setLayout(null);
        jScrollPane1.setBounds(new Rectangle(0, 0, 3, 3));

        jTextField1.setText(String.valueOf(data[0].value));
        jTextField1.addCaretListener(new CaretListener() {

            public void caretUpdate(CaretEvent e) {
                // 如果改变了内容，就可以即时更新 label 显示的内容
                textChangeEvent(e);
            }
        });

        jTextField1.setBounds(new Rectangle(80,254,52,30));
        jTextField2.setText(String.valueOf(data[1].value));
        jTextField2.addCaretListener(new CaretListener() {

            public void caretUpdate(CaretEvent e) {
                // 如果改变了内容，就可以即时更新 label 显示的内容
                textChangeEvent(e);
            }
        });
        jTextField2.setBounds(new Rectangle(178,255,50,31));
        jLabel1.setText("提示信息:");
        jLabel1.setBounds(new Rectangle(41,226,200,23));
        jLabel2.setText(data[0].name);
        jLabel2.setBounds(new Rectangle(42,252,30,33));
        jLabel3.setText(data[1].name);
        jLabel3.setBounds(new Rectangle(140,252,90,33));

        viewText.setBounds(new Rectangle(7, 5, 225, 208));
        viewGraph.setBounds(new Rectangle(234, 5, 219, 208));

        this.getContentPane().add(jScrollPane1, null);
        this.getContentPane().add(jTextField2, null);
        this.getContentPane().add(jTextField1, null);
        this.getContentPane().add(jLabel2, null);
        this.getContentPane().add(jLabel3, null);
        this.getContentPane().add(jLabel1, null);
        this.getContentPane().add(viewText, null);
        this.getContentPane().add(viewGraph, null);

    }
   
    void textChangeEvent(CaretEvent e) {
        try {

            Data[] data = new Data[2];
            data[0] = new Data();
            data[1] = new Data();
            data[0].name = jLabel2.getText();
            int value1 = Integer.parseInt(jTextField1.getText());
            data[0].value=Integer.parseInt(jTextField1.getText());
            data[1].name = jLabel3.getText();
            data[1].value = Integer.parseInt(jTextField2.getText());
            int value2 = Integer.parseInt(jTextField2.getText());
            if ((value1 > 0 && value1 < 100) && 
					(value2 > 0 && value2 < 100)) {
                Model m = new Model(data);
                this.model.changeModel(m);
            } else {
                jLabel1.setText("提示信息:年龄在1到99之间!");
            }


        } catch (Exception ex) {
            //ex.printStackTrace();
            jLabel1.setText("提示信息:年龄在1到99之间!");
        }

    }
}
