1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.gui;
21
22 import java.awt.Component;
23 import java.awt.GridBagLayout;
24 import java.io.IOException;
25 import java.io.OutputStream;
26
27 import javax.swing.JCheckBox;
28 import javax.swing.JComboBox;
29 import javax.swing.JPanel;
30 import javax.swing.JTextField;
31
32 import org.apache.log4j.FileAppender;
33 import org.apache.log4j.Level;
34 import org.apache.log4j.Logger;
35 import org.apache.log4j.PatternLayout;
36 import org.apache.log4j.WriterAppender;
37 import org.xnap.XNap;
38 import org.xnap.gui.action.EnableAction;
39 import org.xnap.gui.component.FilePanel;
40 import org.xnap.gui.component.XNapCheckBox;
41 import org.xnap.gui.util.GUIHelper;
42 import org.xnap.gui.util.GridBagHelper;
43 import org.xnap.util.Preferences;
44
45 public class DebugPanel extends JPanel {
46
47
48
49 /***
50 * Buffer 512 byte for debug log.
51 */
52 public static final int BUFFER_SIZE = 512;
53
54
55
56 private static Preferences prefs = Preferences.getInstance();
57
58 private JTextField jtfLayout;
59 private JComboBox jcbLevel;
60 private JCheckBox jcbLogToFile;
61 private FilePanel fpFilename;
62 private JCheckBox jcbLogToConsole;
63 private ConsolePane cpDebug;
64
65 private FileAppender apFile;
66 private WriterAppender apConsole;
67 private PatternLayout layout = new PatternLayout();
68
69 private Level[] levels = {
70 Level.ALL, Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR,
71 };
72
73
74
75 public DebugPanel()
76 {
77 setLayout(new GridBagLayout());
78
79 GridBagHelper.addLabel(this, XNap.tr("Level"));
80 jcbLevel = new JComboBox(levels);
81 GridBagHelper.add(this, jcbLevel, false);
82
83 GridBagHelper.addLabel(this, XNap.tr("Layout"));
84 jtfLayout = new JTextField("[%-15c{1}] [%-20.20t] %m%n");
85 GridBagHelper.add(this, jtfLayout);
86
87 fpFilename = new FilePanel(prefs.getDebugLogFile(), 20);
88 EnableAction acLogToFile
89 = new EnableAction(XNap.tr("Log To File"),
90 new Component[] { fpFilename }, false);
91 jcbLogToFile = new XNapCheckBox(acLogToFile);
92 GridBagHelper.addComponent(this, jcbLogToFile);
93 GridBagHelper.add(this, fpFilename);
94
95 jcbLogToConsole = new JCheckBox(XNap.tr("Log To Console"));
96 GridBagHelper.add(this, jcbLogToConsole);
97
98 cpDebug = new ConsolePane(false);
99 cpDebug.setBorder(GUIHelper.createDefaultBorder(XNap.tr("Debug Console")));
100 GridBagHelper.addPanel(this, cpDebug);
101 }
102
103
104
105 /***
106 * Activates the selected debug settings.
107 */
108 public void apply()
109 {
110 boolean hasChanged
111 = !layout.getConversionPattern().equals(jtfLayout.getText());
112
113 Logger.getRootLogger().setLevel((Level)jcbLevel.getSelectedItem());
114 layout.setConversionPattern(jtfLayout.getText());
115
116 if (jcbLogToFile.isSelected()) {
117 if (apFile == null
118 || apFile.getFile() != fpFilename.getFilename()) {
119 close(apFile);
120
121 try {
122 apFile = new FileAppender(layout, fpFilename.getFilename(),
123 false, true, BUFFER_SIZE);
124 Logger.getRootLogger().addAppender(apFile);
125 }
126 catch (IOException e) {
127 Dialogs.error(this, e.getLocalizedMessage());
128 }
129 }
130 }
131 else if (apFile != null) {
132
133 close(apFile);
134 apFile = null;
135 }
136
137 if (jcbLogToConsole.isSelected()) {
138 if (apConsole == null || hasChanged) {
139 close(apConsole);
140
141 apConsole
142 = new WriterAppender(layout, new ConsoleOutputStream());
143 apConsole.setLayout(layout);
144 Logger.getRootLogger().addAppender(apConsole);
145 }
146 }
147 else if (apConsole != null) {
148
149 close(apConsole);
150 apConsole = null;
151 }
152 }
153
154 private void close(WriterAppender ap)
155 {
156 if (ap != null) {
157 ap.close();
158 Logger.getRootLogger().removeAppender(ap);
159 }
160 }
161
162 /***
163 * Buffered stream for console panel.
164 */
165 private class ConsoleOutputStream extends OutputStream
166 {
167
168 private StringBuffer buffer = new StringBuffer();
169
170 public void flush()
171 {
172 cpDebug.appendLater(buffer.toString());
173 buffer.setLength(0);
174 }
175
176 public void write(int b)
177 {
178 buffer.append((char)b);
179 if (b == '\n') {
180 flush();
181 }
182 }
183
184 }
185
186 }