summaryrefslogtreecommitdiff
path: root/NET/worlds/console/ClassicSharedTextArea.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/ClassicSharedTextArea.java
downloadworldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.tar.xz
worldsplayer-c7a9d4a6bd53ed7d61731770f2f10e8b9fd435f9.zip
Initial commit
Diffstat (limited to 'NET/worlds/console/ClassicSharedTextArea.java')
-rw-r--r--NET/worlds/console/ClassicSharedTextArea.java258
1 files changed, 258 insertions, 0 deletions
diff --git a/NET/worlds/console/ClassicSharedTextArea.java b/NET/worlds/console/ClassicSharedTextArea.java
new file mode 100644
index 0000000..e571c5a
--- /dev/null
+++ b/NET/worlds/console/ClassicSharedTextArea.java
@@ -0,0 +1,258 @@
+package NET.worlds.console;
+
+import NET.worlds.core.IniFile;
+import java.awt.Color;
+import java.awt.Component;
+import java.awt.Event;
+import java.awt.FontMetrics;
+import java.awt.Graphics;
+import java.awt.Image;
+import java.awt.Point;
+import java.awt.Rectangle;
+import java.awt.TextArea;
+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;
+
+class ClassicSharedTextArea extends TextArea implements SharedTextArea {
+ private static final long serialVersionUID = -3315186117230082655L;
+ private static String sharedText;
+ private boolean isShared;
+ private String unsharedText;
+ private String unaddedText;
+ private boolean haveFocus;
+ private int hwnd = 0;
+ 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 static int chatLengthLimit = IniFile.gamma().getIniInt("ChatLengthLimit", 20000);
+
+ public ClassicSharedTextArea(int rows, int cols, boolean isShared) {
+ super("", rows, cols, 1);
+ this.isShared = isShared;
+ this.setEditable(false);
+ }
+
+ @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.replaceRange("", 0, this.getText().length());
+ this.append(text);
+ }
+
+ this.hwnd = 0;
+ }
+
+ @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 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 ? true : true;
+ }
+
+ @Override
+ public synchronized void println(String msg) {
+ if (this.logFile != null && msg != null) {
+ this.logFile.println(DuplexPart.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.getText().length() == 0) {
+ this.append(this.unaddedText);
+ } else {
+ this.append("\n" + this.unaddedText);
+ }
+
+ this.unaddedText = null;
+ String text = this.getText();
+ if (text.length() > chatLengthLimit) {
+ int linePos = text.indexOf(10, chatLengthLimit / 2 - 80);
+ if (linePos >= 0) {
+ text = text.substring(linePos + 1);
+ linePos = text.lastIndexOf(10);
+ if (linePos > 0) {
+ this.setText(text.substring(0, linePos));
+ this.append(text.substring(linePos));
+ }
+ }
+ }
+
+ if (this.isShared) {
+ sharedText = text;
+ } else {
+ this.unsharedText = text;
+ }
+ }
+ }
+
+ @Override
+ public synchronized void scrollToBottom() {
+ String text = this.getText();
+ int textlen = text.length();
+ this.select(textlen, textlen);
+ }
+
+ @Override
+ public 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;
+ }
+
+ @Override
+ public synchronized void paint(Graphics g) {
+ String[] fulltext = this.getText().split("\n");
+ Rectangle r = this.getBounds();
+ if (r.height >= 0 && r.width >= 0) {
+ Point offset = new Point(0, 0);
+ Image offImage = this.createImage(r.width, r.height);
+ Graphics offGraphic = offImage.getGraphics();
+ offGraphic.setColor(GammaTextArea.getBackgroundColor());
+ offGraphic.fillRect(r.x, r.y, r.width, r.height);
+ offGraphic.setColor(Color.black);
+ offGraphic.setFont(this.getFont());
+ FontMetrics fm = offGraphic.getFontMetrics(this.getFont());
+ int windowY = offset.y;
+ int curY = fm.getHeight();
+ int curLine = 0;
+ int curPos = 0;
+ int mode = -1;
+ System.out
+ .println("paint (" + r.x + "," + r.y + "):" + r.height + "," + r.width + " Offset=" + offset.x + "," + offset.y + " lines=" + fulltext.length);
+ System.out.println("curLine=" + curLine + " curY=" + curY);
+
+ for (int i = curLine; i < fulltext.length && curY <= windowY + r.height; i++) {
+ if (mode != 0) {
+ offGraphic.drawString(fulltext[i], offset.x, curY);
+ curY += fm.getHeight();
+ }
+ }
+ }
+ }
+}