summaryrefslogtreecommitdiff
path: root/NET/worlds/console/ChatArea.java
diff options
context:
space:
mode:
authorFuwn <[email protected]>2026-02-12 22:33:32 -0800
committerFuwn <[email protected]>2026-02-12 22:33:32 -0800
commitc7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9 (patch)
treedf9f48bf128a6c0186a8e91857d6ff30fe0e9f18 /NET/worlds/console/ChatArea.java
downloadworldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.tar.xz
worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.zip
Initial commit
Diffstat (limited to 'NET/worlds/console/ChatArea.java')
-rw-r--r--NET/worlds/console/ChatArea.java248
1 files changed, 248 insertions, 0 deletions
diff --git a/NET/worlds/console/ChatArea.java b/NET/worlds/console/ChatArea.java
new file mode 100644
index 0000000..a25f1e6
--- /dev/null
+++ b/NET/worlds/console/ChatArea.java
@@ -0,0 +1,248 @@
+package NET.worlds.console;
+
+import java.awt.Component;
+import java.awt.Event;
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.text.DateFormat;
+import java.util.Date;
+import java.util.Observer;
+import javax.swing.JScrollPane;
+import javax.swing.JTextArea;
+
+public class ChatArea extends JScrollPane implements SharedTextArea {
+ private JTextArea textArea;
+ private static String sharedText;
+ private boolean isShared;
+ private String unsharedText;
+ private String unaddedText;
+ private boolean haveFocus;
+ private PrintWriter logFile;
+ private String logFileName;
+ private static final long oneMeg = 1048576L;
+ private static final long logLengthLimit = 524288L;
+ private static PublicObservable obsLogFile = new PublicObservable();
+
+ public ChatArea(int rows, int cols, boolean isShared) {
+ this.textArea = new JTextArea(rows, cols);
+ this.isShared = isShared;
+ this.textArea.setEditable(false);
+ this.setViewportView(this.textArea);
+ super.setHorizontalScrollBarPolicy(31);
+ super.setVerticalScrollBarPolicy(20);
+ }
+
+ @Override
+ public Component getComponent() {
+ return this;
+ }
+
+ @Override
+ public void finalize() {
+ this.disableLogging();
+ }
+
+ @Override
+ public synchronized void validate() {
+ super.validate();
+ String text = this.isShared ? sharedText : this.unsharedText;
+ if (text != null) {
+ this.textArea.replaceRange("", 0, this.textArea.getText().length());
+ this.textArea.append(text);
+ }
+ }
+
+ @Override
+ public synchronized void enableLogging(String fileName, String title, boolean append) {
+ if (this.logFile != null) {
+ if (this.logFileName.equals(fileName)) {
+ return;
+ }
+
+ this.logFile.close();
+ }
+
+ try {
+ if (append && new File(fileName).exists()) {
+ this.truncateIfExceeds(fileName, title, 524288L);
+ this.logFile = new PrintWriter(new FileWriter(fileName, true));
+ } else {
+ this.logFile = new PrintWriter(new FileWriter(fileName, false));
+ obsLogFile.setChanged(true);
+ this.logFile.println("<html>");
+ this.logFile.println("<head>");
+ this.logFile.println("<title>" + title + "</title>");
+ this.logFile.println("</head>");
+ this.logFile.println("<body>");
+ }
+
+ this.logFileName = fileName;
+ this.logFile.println("<hr>");
+ this.logFile.println("<h3>Conversation of " + DateFormat.getDateTimeInstance().format(new Date()) + "</h3>");
+ this.logFile.flush();
+ obsLogFile.notifyObservers(this);
+ } catch (IOException var5) {
+ System.out.println("Log file not opened: " + var5);
+ }
+ }
+
+ public static void addLogObserver(Observer o) {
+ obsLogFile.addObserver(o);
+ }
+
+ public static void deleteLogObserver(Observer o) {
+ obsLogFile.deleteObserver(o);
+ }
+
+ private synchronized void truncateIfExceeds(String fileName, String title, long lengthLimit) {
+ File f = new File(fileName);
+ if (f.length() > lengthLimit) {
+ File tf = new File(fileName + ".temp");
+
+ try {
+ BufferedReader in = new BufferedReader(new FileReader(f));
+ PrintWriter out = new PrintWriter(new FileWriter(tf));
+ out.println("<html>");
+ out.println("<head>");
+ out.println("<title>" + title + "</title>");
+ out.println("</head>");
+ out.println("<body>");
+ in.skip(f.length() - lengthLimit / 2L);
+ String line = in.readLine();
+
+ for (String var13 = in.readLine(); var13 != null; var13 = in.readLine()) {
+ out.println(var13);
+ }
+
+ in.close();
+ out.close();
+ f.delete();
+ f = new File(fileName);
+ tf.renameTo(f);
+ } catch (FileNotFoundException var10) {
+ System.out.println("DuplexPart fatal: " + var10);
+ } catch (IOException var11) {
+ System.out.println("DuplexPart: Unable to write, " + var11);
+ }
+ }
+ }
+
+ @Override
+ public synchronized void disableLogging() {
+ if (this.logFile != null) {
+ this.logFile.close();
+ this.logFile = null;
+ }
+ }
+
+ @Override
+ public boolean canAddText() {
+ return !this.haveFocus;
+ }
+
+ private String toHtml(String s) {
+ assert s != null;
+
+ String h = "";
+
+ for (int i = 0; i < s.length(); i++) {
+ char c = s.charAt(i);
+ switch (c) {
+ case '"':
+ h = h + "&quot;";
+ break;
+ case '&':
+ h = h + "&amp;";
+ break;
+ case '<':
+ h = h + "&lt;";
+ break;
+ case '>':
+ h = h + "&gt;";
+ break;
+ default:
+ h = h + c;
+ }
+ }
+
+ return h;
+ }
+
+ @Override
+ public synchronized void println(String msg) {
+ if (this.logFile != null && msg != null) {
+ this.logFile.println(this.toHtml(msg) + "<br>");
+ this.logFile.flush();
+ }
+
+ if (this.unaddedText == null) {
+ this.unaddedText = msg;
+ } else if (msg != null) {
+ this.unaddedText = this.unaddedText + "\n" + msg;
+ }
+
+ if (this.unaddedText != null && this.canAddText()) {
+ if (this.textArea.getText().length() == 0) {
+ this.textArea.append(this.unaddedText);
+ } else {
+ this.textArea.append("\n" + this.unaddedText);
+ }
+
+ this.unaddedText = null;
+ String text = this.textArea.getText();
+ if (text.length() > 20000) {
+ int linePos = text.indexOf(10, 10240);
+ if (linePos >= 0) {
+ text = text.substring(linePos + 1);
+ linePos = text.lastIndexOf(10);
+ if (linePos > 0) {
+ this.textArea.setText(text.substring(0, linePos));
+ this.textArea.append(text.substring(linePos));
+ }
+ }
+ }
+
+ if (this.isShared) {
+ sharedText = text;
+ } else {
+ this.unsharedText = text;
+ }
+ }
+ }
+
+ @Override
+ public synchronized void scrollToBottom() {
+ String text = this.textArea.getText();
+ int textlen = text.length();
+ this.textArea.select(textlen, textlen);
+ }
+
+ @Override
+ public synchronized boolean handleEvent(Event event) {
+ if (event.id == 1004) {
+ this.haveFocus = true;
+ } else if (event.id == 1005) {
+ this.haveFocus = false;
+ }
+
+ this.poll();
+ return super.handleEvent(event);
+ }
+
+ @Override
+ public void poll() {
+ if (this.unaddedText != null) {
+ this.println(null);
+ }
+ }
+
+ @Override
+ public boolean isFocusTraversable() {
+ return false;
+ }
+}