Package ui.tabs

Source Code of ui.tabs.ChatTab$FeedListener

package ui.tabs;


import java.awt.BorderLayout;
import java.io.IOException;
import java.io.ObjectOutputStream;

import common.ClientServerProtocol;
import common.connection.ConnectionWriterInterface;

import chat.ChatForm;
import chat.MessageBufferThread;
import chat.MessageSenderInterface;

import connection.server.ConnectionStateAdapter;
import connection.server.ServerConnection;

import ui.misc.MessageBox;
import ui.misc.feeder.FeederFieldListener;


public class ChatTab extends AbstractTab
{
  private static final long serialVersionUID = 9214768975466654226L;

  private ChatForm form;
  private MessageBufferThread sender;

  public ChatTab()
  {
    super();
    setLayout(new BorderLayout());

    form = new ChatForm(this, new FeedListener(), "Global channel");
    new MessageSender();
  }

  private class MessageSender extends ConnectionStateAdapter implements
      MessageSenderInterface
  {
    public void onConnected(ServerConnection newConnection)
    {
      super.onConnected(newConnection);

      sender = new MessageBufferThread(this);
      form.enable();
    }

    public void onDisconnected()
    {
      super.onDisconnected();

      form.disable();
      sender.terminate();
    }

    public void sendMessage(final String message)
    {
      int protocolId = ClientServerProtocol.PROTOCOL_CHAT;
      int opcode = ClientServerProtocol.Chat.OP_POST_MESSAGE;
      getConnection().send(protocolId, opcode, new ConnectionWriterInterface()
      {
        public void run(ObjectOutputStream os) throws IOException
        {
          os.writeUTF(message);
        }

      });
    }

  }

  private class FeedListener implements FeederFieldListener
  {
    public void feed(String text)
    {
      if (sender == null) // giusto per sicurezza: meglio non fidarsi
                // della GUI.
      {
        MessageBox.showError("Not connected.", "Error");
      }
      else
      {
        sender.queueMessage(text);
      }
    }

  }

  public void logMessage(String sender, String message)
  {
    form.logMessage(sender, message);
  }

}
TOP

Related Classes of ui.tabs.ChatTab$FeedListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.