001package apps.jmrit.log;
002
003import java.util.ArrayList;
004import java.util.List;
005import javax.swing.BoxLayout;
006import javax.swing.JScrollBar;
007import javax.swing.JScrollPane;
008import javax.swing.JTextArea;
009import org.apache.log4j.Logger;
010import org.apache.log4j.spi.LoggerRepository;
011
012/**
013 * Show the current Log4J Logger tree; not dynamic.
014 *
015 * @author Bob Jacobsen Copyright 2010
016 * @since 2.9.4
017 */
018public class Log4JTreePane extends jmri.util.swing.JmriPanel {
019
020    /**
021     * Provide a recommended title for an enclosing frame.
022     */
023    @Override
024    public String getTitle() {
025        return Bundle.getMessage("MenuItemLogTreeAction");
026    }
027
028    /**
029     * Provide menu items
030     */
031    //public List<JMenu> getMenus() { return null; }
032    public Log4JTreePane() {
033        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
034    }
035
036    /**
037     * 2nd stage of initialization, invoked after the constructor is complete.
038     */
039    @SuppressWarnings("unchecked")
040    @Override
041    public void initComponents() {
042        LoggerRepository repo = Logger.getRootLogger().getLoggerRepository();
043
044        List<String> list = new ArrayList<String>();
045        for (java.util.Enumeration<Logger> e = repo.getCurrentLoggers(); e.hasMoreElements();) {
046            Logger l = e.nextElement();
047            list.add(l.getName() + " - "
048                    + (l.getLevel() != null
049                            ? "[" + l.getLevel().toString() + "]"
050                            : "{" + Logger.getRootLogger().getLevel().toString() + "}"));
051        }
052        java.util.Collections.sort(list);
053        StringBuilder result = new StringBuilder();
054        for (String s : list) {
055            result.append(s).append("\n");
056        }
057
058        JTextArea text = new JTextArea();
059        text.setText(result.toString());
060        JScrollPane scroll = new JScrollPane(text);
061        add(scroll);
062
063        // start scrolled to top
064        text.setCaretPosition(0);
065        JScrollBar b = scroll.getVerticalScrollBar();
066        b.setValue(b.getMaximum());
067    }
068
069    /**
070     * 3rd stage of initialization, invoked after Swing components exist.
071     */
072    @Override
073    public void initContext(Object context) {
074    }
075
076    @Override
077    public void dispose() {
078    }
079
080}