2 * jSite2 - LoggingWindow.java -
3 * Copyright © 2008 David Roden
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 package net.pterodactylus.jsite.gui;
22 import java.awt.BorderLayout;
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.Date;
28 import java.util.List;
29 import java.util.logging.Level;
30 import java.util.logging.LogRecord;
31 import java.util.logging.Logger;
33 import javax.swing.BorderFactory;
34 import javax.swing.JComboBox;
35 import javax.swing.JFrame;
36 import javax.swing.JPanel;
37 import javax.swing.JScrollPane;
38 import javax.swing.JTable;
39 import javax.swing.ScrollPaneConstants;
40 import javax.swing.table.AbstractTableModel;
41 import javax.swing.table.DefaultTableColumnModel;
42 import javax.swing.table.JTableHeader;
43 import javax.swing.table.TableColumn;
45 import net.pterodactylus.jsite.i18n.I18n;
46 import net.pterodactylus.jsite.i18n.gui.I18nLabel;
47 import net.pterodactylus.jsite.main.Version;
48 import net.pterodactylus.util.logging.Logging;
49 import net.pterodactylus.util.logging.LoggingListener;
50 import net.pterodactylus.util.swing.SwingUtils;
53 * Frame that shows log messages as they occur.
55 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
57 public class LogWindow extends JFrame implements LoggingListener, ActionListener {
60 private static final Logger logger = Logging.getLogger(LogWindow.class.getName());
62 /** The “log level” label. */
63 private I18nLabel logLevelLabel;
65 /** The “log level” combo box. */
66 private JComboBox logLevelComboBox;
69 private JTable logTable;
71 /** The table model for the log table. */
72 private LogRecordTableModel logTableModel;
75 * Creates a new log window.
78 super(I18n.get("logWindow.title") + " – jSite " + Version.getVersion());
82 SwingUtils.center(this);
90 * Initializes all actions used in this window.
92 private void initActions() {
97 * Initializes all components in this window.
99 private void initComponents() {
100 JPanel contentPane = new JPanel(new BorderLayout(12, 12));
101 contentPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
103 JPanel logLevelPanel = new JPanel(new BorderLayout(12, 12));
104 contentPane.add(logLevelPanel, BorderLayout.PAGE_START);
106 logLevelComboBox = new JComboBox(new Object[] { Level.ALL, Level.FINEST, Level.FINER, Level.FINE, Level.INFO, Level.WARNING, Level.SEVERE });
107 logLevelPanel.add(logLevelComboBox, BorderLayout.CENTER);
108 logLevelComboBox.addActionListener(this);
110 logLevelLabel = new I18nLabel("logWindow.label.logLevel", logLevelComboBox);
111 logLevelPanel.add(logLevelLabel, BorderLayout.LINE_START);
113 DefaultTableColumnModel logTableColumnModel = new DefaultTableColumnModel();
114 logTableColumnModel.addColumn(new TableColumn(0));
115 logTableColumnModel.addColumn(new TableColumn(1));
116 logTableColumnModel.addColumn(new TableColumn(2));
117 logTableModel = new LogRecordTableModel();
118 logTable = new JTable(logTableModel);
119 logTable.setTableHeader(new JTableHeader(logTableColumnModel));
120 logTable.setColumnModel(logTableColumnModel);
121 JScrollPane scrollPane = new JScrollPane(logTable);
122 scrollPane.setCorner(ScrollPaneConstants.UPPER_LEADING_CORNER, logTable.getTableHeader());
123 contentPane.add(scrollPane, BorderLayout.CENTER);
125 setContentPane(contentPane);
129 // INTERFACE LoggingListener
135 public void logged(LogRecord logRecord) {
136 logTableModel.addLogRecord(logRecord);
140 // INTERFACE ActionListener
146 public void actionPerformed(ActionEvent actionEvent) {
147 if (actionEvent.getSource() == logLevelComboBox) {
148 Level newLevel = (Level) logLevelComboBox.getSelectedItem();
149 logger.log(Level.INFO, "setting new log level to “" + newLevel.getName() + "”…");
154 * Table model for the log message table.
156 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
158 private class LogRecordTableModel extends AbstractTableModel {
160 /** The column names. */
161 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") };
163 /** The column classes. */
164 private final Class<?>[] COLUMN_CLASSES = new Class<?>[] { Date.class, Level.class, String.class };
166 /** The log records. */
167 private List<LogRecord> logRecords = Collections.synchronizedList(new ArrayList<LogRecord>());
172 LogRecordTableModel() {
180 * The log record to add
182 @SuppressWarnings("synthetic-access")
183 public synchronized void addLogRecord(LogRecord logRecord) {
184 logRecords.add(logRecord);
185 fireTableRowsInserted(logRecords.size() - 1, logRecords.size() - 1);
191 public int getColumnCount() {
199 public String getColumnName(int column) {
200 return COLUMN_NAMES[column];
207 public Class<?> getColumnClass(int columnIndex) {
208 return COLUMN_CLASSES[columnIndex];
214 public int getRowCount() {
215 return logRecords.size();
221 public Object getValueAt(int rowIndex, int columnIndex) {
222 if (rowIndex >= logRecords.size()) {
225 LogRecord logRecord = logRecords.get(rowIndex);
226 switch (columnIndex) {
228 return logRecord.getMillis();
230 return logRecord.getLevel();
232 return logRecord.getMessage();