📄 loglistenerfactory.java
字号:
/* * jPOS Project [http://jpos.org] * Copyright (C) 2000-2008 Alejandro P. Revilla * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */package org.jpos.ui.factory;import java.awt.Font;import java.io.ByteArrayOutputStream;import java.io.PrintStream;import javax.swing.JComponent;import javax.swing.JTextArea;import javax.swing.SwingUtilities;import javax.swing.text.BadLocationException;import org.jdom.Element;import org.jpos.ui.UI;import org.jpos.ui.UIFactory;import org.jpos.util.LogEvent;import org.jpos.util.LogListener;import org.jpos.util.Logger;import org.jpos.util.NameRegistrar;/** * @author Alejandro Revilla * * a log listener component * * i.e: * <pre> * <log-listener scrollable="true" * logger="Q2" font="fixed-normal-12" * max-events="100" max-lines="50" /> * </pre> * * @see org.jpos.ui.UIFactory */public class LogListenerFactory implements UIFactory { public JComponent create (UI ui, Element e) { JTextArea textArea = new JTextArea (25, 80); String font = e.getAttributeValue ("font"); if (font != null) textArea.setFont (Font.decode (font)); String logId = e.getAttributeValue ("logger", "Q2"); try { int maxEvents = Integer.parseInt ( e.getAttributeValue ("max-events", "100") ); int maxLines = Integer.parseInt ( e.getAttributeValue ("max-lines", "1000") ); Logger logger = (Logger) NameRegistrar.get ("logger." + logId); logger.addListener ( new Listener (logger, ui, textArea, maxEvents, maxLines) ); } catch (NameRegistrar.NotFoundException ex) { textArea.setText (ex.toString ()); } return textArea; } public class Listener implements LogListener, Runnable { final Logger logger; JTextArea text; UI ui; int cnt; int maxEvents; int maxLines; public Listener (Logger logger, UI ui, JTextArea text, int maxEvents, int maxLines) { super (); this.ui = ui; this.text = text; this.logger = logger; this.cnt = 0; this.maxEvents = maxEvents; this.maxLines = maxLines; } public void run () { if (ui.isDestroyed ()) { logger.removeListener (this); text.setText (""); return; } int lc = text.getLineCount (); if (lc > maxLines) { try { int offset = text.getLineStartOffset (lc - maxLines); text.getDocument ().remove (0, offset); } catch (BadLocationException ex) { text.setText (ex.toString()); } } } public LogEvent log (LogEvent evt) { if (ui.isDestroyed ()) { SwingUtilities.invokeLater (this); return evt; } ByteArrayOutputStream str = new ByteArrayOutputStream(); PrintStream ps = new PrintStream (str); evt.dump (ps, ""); text.append (str.toString()); if ((++cnt % maxEvents) == 0) { SwingUtilities.invokeLater (this); } return evt; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -