package gui;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import main.BTMIDlet;
import util.BTConnection;
/**
*
* @author TeXteR
*/
public class MessagingForm extends Form implements CommandListener {
private BTConnection bTConnection;
private TextBox textBox;
private TextField textField;
private Command sendCommand;
private BTMIDlet bTMIDlet;
public MessagingForm(BTMIDlet bTMIDlet, BTConnection bTConnection) {
super("Komunikator");
this.bTMIDlet = bTMIDlet;
this.bTConnection = bTConnection;
sendCommand = new Command("Wyslij", Command.OK, 1);
this.addCommand(sendCommand);
this.addCommand(new Command("Exit", Command.EXIT, 1));
this.setCommandListener(this);
textBox = new TextBox("Rozmowa", null, 24000, 0);
textField = new TextField("Wiadomosc", null, 256, 0);
append(textField);
this.bTMIDlet.getDisplay().setCurrent(this);
new Reciver().start();
}
public void commandAction(Command c, Displayable d) {
System.out.println("commandAction");
if(c.getCommandType() == Command.EXIT) {
bTConnection.close();
bTMIDlet.showMenu();
}
if(c.equals(sendCommand)) {
bTConnection.sendText(textField.getString());
append("Ja: " + textField.getString() + "\n");
textField.setString("");
}
}
private class Reciver extends Thread {
public void run() {
while(true) {
String recivedText = bTConnection.reciveText();
if(recivedText == null) {
bTConnection.close();
bTMIDlet.showMenu();
return;
}
append("Ktos: " + recivedText + "\n");
}
}
}
}