|
@@ -0,0 +1,157 @@
|
|
1
|
+package kr.co.swh.lecture.opensource.project.discode.music.gui;
|
|
2
|
+
|
|
3
|
+/**
|
|
4
|
+ * <pre>
|
|
5
|
+ * kr.co.swh.lecture.opensource.project.discode.music.gui
|
|
6
|
+ * TextAreaOutputStream.java
|
|
7
|
+ *
|
|
8
|
+ * 설명 :
|
|
9
|
+ * </pre>
|
|
10
|
+ *
|
|
11
|
+ * @since : 2021. 5. 30.
|
|
12
|
+ * @author : tobby48
|
|
13
|
+ * @version : v1.0
|
|
14
|
+ */
|
|
15
|
+import java.awt.EventQueue;
|
|
16
|
+import java.io.OutputStream;
|
|
17
|
+import java.io.UnsupportedEncodingException;
|
|
18
|
+import java.util.ArrayList;
|
|
19
|
+import java.util.LinkedList;
|
|
20
|
+import java.util.List;
|
|
21
|
+
|
|
22
|
+import javax.swing.JTextArea;
|
|
23
|
+/**
|
|
24
|
+ *
|
|
25
|
+ * @author Lawrence Dol
|
|
26
|
+ */
|
|
27
|
+public class TextAreaOutputStream extends OutputStream {
|
|
28
|
+
|
|
29
|
+// *************************************************************************************************
|
|
30
|
+// INSTANCE MEMBERS
|
|
31
|
+// *************************************************************************************************
|
|
32
|
+
|
|
33
|
+private byte[] oneByte; // array for write(int val);
|
|
34
|
+private Appender appender; // most recent action
|
|
35
|
+
|
|
36
|
+public TextAreaOutputStream(JTextArea txtara) {
|
|
37
|
+ this(txtara,1000);
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+public TextAreaOutputStream(JTextArea txtara, int maxlin) {
|
|
41
|
+ if(maxlin<1) { throw new IllegalArgumentException("TextAreaOutputStream maximum lines must be positive (value="+maxlin+")"); }
|
|
42
|
+ oneByte=new byte[1];
|
|
43
|
+ appender=new Appender(txtara,maxlin);
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+/** Clear the current console text area. */
|
|
47
|
+public synchronized void clear() {
|
|
48
|
+ if(appender!=null) { appender.clear(); }
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+@Override
|
|
52
|
+public synchronized void close() {
|
|
53
|
+ appender=null;
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+@Override
|
|
57
|
+public synchronized void flush() {
|
|
58
|
+ /* empty */
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+@Override
|
|
62
|
+public synchronized void write(int val) {
|
|
63
|
+ oneByte[0]=(byte)val;
|
|
64
|
+ write(oneByte,0,1);
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+@Override
|
|
68
|
+public synchronized void write(byte[] ba) {
|
|
69
|
+ write(ba,0,ba.length);
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+@Override
|
|
73
|
+public synchronized void write(byte[] ba,int str,int len) {
|
|
74
|
+ if(appender!=null) { appender.append(bytesToString(ba,str,len)); }
|
|
75
|
+ }
|
|
76
|
+
|
|
77
|
+//@edu.umd.cs.findbugs.annotations.SuppressWarnings("DM_DEFAULT_ENCODING")
|
|
78
|
+static private String bytesToString(byte[] ba, int str, int len) {
|
|
79
|
+ try {
|
|
80
|
+ return new String(ba,str,len,"UTF-8");
|
|
81
|
+ } catch(UnsupportedEncodingException thr) {
|
|
82
|
+ return new String(ba,str,len);
|
|
83
|
+ } // all JVMs are required to support UTF-8
|
|
84
|
+ }
|
|
85
|
+
|
|
86
|
+// *************************************************************************************************
|
|
87
|
+// STATIC MEMBERS
|
|
88
|
+// *************************************************************************************************
|
|
89
|
+
|
|
90
|
+ static class Appender
|
|
91
|
+ implements Runnable
|
|
92
|
+ {
|
|
93
|
+ static private final String EOL1="\n";
|
|
94
|
+ static private final String EOL2=System.getProperty("line.separator",EOL1);
|
|
95
|
+
|
|
96
|
+ private final JTextArea textArea;
|
|
97
|
+ private final int maxLines; // maximum lines allowed in text area
|
|
98
|
+ private final LinkedList<Integer> lengths; // length of lines within text area
|
|
99
|
+ private final List<String> values; // values waiting to be appended
|
|
100
|
+
|
|
101
|
+ private int curLength; // length of current line
|
|
102
|
+ private boolean clear;
|
|
103
|
+ private boolean queue;
|
|
104
|
+
|
|
105
|
+ Appender(JTextArea txtara, int maxlin) {
|
|
106
|
+ textArea =txtara;
|
|
107
|
+ maxLines =maxlin;
|
|
108
|
+ lengths =new LinkedList<>();
|
|
109
|
+ values =new ArrayList<>();
|
|
110
|
+
|
|
111
|
+ curLength=0;
|
|
112
|
+ clear =false;
|
|
113
|
+ queue =true;
|
|
114
|
+ }
|
|
115
|
+
|
|
116
|
+ private synchronized void append(String val) {
|
|
117
|
+ values.add(val);
|
|
118
|
+ if(queue) {
|
|
119
|
+ queue=false;
|
|
120
|
+ EventQueue.invokeLater(this);
|
|
121
|
+ }
|
|
122
|
+ }
|
|
123
|
+
|
|
124
|
+ private synchronized void clear() {
|
|
125
|
+ clear=true;
|
|
126
|
+ curLength=0;
|
|
127
|
+ lengths.clear();
|
|
128
|
+ values.clear();
|
|
129
|
+ if(queue) {
|
|
130
|
+ queue=false;
|
|
131
|
+ EventQueue.invokeLater(this);
|
|
132
|
+ }
|
|
133
|
+ }
|
|
134
|
+
|
|
135
|
+ // MUST BE THE ONLY METHOD THAT TOUCHES textArea!
|
|
136
|
+ @Override
|
|
137
|
+ public synchronized void run() {
|
|
138
|
+ if(clear) { textArea.setText(""); }
|
|
139
|
+ values.stream().map((val) -> {
|
|
140
|
+ curLength+=val.length();
|
|
141
|
+ return val;
|
|
142
|
+ }).map((val) -> {
|
|
143
|
+ if(val.endsWith(EOL1) || val.endsWith(EOL2)) {
|
|
144
|
+ if(lengths.size()>=maxLines) { textArea.replaceRange("",0,lengths.removeFirst()); }
|
|
145
|
+ lengths.addLast(curLength);
|
|
146
|
+ curLength=0;
|
|
147
|
+ }
|
|
148
|
+ return val;
|
|
149
|
+ }).forEach((val) -> {
|
|
150
|
+ textArea.append(val);
|
|
151
|
+ });
|
|
152
|
+ values.clear();
|
|
153
|
+ clear =false;
|
|
154
|
+ queue =true;
|
|
155
|
+ }
|
|
156
|
+ }
|
|
157
|
+} /* END PUBLIC CLASS */
|