|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava UI components for Java Swing. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2010 - 2015 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.BorderLayout; |
| 34 | +import java.awt.Color; |
| 35 | +import java.awt.Component; |
| 36 | +import java.awt.Dimension; |
| 37 | +import java.awt.Font; |
| 38 | + |
| 39 | +import javax.swing.JPanel; |
| 40 | +import javax.swing.JScrollPane; |
| 41 | +import javax.swing.JTextPane; |
| 42 | +import javax.swing.text.BadLocationException; |
| 43 | +import javax.swing.text.Style; |
| 44 | +import javax.swing.text.StyleConstants; |
| 45 | +import javax.swing.text.StyledDocument; |
| 46 | + |
| 47 | +import net.miginfocom.swing.MigLayout; |
| 48 | + |
| 49 | +import org.scijava.Context; |
| 50 | +import org.scijava.console.OutputEvent; |
| 51 | +import org.scijava.console.OutputEvent.Source; |
| 52 | +import org.scijava.plugin.Parameter; |
| 53 | +import org.scijava.thread.ThreadService; |
| 54 | +import org.scijava.ui.UIService; |
| 55 | +import org.scijava.ui.console.AbstractConsolePane; |
| 56 | +import org.scijava.ui.console.ConsolePane; |
| 57 | + |
| 58 | +/** |
| 59 | + * Swing implementation of {@link ConsolePane}. |
| 60 | + * |
| 61 | + * @author Curtis Rueden |
| 62 | + */ |
| 63 | +public class SwingConsolePane extends AbstractConsolePane<JPanel> { |
| 64 | + |
| 65 | + @Parameter |
| 66 | + private ThreadService threadService; |
| 67 | + |
| 68 | + private JPanel consolePanel; |
| 69 | + private JTextPane textPane; |
| 70 | + private StyledDocument doc; |
| 71 | + private Style stdoutLocal; |
| 72 | + private Style stderrLocal; |
| 73 | + private Style stdoutGlobal; |
| 74 | + private Style stderrGlobal; |
| 75 | + |
| 76 | + /** |
| 77 | + * The console pane's containing window; e.g., a {@link javax.swing.JFrame} or |
| 78 | + * {@link javax.swing.JInternalFrame}. |
| 79 | + */ |
| 80 | + private Component window; |
| 81 | + |
| 82 | + public SwingConsolePane(final Context context) { |
| 83 | + super(context); |
| 84 | + } |
| 85 | + |
| 86 | + // -- SwingConsolePane methods -- |
| 87 | + |
| 88 | + /** Sets the window which should be shown when {@link #show()} is called. */ |
| 89 | + public void setWindow(final Component window) { |
| 90 | + this.window = window; |
| 91 | + } |
| 92 | + |
| 93 | + // -- ConsolePane methods -- |
| 94 | + |
| 95 | + @Override |
| 96 | + public void append(final OutputEvent event) { |
| 97 | + if (consolePanel == null) initConsolePanel(); |
| 98 | + try { |
| 99 | + doc.insertString(doc.getLength(), event.getOutput(), getStyle(event)); |
| 100 | + } |
| 101 | + catch (final BadLocationException exc) { |
| 102 | + throw new RuntimeException(exc); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public void show() { |
| 108 | + if (window == null) return; |
| 109 | + threadService.queue(new Runnable() { |
| 110 | + |
| 111 | + @Override |
| 112 | + public void run() { |
| 113 | + window.setVisible(true); |
| 114 | + } |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + // -- UIComponent methods -- |
| 119 | + |
| 120 | + @Override |
| 121 | + public JPanel getComponent() { |
| 122 | + if (consolePanel == null) initConsolePanel(); |
| 123 | + return consolePanel; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public Class<JPanel> getComponentType() { |
| 128 | + return JPanel.class; |
| 129 | + } |
| 130 | + |
| 131 | + // -- Helper methods - lazy initialization -- |
| 132 | + |
| 133 | + private synchronized void initConsolePanel() { |
| 134 | + if (consolePanel != null) return; |
| 135 | + |
| 136 | + final JPanel panel = new JPanel(); |
| 137 | + panel.setLayout(new MigLayout("", "[grow,fill]", "[grow,fill,align top]")); |
| 138 | + |
| 139 | + textPane = new JTextPane(); |
| 140 | + textPane.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12)); |
| 141 | + textPane.setEditable(false); |
| 142 | + |
| 143 | + doc = textPane.getStyledDocument(); |
| 144 | + |
| 145 | + stdoutLocal = createStyle("stdoutLocal", null, Color.black, null, null); |
| 146 | + stderrLocal = createStyle("stderrLocal", null, Color.red, null, null); |
| 147 | + stdoutGlobal = createStyle("stdoutGlobal", stdoutLocal, null, null, true); |
| 148 | + stderrGlobal = createStyle("stderrGlobal", stderrLocal, null, null, true); |
| 149 | + |
| 150 | + // NB: We wrap the JTextPane in a JPanel to disable |
| 151 | + // the text pane's intelligent line wrapping behavior. |
| 152 | + // I.e.: we want console lines _not_ to wrap, but instead |
| 153 | + // for the scroll pane to show a horizontal scroll bar. |
| 154 | + // Thanks to: https://tips4java.wordpress.com/2009/01/25/no-wrap-text-pane/ |
| 155 | + final JPanel textPanel = new JPanel(); |
| 156 | + textPanel.setLayout(new BorderLayout()); |
| 157 | + textPanel.add(textPane); |
| 158 | + |
| 159 | + final JScrollPane scrollPane = new JScrollPane(textPanel); |
| 160 | + scrollPane.setPreferredSize(new Dimension(600, 600)); |
| 161 | + panel.add(scrollPane); |
| 162 | + |
| 163 | + consolePanel = panel; |
| 164 | + } |
| 165 | + |
| 166 | + // -- Helper methods -- |
| 167 | + |
| 168 | + private Style createStyle(final String name, final Style parent, |
| 169 | + final Color foreground, final Boolean bold, final Boolean italic) |
| 170 | + { |
| 171 | + final Style style = textPane.addStyle(name, parent); |
| 172 | + if (foreground != null) StyleConstants.setForeground(style, foreground); |
| 173 | + if (bold != null) StyleConstants.setBold(style, bold); |
| 174 | + if (italic != null) StyleConstants.setItalic(style, italic); |
| 175 | + return style; |
| 176 | + } |
| 177 | + |
| 178 | + private Style getStyle(final OutputEvent event) { |
| 179 | + final boolean stderr = event.getSource() == Source.STDERR; |
| 180 | + final boolean contextual = event.isContextual(); |
| 181 | + if (stderr) return contextual ? stderrLocal : stderrGlobal; |
| 182 | + return contextual ? stdoutLocal : stdoutGlobal; |
| 183 | + } |
| 184 | + |
| 185 | + // -- Main method -- |
| 186 | + |
| 187 | + /** A manual test drive of the Swing UI's console pane. */ |
| 188 | + public static void main(final String[] args) throws Exception { |
| 189 | + final Context context = new Context(); |
| 190 | + context.service(UIService.class).showUI(); |
| 191 | + |
| 192 | + System.out.println("Hello!"); |
| 193 | + |
| 194 | + final ThreadService threadService = context.service(ThreadService.class); |
| 195 | + threadService.run(new Runnable() { |
| 196 | + |
| 197 | + @Override |
| 198 | + public void run() { |
| 199 | + System.out.println("This is a test of the emergency console system."); |
| 200 | + System.err.println("In a real emergency, your computer would explode."); |
| 201 | + } |
| 202 | + |
| 203 | + }).get(); |
| 204 | + |
| 205 | + System.err.println("Goodbye!"); |
| 206 | + } |
| 207 | + |
| 208 | +} |
0 commit comments