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(""); this.logFile.println(""); this.logFile.println("" + title + ""); this.logFile.println(""); this.logFile.println(""); } this.logFileName = fileName; this.logFile.println("
"); this.logFile.println("

Conversation of " + DateFormat.getDateTimeInstance().format(new Date()) + "

"); 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(""); out.println(""); out.println("" + title + ""); out.println(""); out.println(""); 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) + "
"); 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(); } } } } }