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

📄 chatwin.java

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

import java.util.Queue;

import net.jxta.document.MimeMediaType;
import net.jxta.endpoint.Message;
import net.jxta.peergroup.PeerGroup;
import net.jxta.pipe.OutputPipe;
import net.jxta.util.JxtaBiDiPipe;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import com.cn.darkblue.helper.GroupHelper;
import com.cn.darkblue.helper.IGroupChat;
import com.cn.darkblue.helper.IMsgScreen;
import com.cn.darkblue.helper.IPGResource;
import com.cn.darkblue.listener.CFOutgoingMessageEventListener;
import com.cn.darkblue.listener.GroupBaseCommListener;
import com.cn.darkblue.listener.SecChatListener;
import com.cn.darkblue.util.CFDefData;
import com.cn.darkblue.util.CFResource;
import com.cn.darkblue.util.MessageUtil;

public class ChatWin extends CFShell implements IMsgScreen{
	private static final Log log = LogFactory.getLog(ChatWin.class); 
	private StyledText chatInput;
	private StyledText chatScreen;
	
	String chater;
	PeerGroup group;
	JxtaBiDiPipe bidiOut = null;
	CFOutgoingMessageEventListener outListener = null;
	String cName = "";
	
	String friendName = "";
	
	public JxtaBiDiPipe getBidiOut() {
		return bidiOut;
	}

	public void setBidiOut(JxtaBiDiPipe bidiOut) {
		this.bidiOut = bidiOut;
	}

	public String getChater() {
		return chater;
	}

	public void setChater(String chater) {
		this.chater = chater;
	}

	public PeerGroup getGroup() {
		return group;
	}

	public void setGroup(PeerGroup group) {
		this.group = group;
	}

	public ChatWin(Shell shell, int style, String chater,PeerGroup pg ,String fName) {
		super(shell.getDisplay(), style);
		final GridLayout gridLayout = new GridLayout();
		setLayout(gridLayout);

		this.chater = chater;
		group = pg;
		creatPanel();
		cName = group.getPeerName();
		
		friendName = fName;
		IPGResource res = (IPGResource)CFResource.getInstance().getJoinPGMap().get(group);
		res.getOScreens().put(chater, this);
	}
	
	private void creatPanel(){
		chatScreen = new StyledText(this, SWT.BORDER);
		final GridData gd_chatScreen = new GridData(SWT.FILL, SWT.FILL, true, true);
		chatScreen.setLayoutData(gd_chatScreen);

		chatInput = new StyledText(this, 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 = new Composite(this, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		final GridLayout gridLayout_1 = new GridLayout();
		gridLayout_1.numColumns = 8;
		composite.setLayout(gridLayout_1);

		final Button button = new Button(composite, SWT.NONE);
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				IPGResource pgres = null;
				if(bidiOut==null){
					pgres = (IPGResource)CFResource.getInstance().getJoinPGMap().get(group);
					bidiOut = (JxtaBiDiPipe)pgres.getOutputPipes().get(chater);
					System.out.println(" Chat target init : "+chater);
				}
				if(bidiOut==null){
					System.out.println(" Send a chat cmd to : "+chater);
					OutputPipe baseOut = (OutputPipe)pgres.getOutputPipes().get(GroupHelper.PIPE_OUT_KEY);
					sendChatCmd(baseOut);
				}
				int time = 0;
				while(bidiOut==null){
					bidiOut = (JxtaBiDiPipe)pgres.getOutputPipes().get(chater);
					System.out.println("Wait for BidiPipe ......"+time);
					if(time>10) break;
					try{
						Thread.sleep(6*1000);
						
						if(bidiOut!=null){
							System.out.println("BidiPipe :\n"+bidiOut.getPipeAdvertisement().getDocument(MimeMediaType.XMLUTF8));
							System.out.println("Listeter : "+pgres.getInputListener().get(chater).toString());
							
							outListener = new CFOutgoingMessageEventListener();

						}
					}catch(Exception ex){}
					time++;
					
				}
				
				//bindScreen(pgres);
				
				String msg = chatInput.getText();
				if(msg==null||"".equals(msg)) return;
				msg = cName+" 说:"+msg;
				sendMessage(msg);
				println(msg);
				chatInput.setText("");

			}
		});
		button.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, false, 7, 1));
		button.setText("发送");

		final Button button_1 = new Button(composite, SWT.NONE);
		button_1.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				exit();
			}
		});
		button_1.setText("退出");
		
		this.setText("好友:"+friendName);
	}
	
	/*
	private void bindScreen(IPGResource pgres){
//		if(bidiOut!=null){
//			Object obj = pgres.getInputListener().get(chater);
//			SecChatListener lis = (SecChatListener)obj;
//			lis.setScreen(this);
//		}
//		
		if(pgres==null) return;
		SecChatListener listener = (SecChatListener)pgres.getInputListener().get(chater);
		if(listener==null){
			System.out.println("加入Bidi 定向聊天listener.......");
			listener = new SecChatListener(group,chater);
			listener.setScreen(this);
			bidiOut.setMessageListener(listener);
            pgres.getInputListener().put(chater, listener);
		}else{
			listener.setScreen(this);
		}
	}*/
	
	public void exit(){	
		close();
	}
	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 void sendMessage(Object msg) {
		/*
		IPGResource pgres = (IPGResource)CFResource.getInstance().getJoinPGMap().get(group);
		
        Object obj = pgres.getOutputPipes().get(chater);
        
//        if(obj==null){
//        	OutputPipe pout = (OutputPipe)pgres.getOutputPipes().get(GroupHelper.PIPE_OUT_KEY);
//        	sendChatCmd(pout);
//        }else{
    	bidiOut = (JxtaBiDiPipe)obj;
    	*/
    	
		/*
    	Message message = null;
    	if(msg instanceof Message){
    		message = (Message)msg;
    	}else{
    		message = new Message();
    		MessageUtil.addStringToMessage(message, SecChatListener.CHAT_COMM_SPACE, 
    				SecChatListener.CHAT_COMM_MSG, msg.toString());
    	}*/
    	try{
    		System.out.println(" Will send :"+msg);
    		Message mPack = new Message();

    		MessageUtil.addStringToMessage(mPack,SecChatListener.CHAT_COMM_SPACE, 
    				SecChatListener.CHAT_COMM_MSG, msg.toString());
    		 
    		
    		System.out.println(" this message is :"+mPack);
    		if(bidiOut!=null){
    			System.out.println(" this bidiout connetion is :"+bidiOut.isBound());
    			
    			System.out.println("the PIPD SDV:\n"+bidiOut.getPipeAdvertisement().getDocument(MimeMediaType.XMLUTF8).toString());

    			bidiOut.sendMessage(mPack);
    		}else{
    			System.out.println(" this bidiout connetion is not OK the message in send queue");
    			IPGResource pgres = (IPGResource)CFResource.getInstance().getJoinPGMap().get(group);
    			Queue que = (Queue)pgres.getSendQueues().get(chater);
    			que.add(mPack);
    		}
    		//bidiOut.
    	}catch(Exception e){
    		log.error(e.getMessage());
    		e.printStackTrace();
    	}
//        }
	}
	
    private boolean sendChatCmd(OutputPipe out){
    	Message msg = new Message();
		String sender = group.getPeerGroupID()+CFDefData.COMBO_SPSTR+group.getPeerID();
		try{
			MessageUtil.addStringToMessage(msg, GroupBaseCommListener.BASE_COMM_NAMESPACE,
					GroupBaseCommListener.PUB_NAME_STR, sender);//发送者
			MessageUtil.addStringToMessage(msg, GroupBaseCommListener.BASE_COMM_NAMESPACE,
					GroupBaseCommListener.REQ_COMM_STR, chater);//要求接收者
			MessageUtil.addIntegerToMessage(msg, GroupBaseCommListener.BASE_COMM_NAMESPACE,
					GroupBaseCommListener.RES_CMD_STR, GroupBaseCommListener.CHAT_CMD);//要求接收者
			out.send(msg);
		}catch(Exception e){
			log.error(e.getMessage());
			return false;
		}
		return true;
	}

}

⌨️ 快捷键说明

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