|
| 1 | +package org.mastodon.revised.mamut; |
| 2 | + |
| 3 | +import java.awt.BorderLayout; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
| 6 | + |
| 7 | +import javax.swing.BoxLayout; |
| 8 | +import javax.swing.JPanel; |
| 9 | +import javax.swing.JProgressBar; |
| 10 | + |
| 11 | +import org.scijava.Context; |
| 12 | +import org.scijava.log.LogLevel; |
| 13 | +import org.scijava.log.LogMessage; |
| 14 | +import org.scijava.log.LogSource; |
| 15 | +import org.scijava.ui.swing.console.LoggingPanel; |
| 16 | + |
| 17 | +public class MastodonLogPanel extends JPanel |
| 18 | +{ |
| 19 | + |
| 20 | + private static final long serialVersionUID = 1L; |
| 21 | + |
| 22 | + private final LogSource root = LogSource.newRoot(); |
| 23 | + |
| 24 | + private final LogSource unknownSource = root.subSource( "Unkown source" ); |
| 25 | + |
| 26 | + private static final String PREF_KEY = "MastodonLoggerPreferencesKey"; |
| 27 | + |
| 28 | + private final Context context; |
| 29 | + |
| 30 | + private final LoggingPanel loggingPanel; |
| 31 | + |
| 32 | + private final JPanel panelProgressBars; |
| 33 | + |
| 34 | + private final Map< LogSource, JProgressBar > progressBars; |
| 35 | + |
| 36 | + public MastodonLogPanel(final Context context) |
| 37 | + { |
| 38 | + this.context = ( null == context ) ? new Context() : context; |
| 39 | + setLayout( new BorderLayout( 5, 5 ) ); |
| 40 | + |
| 41 | + panelProgressBars = new JPanel(); |
| 42 | + add( panelProgressBars, BorderLayout.NORTH ); |
| 43 | + panelProgressBars.setLayout( new BoxLayout( panelProgressBars, BoxLayout.PAGE_AXIS ) ); |
| 44 | + |
| 45 | + loggingPanel = new LoggingPanel( this.context, PREF_KEY ); |
| 46 | + loggingPanel.setOpaque( false ); |
| 47 | + add( loggingPanel, BorderLayout.CENTER ); |
| 48 | + |
| 49 | + this.progressBars = new HashMap<>(); |
| 50 | + } |
| 51 | + |
| 52 | + public LogSource getRootSource() |
| 53 | + { |
| 54 | + return root; |
| 55 | + } |
| 56 | + |
| 57 | + protected void log( final String msg, final int level, final LogSource source ) |
| 58 | + { |
| 59 | + loggingPanel.messageLogged( new LogMessage( source, level, msg ) ); |
| 60 | + } |
| 61 | + |
| 62 | + public void info( final String msg, final LogSource source ) |
| 63 | + { |
| 64 | + log( msg, LogLevel.INFO, source ); |
| 65 | + } |
| 66 | + |
| 67 | + public void info( final String msg ) |
| 68 | + { |
| 69 | + info( msg, unknownSource ); |
| 70 | + } |
| 71 | + |
| 72 | + public void error( final String msg, final LogSource source ) |
| 73 | + { |
| 74 | + log( msg, LogLevel.ERROR, source ); |
| 75 | + } |
| 76 | + |
| 77 | + public void error( final String msg ) |
| 78 | + { |
| 79 | + error( msg, unknownSource ); |
| 80 | + } |
| 81 | + |
| 82 | + public void setStatus( final String status, final LogSource source ) |
| 83 | + { |
| 84 | + final JProgressBar pb = getProgressBar( source ); |
| 85 | + pb.setString( status ); |
| 86 | + } |
| 87 | + |
| 88 | + public void setStatus( final String status ) |
| 89 | + { |
| 90 | + setStatus( status, unknownSource ); |
| 91 | + } |
| 92 | + |
| 93 | + public void setProgress( final double progress, final LogSource source ) |
| 94 | + { |
| 95 | + if ( progress >= 1 ) |
| 96 | + { |
| 97 | + removeProgressBar( source ); |
| 98 | + return; |
| 99 | + } |
| 100 | + final JProgressBar pb = getProgressBar( source ); |
| 101 | + pb.setValue( ( int ) ( 100 * progress ) ); |
| 102 | + } |
| 103 | + |
| 104 | + public void setProgress( final double progress ) |
| 105 | + { |
| 106 | + setProgress( progress, unknownSource ); |
| 107 | + } |
| 108 | + |
| 109 | + private void removeProgressBar( final LogSource source ) |
| 110 | + { |
| 111 | + final JProgressBar pb = progressBars.remove( source ); |
| 112 | + if ( null == pb ) |
| 113 | + return; |
| 114 | + |
| 115 | + panelProgressBars.remove( pb ); |
| 116 | + revalidate(); |
| 117 | + } |
| 118 | + |
| 119 | + private JProgressBar getProgressBar( final LogSource source ) |
| 120 | + { |
| 121 | + JProgressBar pg = progressBars.get( source ); |
| 122 | + if (null == pg) |
| 123 | + { |
| 124 | + pg = new JProgressBar( 0, 100 ); |
| 125 | + pg.setStringPainted( true ); |
| 126 | + panelProgressBars.add( pg ); |
| 127 | + revalidate(); |
| 128 | + progressBars.put( source, pg ); |
| 129 | + } |
| 130 | + return pg; |
| 131 | + } |
| 132 | +} |
0 commit comments