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

public class ViewText extends JPanel implements Observer {
	Model model;

	public ViewText(Model model) {
		try {   
			this.model = model;
			init();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void init() throws Exception {
		this.setBackground(Color.white);
		this.setBorder(
				new TitledBorder(
					BorderFactory.createLineBorder(Color.black, 1), 
					"视图1"));
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (model == null){
			return;
		}
		int x = 20, y = 50;
		int h = g.getFontMetrics().getHeight();
		for (int i = 0; i < model.size(); i++) {
			Data data = model.getData(i);
			g.drawString(data.name, x, y);
			y += h;
			g.drawString(String.valueOf(data.value), x, y);
			y += h;
		}
	}

	// 当模型数据发生改变时，会自动调用此方法来刷新图形 
	public void dataUpdate(Model model) {	
		this.model = model;
		repaint();
	}
}
