⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 groupitrtwin.java

📁 CoolFace是基于jxta的P2P系统
💻 JAVA
字号:
package com.cn.darkblue.window;

import java.util.HashMap;
import java.util.Set;

import net.jxta.document.AdvertisementFactory;
import net.jxta.document.MimeMediaType;
import net.jxta.endpoint.Message;
import net.jxta.impl.endpoint.relay.RelayServer;
import net.jxta.impl.protocol.RelayConfigAdv;
import net.jxta.peergroup.PeerGroup;
import net.jxta.pipe.OutputPipe;
import net.jxta.protocol.PeerAdvertisement;
import net.jxta.rendezvous.RendezVousService;

import org.CoolButton;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.FillLayout;
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.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;

import com.cn.darkblue.entity.IFriendInfo;
import com.cn.darkblue.helper.GroupHelper;
import com.cn.darkblue.helper.IGroupChat;
import com.cn.darkblue.helper.IPGResource;
import com.cn.darkblue.listener.CFRDVListener;
import com.cn.darkblue.listener.GroupBaseCommListener;
import com.cn.darkblue.util.CFResource;
import com.cn.darkblue.util.CFUtil;
import com.cn.darkblue.util.MessageUtil;
import com.cn.darkblue.util.SysUtil;
import com.swtdesigner.SWTResourceManager;

public class GroupItrtWin extends CFShell implements IGroupChat{

	public static final String CFV_KEY = "coolfaceViewKey";
	public static final String LIS_KEY = "ListenerKey";
	private Button f2Button;
	private Button f1Button;
	private Group friendGroup;
	private StyledText chatInput;
	private StyledText chatScreen;
	
	HashMap headMap = null;
	HashMap friMap = null;
	
	PeerGroup peerGroup = null;
	OutputPipe baseOut = null;
	
	String cName = "";
	
	public GroupItrtWin(Shell shell,int style,PeerGroup pg){
		this(shell.getDisplay(),style,pg);
		
	}
	
	public GroupItrtWin(Display display, int style ,PeerGroup pg) {
		super(display, style);
		setText(" 组操作");
		headMap = new HashMap();
		friMap = new HashMap();
		peerGroup = pg;
		creatPanel();
		cName = peerGroup.getPeerName();
		
		CFResource.getInstance().getGroupItrtWins().add(this);
	}
	private void creatPanel(){
		setLayout(new FillLayout());
		final TabFolder tabFolder = new TabFolder(this, SWT.BORDER);

		final TabItem tabItem = new TabItem(tabFolder, SWT.NONE);
		tabItem.setText("聊天");

		final Composite composite = new Composite(tabFolder, SWT.NONE);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		composite.setLayout(gridLayout);
		tabItem.setControl(composite);

		friendGroup = new Group(composite, SWT.NONE);
		final GridLayout gridLayout_2 = new GridLayout();
		friendGroup.setLayout(gridLayout_2);
		final GridData gd_friendGroup = new GridData(SWT.LEFT, SWT.FILL, false, true);
		friendGroup.setLayoutData(gd_friendGroup);

		f1Button = new Button(friendGroup, SWT.NONE);
		/*
		f1Button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				try{
					Button but = (Button)e.getSource();
					IFriendInfo fi = (IFriendInfo)but.getData(CFV_KEY);
					if(fi!=null){
						System.out.println("will open the chat window");
						ChatWin win = new ChatWin(SysUtil.getSysShell(),SWT.SHELL_TRIM,fi.getId(),peerGroup);
						win.open();
					}else{
						MessageDialog.openInformation(SysUtil.getSysShell()," 温馨提示 "," 对不起还没有绑定到好友 ");
					}
					}catch(Exception ex){
						ex.printStackTrace();
					}
			}
		});*/
		f1Button.setText("     F1    ");

		f2Button = new Button(friendGroup, SWT.NONE);
		f2Button.setText("     F2    ");
		/*
		f2Button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				try{
					Button but = (Button)e.getSource();
					IFriendInfo fi = (IFriendInfo)but.getData(CFV_KEY);
					if(fi!=null){
						System.out.println("will open the chat window");
						ChatWin win = new ChatWin(SysUtil.getSysShell(),SWT.SHELL_TRIM,fi.getId(),peerGroup);
						win.open();
					}else{
						MessageDialog.openInformation(SysUtil.getSysShell()," 温馨提示 "," 对不起还没有绑定到好友 ");
					}
					}catch(Exception ex){
						ex.printStackTrace();
					}
			}
		});*/

		final Group group_1 = new Group(composite, SWT.NONE);
		group_1.setLayout(new GridLayout());
		group_1.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

		chatScreen = new StyledText(group_1, SWT.V_SCROLL | SWT.READ_ONLY | SWT.MULTI | SWT.BORDER);
		final GridData gd_chatScreen = new GridData(SWT.FILL, SWT.FILL, true, true);
		chatScreen.setLayoutData(gd_chatScreen);

		chatInput = new StyledText(group_1, SWT.V_SCROLL | SWT.MULTI | SWT.BORDER);
		final GridData gd_chatInput = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_chatInput.heightHint = 60;
		chatInput.setLayoutData(gd_chatInput);

		final Composite composite_1 = new Composite(group_1, SWT.NONE);
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		final GridLayout gridLayout_1 = new GridLayout();
		gridLayout_1.numColumns = 7;
		composite_1.setLayout(gridLayout_1);

		final Button button_2 = new Button(composite_1, SWT.NONE);
		button_2.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				try{
				HashMap map = new HashMap();
				CFUtil.seatchPeer(peerGroup, map);
				}catch(Exception ex){
					ex.printStackTrace();
				}
				//MessageDialog.openInformation(SysUtil.getSysShell()," 温馨提示 "," 查询已经开始 ");
			}
		});
		button_2.setText("查询成员");

		final Button button_3 = new Button(composite_1, SWT.NONE);
		button_3.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				System.out.println("add the peer size");
				bindFri();
				/*
				//initFreiHeader();
				IPGResource pgres = (IPGResource)CFResource.getInstance().getJoinPGMap().get(peerGroup);

				String mySelfId = peerGroup.getPeerID().toString();
				HashMap map = pgres.getMemebers();
				Object[] keys = map.keySet().toArray();
				for(int i=0;i<keys.length;i++){
					if(keys[i].toString().indexOf(mySelfId)!=-1) continue;
					
					if(i==0){
						System.out.println("f1Button peer is :"+keys[i]);
						f1Button.setData(CFV_KEY, map.get(keys[i]));
					}
					
					if(i==1){
						System.out.println("f2Button peer is :"+keys[i].toString());
						f2Button.setData(CFV_KEY, map.get(keys[i]));
						break;
					}
				}*/
			}
		});
		button_3.setText("更新成员");
		
		final Button f3Button = new Button(friendGroup, SWT.NONE);
		f3Button.setText("     f3     ");

		final Button f4Button = new Button(friendGroup, SWT.NONE);
		f4Button.setText("     f4     ");

		final Button f5Button = new Button(friendGroup, SWT.NONE);
		f5Button.setText("     f5     ");

		final Button f6Button = new Button(friendGroup, SWT.NONE);
		f6Button.setText("     f6     ");

		final Button f7Button = new Button(friendGroup, SWT.NONE);
		f7Button.setText("     f7     ");
		
		final Button f8Button = new Button(friendGroup, SWT.NONE);
		f8Button.setText("     f8     ");

		final Button act_rdvButton = new Button(composite_1, SWT.NONE);
		act_rdvButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				RendezVousService rvs = peerGroup.getRendezVousService();
				rvs.addListener(new CFRDVListener());
				rvs.startRendezVous();
			}
		});
		act_rdvButton.setText("ACT_RDV");

		final Button con_rdvButton = new Button(composite_1, SWT.NONE);
		con_rdvButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				RendezVousService rvs = peerGroup.getRendezVousService();
				PeerAdvertisement adv = (PeerAdvertisement) CFUtil.loadAdvertisement(SysUtil.getRoot()+"/WAYKE.xml");
				try{
					rvs.addListener(new CFRDVListener());
					System.out.println(" start connet to the rdv....."+adv.getPeerID().toString()+" name = "+adv.getName());
					rvs.connectToRendezVous(adv);
					
					//rvs.w
					int ii = 0;
					/*
					while(!rvs.isConnectedToRendezVous()){
						rvs.connectToRendezVous(adv);
						ii++;
						System.out.println(" start connet to the rdv....."+ii);
						try{
							Thread.sleep(5*1000);
						}catch(Exception ex){
							ex.printStackTrace();
						}
					}*/
				}catch(Exception ee){
					ee.printStackTrace();
				}
			}
		});
		con_rdvButton.setText("CON_RDV");

		final Button button = new Button(composite_1, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				String chats = chatInput.getText();
				if(chats==null || "".equals(chats))
					return;
				
				chats = cName+" 说:"+chats;
				sendPGMsg(chats);
				
				chatInput.setText("");
			}
		});
		button.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false));
		button.setText("发送");

		final Button button_1 = new Button(composite_1, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				exit();
			}
		});
		button_1.setText("退出");

		final TabItem tabItem_1 = new TabItem(tabFolder, SWT.NONE);
		tabItem_1.setText("资源");
		
		this.setSize(500, 400);
		this.setText("组:"+peerGroup.getPeerGroupName());
		
		startRelay();
		//initFreiHeader();
	}
	
	private void startRelay(){
		try{
			RelayConfigAdv rca = (RelayConfigAdv)AdvertisementFactory.newAdvertisement(RelayConfigAdv.getAdvertisementType());
		
			rca.setServerEnabled(true);
			rca.setClientEnabled(true);
			
			//rca.setAclUri(arg0);
			//rca.setAclUri(arg0)
			System.out.println("the Relay Adv \n"+rca.getDocument(MimeMediaType.XMLUTF8));
			//peerGroup.refRelayProtoSpecID
			//rca.
		}catch(Exception e){
			e.printStackTrace();
		}
		
		//RelayServer srv = new RelayServer(peerGroup,"wayke01",);
	}
	
	private void bindFri(){
		IPGResource pgres = (IPGResource)CFResource.getInstance().getJoinPGMap().get(peerGroup);

		String mySelfId = peerGroup.getPeerID().toString();
		HashMap map = pgres.getMemebers();
		
		System.out.println("All Friends :"+map.size());
		
		Control[] objs = friendGroup.getChildren();
		
		Object[] keys = map.keySet().toArray();
		Button btn = null;
		FriChatClickListener lis;
		int index = 0;
		for(int i=0;i<keys.length;i++){
			if(keys[i].toString().indexOf(mySelfId)!=-1) continue;

			if(index<objs.length){
				btn = (Button)objs[index];
				IFriendInfo fi = (IFriendInfo)map.get(keys[i]);
				System.out.println("Friend Name is :"+fi.getName());
				System.out.println("Friend Name ID :"+fi.getId());
				btn.setText(fi.getName());
			
				btn.setData(CFV_KEY, fi);
				lis = (FriChatClickListener)btn.getData(LIS_KEY);
				if(lis!=null)
				btn.removeSelectionListener(lis);
				
			    lis = new FriChatClickListener();
				
				btn.addSelectionListener(lis);
				btn.setData(LIS_KEY, lis);
				index ++;
			}
		}
	}
	
	public void exit(){
		
		close();
	}
	
	private void initFreiHeader(){
		IPGResource pgres = (IPGResource)CFResource.getInstance().getJoinPGMap().get(peerGroup);

		HashMap map = pgres.getMemebers();
		Object[] keys = map.keySet().toArray();
		for(int i=0;i<keys.length;i++){
			System.out.println("peer is :"+keys[i].toString());
			addFreiHeader(map.get(keys[i]));
		}
	}
	
	private CoolButton getButton(Group group, String title) {

		return null;
	}
	
	private void sendPGMsg(String msg){
		if(baseOut==null){
			IPGResource pgres = (IPGResource)CFResource.getInstance().getJoinPGMap().get(peerGroup);
			baseOut = (OutputPipe)pgres.getOutputPipes().get(GroupHelper.PIPE_OUT_KEY);
			
			GroupBaseCommListener listener = (GroupBaseCommListener)pgres.getInputListener().get(GroupBaseCommListener.BASE_COMM_KEY);
			try{
			listener.setScreen(this);
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		
		Message sMsg = new Message();
		MessageUtil.addStringToMessage(sMsg,GroupBaseCommListener.BASE_COMM_NAMESPACE, GroupBaseCommListener.CHAT_STR, msg);
		MessageUtil.addIntegerToMessage(sMsg,GroupBaseCommListener.BASE_COMM_NAMESPACE, GroupBaseCommListener.RES_CMD_STR, GroupBaseCommListener.CHAT_PGALL_CMD);
		
		try{
			baseOut.send(sMsg);
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}
////////////////////IGroupChat/////////////////
	public boolean addFreiHeader(Object header){
		IFriendInfo fi = (IFriendInfo)header;
		friMap.put(fi.getId(),fi);
		headMap.put(fi.getId(),getButton(friendGroup,"cc"));//fi.getName()
		return true;
		
	}
	public boolean removeFreiHeader(Object header){
		return true;
	}
	public void sendMessage(Object msg){
		//TODO 选择管道 发送消息
	}
	public void jitterHeader(Object obj){
		IFriendInfo fi = (IFriendInfo)obj;
		CoolButton cb = (CoolButton)headMap.get(fi.getId());
		Color cr1 = SWTResourceManager.getColor(0, 0, 0);
		cb.setClr1(cr1);
		cb.redraw();
	}

	public void print(String message) {
		String value = chatScreen.getText();
		if(value == null) value="";
		
		chatScreen.setText(value+message);
		chatScreen.setSelection((value+message).length());
		
	}

	public void println(String message) {
		String value = chatScreen.getText();
		if(value == null) value="";
		
		chatScreen.setText(value+message+"\n");
		chatScreen.setSelection((value+message).length());
	}

	public PeerGroup getPeerGroup() {
		return peerGroup;
	}

	public void setPeerGroup(PeerGroup peerGroup) {
		this.peerGroup = peerGroup;
	}
	
	class FriChatClickListener extends SelectionAdapter{
		public void widgetSelected(SelectionEvent e) {
			try{
				Button but = (Button)e.getSource();
				IFriendInfo fi = (IFriendInfo)but.getData(CFV_KEY);
				if(fi!=null){
					System.out.println("will open the chat window");
					ChatWin win = new ChatWin(SysUtil.getSysShell(),SWT.SHELL_TRIM,fi.getId(),peerGroup,fi.getName());
					win.open();
				}else{
					MessageDialog.openInformation(SysUtil.getSysShell()," 温馨提示 "," 对不起还没有绑定到好友 ");
				}
				}catch(Exception ex){
					ex.printStackTrace();
				}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -