1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
|
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.xml.parsers.*;
import java.io.*;
import java.net.*;
import org.xml.sax.*;
import org.w3c.dom.*;
public class Applet
extends JApplet
implements ActionListener, EventStreamReader.EventStreamReaderI {
protected JTextField textField;
protected JTextArea textArea;
protected JComboBox channelBox;
protected JScrollPane scrollPane;
protected EventStreamReader streamReader;
protected StringBuffer data = new StringBuffer();
protected DocumentBuilder builder;
public void init() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
builder = factory.newDocumentBuilder();
System.err.println("CreateDOC" + factory.getClass().getName());
} catch (Exception e) {
System.err.println("CreateDOC" + e.getMessage());
}
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
startSteam();
}
});
} catch (Exception e) {
System.err.println("CreateGUI" + e.getMessage());
}
}
public void destroy() {
if (streamReader != null) streamReader.close();
}
private void createGUI() {
Container pane = getContentPane();
pane.setLayout(new BorderLayout());
textField = new JTextField(30);
textField.addActionListener(this);
textArea = new JTextArea(5, 30);
textArea.setEditable(false);
channelBox = new JComboBox();
scrollPane = new JScrollPane(textArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
Panel end = new Panel();
end.setLayout(new BorderLayout());
end.add(textField, BorderLayout.CENTER);
end.add(channelBox, BorderLayout.LINE_END);
pane.add(scrollPane, BorderLayout.CENTER);
pane.add(end, BorderLayout.PAGE_END);
}
private void startSteam() {
String sid = getParameter("sid");
if (sid == null) return;
String host = getDocumentBase().getHost();
int port = getDocumentBase().getPort();
String url = "/appletstream?sid=" + sid;
streamReader = new EventStreamReader(this, host, port, url);
streamReader.start();
}
public void handleData(String s) {
// Add more data
data.append(s);
// Data items are seperated by newlines
int index;
while ((index = data.indexOf("\n")) != -1) {
// Get the current line
String current = data.substring(0, index);
handleXML(current);
// Remove this from the data
data.delete(0, index + 1);
}
}
private String getXMLString(Node n) {
StringBuffer result = new StringBuffer();
NodeList children = n.getChildNodes();
for (int c=0; c<children.getLength(); c++) {
Node child = children.item(c);
result.append(child.getNodeValue());
}
return result.toString();
}
private void handleXML(String s) {
try {
Document document = builder.parse(new StringBufferInputStream(s));
NodeList children = document.getChildNodes();
for (int c=0; c<children.getLength(); c++) {
Node child = children.item(c);
if (child.getNodeName().equals("chat")) {
addText(getXMLString(child)+"\n");
} else if (child.getNodeName().equals("addchannel")) {
addChannel(getXMLString(child));
}
}
} catch (Exception spe) {
}
}
private void addText(String text) {
// Add some more text to the display
// Make sure the scrollbar points to the end
final String str = text;
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
JScrollBar scrollBar = scrollPane.getVerticalScrollBar();
boolean follow = (scrollBar.getMaximum() == scrollBar.getValue());
textArea.append(str);
if (follow) scrollBar.setValue(scrollBar.getMaximum());
}
});
} catch (Exception e) {
System.err.println("addText" + e.getMessage());
}
}
private void addChannel(String channel) {
final String cha = channel;
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
channelBox.addItem(cha);
channelBox.setSelectedItem("general");
}
});
} catch (Exception e) {
System.err.println("addText" + e.getMessage());
}
}
public void actionPerformed(ActionEvent evt) {
// User has hit return
String text = textField.getText();
textField.setText("");
// Send text
String sid = getParameter("sid");
if (sid == null) return;
String host = getDocumentBase().getHost();
int port = getDocumentBase().getPort();
String channel = channelBox.getSelectedItem().toString();
try {
text = URLEncoder.encode(text, "UTF-8");
} catch (Exception ex) {
}
String url = "/action?sid=" + sid + "&action=chat&text=" + text + "&channel=" + channel;
new EventStreamReader(this, host, port, url).start();
}
}
|