Skip to content

Commit 26bb777

Browse files
committed
Add LoggingPanel to show LogMessages separatly
At this point LoggingPanel is basically a copy of ConsolPanel. But it will envole to something very different. Features like filtering messages by log source, level and text, and options for message formatting will be added.
1 parent 74bc7f1 commit 26bb777

File tree

3 files changed

+247
-3
lines changed

3 files changed

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

src/main/java/org/scijava/ui/swing/console/SwingConsolePane.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,25 @@
3232

3333
import java.awt.Component;
3434

35-
import javax.swing.JPanel;
36-
import javax.swing.JTextPane;
35+
import javax.swing.*;
3736

3837
import net.miginfocom.swing.MigLayout;
3938

4039
import org.scijava.Context;
4140
import org.scijava.console.OutputEvent;
41+
import org.scijava.log.LogService;
4242
import org.scijava.plugin.Parameter;
4343
import org.scijava.thread.ThreadService;
4444
import org.scijava.ui.console.AbstractConsolePane;
4545
import org.scijava.ui.console.ConsolePane;
4646

4747
/**
4848
* Swing implementation of {@link ConsolePane}.
49+
* <p>
50+
* This implementation consists of a <em>console</em> tab and a <em>log</em>
51+
* tab, provided by a {@link ConsolePanel} and {@link LoggingPanel}
52+
* respectively.
53+
* </p>
4954
*
5055
* @author Curtis Rueden
5156
*/
@@ -54,8 +59,13 @@ public class SwingConsolePane extends AbstractConsolePane<JPanel> {
5459
@Parameter
5560
private ThreadService threadService;
5661

62+
@Parameter
63+
private LogService logService;
64+
5765
private ConsolePanel consolePanel;
5866

67+
private LoggingPanel loggingPanel;
68+
5969
/**
6070
* The console pane's containing window; e.g., a {@link javax.swing.JFrame} or
6171
* {@link javax.swing.JInternalFrame}.
@@ -121,8 +131,13 @@ private ConsolePanel consolePanel() {
121131
private synchronized void initLoggingPanel() {
122132
if (consolePanel != null) return;
123133
consolePanel = new ConsolePanel(threadService);
134+
loggingPanel = new LoggingPanel(threadService);
135+
logService.addLogListener(loggingPanel);
124136
component = new JPanel(new MigLayout("", "[grow]", "[grow]"));
125-
component.add(consolePanel, "grow");
137+
JTabbedPane tabs = new JTabbedPane();
138+
tabs.addTab("Console", consolePanel);
139+
tabs.addTab("Log", loggingPanel);
140+
component.add(tabs, "grow");
126141
}
127142

128143
// -- Helper methods - testing --

0 commit comments

Comments
 (0)