package frame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.File;
import java.util.ArrayList;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.DefaultListModel;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JToolBar;
import javax.swing.ListSelectionModel;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import mailutil.SendAttachMail;
import utils.EditorPopupMenu;
import utils.EditorUtils;
import utils.SendedMailTable;
/**
* 发送邮件界面
*/
public class SendFrame extends JInternalFrame implements ActionListener,
MouseListener, MouseMotionListener, FocusListener {
private JComboBox fontSizeCB;// 字体大小列表
private JComboBox fontCB;// 字体列表
private JTextPane sendCotent;// 发送内容面板
private JTextField subjectTF;// 邮件主题文本框
private JTextField copy_to;// 抄送
private JTextField to_mail;// 收件人
private JList attachmentList = null;// 附件列表,最多能添加三个附件
private JScrollPane scrollPane = null;// 正文编辑窗口
private JScrollPane jsp = null;// 用于显示附件
private DefaultListModel listmodel = null;// 附件列表模型
private JLabel adjunctL = null;// 附件标签
private JLabel to_mailLabel = null, copy_toLabel = null,
subject_Label = null;
private JButton sendButton = null;// 发送按钮
private JButton resetButton = null;// 重置
private JButton attachmentButton = null;// 插入附件按钮
private JButton selectColorButton = null;// 颜色选择按钮
private Box baseBox = null, boxV1 = null, boxV2 = null;
private ArrayList<String> attachArrayList = new ArrayList<String>();// 用于存储附件路径的链表
private Color color = Color.black;
// 属性定义
private Action boldAction = new StyledEditorKit.BoldAction();// 添加加粗侦听器
private Action underlineAction = new StyledEditorKit.UnderlineAction(); // 添加加下划线侦听器
private Action italicAction = new StyledEditorKit.ItalicAction(); // 添加倾斜侦听器
private HTMLDocument document = null;// 声明一个网页文档对象变量
private SendAttachMail sendMail = null;
private EditorPopupMenu rightMouseButton = null;
private JProgressBarFrame progressBar = null;// 进度条实例
public SendFrame() {
super("新邮件");
this.setFrameIcon(EditorUtils.createIcon("newMail.gif"));
// 初始化基本项
getContentPane().setLayout(new BorderLayout());// 设置空布局
setIconifiable(true);// 是否使 JInternalFrame 变成一个图标
setClosable(true);// 是否关闭
setMaximizable(true);// 窗口最大化设置
setResizable(true);// 设置窗口课以调整大小
setBounds(10, 10, 640, 600);// 设置界面的大小
setVisible(true);
// 设置收件人标签
to_mailLabel = new JLabel();
to_mailLabel.setText("收件人:");
// 抄送标签
copy_toLabel = new JLabel();
copy_toLabel.setText("抄送:");
// 主题标签
subject_Label = new JLabel();
subject_Label.setText("主题:");
// 收件人文本框
to_mail = new JTextField(80);
to_mail.addFocusListener(this);
to_mail.setToolTipText("将收件人地址以逗号分隔");
// 抄送文本框
copy_to = new JTextField(80);
copy_to.addFocusListener(this);
// 主题文本框
subjectTF = new JTextField(80);
JPanel setPanel = new JPanel();// 上半部
setPanel.add(box());
scrollPane = new JScrollPane();
sendCotent = new JTextPane();
sendCotent.setContentType("text/html");
HTMLEditorKit editorKit = new HTMLEditorKit();
document = (HTMLDocument) editorKit.createDefaultDocument();// 创建默认文档指向网页引用document
sendCotent.setEditorKit(editorKit);// 设置为html格式的编辑器
sendCotent.setDocument(document);
sendCotent.addMouseListener(this);
scrollPane.setViewportView(sendCotent);
// 工具条
final JToolBar toolBar = new JToolBar();
getContentPane().add(toolBar);
sendButton = new JButton("发送", EditorUtils.createIcon("newsend.gif"));
sendButton.addActionListener(this);
toolBar.add(sendButton);
resetButton = new JButton("重写", EditorUtils.createIcon("rewrite.gif"));
resetButton.addActionListener(this);
toolBar.add(resetButton);
// 附件列表
listmodel = new DefaultListModel();
adjunctL = new JLabel("附件:");
jsp = new JScrollPane();// 用于显示JList
jsp.setPreferredSize(new Dimension(350, 20));
attachmentList = new JList(listmodel);
attachmentList.addMouseListener(this);// 为邮件列表添加鼠标事件
jsp.setViewportView(attachmentList);// 设置JScrollPanel的视图为JList
attachmentList
.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
attachmentList.setVisibleRowCount(1);
attachmentList.setLayoutOrientation(JList.VERTICAL_WRAP);
// 插入附件按钮
attachmentButton = new JButton("插入附件",
EditorUtils.createIcon("attach.png"));
attachmentButton.addActionListener(this);
toolBar.add(attachmentButton);
// 斜体按钮
JButton italicButton = new JButton(italicAction);
italicButton.setIcon(EditorUtils.createIcon("italic.gif"));
italicButton.setText("");
italicButton.setPreferredSize(new Dimension(22, 22));
// 粗体按钮
JButton blodButton = new JButton(boldAction);
blodButton.setIcon(EditorUtils.createIcon("blod.gif"));
blodButton.setText("");
blodButton.setPreferredSize(new Dimension(22, 22));
// 下划线按钮
JButton underlineButton = new JButton(underlineAction);
underlineButton.setIcon(EditorUtils.createIcon("underline.gif"));
underlineButton.setText("");
underlineButton.setPreferredSize(new Dimension(22, 22));
// 字体
final JLabel fontLabel = new JLabel("字体");
GraphicsEnvironment ge = GraphicsEnvironment
.getLocalGraphicsEnvironment();// 获得本地 计算机上字体可用的名称
String font[] = ge.getAvailableFontFamilyNames();
fontCB = new JComboBox(font);
fontCB.addActionListener(this);
// 字号列表
final JLabel fontSizeLabel = new JLabel("字号");
String fontSize[] = { "10", "11", "12", "13", "14", "16", "18", "20",
"22", "24", "26", "28", "36", "48" };
fontSizeCB = new JComboBox(fontSize);
fontSizeCB.addActionListener(this);
fontSizeCB.setPreferredSize(new Dimension(50, 23));
// 颜色
final JLabel colorLabel = new JLabel("颜色");
selectColorButton = new JButton("选 色");
selectColorButton.addActionListener(this);
JPanel editorToolBarPanel = new JPanel();// 编辑区工具条
editorToolBarPanel.add(italicButton);
editorToolBarPanel.add(blodButton);
editorToolBarPanel.add(underlineButton);
editorToolBarPanel.add(new JLabel(" "));
editorToolBarPanel.add(fontLabel);
editorToolBarPanel.add(fontCB);
editorToolBarPanel.add(new JLabel(" "));
editorToolBarPanel.add(fontSizeLabel);
editorToolBarPanel.add(fontSizeCB);
editorToolBarPanel.add(new JLabel(" "));
editorToolBarPanel.add(colorLabel);
editorToolBarPanel.add(selectColorButton);
// 编辑区面板
JPanel editorPanel = new JPanel(new BorderLayout());// 编辑区
editorPanel.add(editorToolBarPanel, BorderLayout.NORTH);
editorPanel.add(scrollPane, BorderLayout.CENTER);
// 添加一个分割窗口
JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
setPanel, editorPanel);
splitPane.setOneTouchExpa