diff options
Diffstat (limited to 'NET/worlds/console/NewSharedTextArea.java')
| -rw-r--r-- | NET/worlds/console/NewSharedTextArea.java | 223 |
1 files changed, 223 insertions, 0 deletions
diff --git a/NET/worlds/console/NewSharedTextArea.java b/NET/worlds/console/NewSharedTextArea.java new file mode 100644 index 0000000..581ab3e --- /dev/null +++ b/NET/worlds/console/NewSharedTextArea.java @@ -0,0 +1,223 @@ +package NET.worlds.console; + +import NET.worlds.core.IniFile; +import java.awt.AWTEvent; +import java.awt.Component; +import java.awt.event.FocusEvent; +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 NewSharedTextArea extends GammaTextArea implements SharedTextArea { + private static final long serialVersionUID = 6732411958099438561L; + private static String sharedText; + private boolean isShared; + private String unsharedText; + private String unaddedText = null; + private PrintWriter logFile; + private String logFileName; + private static final long oneMeg = 1048576L; + private static final long logLengthLimit = 524288L; + private static PublicObservable obsLogFile = new PublicObservable(); + private static int autoScrollLimit = IniFile.gamma().getIniInt("AutoScrollLimit", 0); + public static int chatLengthLimit = IniFile.gamma().getIniInt("ChatLengthLimit", 20000); + + public NewSharedTextArea(int rows, int cols, boolean isShared) { + super("", rows, cols, 2); + this.isShared = isShared; + this.setEditable(false); + } + + @Override + public void finalize() { + this.disableLogging(); + } + + @Override + public Component getComponent() { + return this; + } + + @Override + public synchronized void validate() { + super.validate(); + String text = this.isShared ? sharedText : this.unsharedText; + if (text != null) { + this.setText(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 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() { + if (this.getText().length() < 3) { + return true; + } else { + return autoScrollLimit > 0 && autoScrollLimit < this.unaddedText.length() ? true : this.isLastLineVisible(); + } + } + + @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; + } + + this.repaint(); + } + } + + @Override + public synchronized void scrollToBottom() { + String text = this.getText(); + this.setText(""); + this.append(text); + this.repaint(); + } + + @Override + protected void processFocusEvent(FocusEvent e) { + super.processFocusEvent(e); + } + + @Override + protected void processEvent(AWTEvent e) { + this.poll(); + super.processEvent(e); + } + + @Override + public synchronized void poll() { + if (this.unaddedText != null) { + this.println(null); + } + } + + @Override + public boolean isFocusTraversable() { + return false; + } +} |