ChatWindow.java
上传用户:damzkj
上传日期:2022-01-07
资源大小:24k
文件大小:8k
- package jxtamessenger;
- import java.io.DataOutput;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.logging.Logger;
- import jxtamessenger.bean.OnlineMessage;
- import jxtamessenger.service.JxtaServerSocketService;
- import jxtamessenger.util.Constants;
- import jxtamessenger.util.MiscUtil;
- import jxtamessenger.util.PipeUtil;
- import jxtamessenger.xml.XmlCreator;
- import net.jxta.peergroup.PeerGroup;
- import net.jxta.socket.JxtaSocket;
- import org.apache.commons.lang.StringUtils;
- import org.eclipse.jface.window.Window;
- import org.eclipse.swt.SWT;
- import org.eclipse.swt.custom.SashForm;
- import org.eclipse.swt.events.ControlAdapter;
- import org.eclipse.swt.events.ControlEvent;
- import org.eclipse.swt.events.KeyAdapter;
- import org.eclipse.swt.events.KeyEvent;
- import org.eclipse.swt.events.MouseAdapter;
- import org.eclipse.swt.events.MouseEvent;
- import org.eclipse.swt.layout.FillLayout;
- import org.eclipse.swt.widgets.Button;
- import org.eclipse.swt.widgets.Composite;
- import org.eclipse.swt.widgets.Control;
- import org.eclipse.swt.widgets.Shell;
- import org.eclipse.swt.widgets.Text;
- public class ChatWindow extends Window {
- private static final Logger LOG = Logger.getLogger(ChatWindow.class.getName());
- private JxtaSocket socket;
- private OnlineMessage msg;
- private PeerGroup pg;
-
- private static final int BTNWIDTH = 32;
- private static final int BTNHEIGHT = 22;
-
- private Text up_text;
- public Text getUp_text() {
- return up_text;
- }
- private Text down_text;
- // public Text getDown_text() {
- // return down_text;
- // }
- private Button down_send;
- // public Button getDown_send() {
- // return down_send;
- // }
- public void disableInputAndSend(String userName) {
- down_text.setText(userName + "已离开会话");
- down_text.setEnabled(false);
- down_send.setEnabled(false);
- }
-
- public void enableInputAndSend() {
- try {
- // TODO: Terminated gracefully
- // if(!socket.isClosed())
- // socket.close();
- socket = new JxtaSocket(pg,
- null, //no specific peerid
- PipeUtil.findPipeAdv(pg, JxtaServerSocketService.JxtaServerSocketPipeAdvPrefix + msg.getHostName()),
- 60000, //general TO: 60 seconds
- true); // reliable connection
- } catch (IOException e) {
- LOG.warning("JxtaSocket initialize failed!");
- e.printStackTrace();
- }
- down_text.setText("");
- down_text.setEnabled(true);
- down_send.setEnabled(true);
- }
-
- public ChatWindow(Shell parentShell, PeerGroup pg, OnlineMessage msg) {
- super(parentShell);
- this.msg = msg;
- this.pg = pg;
- try {
- socket = new JxtaSocket(pg,
- null, //no specific peerid
- PipeUtil.findPipeAdv(pg, JxtaServerSocketService.JxtaServerSocketPipeAdvPrefix + msg.getHostName()),
- 60000, //general TO: 60 seconds
- true); // reliable connection
- } catch (IOException e) {
- LOG.warning("JxtaSocket initialize failed!");
- e.printStackTrace();
- }
- }
- /* (non-Javadoc)
- * @see org.eclipse.jface.window.Window#createContents(org.eclipse.swt.widgets.Composite)
- */
- @Override
- protected Control createContents(Composite parent) {
- SashForm form = new SashForm(parent, SWT.VERTICAL);
- form.setLayout(new FillLayout());
-
- Composite up = new Composite(form,SWT.NONE);
- up.setLayout(new FillLayout());
- up_text = new Text(up, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.READ_ONLY | SWT.H_SCROLL);
- up_text.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));
- Composite down = new Composite(form,SWT.NONE);
- down.setLayout(new FillLayout());
- down_text = new Text(down, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL);
- down_text.addControlListener(new ControlAdapter() {
- public void controlResized(ControlEvent e) {
- Text source = (Text)e.getSource();
- source.setSize(source.getParent().getSize().x - BTNWIDTH-8, source.getParent().getSize().y);
- }
- });
- down_text.setFocus();
- down_send = new Button(down, SWT.PUSH);
- down_send.setText("发送");
- down_send.addControlListener(new ControlAdapter() {
- public void controlResized(ControlEvent e) {
- Button source = (Button)e.getSource();
- source.pack();
- int x = source.getParent().getSize().x - BTNWIDTH - 4;
- int y = (source.getParent().getSize().y - BTNHEIGHT)/2;
- source.setLocation(x, y);
- }
- });
-
- down_text.addKeyListener(new KeyAdapter() {
- public void keyPressed(KeyEvent event) {
- // CTRL+A --- Select All
- if(event.stateMask == SWT.CTRL && event.keyCode == 'a') // If has better mothed?
- down_text.selectAll();
- /* // when user clicks CTRL+RETURN, then send the message, just like down_send button is clicked!
- * else if(event.stateMask == SWT.CTRL && (event.keyCode == SWT.CR || event.keyCode == SWT.LF)) {
- if(!StringUtils.isEmpty(down_text.getText())) {
- try {
- down_send.setEnabled(false);
- OutputStream out = (OutputStream) socket.getOutputStream();
- DataOutput dos = new DataOutputStream(out);
- dos.writeInt(Constants.MESSAGE_CHAT);
- Object[] chatMsgArr = MiscUtil.splitUTFString(XmlCreator.createChatMessage(down_text.getText()));
- dos.writeInt(chatMsgArr.length);
- for(int i=0; i<chatMsgArr.length; i++)
- dos.writeUTF((String)chatMsgArr[i]);
-
- up_text.append(MiscUtil.getUserName());
- up_text.append(":");
- up_text.append(down_text.getText());
- *//**
- * @see org.eclipse.swt.widgets.Text.sendKeyEvent
- *//*
- up_text.append("r");
- down_text.setText("");
- out.flush();
- down_send.setEnabled(true);
- } catch (IOException e) {
- LOG.warning("Send chat message failed.");
- e.printStackTrace();
- }
- }
- }*/
- }
- });
- down_send.addMouseListener(new MouseAdapter() {
- public void mouseUp(MouseEvent event) {
- if(!StringUtils.isEmpty(down_text.getText())) {
- try {
- down_send.setEnabled(false);
- OutputStream out = (OutputStream) socket.getOutputStream();
- DataOutput dos = new DataOutputStream(out);
- dos.writeInt(Constants.MESSAGE_CHAT);
- Object[] chatMsgArr = MiscUtil.splitUTFString(XmlCreator.createChatMessage(down_text.getText()));
- dos.writeInt(chatMsgArr.length);
- for(int i=0; i<chatMsgArr.length; i++)
- dos.writeUTF((String)chatMsgArr[i]);
-
- up_text.append(MiscUtil.getUserName());
- up_text.append(":");
- up_text.append(down_text.getText());
- up_text.append("rn"); // hard-code cariage return
- down_text.setText("");
- out.flush();
- down_send.setEnabled(true);
- } catch (IOException e) {
- // TODO: 发送失败
- LOG.warning("Send chat message failed.");
- e.printStackTrace();
- }
- }
- }
- });
-
- form.setWeights(new int[] {60,40});
-
- return parent;
- }
- /* (non-Javadoc)
- * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
- */
- @Override
- protected void configureShell(Shell newShell) {
- super.configureShell(newShell);
- newShell.setSize(300, 250);
-
- FillLayout layout = new FillLayout();
- layout.type = SWT.VERTICAL;
- layout.marginWidth = 4;
- layout.marginHeight = 4;
- newShell.setLayout(layout);
-
- newShell.setText("与" + msg.getUserName() + "交谈");
- }
- /* (non-Javadoc)
- * @see org.eclipse.jface.window.Window#close()
- */
- @Override
- public boolean close() {
- try {
- MainApplicationWindow.chatwin.remove(msg.getHostName());
- OutputStream out = (OutputStream) socket.getOutputStream();
- DataOutput dos = new DataOutputStream(out);
- dos.writeInt(Constants.MESSAGE_EXITWINDOW);
- // dos.writeUTF(MiscUtil.getHostName());
- // dos.writeUTF(MiscUtil.getUserName());
- out.flush();
- out.close();
- // TODO: Close socket gracefully!
- // socket.close();
- } catch (IOException e) {
- LOG.warning("JxtaSocket close failed!");
- e.printStackTrace();
- }
-
- LOG.info("super.close()");
- return super.close();
- }
-
-
- }