From: David ‘Bombe’ Roden Date: Thu, 8 May 2008 21:53:03 +0000 (+0000) Subject: add log window X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=3c76acc8cdbc415a08177ce78a087b1b8018d471;p=jSite2.git add log window git-svn-id: http://trooper/svn/projects/jSite/trunk@789 c3eda9e8-030b-0410-8277-bc7414b0a119 --- diff --git a/src/net/pterodactylus/jsite/gui/LogWindow.java b/src/net/pterodactylus/jsite/gui/LogWindow.java new file mode 100644 index 0000000..0cd0a8f --- /dev/null +++ b/src/net/pterodactylus/jsite/gui/LogWindow.java @@ -0,0 +1,241 @@ +/* + * jSite2 - LoggingWindow.java - + * Copyright © 2008 David Roden + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 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 General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package net.pterodactylus.jsite.gui; + +import java.awt.BorderLayout; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +import javax.swing.BorderFactory; +import javax.swing.JComboBox; +import javax.swing.JFrame; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JTable; +import javax.swing.ScrollPaneConstants; +import javax.swing.table.AbstractTableModel; +import javax.swing.table.DefaultTableColumnModel; +import javax.swing.table.JTableHeader; +import javax.swing.table.TableColumn; + +import net.pterodactylus.jsite.i18n.I18n; +import net.pterodactylus.jsite.i18n.gui.I18nLabel; +import net.pterodactylus.jsite.main.Version; +import net.pterodactylus.util.logging.Logging; +import net.pterodactylus.util.logging.LoggingListener; +import net.pterodactylus.util.swing.SwingUtils; + +/** + * Frame that shows log messages as they occur. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + * @version $Id$ + */ +public class LogWindow extends JFrame implements LoggingListener, ActionListener { + + /** The logger. */ + private static final Logger logger = Logging.getLogger(LogWindow.class.getName()); + + /** The “log level” label. */ + private I18nLabel logLevelLabel; + + /** The “log level” combo box. */ + private JComboBox logLevelComboBox; + + /** The log table. */ + private JTable logTable; + + /** The table model for the log table. */ + private LogRecordTableModel logTableModel; + + /** + * Creates a new log window. + */ + public LogWindow() { + super(I18n.get("logWindow.title") + " – jSite " + Version.getVersion()); + initActions(); + initComponents(); + pack(); + SwingUtils.center(this); + } + + // + // PRIVATE METHODS + // + + /** + * Initializes all actions used in this window. + */ + private void initActions() { + /* TODO */ + } + + /** + * Initializes all components in this window. + */ + private void initComponents() { + JPanel contentPane = new JPanel(new BorderLayout(12, 12)); + contentPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12)); + + JPanel logLevelPanel = new JPanel(new BorderLayout(12, 12)); + contentPane.add(logLevelPanel, BorderLayout.PAGE_START); + + logLevelComboBox = new JComboBox(new Object[] { Level.ALL, Level.FINEST, Level.FINER, Level.FINE, Level.INFO, Level.WARNING, Level.SEVERE }); + logLevelPanel.add(logLevelComboBox, BorderLayout.CENTER); + logLevelComboBox.addActionListener(this); + + logLevelLabel = new I18nLabel("logWindow.label.logLevel", logLevelComboBox); + logLevelPanel.add(logLevelLabel, BorderLayout.LINE_START); + + DefaultTableColumnModel logTableColumnModel = new DefaultTableColumnModel(); + logTableColumnModel.addColumn(new TableColumn(0)); + logTableColumnModel.addColumn(new TableColumn(1)); + logTableColumnModel.addColumn(new TableColumn(2)); + logTableModel = new LogRecordTableModel(); + logTable = new JTable(logTableModel); + logTable.setTableHeader(new JTableHeader(logTableColumnModel)); + logTable.setColumnModel(logTableColumnModel); + JScrollPane scrollPane = new JScrollPane(logTable); + scrollPane.setCorner(ScrollPaneConstants.UPPER_LEADING_CORNER, logTable.getTableHeader()); + contentPane.add(scrollPane, BorderLayout.CENTER); + + setContentPane(contentPane); + } + + // + // INTERFACE LoggingListener + // + + /** + * {@inheritDoc} + */ + public void logged(LogRecord logRecord) { + logTableModel.addLogRecord(logRecord); + } + + // + // INTERFACE ActionListener + // + + /** + * {@inheritDoc} + */ + public void actionPerformed(ActionEvent actionEvent) { + if (actionEvent.getSource() == logLevelComboBox) { + Level newLevel = (Level) logLevelComboBox.getSelectedItem(); + logger.log(Level.INFO, "setting new log level to “" + newLevel.getName() + "”…"); + } + } + + /** + * Table model for the log message table. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + * @version $Id$ + */ + private class LogRecordTableModel extends AbstractTableModel { + + /** The column names. */ + private final String[] COLUMN_NAMES = new String[] { I18n.get("logWindow.table.column.time.name"), I18n.get("logWindow.table.column.level.name"), I18n.get("logWindow.table.column.message.name") }; + + /** The column classes. */ + private final Class[] COLUMN_CLASSES = new Class[] { Date.class, Level.class, String.class }; + + /** The log records. */ + private List logRecords = Collections.synchronizedList(new ArrayList()); + + /** + * Empty constructor. + */ + LogRecordTableModel() { + /* do nothing. */ + } + + /** + * Adds a log record. + * + * @param logRecord + * The log record to add + */ + @SuppressWarnings("synthetic-access") + public synchronized void addLogRecord(LogRecord logRecord) { + logRecords.add(logRecord); + fireTableRowsInserted(logRecords.size() - 1, logRecords.size() - 1); + } + + /** + * {@inheritDoc} + */ + public int getColumnCount() { + return 3; + } + + /** + * {@inheritDoc} + */ + @Override + public String getColumnName(int column) { + return COLUMN_NAMES[column]; + } + + /** + * {@inheritDoc} + */ + @Override + public Class getColumnClass(int columnIndex) { + return COLUMN_CLASSES[columnIndex]; + } + + /** + * {@inheritDoc} + */ + public int getRowCount() { + return logRecords.size(); + } + + /** + * {@inheritDoc} + */ + public Object getValueAt(int rowIndex, int columnIndex) { + if (rowIndex >= logRecords.size()) { + return null; + } + LogRecord logRecord = logRecords.get(rowIndex); + switch (columnIndex) { + case 0: + return logRecord.getMillis(); + case 1: + return logRecord.getLevel(); + case 2: + return logRecord.getMessage(); + } + return null; + } + + } + +}