|
| 1 | +package org.scijava.ui.swing.console; |
| 2 | + |
| 3 | +import net.miginfocom.swing.MigLayout; |
| 4 | +import org.scijava.Context; |
| 5 | +import org.scijava.command.Command; |
| 6 | +import org.scijava.command.CommandService; |
| 7 | +import org.scijava.log.DefaultLogger; |
| 8 | +import org.scijava.log.LogService; |
| 9 | +import org.scijava.log.LogSource; |
| 10 | +import org.scijava.log.Logger; |
| 11 | +import org.scijava.plugin.Parameter; |
| 12 | +import org.scijava.thread.ThreadService; |
| 13 | +import org.scijava.ui.UIService; |
| 14 | + |
| 15 | +import javax.swing.*; |
| 16 | +import java.awt.*; |
| 17 | + |
| 18 | +/** |
| 19 | + * {@link LoggingDemo} demonstrate the capabilitie of {@link SwingConsolePane} |
| 20 | + * and {@link LoggingPanel} |
| 21 | + * |
| 22 | + * @author Matthias Arzt |
| 23 | + */ |
| 24 | +public class LoggingDemo { |
| 25 | + |
| 26 | + public static void main(String... args) throws InterruptedException { |
| 27 | + Context context = new Context(); |
| 28 | + UIService ui = context.service(UIService.class); |
| 29 | + ui.showUI(); |
| 30 | + CommandService commandService = context.service(CommandService.class); |
| 31 | + commandService.run(PluginThatLogs.class, true); |
| 32 | + commandService.run(LoggingLoop.class, true); |
| 33 | + } |
| 34 | + |
| 35 | + public static class LoggingLoop implements Command { |
| 36 | + |
| 37 | + @Parameter |
| 38 | + private Logger logger; |
| 39 | + |
| 40 | + @Override |
| 41 | + public void run() { |
| 42 | + while (true) { |
| 43 | + logger.warn("Message Text"); |
| 44 | + System.out.println("Hello World"); |
| 45 | + try { |
| 46 | + Thread.sleep(1000); |
| 47 | + } catch (InterruptedException e) { |
| 48 | + logger.warn(e); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + public static class PluginThatLogs implements Command { |
| 55 | + |
| 56 | + @Parameter |
| 57 | + private Context context; |
| 58 | + |
| 59 | + @Parameter |
| 60 | + private Logger logger; |
| 61 | + |
| 62 | + @Parameter |
| 63 | + private LogService logService; |
| 64 | + |
| 65 | + private Logger privateLogger = new DefaultLogger(ignore -> {}, LogSource.newRoot(), 100); |
| 66 | + |
| 67 | + @Override |
| 68 | + public void run() { |
| 69 | + LoggingPanel panel = new LoggingPanel(context.service(ThreadService.class)); |
| 70 | + logger.addLogListener(panel); |
| 71 | + privateLogger.addLogListener(panel); |
| 72 | + |
| 73 | + JFrame frame = new JFrame("Plugin that logs"); |
| 74 | + frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); |
| 75 | + frame.setLayout(new MigLayout("","[grow]","[][grow]")); |
| 76 | + frame.add(newButton("both log", () -> writeToLogger(logger)), "split"); |
| 77 | + frame.add(newButton("main log", () -> writeToLogger(logService))); |
| 78 | + frame.add(newButton("private log", () -> writeToLogger(privateLogger)), "wrap"); |
| 79 | + frame.add(panel, "grow"); |
| 80 | + frame.pack(); |
| 81 | + frame.setVisible(true); |
| 82 | + } |
| 83 | + |
| 84 | + private void writeToLogger(Logger logger) { |
| 85 | + logger.error("Error message test"); |
| 86 | + logger.warn("Text describing a warning"); |
| 87 | + logger.info("An Information"); |
| 88 | + logger.debug("Something help debugging"); |
| 89 | + logger.trace("Trace everything"); |
| 90 | + logger.log(42, "Whats the best log level"); |
| 91 | + } |
| 92 | + |
| 93 | + private Component newButton(String title, Runnable action) { |
| 94 | + JButton button = new JButton(title); |
| 95 | + button.addActionListener(a -> action.run()); |
| 96 | + return button; |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments