Skip to content

Commit c60b561

Browse files
committed
Add LoggingPanel as copy of ConsolePanel
1 parent cca4d54 commit c60b561

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
* #%L
3+
* SciJava UI components for Java Swing.
4+
* %%
5+
* Copyright (C) 2010 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison.
7+
* %%
8+
* Redistribution and use in source and binary forms, with or without
9+
* modification, are permitted provided that the following conditions are met:
10+
*
11+
* 1. Redistributions of source code must retain the above copyright notice,
12+
* this list of conditions and the following disclaimer.
13+
* 2. Redistributions in binary form must reproduce the above copyright notice,
14+
* this list of conditions and the following disclaimer in the documentation
15+
* and/or other materials provided with the distribution.
16+
*
17+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
21+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
* POSSIBILITY OF SUCH DAMAGE.
28+
* #L%
29+
*/
30+
31+
package org.scijava.ui.swing.console;
32+
33+
import java.awt.*;
34+
35+
import javax.swing.*;
36+
import javax.swing.text.BadLocationException;
37+
import javax.swing.text.Style;
38+
import javax.swing.text.StyleConstants;
39+
import javax.swing.text.StyledDocument;
40+
41+
import net.miginfocom.swing.MigLayout;
42+
43+
import org.scijava.console.OutputEvent;
44+
import org.scijava.console.OutputListener;
45+
import org.scijava.log.IgnoreAsCallingClass;
46+
import org.scijava.log.LogListener;
47+
import org.scijava.log.LogService;
48+
import org.scijava.log.Logger;
49+
import org.scijava.thread.ThreadService;
50+
import org.scijava.ui.swing.StaticSwingUtils;
51+
52+
/**
53+
* LoggingPanel can display log message and console output as a list, and
54+
* provides convenient ways for the user to filter this list.
55+
* LoggingPanel implements {@link LogListener} and {@link OutputListener}, that
56+
* way it can receive log message and console output from {@link LogService},
57+
* {@link Logger} and {@link org.scijava.console.ConsoleService}
58+
*
59+
* @see LogService
60+
* @see Logger
61+
* @see org.scijava.console.ConsoleService
62+
* @author Matthias Arzt
63+
*/
64+
@IgnoreAsCallingClass
65+
public class LoggingPanel extends JPanel implements OutputListener
66+
{
67+
private JTextPane textPane;
68+
private JScrollPane scrollPane;
69+
70+
private StyledDocument doc;
71+
private Style stdoutLocal;
72+
private Style stderrLocal;
73+
private Style stdoutGlobal;
74+
private Style stderrGlobal;
75+
76+
private final ThreadService threadService;
77+
78+
public LoggingPanel(ThreadService threadService) {
79+
this.threadService = threadService;
80+
initGui();
81+
}
82+
83+
public void clear() {
84+
textPane.setText("");
85+
}
86+
87+
@Override
88+
public void outputOccurred(OutputEvent event) {
89+
threadService.queue(new Runnable() {
90+
91+
@Override
92+
public void run() {
93+
final boolean atBottom =
94+
StaticSwingUtils.isScrolledToBottom(scrollPane);
95+
try {
96+
doc.insertString(doc.getLength(), event.getOutput(), getStyle(event));
97+
}
98+
catch (final BadLocationException exc) {
99+
throw new RuntimeException(exc);
100+
}
101+
if (atBottom) StaticSwingUtils.scrollToBottom(scrollPane);
102+
}
103+
});
104+
}
105+
106+
private synchronized void initGui() {
107+
setLayout(new MigLayout("inset 0", "[grow,fill]", "[grow,fill,align top]"));
108+
109+
textPane = new JTextPane();
110+
textPane.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
111+
textPane.setEditable(false);
112+
113+
doc = textPane.getStyledDocument();
114+
115+
stdoutLocal = createStyle("stdoutLocal", null, Color.black, null, null);
116+
stderrLocal = createStyle("stderrLocal", null, Color.red, null, null);
117+
stdoutGlobal = createStyle("stdoutGlobal", stdoutLocal, null, null, true);
118+
stderrGlobal = createStyle("stderrGlobal", stderrLocal, null, null, true);
119+
120+
// NB: We wrap the JTextPane in a JPanel to disable
121+
// the text pane's intelligent line wrapping behavior.
122+
// I.e.: we want console lines _not_ to wrap, but instead
123+
// for the scroll pane to show a horizontal scroll bar.
124+
// Thanks to: https://tips4java.wordpress.com/2009/01/25/no-wrap-text-pane/
125+
final JPanel textPanel = new JPanel();
126+
textPanel.setLayout(new BorderLayout());
127+
textPanel.add(textPane);
128+
129+
scrollPane = new JScrollPane(textPanel);
130+
scrollPane.setPreferredSize(new Dimension(600, 600));
131+
132+
// Make the scroll bars move at a reasonable pace.
133+
final FontMetrics fm = scrollPane.getFontMetrics(scrollPane.getFont());
134+
final int charWidth = fm.charWidth('a');
135+
final int lineHeight = fm.getHeight();
136+
scrollPane.getHorizontalScrollBar().setUnitIncrement(charWidth);
137+
scrollPane.getVerticalScrollBar().setUnitIncrement(2 * lineHeight);
138+
139+
add(scrollPane);
140+
}
141+
// -- Helper methods --
142+
143+
private Style createStyle(final String name, final Style parent,
144+
final Color foreground, final Boolean bold, final Boolean italic)
145+
{
146+
final Style style = textPane.addStyle(name, parent);
147+
if (foreground != null) StyleConstants.setForeground(style, foreground);
148+
if (bold != null) StyleConstants.setBold(style, bold);
149+
if (italic != null) StyleConstants.setItalic(style, italic);
150+
return style;
151+
}
152+
153+
private Style getStyle(final OutputEvent event) {
154+
final boolean stderr = event.getSource() == OutputEvent.Source.STDERR;
155+
final boolean contextual = event.isContextual();
156+
if (stderr) return contextual ? stderrLocal : stderrGlobal;
157+
return contextual ? stdoutLocal : stdoutGlobal;
158+
}
159+
160+
public JTextPane getTextPane() {
161+
return textPane;
162+
}
163+
}

0 commit comments

Comments
 (0)