/**
* @(#)frm.IPMsgMainFrm.java 2008-10-8
* Copy Right Information : Tarena
* Project : IPMsg
* JDK version used : jdk1.6.4
* Comments : 飞鸽传书的主窗体类。
* Version : 1.0
* Sr Date Modified By Why & What is modified
* 1. 2008-10-8 小猪 新建
**/
package frm;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetAdapter;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableRowSorter;
import about.About;
import tools.DateDeal;
import data.User;
/**
* 飞鸽传书的主窗体类。
* 2008-10-8
* @author 达内科技[Tarena Training Group]
* @version 1.0
* @since JDK1.6(建议)
*/
@SuppressWarnings("serial")
public class IPMsgMainFrm extends JFrame implements ActionListener,TableModelListener{
/** 主面板上显示用户列表的JTable */
public JTable tableUser = null;
/** JTable的DefaultTableModel */
public MyDefaultTableModel model = null;
/** 主面板上显示信息的JLabel */
private JLabel lblInfo = new JLabel("在线用户");
/** 主面板上显示在线用户的JLabel */
public JLabel lblOnlineNum = new JLabel("0");
/** 主面板上的刷新按钮JButton */
public JButton btnFlush = new JButton("刷新(R)");
/** 主面板上聊天面板JTextArea */
public JTextArea areaChat = new JTextArea();
/** 主面板上的发送按钮JButton */
public JButton btnSend = new JButton("发送(S)");
/** 主面板上的封装选项扭JCheckbox */
private JCheckBox boxIsPackage = new JCheckBox("封装(E)");
/** 主面板上的上锁选项扭 */
private JCheckBox boxIsLock = new JCheckBox("上锁(K)");
/** 传送文件的对话框 */
private SendListDialog listDialog = null;
/** 传送显示即将传送文件的按钮 */
private JButton btnSendList = new JButton();
/** 显示传送文件按钮的JPanel */
private JPanel paneSendList = new JPanel();
/** 系统托盘右键菜单 */
private PopupMenu popupMenu = null;
/** 显示飞鸽传书 */
private MenuItem itemShow = null;
/** 显示文件传送监视器 */
private MenuItem itemMonitor = null;
/** 将窗口至于最前端 */
private MenuItem itemSetTop = null;
/** 查看通信记录 */
private MenuItem itemSeeLog = null;
/** 关于 */
private MenuItem itemAbout = null;
/** 退出飞鸽传书 */
public MenuItem itemQuit = null;
/** 关于对话框 */
private About about = null;
/** 文件传送监视器 */
private FileSendMonitor monitor = null;
/** 程序图标 */
private Image image = null;
private TrayIcon trayIcon = null;
public IPMsgMainFrm() {
setTitle("飞鸽传书 IP Messenger");
setSize(340,320);
setMinimumSize(new Dimension(300,210));
//setResizable(false);
Toolkit tk=Toolkit.getDefaultToolkit();
setLocation((tk.getScreenSize().width-getSize().width)/2,(tk.getScreenSize().height-getSize().height)/2);
try {
image = ImageIO.read(getClass().getResource("/frm/img/ipmsg.gif"));
setIconImage(image);
} catch (IOException e) {
image = tk.getImage(getClass().getResource("/frm/img/ipmsg.gif"));
}
init();
setVisible(false);
new DropTarget(this,new MyDropTargetAdapter());
initSystemTray();
}
/**
* 初始化托盘图标。
*/
private void initSystemTray(){
//支持系统托盘图标
if(SystemTray.isSupported()){
popupMenu = new PopupMenu();
itemShow = new MenuItem("显示飞鸽传书");
itemShow.addActionListener(this);
itemMonitor = new MenuItem("文件传送监视器");
itemMonitor.addActionListener(this);
itemSetTop = new MenuItem("将窗口至于最前端");
itemSetTop.addActionListener(this);
itemSeeLog = new MenuItem("查看通信记录");
itemSeeLog.addActionListener(this);
itemAbout = new MenuItem("关于");
itemAbout.addActionListener(this);
itemQuit = new MenuItem("退出飞鸽传书");
//itemQuit.addActionListener(this);
popupMenu.add(itemShow);
popupMenu.addSeparator();
popupMenu.add(itemMonitor);
popupMenu.addSeparator();
popupMenu.add(itemSetTop);
popupMenu.add(itemSeeLog);
popupMenu.add(itemAbout);
popupMenu.addSeparator();
popupMenu.add(itemQuit);
SystemTray tray = SystemTray.getSystemTray();
try {
trayIcon = new TrayIcon(image.getScaledInstance(16,16,0),"飞鸽传书(0)",popupMenu);
trayIcon.addMouseListener(new MyMouseAdapter());
tray.add(trayIcon);
} catch (AWTException e) {
System.out.println("错误:"+e.getMessage());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
addWindowListener(new MyWindowAdapter());
}
}else{//不支持系统托盘图标
setVisible(true);
}
}
/**
* 初始化面板。
*/
@SuppressWarnings("unchecked")
private void init(){
listDialog = new SendListDialog(this,true,false);
btnFlush.setPreferredSize(new Dimension(65,25));
btnFlush.setMargin(new Insets(0,0,0,0));
btnFlush.addActionListener(this);
btnFlush.setMnemonic(KeyEvent.VK_R);
btnSend.setPreferredSize(new Dimension(65,25));
btnSend.setMargin(new Insets(0,0,0,0));
btnSend.addActionListener(this);
btnSendList.setFocusPainted(false);
btnSendList.addActionListener(this);
btnSendList.setContentAreaFilled(false);
btnSend.setMnemonic(KeyEvent.VK_S);
lblInfo.setPreferredSize(new Dimension(65,15));
lblInfo.setHorizontalAlignment(SwingConstants.CENTER);
lblOnlineNum.setPreferredSize(new Dimension(65,30));
lblOnlineNum.setHorizontalAlignment(SwingConstants.CENTER);
lblOnlineNum.setVerticalAlignment(SwingConstants.TOP);
boxIsPackage.setMnemonic(KeyEvent.VK_E);
boxIsPackage.addActionListener(this);
boxIsLock.setMnemonic(KeyEvent.VK_K);
boxIsLock.setEnabled(boxIsPackage.isSelected());
areaChat.setLineWrap(true);
paneSendList.setPreferredSize(new Dimension(500,3));
paneSendList.setLayout(new BorderLayout(3,3));
paneSendList.add(new FillWidth(0,0),BorderLayout.NORTH);
paneSendList.add(btnSendList);
model = new MyDefaultTableModel();
model.addTableModelListener(this);
String[] columns = {"用户名","主机名","IP地址","登录名","优先级"};
for(String cloumn:columns)
model.addColumn(cloumn);
tableUser = new JTable(model);
TableRowSorter sorter = new TableRowSorter(model);
tableUser.setRowSorter(sorter);
tableUser.setFillsViewportHeight(true);
tableUser.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tableUser.setPreferredScrollableViewportSize(new Dimension(300,200));
TableColumn cloumn0 = tableUser.getColumn(columns[0]);
cloumn0.setPreferredWidth(80);
//cloumn0.setMinWidth(100);
//TableColumn cloumn1 = tableUser.getColumn(columns[1]);
//cloumn0.setPreferredWidth(180);
//cloumn1.setMinWidth(180);
TableColumn cloumn2 = tableUser.getColumn(columns[2]);
cloumn2.setPreferredWidth(12