package com.mr.email;

import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.LineBorder;

/**
 * 模拟发送邮件
 */
public class EmailFrame extends JFrame { // 创建“邮件”类继承JFrame
	// 声明成员组件
	private JTextField addresseeTextField; // 收件人文本框
	private JTextField titleTextField; // 邮件主题文本框
	private JTextArea textArea; // 邮件正文文本域
	private JLabel senderLabel; // 发件人邮箱标签
	private JList<String> emailList; // 列表框
	private JRadioButton normalRadio; // "普通发送"的单选框
	private JComboBox<String> fontComboBox; // 下拉菜单
	private JRadioButton encryptRadio;// 加密发送单选框
	private JButton sendBtn;// 发送按钮
	private JButton saveDraftBtn;// 保存草稿按钮
	private JButton switchUserBtn;// 切换用户按钮
	private JCheckBox ordinaryEmailCheck;// 普通邮件多选框
	private JCheckBox urgentEmailCheck;// 紧急邮件多选框
	private JCheckBox receiptCheck;// 阅读回执多选框

	public static void main(String[] args) {
		EmailFrame email = new EmailFrame(); // 创建名为“email”的窗体
		email.setVisible(true); // 让这个窗体可视
	}

	/**
	 * 更改发件人邮箱
	 * 
	 * @param newEmail
	 *            新发件人邮箱
	 */
	public void setSenderEmail(String newEmail) {
		senderLabel.setText(newEmail);
	}

	/**
	 * 点击发送按钮后，收集用户输入的信息并发送
	 */
	private void sendEmail() {
		StringBuilder sb = new StringBuilder();// 用于保存用户输入的文本信息报表
		sb.append("收件人：").append(addresseeTextField.getText()).append("\n");
		sb.append("邮件主题：").append(titleTextField.getText()).append("\n");

		/* 判断用户输入的邮件类型 */
		sb.append("邮件类型：");
		if (ordinaryEmailCheck.isSelected()) {// 如果"普通邮件"多选框被选中
			sb.append(ordinaryEmailCheck.getText() + " ");// 将选中的多选框内容添加到报表中
		}
		if (urgentEmailCheck.isSelected()) {
			sb.append(urgentEmailCheck.getText() + " ");
		}
		if (receiptCheck.isSelected()) {
			sb.append(receiptCheck.getText());
		}
		sb.append("\n");
		/* end:判断用户输入的邮件类型 */

		/* 判断用户输入的发送方式 */
		sb.append("发送方式：");
		if (normalRadio.isSelected()) {
			sb.append(normalRadio.getText() + " ");
		}
		if (encryptRadio.isSelected()) {
			sb.append(encryptRadio.getText());
		}
		sb.append("\n");
		/* 判断用户输入的发送方式 */

		sb.append("邮件正文：").append(textArea.getText());

		System.out.println(sb.toString());// 在控制台打印
	}

	/*
	 * 创建窗体
	 */
	public EmailFrame() { // “邮件”类的构造方法
		setTitle("邮箱客户端——发送邮件"); // 设置对话框的标题
		setResizable(false); // 设置窗体不可改变大小
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 设置窗体的关闭方式
		setBounds(100, 100, 950, 615); // 设置窗体横坐标、纵坐标、宽高

		Container contentPane = getContentPane();// 获取窗口容器
		contentPane.setLayout(null);// 设为恐怖剧

		/*
		 * 创建头（导航栏）面板，展示发送按钮、保存草稿按钮、切换用户按钮
		 */
		JPanel headPanel = new JPanel();
		// 面板添加边框，边框颜色为：添加活动窗口的窗口标题提供的背景色，边框宽度：1像素
		headPanel.setBorder(BorderFactory.createLineBorder(SystemColor.activeCaption, 1));
		headPanel.setBackground(SystemColor.inactiveCaption);// 面板添加背景色，颜色为：非活动窗口的窗口标题提供的背景色
		headPanel.setBounds(0, 0, 944, 68);
		headPanel.setLayout(null);// 绝对布局
		contentPane.add(headPanel);

		/*
		 * 发送按钮
		 */
		sendBtn = new JButton("");// 发送按钮
		sendBtn.setIcon(new ImageIcon("src/com/mr/email/send.png"));// 按钮添加图片
		sendBtn.setBounds(10, 6, 56, 56);
		sendBtn.addActionListener(new ActionListener() {// 发送按钮监听事件
			@Override
			public void actionPerformed(ActionEvent e) {
				sendEmail();
			}
		});
		headPanel.add(sendBtn);
		/*
		 * 保存草稿按钮
		 */
		saveDraftBtn = new JButton();
		saveDraftBtn.setIcon(new ImageIcon("src/com/mr/email/save.png"));// 按钮添加图片
		saveDraftBtn.setBounds(76, 6, 56, 56);
		headPanel.add(saveDraftBtn);
		/*
		 * 切换用户按钮
		 */
		switchUserBtn = new JButton();
		switchUserBtn.setIcon(new ImageIcon("src/com/mr/email/switch.png"));
		switchUserBtn.setBounds(142, 6, 56, 56);
		switchUserBtn.addActionListener(new ActionListener() {// 切换用户按钮事件
			public void actionPerformed(ActionEvent e) {
				new LoginJDialog(EmailFrame.this).setVisible(true); // 是的对话框可视
			}
		});
		headPanel.add(switchUserBtn);

		/*
		 * 右侧联系人面板
		 */
		JPanel contactsPanel = new JPanel();
		contactsPanel.setBackground(SystemColor.inactiveCaptionBorder);
		contactsPanel.setBorder(BorderFactory.createLineBorder(SystemColor.activeCaption));
		contactsPanel.setBounds(750, 67, 194, 519);
		contentPane.add(contactsPanel);
		contactsPanel.setLayout(null);
		/*
		 * 联系人标题面板
		 */
		JPanel titlePanel = new JPanel();
		titlePanel.setBorder(BorderFactory.createLineBorder(SystemColor.activeCaption));
		titlePanel.setBackground(SystemColor.inactiveCaption);
		titlePanel.setBounds(0, 0, 194, 28);
		contactsPanel.add(titlePanel);
		titlePanel.setLayout(null);
		/*
		 * 联系人标签
		 */
		JLabel contactsLabel = new JLabel("　联系人");
		contactsLabel.setForeground(new Color(0, 102, 153));
		contactsLabel.setFont(new Font("微软雅黑", Font.BOLD, 13));
		contactsLabel.setBounds(0, 0, 194, 28);
		titlePanel.add(contactsLabel);
		/*
		 * 联系人列表滚动面板
		 */
		JScrollPane emailLstScroll = new JScrollPane();
		emailLstScroll.setBounds(10, 36, 174, 186);
		contactsPanel.add(emailLstScroll);

		String[] values = { "4006751066@qq.com", "zhangsan@163.com", "mingrisoft@mingrisoft.com", "53498653@qq.com",
				"mingrixueyuan@126.com", "admin@qq.com", "mrsoft@qq.com", "xiaoqiang@163.com" }; // 声明一个字符串数组
		emailList = new JList<String>(values); // 把字符串数组放到列表框中
		emailList.setFont(new Font("Times New Roman", Font.PLAIN, 14)); // 设置列表框中字体内容的样式:普通样式
		emailLstScroll.setViewportView(emailList); // 把列表框放到滚动面板中
		/*
		 * 收件人确认按钮，将用户确认联系人的地址添加到“收件人”中
		 */
		JButton submitAddressBtn = new JButton("确　认");
		submitAddressBtn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				StringBuilder builder = new StringBuilder(); // 字符串存储容器
				List<String> list = emailList.getSelectedValuesList();// 获取被选中的值
				for (String tmp : list) {// 遍历被选中的值
					builder.append(tmp).append(";"); // 两次添加字符串
				}
				addresseeTextField.setText(builder.toString()); // 把被选中的选项添加到收件人中
			}
		});
		submitAddressBtn.setFont(new Font("宋体", Font.PLAIN, 12));
		submitAddressBtn.setBounds(114, 230, 70, 23);
		contactsPanel.add(submitAddressBtn);
		/*
		 * 程序主功能面板，展示收件人文本框、主题文本框、右键正文文本域等组件
		 */
		JPanel mainPanel = new JPanel();
		mainPanel.setBorder(new LineBorder(SystemColor.inactiveCaption));
		mainPanel.setBounds(0, 67, 751, 519);
		mainPanel.setBackground(SystemColor.inactiveCaptionBorder);
		contentPane.add(mainPanel);
		mainPanel.setLayout(null);
		/*
		 * 发件人标签
		 */
		JLabel sendLabel = new JLabel();
		sendLabel.setIcon(new ImageIcon("src/com/mr/email/from.png"));// 给添加标签图片
		sendLabel.setBounds(30, 15, 65, 22);
		mainPanel.add(sendLabel);
		/*
		 * 收件人标签
		 */
		JLabel receiveLabel = new JLabel();
		receiveLabel.setIcon(new ImageIcon("src/com/mr/email/to.png"));// 给添加标签图片
		receiveLabel.setBounds(30, 42, 65, 22);
		mainPanel.add(receiveLabel);
		/*
		 * 发件人邮箱标签，此标签展示当前发件人的邮箱地址
		 */
		senderLabel = new JLabel("976853142@XXX.com");
		senderLabel.setFont(new Font("Times New Roman", Font.PLAIN, 12));
		senderLabel.setBounds(105, 15, 616, 22);
		mainPanel.add(senderLabel);
		/*
		 * 收件人地址文本框，所有收件人使用“;”隔开
		 */
		addresseeTextField = new JTextField();
		addresseeTextField.setBounds(105, 42, 616, 22);
		mainPanel.add(addresseeTextField);
		addresseeTextField.setColumns(10);
		/*
		 * 邮件主题标签
		 */
		JLabel titleLabel = new JLabel("主　题:");
		titleLabel.setFont(new Font("宋体", Font.PLAIN, 13));
		titleLabel.setHorizontalAlignment(SwingConstants.RIGHT);// 文本右对齐
		titleLabel.setBounds(30, 69, 65, 22);
		mainPanel.add(titleLabel);
		/*
		 * 邮件主题文本输入框
		 */
		titleTextField = new JTextField();
		titleTextField.setBounds(105, 69, 616, 22);
		mainPanel.add(titleTextField);
		titleTextField.setColumns(10);
		/*
		 * 普通邮件复选框
		 */
		ordinaryEmailCheck = new JCheckBox("普通邮件");
		ordinaryEmailCheck.setFont(new Font("宋体", Font.PLAIN, 12));
		ordinaryEmailCheck.setBackground(SystemColor.inactiveCaptionBorder);
		ordinaryEmailCheck.setBounds(478, 100, 75, 22);
		mainPanel.add(ordinaryEmailCheck);
		/*
		 * 紧急邮件复选框
		 */
		urgentEmailCheck = new JCheckBox("紧急邮件");
		urgentEmailCheck.setFont(new Font("宋体", Font.PLAIN, 12));
		urgentEmailCheck.setBackground(SystemColor.inactiveCaptionBorder);
		urgentEmailCheck.setBounds(562, 100, 75, 22);
		mainPanel.add(urgentEmailCheck);
		/*
		 * 阅读回执复选框
		 */
		receiptCheck = new JCheckBox("阅读回执");
		receiptCheck.setFont(new Font("宋体", Font.PLAIN, 12));
		receiptCheck.setBackground(SystemColor.inactiveCaptionBorder);
		receiptCheck.setBounds(646, 100, 75, 22);
		mainPanel.add(receiptCheck);
		/*
		 * 普通发送单选按钮
		 */
		normalRadio = new JRadioButton("普通发送");
		normalRadio.setSelected(true);
		normalRadio.setFont(new Font("宋体", Font.PLAIN, 12));
		normalRadio.setBackground(SystemColor.inactiveCaptionBorder);
		normalRadio.setBounds(562, 126, 75, 22);
		mainPanel.add(normalRadio);
		/*
		 * 加密发送单选按钮
		 */
		encryptRadio = new JRadioButton("加密发送");
		encryptRadio.setFont(new Font("宋体", Font.PLAIN, 12));
		encryptRadio.setBackground(SystemColor.inactiveCaptionBorder);
		encryptRadio.setBounds(646, 126, 75, 22);
		mainPanel.add(encryptRadio);
		/*
		 * 发送模式按钮组，按钮组成员：普通发送单选按钮，加密发送单选按钮
		 */
		ButtonGroup group = new ButtonGroup();
		group.add(normalRadio);
		group.add(encryptRadio);
		/*
		 * 邮件正文文本域滚动面板
		 */
		JScrollPane textAreaScroll = new JScrollPane();
		textAreaScroll.setBounds(105, 157, 616, 352);
		mainPanel.add(textAreaScroll);
		/*
		 * 邮件正文文本域
		 */
		textArea = new JTextArea();
		textAreaScroll.setViewportView(textArea);
		/*
		 * 内容标签
		 */
		JLabel contentLabel = new JLabel("内　容：");
		contentLabel.setFont(new Font("宋体", Font.PLAIN, 13));
		contentLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		contentLabel.setBounds(30, 324, 65, 22);
		mainPanel.add(contentLabel);
		/*
		 * 字体选择下拉菜单，用于修改邮件正文格式
		 */
		fontComboBox = new JComboBox<String>(new String[] { "宋体", "黑体", "隶书" });
		fontComboBox.setBackground(Color.WHITE);
		fontComboBox.setFont(new Font("宋体", Font.PLAIN, 12));
		fontComboBox.setBounds(104, 113, 75, 23);
		mainPanel.add(fontComboBox);
		/*
		 * 应用按钮，用于确认字体
		 */
		JButton btnApply = new JButton("应用");
		btnApply.setBounds(189, 113, 70, 23);
		btnApply.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String selected = (String) fontComboBox.getSelectedItem();// 获取下拉列表选中的文本内容
				switch (selected) {// 判断文本内容
				case "宋体":// 如果选中“宋体”
					textArea.setFont(new Font("宋体", Font.PLAIN, 16));// 将邮件正文格式改为宋体、标准字体，16号字
					break;
				case "黑体":
					textArea.setFont(new Font("黑体", Font.PLAIN, 16));
					break;
				case "隶书":
					textArea.setFont(new Font("隶书", Font.PLAIN, 16));
					break;
				default:
					textArea.setFont(new Font("宋体", Font.PLAIN, 16));// 邮件正文格式默认为为宋体、标准字体，16号字
				}
			}
		});
		mainPanel.add(btnApply);
	}
}
