FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
processing/app/src/processing/app/EditorConsole.java at master · botbit/processing · GitHub
botbit
processing
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
processing
/
app
/
src
/
processing
/
app
/
EditorConsole.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
464 lines (378 loc) · 16 KB
Breadcrumbs
processing
/
app
/
src
/
processing
/
app
/
EditorConsole.java
Copy path
File metadata and controls
464 lines (378 loc) · 16 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2004-10 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package
processing
.
app
;
import
java
.
awt
.*;
import
java
.
awt
.
event
.*;
import
java
.
io
.*;
import
javax
.
swing
.*;
import
javax
.
swing
.
text
.*;
import
processing
.
core
.
PApplet
;
import
java
.
text
.
SimpleDateFormat
;
import
java
.
util
.*;
/**
* Message console that sits below the editing area.
* <p />
* Be careful when debugging this class, because if it's throwing exceptions,
* don't take over System.err, and debug while watching just System.out
* or just call println() or whatever directly to systemOut or systemErr.
* <p />
* Also note that encodings will not work properly when run from Eclipse. This
* means that if you use non-ASCII characters in a println() or some such,
* the characters won't print properly in the Processing and/or Eclipse console.
* It seems that Eclipse's console-grabbing and that of Processing don't
* get along with one another. Use 'ant run' to work on encoding-related issues.
*/
public
class
EditorConsole
extends
JScrollPane
{
Editor
editor
;
JTextPane
consoleTextPane
;
BufferedStyledDocument
consoleDoc
;
MutableAttributeSet
stdStyle
;
MutableAttributeSet
errStyle
;
int
maxLineCount
;
PrintStream
sketchOut
;
PrintStream
sketchErr
;
// Single static instance shared because there's only one real System.out.
// Within the input handlers, the currentConsole variable will be used to
// echo things to the correct location.
static
PrintStream
systemOut
;
static
PrintStream
systemErr
;
static
PrintStream
consoleOut
;
static
PrintStream
consoleErr
;
static
OutputStream
stdoutFile
;
static
OutputStream
stderrFile
;
static
EditorConsole
currentConsole
;
// For 0185, moved the first init to this static { } block, so that we never
// have a situation that causes systemOut/Err to not be set properly.
static
{
systemOut
=
System
.
out
;
systemErr
=
System
.
err
;
// placing everything inside a try block because this can be a dangerous
// time for the lights to blink out and crash for and obscure reason.
try
{
// Create output files that will have a randomized name. Has to
// be randomized otherwise another instance of Processing (or one of its
// sister IDEs) might collide with the file causing permissions problems.
// The files and folders are not deleted on exit because they may be
// needed for debugging or bug reporting.
SimpleDateFormat
formatter
=
new
SimpleDateFormat
(
"yyMMdd"
);
String
randy
=
PApplet
.
nf
((
int
) (
1000
*
Math
.
random
()),
4
);
String
stamp
=
formatter
.
format
(
new
Date
()) +
"_"
+
randy
;
File
consoleDir
=
Base
.
getSettingsFile
(
"console"
);
consoleDir
.
mkdirs
();
File
outFile
=
new
File
(
consoleDir
,
stamp
+
".out"
);
stdoutFile
=
new
FileOutputStream
(
outFile
);
File
errFile
=
new
File
(
consoleDir
,
stamp
+
".err"
);
stderrFile
=
new
FileOutputStream
(
errFile
);
consoleOut
=
new
PrintStream
(
new
EditorConsoleStream
(
false
,
null
));
consoleErr
=
new
PrintStream
(
new
EditorConsoleStream
(
true
,
null
));
System
.
setOut
(
consoleOut
);
System
.
setErr
(
consoleErr
);
// } catch (Exception e) {
// stdoutFile = null;
// stderrFile = null;
//
// e.printStackTrace();
// Base.showWarning("Console Error",
// "A problem occurred while trying to open the\n" +
// "files used to store the console output.", e);
}
catch
(
Exception
e
) {
stdoutFile
=
null
;
stderrFile
=
null
;
consoleOut
=
null
;
consoleErr
=
null
;
System
.
setOut
(
systemOut
);
System
.
setErr
(
systemErr
);
e
.
printStackTrace
(
systemErr
);
}
}
public
EditorConsole
(
Editor
editor
) {
this
.
editor
=
editor
;
maxLineCount
=
Preferences
.
getInteger
(
"console.length"
);
consoleDoc
=
new
BufferedStyledDocument
(
10000
,
maxLineCount
);
consoleTextPane
=
new
JTextPane
(
consoleDoc
);
consoleTextPane
.
setEditable
(
false
);
updateMode
();
// add the jtextpane to this scrollpane
this
.
setViewportView
(
consoleTextPane
);
// to fix ugliness.. normally macosx java 1.3 puts an
// ugly white border around this object, so turn it off.
if
(
Base
.
isMacOS
()) {
setBorder
(
null
);
}
sketchOut
=
new
PrintStream
(
new
EditorConsoleStream
(
false
,
this
));
sketchErr
=
new
PrintStream
(
new
EditorConsoleStream
(
true
,
this
));
// periodically post buffered messages to the console
// should the interval come from the preferences file?
new
javax
.
swing
.
Timer
(
250
,
new
ActionListener
() {
public
void
actionPerformed
(
ActionEvent
evt
) {
// only if new text has been added
if
(
consoleDoc
.
hasAppendage
) {
// insert the text that's been added in the meantime
consoleDoc
.
insertAll
();
// always move to the end of the text as it's added
consoleTextPane
.
setCaretPosition
(
consoleDoc
.
getLength
());
}
}
}).
start
();
}
public
PrintStream
getOut
() {
return
sketchOut
;
}
public
PrintStream
getErr
() {
return
sketchErr
;
}
/**
* Update the font family and sizes based on the Preferences window.
*/
protected
void
updateAppearance
() {
String
fontFamily
=
Preferences
.
get
(
"editor.font.family"
);
int
fontSize
=
Preferences
.
getInteger
(
"console.font.size"
);
StyleConstants
.
setFontFamily
(
stdStyle
,
fontFamily
);
StyleConstants
.
setFontSize
(
stdStyle
,
fontSize
);
StyleConstants
.
setFontFamily
(
errStyle
,
fontFamily
);
StyleConstants
.
setFontSize
(
errStyle
,
fontSize
);
clear
();
// otherwise we'll have mixed fonts
}
/**
* Change coloring, fonts, etc in response to a mode change.
*/
protected
void
updateMode
() {
Mode
mode
=
editor
.
getMode
();
// necessary?
MutableAttributeSet
standard
=
new
SimpleAttributeSet
();
StyleConstants
.
setAlignment
(
standard
,
StyleConstants
.
ALIGN_LEFT
);
consoleDoc
.
setParagraphAttributes
(
0
,
0
,
standard
,
true
);
Font
font
=
Preferences
.
getFont
(
"console.font"
);
// build styles for different types of console output
Color
bgColor
=
mode
.
getColor
(
"console.color"
);
Color
fgColorOut
=
mode
.
getColor
(
"console.output.color"
);
Color
fgColorErr
=
mode
.
getColor
(
"console.error.color"
);
stdStyle
=
new
SimpleAttributeSet
();
StyleConstants
.
setForeground
(
stdStyle
,
fgColorOut
);
StyleConstants
.
setBackground
(
stdStyle
,
bgColor
);
StyleConstants
.
setFontSize
(
stdStyle
,
font
.
getSize
());
StyleConstants
.
setFontFamily
(
stdStyle
,
font
.
getFamily
());
StyleConstants
.
setBold
(
stdStyle
,
font
.
isBold
());
StyleConstants
.
setItalic
(
stdStyle
,
font
.
isItalic
());
errStyle
=
new
SimpleAttributeSet
();
StyleConstants
.
setForeground
(
errStyle
,
fgColorErr
);
StyleConstants
.
setBackground
(
errStyle
,
bgColor
);
StyleConstants
.
setFontSize
(
errStyle
,
font
.
getSize
());
StyleConstants
.
setFontFamily
(
errStyle
,
font
.
getFamily
());
StyleConstants
.
setBold
(
errStyle
,
font
.
isBold
());
StyleConstants
.
setItalic
(
errStyle
,
font
.
isItalic
());
if
(
UIManager
.
getLookAndFeel
().
getID
().
equals
(
"Nimbus"
)) {
getViewport
().
setBackground
(
bgColor
);
consoleTextPane
.
setOpaque
(
false
);
consoleTextPane
.
setBackground
(
new
Color
(
0
,
0
,
0
,
0
));
}
else
{
consoleTextPane
.
setBackground
(
bgColor
);
}
// calculate height of a line of text in pixels
// and size window accordingly
FontMetrics
metrics
=
this
.
getFontMetrics
(
font
);
int
height
=
metrics
.
getAscent
() +
metrics
.
getDescent
();
int
lines
=
Preferences
.
getInteger
(
"console.lines"
);
//, 4);
int
sizeFudge
=
6
;
//10; // unclear why this is necessary, but it is
setPreferredSize
(
new
Dimension
(
1024
, (
height
*
lines
) +
sizeFudge
));
setMinimumSize
(
new
Dimension
(
1024
, (
height
*
4
) +
sizeFudge
));
}
static
public
void
setEditor
(
Editor
editor
) {
currentConsole
=
editor
.
console
;
}
/**
* Close the streams so that the temporary files can be deleted.
* <p/>
* File.deleteOnExit() cannot be used because the stdout and stderr
* files are inside a folder, and have to be deleted before the
* folder itself is deleted, which can't be guaranteed when using
* the deleteOnExit() method.
*/
public
static
void
handleQuit
() {
// replace original streams to remove references to console's streams
System
.
setOut
(
systemOut
);
System
.
setErr
(
systemErr
);
try
{
// close the PrintStream
if
(
consoleOut
!=
null
)
consoleOut
.
close
();
if
(
consoleErr
!=
null
)
consoleErr
.
close
();
// also have to close the original FileOutputStream
// otherwise it won't be shut down completely
if
(
stdoutFile
!=
null
)
stdoutFile
.
close
();
if
(
stderrFile
!=
null
)
stderrFile
.
close
();
}
catch
(
IOException
e
) {
e
.
printStackTrace
(
systemErr
);
}
}
synchronized
public
void
message
(
String
what
,
boolean
err
) {
if
(
err
) {
systemErr
.
print
(
what
);
}
else
{
systemOut
.
print
(
what
);
}
if
(
err
&& (
what
.
contains
(
"invalid context 0x0"
) || (
what
.
contains
(
"invalid drawable"
)))) {
// Respectfully declining... This is a quirk of more recent releases of
// Java on Mac OS X, but is widely reported as the source of any other
// bug or problem that a user runs into. It may well be a Processing
// bug, but until we know, we're suppressing the messages.
}
else
if
(
err
&&
what
.
contains
(
"Make pbuffer:"
)) {
// Remove initalization warning from LWJGL.
}
else
if
(
err
&&
what
.
contains
(
"XInitThreads() called for concurrent"
)) {
// "Info: XInitThreads() called for concurrent Thread support" message on Linux
}
else
if
(!
err
&&
what
.
contains
(
"Listening for transport dt_socket at address"
)) {
// Message from the JVM about the socket launch for debug
// Listening for transport dt_socket at address: 8727
}
else
{
// Append a piece of text to the console. Swing components are NOT
// thread-safe, and since the MessageSiphon instantiates new threads,
// and in those callbacks, they often print output to stdout and stderr,
// which are wrapped by EditorConsoleStream and eventually leads to
// EditorConsole.appendText(), which directly updates the Swing text
// components, causing deadlock. Updates are buffered to the console and
// displayed at regular intervals on Swing's event-dispatching thread.
// (patch by David Mellis)
consoleDoc
.
appendString
(
what
,
err
?
errStyle
:
stdStyle
);
}
}
public
void
clear
() {
try
{
consoleDoc
.
remove
(
0
,
consoleDoc
.
getLength
());
}
catch
(
BadLocationException
e
) {
// ignore the error otherwise this will cause an infinite loop
// maybe not a good idea in the long run?
}
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
private
static
class
EditorConsoleStream
extends
OutputStream
{
final
boolean
err
;
// whether stderr or stdout
final
byte
single
[] =
new
byte
[
1
];
EditorConsole
console
;
public
EditorConsoleStream
(
boolean
err
,
EditorConsole
console
) {
this
.
err
=
err
;
this
.
console
=
console
;
}
public
void
close
() { }
public
void
flush
() { }
public
void
write
(
byte
b
[]) {
// appears never to be used
write
(
b
,
0
,
b
.
length
);
}
public
void
write
(
byte
b
[],
int
offset
,
int
length
) {
if
(
console
!=
null
) {
console
.
message
(
new
String
(
b
,
offset
,
length
),
err
);
}
else
if
(
currentConsole
!=
null
) {
currentConsole
.
message
(
new
String
(
b
,
offset
,
length
),
err
);
}
else
{
// If no console is present, still need to write this to the actual
// System.out or System.err. Otherwise we can't !#$!% debug anything.
if
(
err
) {
systemErr
.
write
(
b
,
offset
,
length
);
}
else
{
systemOut
.
write
(
b
,
offset
,
length
);
}
}
final
OutputStream
echo
=
err
?
stderrFile
:
stdoutFile
;
if
(
echo
!=
null
) {
try
{
echo
.
write
(
b
,
offset
,
length
);
echo
.
flush
();
}
catch
(
IOException
e
) {
e
.
printStackTrace
();
}
}
}
public
void
write
(
int
b
) {
single
[
0
] = (
byte
)
b
;
write
(
single
,
0
,
1
);
}
}
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
/**
* Buffer updates to the console and output them in batches. For info, see:
* http://java.sun.com/products/jfc/tsc/articles/text/element_buffer and
* http://javatechniques.com/public/java/docs/gui/jtextpane-speed-part2.html
* appendString() is called from multiple threads, and insertAll from the
* swing event thread, so they need to be synchronized
*/
class
BufferedStyledDocument
extends
DefaultStyledDocument
{
ArrayList
<
ElementSpec
>
elements
=
new
ArrayList
<
ElementSpec
>();
int
maxLineLength
,
maxLineCount
;
int
currentLineLength
=
0
;
boolean
needLineBreak
=
false
;
boolean
hasAppendage
=
false
;
public
BufferedStyledDocument
(
int
maxLineLength
,
int
maxLineCount
) {
this
.
maxLineLength
=
maxLineLength
;
this
.
maxLineCount
=
maxLineCount
;
}
/** buffer a string for insertion at the end of the DefaultStyledDocument */
public
synchronized
void
appendString
(
String
str
,
AttributeSet
a
) {
// do this so that it's only updated when needed (otherwise console
// updates every 250 ms when an app isn't even running.. see bug 180)
hasAppendage
=
true
;
// process each line of the string
while
(
str
.
length
() >
0
) {
// newlines within an element have (almost) no effect, so we need to
// replace them with proper paragraph breaks (start and end tags)
if
(
needLineBreak
||
currentLineLength
>
maxLineLength
) {
elements
.
add
(
new
ElementSpec
(
a
,
ElementSpec
.
EndTagType
));
elements
.
add
(
new
ElementSpec
(
a
,
ElementSpec
.
StartTagType
));
currentLineLength
=
0
;
}
if
(
str
.
indexOf
(
'\n'
) == -
1
) {
elements
.
add
(
new
ElementSpec
(
a
,
ElementSpec
.
ContentType
,
str
.
toCharArray
(),
0
,
str
.
length
()));
currentLineLength
+=
str
.
length
();
needLineBreak
=
false
;
str
=
str
.
substring
(
str
.
length
());
// eat the string
}
else
{
elements
.
add
(
new
ElementSpec
(
a
,
ElementSpec
.
ContentType
,
str
.
toCharArray
(),
0
,
str
.
indexOf
(
'\n'
) +
1
));
needLineBreak
=
true
;
str
=
str
.
substring
(
str
.
indexOf
(
'\n'
) +
1
);
// eat the line
}
}
}
/** insert the buffered strings */
public
synchronized
void
insertAll
() {
ElementSpec
[]
elementArray
=
new
ElementSpec
[
elements
.
size
()];
elements
.
toArray
(
elementArray
);
try
{
// check how many lines have been used so far
// if too many, shave off a few lines from the beginning
Element
element
=
super
.
getDefaultRootElement
();
int
lineCount
=
element
.
getElementCount
();
int
overage
=
lineCount
-
maxLineCount
;
if
(
overage
>
0
) {
// if 1200 lines, and 1000 lines is max,
// find the position of the end of the 200th line
//systemOut.println("overage is " + overage);
Element
lineElement
=
element
.
getElement
(
overage
);
if
(
lineElement
==
null
)
return
;
// do nuthin
int
endOffset
=
lineElement
.
getEndOffset
();
// remove to the end of the 200th line
super
.
remove
(
0
,
endOffset
);
}
super
.
insert
(
super
.
getLength
(),
elementArray
);
}
catch
(
BadLocationException
e
) {
// ignore the error otherwise this will cause an infinite loop
// maybe not a good idea in the long run?
}
elements
.
clear
();
hasAppendage
=
false
;
}
}
Back
|
FazBrowse Home
|
New Git URL