summaryrefslogtreecommitdiff
path: root/NET/worlds/console/NewSharedTextArea.java
blob: 581ab3e4f6329ae22c6477f703ba8f25cf86013f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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;
   }
}