whitespace fixups
[jSite2.git] / src / net / pterodactylus / jsite / gui / LogWindow.java
1 /*
2  * jSite2 - LoggingWindow.java -
3  * Copyright © 2008 David Roden
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 package net.pterodactylus.jsite.gui;
21
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;
32
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;
44
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;
51
52 /**
53  * Frame that shows log messages as they occur.
54  *
55  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
56  */
57 public class LogWindow extends JFrame implements LoggingListener, ActionListener {
58
59         /** The logger. */
60         private static final Logger logger = Logging.getLogger(LogWindow.class.getName());
61
62         /** The “log level” label. */
63         private I18nLabel logLevelLabel;
64
65         /** The “log level” combo box. */
66         private JComboBox logLevelComboBox;
67
68         /** The log table. */
69         private JTable logTable;
70
71         /** The table model for the log table. */
72         private LogRecordTableModel logTableModel;
73
74         /**
75          * Creates a new log window.
76          */
77         public LogWindow() {
78                 super(I18n.get("logWindow.title") + " – jSite " + Version.getVersion());
79                 initActions();
80                 initComponents();
81                 pack();
82                 SwingUtils.center(this);
83         }
84
85         //
86         // PRIVATE METHODS
87         //
88
89         /**
90          * Initializes all actions used in this window.
91          */
92         private void initActions() {
93                 /* TODO */
94         }
95
96         /**
97          * Initializes all components in this window.
98          */
99         private void initComponents() {
100                 JPanel contentPane = new JPanel(new BorderLayout(12, 12));
101                 contentPane.setBorder(BorderFactory.createEmptyBorder(12, 12, 12, 12));
102
103                 JPanel logLevelPanel = new JPanel(new BorderLayout(12, 12));
104                 contentPane.add(logLevelPanel, BorderLayout.PAGE_START);
105
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);
109
110                 logLevelLabel = new I18nLabel("logWindow.label.logLevel", logLevelComboBox);
111                 logLevelPanel.add(logLevelLabel, BorderLayout.LINE_START);
112
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);
124
125                 setContentPane(contentPane);
126         }
127
128         //
129         // INTERFACE LoggingListener
130         //
131
132         /**
133          * {@inheritDoc}
134          */
135         public void logged(LogRecord logRecord) {
136                 logTableModel.addLogRecord(logRecord);
137         }
138
139         //
140         // INTERFACE ActionListener
141         //
142
143         /**
144          * {@inheritDoc}
145          */
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() + "”…");
150                 }
151         }
152
153         /**
154          * Table model for the log message table.
155          *
156          * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
157          */
158         private class LogRecordTableModel extends AbstractTableModel {
159
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") };
162
163                 /** The column classes. */
164                 private final Class<?>[] COLUMN_CLASSES = new Class<?>[] { Date.class, Level.class, String.class };
165
166                 /** The log records. */
167                 private List<LogRecord> logRecords = Collections.synchronizedList(new ArrayList<LogRecord>());
168
169                 /**
170                  * Empty constructor.
171                  */
172                 LogRecordTableModel() {
173                         /* do nothing. */
174                 }
175
176                 /**
177                  * Adds a log record.
178                  *
179                  * @param logRecord
180                  *            The log record to add
181                  */
182                 @SuppressWarnings("synthetic-access")
183                 public synchronized void addLogRecord(LogRecord logRecord) {
184                         logRecords.add(logRecord);
185                         fireTableRowsInserted(logRecords.size() - 1, logRecords.size() - 1);
186                 }
187
188                 /**
189                  * {@inheritDoc}
190                  */
191                 public int getColumnCount() {
192                         return 3;
193                 }
194
195                 /**
196                  * {@inheritDoc}
197                  */
198                 @Override
199                 public String getColumnName(int column) {
200                         return COLUMN_NAMES[column];
201                 }
202
203                 /**
204                  * {@inheritDoc}
205                  */
206                 @Override
207                 public Class<?> getColumnClass(int columnIndex) {
208                         return COLUMN_CLASSES[columnIndex];
209                 }
210
211                 /**
212                  * {@inheritDoc}
213                  */
214                 public int getRowCount() {
215                         return logRecords.size();
216                 }
217
218                 /**
219                  * {@inheritDoc}
220                  */
221                 public Object getValueAt(int rowIndex, int columnIndex) {
222                         if (rowIndex >= logRecords.size()) {
223                                 return null;
224                         }
225                         LogRecord logRecord = logRecords.get(rowIndex);
226                         switch (columnIndex) {
227                         case 0:
228                                 return logRecord.getMillis();
229                         case 1:
230                                 return logRecord.getLevel();
231                         case 2:
232                                 return logRecord.getMessage();
233                         }
234                         return null;
235                 }
236
237         }
238
239 }