Listing 1.
public void init() {
super.init();
chatLogFileName = getParameter("logfile")
!= null ? getParameter("logfile") :"chatlog";
cgiPath = getParameter("cgipath") != null ? getParameter("cgipath") : "cgi-bin/";
chatScreen = new ChatPanel(this,chatLogFileName);
setLayout(new BorderLayout());
add("Center",chatScreen);
}
Listing 2.
public class ChatPanel extends Panel implements
Runnable{
ChatPanel(Applet app,String fileName) {
super();
this.app = app;
chatFileName = fileName;
//Build the Setup Panel
Panel setup = new Panel();
Panel setupNorth = new Panel();
Panel setupCenter = new Panel();
Panel setupSouth = new Panel();
setup.setBackground(Color.cyan);
setupButton = new Button("Enter Chat Room");
nameField = new TextField(20);
nameField.setBackground(Color.white);
setupCenter.add(new Label("Enter Name:"));
setupCenter.add(nameField);
setupCenter.add(setupButton);
setupNorth.setFont(new Font("TimesRoman",Font.BOLD,36));
setupNorth.add(new Label("Welcome to Chat"));
setupSouth.setFont(new Font("TimesRoman",Font.BOLD,36));
setupSouth.add(new Label("You Must Enter A Chat Name"));
setup.setLayout(new BorderLayout());
setup.add("North", setupNorth);
setup.add("Center", setupCenter);
setup.add("South", setupSouth);
//Build the Chat Panel
Panel chat = new Panel();
chat.setBackground(Color.black);
chat.setLayout(new BorderLayout(10,10));
northPanel = new Panel();
southPanel = new Panel();
northPanel.setBackground(Color.cyan);
northPanel.setFont(new Font("TimesRoman", Font.BOLD,24));
northPanel.add(new Label("Joseph DiBella's Cyber-Chatterbox"));
chat.add("North", northPanel);
chatArea = new TextArea("Welcome to Joe DiBella\'s Chat Applet\n\n");
chatArea.setEditable(false);
chatArea.setBackground(Color.cyan);
chatArea.setForeground(Color.black);
chatArea.setFont(new Font("Helvetica", Font.BOLD,14));
chat.add("Center",chatArea);
chatLine = new TextField();
chatLine.setBackground(Color.white);
sendButton = new Button("Send");
southPanel.setLayout(new BorderLayout(5,5));
southPanel.add("Center",chatLine);
southPanel.add("East", sendButton);
chat.add("South", southPanel);
clayout=new CardLayout();
setLayout(clayout);
add("Setup", setup);
add("Chat", chat);
}
Listing 3
public void start(){
if(identity != null){
new SubmitToChatServer("*** "+identity+" Has Entered This Chat Room. ***",app,chatFileName).start();
if(runner == null){
runner=new Thread(this);
runner.start();
}
}else{
clayout.show(this,"Setup");
nameField.requestFocus();
}
}
Listing 4.
public void run(){
chatLine.requestFocus();
// Figure out line length in pixels
FontMetrics fm = chatArea.getFontMetrics(chatArea.getFont());
aveChar = fm.stringWidth("ABCDEFGHIJKLMNOPQRSTUVWXYZ
abcdefghijklmnopqrstuvwxyz
.. .. ")/62;
aveCharsPerLine = this.size().width/aveChar;
// Start the chat engine
while(true){
refresh();
try{
Thread.sleep(refreshTime);
}catch(InterruptedException e){}
}
}
Listing 5.
void refresh(){
try{
log = new URL(app.getDocumentBase(),chatFileName+".log");
DataInputStream din = new DataInputStream(log.openStream());
String tempLastLine=lastLine;
String in="";
String chatData = "";
boolean first = true;
boolean finished = false;
while((in=din.readLine())!=null){
if(in.startsWith(lastLine)){
finished = true;
}
if(!finished){
if(first){
tempLastLine=in;
first=false;
}
chatData = processText(in)+chatData;
}
}
chatArea.appendText(chatData);
lastLine=tempLastLine;
din.close();
}catch(Exception e){
System.out.println("Error in refresh:" + e.toString());
}
// Force the Text Area to scroll to the end
chatArea.select(chatArea.getText().
length(),chatArea.getText().length());
}
Listing 6.
String processText(String
text){
/* This method is responsible for word wrapping in the text area. If a
line is longer than
the width of the Text area, we will insert a new-line chatacter in place
of a space */
int textLength = text.length();
if(textLength <= aveCharsPerLine){
return text+"\n\n";
}
// We will need the name of the person if we need to wrap. This extracts
that info
String chatName = text.substring(0,text.indexOf(">:")+2);
String processedText = "";
while(text.length() > aveCharsPerLine){
int i = text.lastIndexOf((int)' ',aveCharsPerLine);
try{
processedText += text.substring(0,i)+"\n";
text = chatName + text.substring(i);
}catch(Exception e){/* code for exception*/
System.out.println("Error processing text:" + e.toString());
}
}
return processedText+text+"\n\n";
}
Listing 7.
public boolean handleEvent(Event
event) {
if (event.id == Event.ACTION_EVENT && (event.target == sendButton||event.target
== chatLine)) {
sendChat();
chatLine.requestFocus();
return true;
}
if (event.id == Event.ACTION_EVENT && (event.target == setupButton||event.target
== nameField)){
if(nameField.getText().equals("")){
app.showStatus("You must enter a name");
nameField.requestFocus();
}else{
app.showStatus("Entering Chat Room");
setIdentity(nameField.getText());
clayout.show(this,"Chat");
start();
}
return true;
}
return super.handleEvent(event);
}
Listing 8.
public void setRefreshTime(int
i){
refreshTime = i;
}
public void setIdentity(String
i){
identity = i;
}
public String getIdentity(){
return identity;
}
public void setChatLogName(String
i){
chatFileName = i;
}
//{{DECLARE_CONTROLS
private TextArea chatArea;
private TextField chatLine;
private Panel northPanel,
southPanel;
private Button sendButton;
private Applet app;
private int refreshTime
= 10000;
private int aveChar, aveCharsPerLine,
chatSize;
private String identity
= null;
private String chatFileName
= "chatlog";
private URL log;
private String lastLine="
";
private Thread runner;
private Button setupButton;
private TextField nameField;
private CardLayout clayout;
//}}
}