|
| 1 | +/* |
| 2 | + * This file is part of Arduino. |
| 3 | + * |
| 4 | + * Arduino is free software; you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation; either version 2 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program; if not, write to the Free Software |
| 16 | + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | + * |
| 18 | + * As a special exception, you may use this file as part of a free software |
| 19 | + * library without restriction. Specifically, if other files instantiate |
| 20 | + * templates or use macros or inline functions from this file, or you compile |
| 21 | + * this file and link it with other files to produce an executable, this |
| 22 | + * file does not by itself cause the resulting executable to be covered by |
| 23 | + * the GNU General Public License. This exception does not however |
| 24 | + * invalidate any other reasons why the executable file might be covered by |
| 25 | + * the GNU General Public License. |
| 26 | + * |
| 27 | + * Copyright 2015 Arduino LLC (http://www.arduino.cc/) |
| 28 | + * |
| 29 | + * Original version of this file courtesy of Rob Camick |
| 30 | + * <p> |
| 31 | + * https://tips4java.wordpress.com/2008/11/08/message-console/ |
| 32 | + * <p> |
| 33 | + * About page at https://tips4java.wordpress.com/about/ says something |
| 34 | + * like MIT |
| 35 | + */ |
| 36 | + |
| 37 | +package cc.arduino; |
| 38 | + |
| 39 | +import processing.app.EditorConsole; |
| 40 | + |
| 41 | +import javax.swing.*; |
| 42 | +import javax.swing.text.BadLocationException; |
| 43 | +import javax.swing.text.Document; |
| 44 | +import javax.swing.text.SimpleAttributeSet; |
| 45 | +import java.io.ByteArrayOutputStream; |
| 46 | +import java.io.PrintStream; |
| 47 | + |
| 48 | +/* |
| 49 | + * Class to intercept output from a PrintStream and add it to a Document. |
| 50 | + * The output can optionally be redirected to a different PrintStream. |
| 51 | + * The text displayed in the Document can be color coded to indicate |
| 52 | + * the output source. |
| 53 | + */ |
| 54 | +public class ConsoleOutputStream extends ByteArrayOutputStream { |
| 55 | + |
| 56 | + private final SimpleAttributeSet attributes; |
| 57 | + private final PrintStream printStream; |
| 58 | + private final StringBuilder buffer; |
| 59 | + private final Timer timer; |
| 60 | + private JScrollPane scrollPane; |
| 61 | + private Document document; |
| 62 | + |
| 63 | + public ConsoleOutputStream(SimpleAttributeSet attributes, PrintStream printStream) { |
| 64 | + this.attributes = attributes; |
| 65 | + this.printStream = printStream; |
| 66 | + this.buffer = new StringBuilder(); |
| 67 | + |
| 68 | + this.timer = new Timer(100, (e) -> { |
| 69 | + if (scrollPane != null) { |
| 70 | + synchronized (scrollPane) { |
| 71 | + scrollPane.getHorizontalScrollBar().setValue(0); |
| 72 | + scrollPane.getVerticalScrollBar().setValue(scrollPane.getVerticalScrollBar().getMaximum()); |
| 73 | + } |
| 74 | + } |
| 75 | + }); |
| 76 | + timer.setRepeats(false); |
| 77 | + } |
| 78 | + |
| 79 | + public synchronized void setCurrentEditorConsole(EditorConsole console) { |
| 80 | + this.scrollPane = console; |
| 81 | + this.document = console.getDocument(); |
| 82 | + } |
| 83 | + |
| 84 | + public synchronized void flush() { |
| 85 | + String message = toString(); |
| 86 | + |
| 87 | + if (message.length() == 0) { |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + handleAppend(message); |
| 92 | + |
| 93 | + reset(); |
| 94 | + } |
| 95 | + |
| 96 | + private void handleAppend(String message) { |
| 97 | + resetBufferIfDocumentEmpty(); |
| 98 | + |
| 99 | + buffer.append(message); |
| 100 | + |
| 101 | + clearBuffer(); |
| 102 | + } |
| 103 | + |
| 104 | + private void resetBufferIfDocumentEmpty() { |
| 105 | + if (document != null && document.getLength() == 0) { |
| 106 | + buffer.setLength(0); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private void clearBuffer() { |
| 111 | + String line = buffer.toString(); |
| 112 | + buffer.setLength(0); |
| 113 | + |
| 114 | + printStream.print(line); |
| 115 | + |
| 116 | + if (document != null) { |
| 117 | + SwingUtilities.invokeLater(() -> { |
| 118 | + try { |
| 119 | + String lineWithoutSlashR = line.replace("\r\n", "\n").replace("\r", "\n"); |
| 120 | + int offset = document.getLength(); |
| 121 | + document.insertString(offset, lineWithoutSlashR, attributes); |
| 122 | + } catch (BadLocationException ble) { |
| 123 | + //ignore |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + if (!timer.isRunning()) { |
| 128 | + timer.restart(); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | +} |
0 commit comments