Skip to content

Commit cca4d54

Browse files
committed
SwingConsolePane: extract ConsolePanel from SwingConsolePane
SwingConsolePane is responsible for lazy initialization and showing the console window at correct time. ConsolePanel is responsible for holding a text area to show console output. ConsolePanel will become a starting point for implementing a LoggingPanel.
1 parent 993b164 commit cca4d54

File tree

3 files changed

+184
-110
lines changed

3 files changed

+184
-110
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 ConsolePanel 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 ConsolePanel(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+
}

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

Lines changed: 19 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,19 @@
3030

3131
package org.scijava.ui.swing.console;
3232

33-
import java.awt.BorderLayout;
34-
import java.awt.Color;
3533
import java.awt.Component;
36-
import java.awt.Dimension;
37-
import java.awt.Font;
38-
import java.awt.FontMetrics;
3934

4035
import javax.swing.JPanel;
41-
import javax.swing.JScrollPane;
4236
import javax.swing.JTextPane;
43-
import javax.swing.text.BadLocationException;
44-
import javax.swing.text.Style;
45-
import javax.swing.text.StyleConstants;
46-
import javax.swing.text.StyledDocument;
4737

4838
import net.miginfocom.swing.MigLayout;
4939

5040
import org.scijava.Context;
5141
import org.scijava.console.OutputEvent;
52-
import org.scijava.console.OutputEvent.Source;
5342
import org.scijava.plugin.Parameter;
5443
import org.scijava.thread.ThreadService;
5544
import org.scijava.ui.console.AbstractConsolePane;
5645
import org.scijava.ui.console.ConsolePane;
57-
import org.scijava.ui.swing.StaticSwingUtils;
5846

5947
/**
6048
* Swing implementation of {@link ConsolePane}.
@@ -66,22 +54,16 @@ public class SwingConsolePane extends AbstractConsolePane<JPanel> {
6654
@Parameter
6755
private ThreadService threadService;
6856

69-
private JPanel consolePanel;
70-
private JTextPane textPane;
71-
private JScrollPane scrollPane;
72-
73-
private StyledDocument doc;
74-
private Style stdoutLocal;
75-
private Style stderrLocal;
76-
private Style stdoutGlobal;
77-
private Style stderrGlobal;
57+
private ConsolePanel consolePanel;
7858

7959
/**
8060
* The console pane's containing window; e.g., a {@link javax.swing.JFrame} or
8161
* {@link javax.swing.JInternalFrame}.
8262
*/
8363
private Component window;
8464

65+
private JPanel component;
66+
8567
public SwingConsolePane(final Context context) {
8668
super(context);
8769
}
@@ -93,41 +75,15 @@ public void setWindow(final Component window) {
9375
this.window = window;
9476
}
9577

96-
public JTextPane getTextPane() {
97-
if (consolePanel == null) initConsolePanel();
98-
return textPane;
99-
}
100-
101-
public JScrollPane getScrollPane() {
102-
if (consolePanel == null) initConsolePanel();
103-
return scrollPane;
104-
}
105-
10678
public void clear() {
107-
if (consolePanel == null) initConsolePanel();
108-
textPane.setText("");
79+
consolePanel().clear();
10980
}
11081

11182
// -- ConsolePane methods --
11283

11384
@Override
11485
public void append(final OutputEvent event) {
115-
if (consolePanel == null) initConsolePanel();
116-
threadService.queue(new Runnable() {
117-
118-
@Override
119-
public void run() {
120-
final boolean atBottom =
121-
StaticSwingUtils.isScrolledToBottom(scrollPane);
122-
try {
123-
doc.insertString(doc.getLength(), event.getOutput(), getStyle(event));
124-
}
125-
catch (final BadLocationException exc) {
126-
throw new RuntimeException(exc);
127-
}
128-
if (atBottom) StaticSwingUtils.scrollToBottom(scrollPane);
129-
}
130-
});
86+
consolePanel().outputOccurred(event);
13187
}
13288

13389
@Override
@@ -146,8 +102,8 @@ public void run() {
146102

147103
@Override
148104
public JPanel getComponent() {
149-
if (consolePanel == null) initConsolePanel();
150-
return consolePanel;
105+
if (consolePanel == null) initLoggingPanel();
106+
return component;
151107
}
152108

153109
@Override
@@ -157,64 +113,21 @@ public Class<JPanel> getComponentType() {
157113

158114
// -- Helper methods - lazy initialization --
159115

160-
private synchronized void initConsolePanel() {
161-
if (consolePanel != null) return;
162-
163-
final JPanel panel = new JPanel();
164-
panel.setLayout(new MigLayout("", "[grow,fill]", "[grow,fill,align top]"));
165-
166-
textPane = new JTextPane();
167-
textPane.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
168-
textPane.setEditable(false);
169-
170-
doc = textPane.getStyledDocument();
171-
172-
stdoutLocal = createStyle("stdoutLocal", null, Color.black, null, null);
173-
stderrLocal = createStyle("stderrLocal", null, Color.red, null, null);
174-
stdoutGlobal = createStyle("stdoutGlobal", stdoutLocal, null, null, true);
175-
stderrGlobal = createStyle("stderrGlobal", stderrLocal, null, null, true);
176-
177-
// NB: We wrap the JTextPane in a JPanel to disable
178-
// the text pane's intelligent line wrapping behavior.
179-
// I.e.: we want console lines _not_ to wrap, but instead
180-
// for the scroll pane to show a horizontal scroll bar.
181-
// Thanks to: https://tips4java.wordpress.com/2009/01/25/no-wrap-text-pane/
182-
final JPanel textPanel = new JPanel();
183-
textPanel.setLayout(new BorderLayout());
184-
textPanel.add(textPane);
185-
186-
scrollPane = new JScrollPane(textPanel);
187-
scrollPane.setPreferredSize(new Dimension(600, 600));
188-
189-
// Make the scroll bars move at a reasonable pace.
190-
final FontMetrics fm = scrollPane.getFontMetrics(scrollPane.getFont());
191-
final int charWidth = fm.charWidth('a');
192-
final int lineHeight = fm.getHeight();
193-
scrollPane.getHorizontalScrollBar().setUnitIncrement(charWidth);
194-
scrollPane.getVerticalScrollBar().setUnitIncrement(2 * lineHeight);
195-
196-
panel.add(scrollPane);
197-
198-
consolePanel = panel;
116+
private ConsolePanel consolePanel() {
117+
if (consolePanel == null) initLoggingPanel();
118+
return consolePanel;
199119
}
200120

201-
// -- Helper methods --
202-
203-
private Style createStyle(final String name, final Style parent,
204-
final Color foreground, final Boolean bold, final Boolean italic)
205-
{
206-
final Style style = textPane.addStyle(name, parent);
207-
if (foreground != null) StyleConstants.setForeground(style, foreground);
208-
if (bold != null) StyleConstants.setBold(style, bold);
209-
if (italic != null) StyleConstants.setItalic(style, italic);
210-
return style;
121+
private synchronized void initLoggingPanel() {
122+
if (consolePanel != null) return;
123+
consolePanel = new ConsolePanel(threadService);
124+
component = new JPanel(new MigLayout("", "[grow]", "[grow]"));
125+
component.add(consolePanel, "grow");
211126
}
212127

213-
private Style getStyle(final OutputEvent event) {
214-
final boolean stderr = event.getSource() == Source.STDERR;
215-
final boolean contextual = event.isContextual();
216-
if (stderr) return contextual ? stderrLocal : stderrGlobal;
217-
return contextual ? stdoutLocal : stdoutGlobal;
218-
}
128+
// -- Helper methods - testing --
219129

130+
JTextPane getTextPane() {
131+
return consolePanel().getTextPane();
132+
}
220133
}

src/test/java/org/scijava/ui/swing/console/SwingConsolePaneBenchmark.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ public static void main(final String[] args) throws Exception {
5959
final Context context = new Context();
6060
context.service(UIService.class).showUI();
6161

62-
System.out.print("Hello ");
63-
System.err.println("world!");
62+
System.out.println("Hello world!");
6463

6564
final int numThreads = 50;
6665
final int numOperations = 20;
@@ -99,8 +98,7 @@ public void run() {
9998
f[t].get();
10099
}
101100

102-
System.err.print("Goodbye ");
103-
System.out.println("cruel world!");
101+
System.err.println("Goodbye cruel world!");
104102

105103
final long end = System.currentTimeMillis();
106104
System.out.println();

0 commit comments

Comments
 (0)