/*
* Hamsam - Instant Messaging API
* Copyright (C) 2003 Raghu K
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package gui.action;
import gui.ChatPanel;
import hamsam.api.Buddy;
import hamsam.api.Message;
import hamsam.api.TextComponent;
import hamsam.exception.IllegalStateException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
/**
*
* @author Fr�d�ric DIB
*/
public class SendMessage implements ActionListener, KeyListener
{
private ChatPanel owner;
private Buddy buddy;
/** Creates a new instance of SendMessage */
public SendMessage(ChatPanel panel, Buddy buddy)
{
this.owner = panel;
this.buddy = buddy;
}
/** Invoked when an action occurs.
*
*/
public void actionPerformed(ActionEvent evt)
{
sendMessage();
}
public void keyTyped(KeyEvent evt)
{
}
public void keyPressed(KeyEvent evt)
{
}
public void keyReleased(KeyEvent evt)
{
int code = evt.getKeyCode();
//If "Enter" key is pressed
if (code == KeyEvent.VK_ENTER)
{
sendMessage();
}
}
private void sendMessage()
{
try
{
Message msg = new Message();
String text = this.owner.getMessageFieldText();
msg.addComponent(new TextComponent(text));
this.buddy.sendInstantMessage(msg);
this.owner.messageSent(text);
this.owner.setMessageFieldText("");
}
catch(IllegalStateException e)
{
e.printStackTrace();
}
}
}