package tk.ipmsg.swt;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Vector;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.dnd.DND;
import org.eclipse.swt.dnd.DropTarget;
import org.eclipse.swt.dnd.DropTargetAdapter;
import org.eclipse.swt.dnd.DropTargetEvent;
import org.eclipse.swt.dnd.FileTransfer;
import org.eclipse.swt.dnd.Transfer;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.events.ShellAdapter;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tray;
import org.eclipse.swt.widgets.TrayItem;
import tk.ipmsg.swt.Main.GroupComparator;
import tk.ipmsg.swt.Main.GroupSortListener;
import tk.ipmsg.swt.Main.HostComparator;
import tk.ipmsg.swt.Main.HostSortListener;
import tk.ipmsg.swt.Main.SendListener;
import tk.ipmsg.swt.Main.UserComparator;
import tk.ipmsg.swt.Main.UserSortListener;
public class MainWindow2{
protected Shell shell;
protected Table table;
protected Text text;
private Label sumUser;//所有用户数
private Button btn2;//封装btn
private Button lock;
private Button fileBtn;//传文件btn
private SashForm sf;
private boolean sendFileFlag = false;
private Menu popup;
private boolean debug;
private Messenger messenger;
private boolean isLiftClick = false;//鼠标左键点击
private String userName="";
private String addressName="";
private String groupName="";
private Composite panel3;
private FileTransfer fileTransfer;
private String[] files;
private String msgStr;
private GridLayout gridLayout;
private GridData gd;
public static void main(String[] args){
boolean debug = false;
MainWindow2 mw = new MainWindow2(debug);
}
public MainWindow2(boolean debug) {
Display display = new Display();
//final Shell shell = new Shell(display,SWT.MIN | SWT.RESIZE);
shell = new Shell(display);
shell.setImage(new Image(display,"飞鸽2\\images\\ipmsg.gif"));
initGui(shell);
shell.open();
shell.addShellListener(new ShellAdapter() {
public void shellClosed(ShellEvent e){
e.doit = false;////防止点击窗口的关闭按钮退出
shell.setVisible(false);
}
});
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
logout();
display.dispose();
System.exit(0);
}
public Shell getShell(){
return this.shell;
}
public boolean getDebugMode(){
return this.debug;
}
public void logout(){
if(messenger!=null){
try {
messenger.logout();
} catch(IOException ex){
ex.printStackTrace();
MessageBox mesBox = new MessageBox(shell,SWT.OK|SWT.ICON_ERROR);
mesBox.setText("确定");
mesBox.setMessage(ex.toString());
mesBox.open();
}
}
}
private void getUserName(){
Properties prop = new Properties(System.getProperties());
Hashtable hashKey = new Hashtable();
hashKey.put("user.name", "用户名");
Enumeration enum2 = hashKey.keys();
String str = "";
while (enum2.hasMoreElements()) {
str = (String) enum2.nextElement();
}
userName = prop.getProperty(str);
}
private void getAddress(){
try {
InetAddress address=InetAddress.getLocalHost();
addressName = address.getHostAddress();
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
protected void initGui(Shell shell) {
try {
getAddress();
getUserName();
// messenger = new Messenger(userName,"ibm", hostName,addressName,userName,this);
messenger = new Messenger(userName, userName,"addressName",this);
messenger.login();
messenger.start();
} catch(Exception ex){}
shell.setText("飞鸽传书 ^0^");
shell.setSize(336, 345);
shell.setLayout(new GridLayout());
sf = new SashForm(shell, SWT.VERTICAL | SWT.SMOOTH);
sf.setLayoutData(new GridData(GridData.FILL_BOTH));
Composite panel1 = new Composite(sf, SWT.NONE);
gridLayout = new GridLayout();
gridLayout.numColumns = 2;
gridLayout.marginLeft = -3;
gridLayout.marginTop = -3;
panel1.setLayout(gridLayout);
table = createTable(panel1, SWT.NONE);
// table.setBackground(new Color(display,236, 233, 216));
gd = new GridData(GridData.FILL_BOTH);
gd.widthHint = 290;
gd.heightHint = 100;
table.setLayoutData(gd);
Composite panel2 = new Composite(panel1, SWT.NONE);
gd = new GridData();
gd.widthHint = 55;
gd.heightHint = 100;
panel2.setLayoutData(gd);
panel2.setLayout(new GridLayout());
Label lb1 = new Label(panel2,SWT.NONE);
lb1.setText("在线用户");
sumUser = new Label(panel2,SWT.NONE);
gd = new GridData();
gd.widthHint = 30;
gd.heightHint = 15;
gd.horizontalAlignment = GridData.CENTER;
sumUser.setLayoutData(gd);
sumUser.setText(" "+table.getItemCount());
Button refurbish = new Button(panel2,SWT.NONE);
refurbish.setText("刷新");
gd = new GridData();
gd.widthHint = 45;
gd.heightHint = 24;
refurbish.setLayoutData(gd);
refurbish.addSelectionListener(new refurbishListener());
//-----------------------------------------------------------
panel3 = new Composite(sf, SWT.NONE);
gridLayout = new GridLayout();
gridLayout.marginLeft = -3;
gridLayout.marginTop = -3;
panel3.setLayout(gridLayout);
panel3.setLayoutData(new GridData(GridData.FILL_BOTH));
initText();
//---------------------------------------------------------------
Composite buttonComp = new Composite(shell,SWT.NONE);
gridLayout = new GridLayout();
gridLayout.horizontalSpacing = 20;
gridLayout.marginLeft = 25;
gridLayout.numColumns = 3;
buttonComp.setLayout(gridLayout);
gd = new GridData();
gd.widthHint = 300;
gd.heightHint = 35;
gd.horizontalAlignment = GridData.CENTER;
buttonComp.setLayoutData(gd);
Button sendBtn = new Button(buttonComp,SWT.NONE);
sendBtn.setText("发送");
gd = new GridData();
gd.widthHint = 105;
gd.heightHint = 29;
sendBtn.setLayoutData(gd);
sendBtn.addSelectionListener(new SendListener());
btn2 = new Button(buttonComp,SWT.CHECK);
btn2.setText("封装");
lock = new Button(buttonComp,SWT.CHECK);
lock.setText("上锁");
// lock.setEnabled(false);
sf.setWeights(new int []{3,2});
//add popup menu