summaryrefslogtreecommitdiff
path: root/NET/worlds/console/ChatArea.java
diff options
context:
space:
mode:
authorFuwn <[email protected]>2021-05-03 16:38:41 -0700
committerFuwn <[email protected]>2021-05-03 16:38:41 -0700
commite1e781bb2135ef78592226f1a3eaba4925702f1f (patch)
tree8a5b590463ed413e1c6eabb719130e701b95ca63 /NET/worlds/console/ChatArea.java
downloadworlds.jar-e1e781bb2135ef78592226f1a3eaba4925702f1f.tar.xz
worlds.jar-e1e781bb2135ef78592226f1a3eaba4925702f1f.zip
:star:HEADmain
Diffstat (limited to 'NET/worlds/console/ChatArea.java')
-rw-r--r--NET/worlds/console/ChatArea.java423
1 files changed, 423 insertions, 0 deletions
diff --git a/NET/worlds/console/ChatArea.java b/NET/worlds/console/ChatArea.java
new file mode 100644
index 0000000..658d65f
--- /dev/null
+++ b/NET/worlds/console/ChatArea.java
@@ -0,0 +1,423 @@
+/* */ 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.PrintStream;
+/* */ 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 static final long serialVersionUID = 1L;
+/* */ 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;
+/* */
+/* */ public ChatArea(int rows, int cols, boolean isShared)
+/* */ {
+/* 86 */ this.textArea = new JTextArea(rows, cols);
+/* */
+/* 88 */ this.isShared = isShared;
+/* 89 */ this.textArea.setEditable(false);
+/* */
+/* 91 */ setViewportView(this.textArea);
+/* 92 */ super.setHorizontalScrollBarPolicy(31);
+/* 93 */ super.setVerticalScrollBarPolicy(20);
+/* */ }
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */ public Component getComponent()
+/* */ {
+/* 109 */ return this;
+/* */ }
+/* */
+/* */ public void finalize()
+/* */ {
+/* 114 */ disableLogging();
+/* */ }
+/* */
+/* */
+/* */ public synchronized void validate()
+/* */ {
+/* 120 */ super.validate();
+/* 121 */ String text = this.isShared ? sharedText : this.unsharedText;
+/* 122 */ if (text != null) {
+/* 123 */ this.textArea.replaceRange("", 0, this.textArea.getText().length());
+/* 124 */ this.textArea.append(text);
+/* */ }
+/* */ }
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */ public synchronized void enableLogging(String fileName, String title, boolean append)
+/* */ {
+/* 140 */ if (this.logFile != null)
+/* */ {
+/* 142 */ if (this.logFileName.equals(fileName)) {
+/* 143 */ return;
+/* */ }
+/* 145 */ this.logFile.close();
+/* */ }
+/* */ try {
+/* 148 */ if ((append) && (new File(fileName).exists())) {
+/* 149 */ truncateIfExceeds(fileName, title, 524288L);
+/* 150 */ this.logFile = new PrintWriter(new FileWriter(fileName, true));
+/* */ } else {
+/* 152 */ this.logFile = new PrintWriter(new FileWriter(fileName, false));
+/* 153 */ obsLogFile.setChanged(true);
+/* 154 */ this.logFile.println("<html>");
+/* 155 */ this.logFile.println("<head>");
+/* 156 */ this.logFile.println("<title>" + title + "</title>");
+/* 157 */ this.logFile.println("</head>");
+/* 158 */ this.logFile.println("<body>");
+/* */ }
+/* 160 */ this.logFileName = fileName;
+/* 161 */ this.logFile.println("<hr>");
+/* 162 */ this.logFile.println("<h3>Conversation of " +
+/* 163 */ DateFormat.getDateTimeInstance().format(new Date()) +
+/* 164 */ "</h3>");
+/* */
+/* 166 */ this.logFile.flush();
+/* 167 */ obsLogFile.notifyObservers(this);
+/* */ } catch (IOException ex) {
+/* 169 */ System.out.println("Log file not opened: " + ex);
+/* */ }
+/* */ }
+/* */
+/* */
+/* */
+/* 175 */ private static PublicObservable obsLogFile = new PublicObservable();
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */ public static void addLogObserver(Observer o)
+/* */ {
+/* 183 */ obsLogFile.addObserver(o);
+/* */ }
+/* */
+/* */
+/* */
+/* */ public static void deleteLogObserver(Observer o)
+/* */ {
+/* 190 */ obsLogFile.deleteObserver(o);
+/* */ }
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */ private synchronized void truncateIfExceeds(String fileName, String title, long lengthLimit)
+/* */ {
+/* 203 */ File f = new File(fileName);
+/* 204 */ if (f.length() > lengthLimit) {
+/* 205 */ File tf = new File(fileName + ".temp");
+/* */
+/* */ try
+/* */ {
+/* 209 */ BufferedReader in = new BufferedReader(new FileReader(f));
+/* 210 */ PrintWriter out = new PrintWriter(new FileWriter(tf));
+/* 211 */ out.println("<html>");
+/* 212 */ out.println("<head>");
+/* 213 */ out.println("<title>" + title + "</title>");
+/* 214 */ out.println("</head>");
+/* 215 */ out.println("<body>");
+/* 216 */ in.skip(f.length() - lengthLimit / 2L);
+/* 217 */ String line = in.readLine();
+/* 218 */ line = in.readLine();
+/* 219 */ while (line != null) {
+/* 220 */ out.println(line);
+/* 221 */ line = in.readLine();
+/* */ }
+/* 223 */ in.close();
+/* 224 */ out.close();
+/* */
+/* */
+/* 227 */ f.delete();
+/* 228 */ f = new File(fileName);
+/* 229 */ tf.renameTo(f);
+/* */ } catch (FileNotFoundException ex) {
+/* 231 */ System.out.println("DuplexPart fatal: " + ex);
+/* */ } catch (IOException ex) {
+/* 233 */ System.out.println("DuplexPart: Unable to write, " + ex);
+/* */ }
+/* */ }
+/* */ }
+/* */
+/* */
+/* */
+/* */ public synchronized void disableLogging()
+/* */ {
+/* 242 */ if (this.logFile != null) {
+/* 243 */ this.logFile.close();
+/* 244 */ this.logFile = null;
+/* */ }
+/* */ }
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */ public boolean canAddText()
+/* */ {
+/* 255 */ if (!this.haveFocus) {
+/* 256 */ return true;
+/* */ }
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* 293 */ return false;
+/* */ }
+/* */
+/* */
+/* */
+/* */ private String toHtml(String s)
+/* */ {
+/* 300 */ assert (s != null);
+/* 301 */ String h = "";
+/* 302 */ for (int i = 0; i < s.length(); i++) {
+/* 303 */ char c = s.charAt(i);
+/* 304 */ switch (c) {
+/* */ case '<':
+/* 306 */ h = h + "&lt;";
+/* 307 */ break;
+/* */ case '>':
+/* 309 */ h = h + "&gt;";
+/* 310 */ break;
+/* */ case '"':
+/* 312 */ h = h + "&quot;";
+/* 313 */ break;
+/* */ case '&':
+/* 315 */ h = h + "&amp;";
+/* 316 */ break;
+/* */ default:
+/* 318 */ h = h + c;
+/* */ }
+/* */
+/* */ }
+/* 322 */ return h;
+/* */ }
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */ public synchronized void println(String msg)
+/* */ {
+/* 334 */ if ((this.logFile != null) && (msg != null)) {
+/* 335 */ this.logFile.println(toHtml(msg) + "<br>");
+/* 336 */ this.logFile.flush();
+/* */ }
+/* */
+/* */
+/* */
+/* */
+/* 342 */ if (this.unaddedText == null) {
+/* 343 */ this.unaddedText = msg;
+/* 344 */ } else if (msg != null) {
+/* 345 */ this.unaddedText = (this.unaddedText + "\n" + msg);
+/* */ }
+/* 347 */ if ((this.unaddedText == null) || (!canAddText())) {
+/* 348 */ return;
+/* */ }
+/* 350 */ if (this.textArea.getText().length() == 0) {
+/* 351 */ this.textArea.append(this.unaddedText);
+/* */ } else
+/* 353 */ this.textArea.append("\n" + this.unaddedText);
+/* 354 */ this.unaddedText = null;
+/* */
+/* */
+/* 357 */ String text = this.textArea.getText();
+/* 358 */ if (text.length() > 20000) {
+/* 359 */ int linePos = text.indexOf('\n', 10240);
+/* 360 */ if (linePos >= 0) {
+/* 361 */ text = text.substring(linePos + 1);
+/* */
+/* */
+/* 364 */ linePos = text.lastIndexOf('\n');
+/* 365 */ if (linePos > 0) {
+/* 366 */ this.textArea.setText(text.substring(0, linePos));
+/* 367 */ this.textArea.append(text.substring(linePos));
+/* */ }
+/* */ }
+/* */ }
+/* */
+/* 372 */ if (this.isShared) {
+/* 373 */ sharedText = text;
+/* */ } else {
+/* 375 */ this.unsharedText = text;
+/* */ }
+/* */ }
+/* */
+/* */ public synchronized void scrollToBottom() {
+/* 380 */ String text = this.textArea.getText();
+/* 381 */ int textlen = text.length();
+/* 382 */ this.textArea.select(textlen, textlen);
+/* */ }
+/* */
+/* */
+/* */
+/* */ @Deprecated
+/* */ public synchronized boolean handleEvent(Event event)
+/* */ {
+/* 390 */ if (event.id == 1004) {
+/* 391 */ this.haveFocus = true;
+/* 392 */ } else if (event.id == 1005)
+/* */ {
+/* 394 */ this.haveFocus = false;
+/* */ }
+/* */
+/* 397 */ poll();
+/* */
+/* 399 */ return super.handleEvent(event);
+/* */ }
+/* */
+/* */ public void poll()
+/* */ {
+/* 404 */ if (this.unaddedText != null) {
+/* 405 */ println(null);
+/* */ }
+/* */ }
+/* */
+/* */
+/* */
+/* */
+/* */
+/* */ public boolean isFocusTraversable()
+/* */ {
+/* 415 */ return false;
+/* */ }
+/* */ }
+
+
+/* Location: C:\Program Files (x86)\Worlds Inc\WorldsPlayer - Win7\lib\worlds.jar!\NET\worlds\console\ChatArea.class
+ * Java compiler version: 6 (50.0)
+ * JD-Core Version: 0.7.1
+ */ \ No newline at end of file