1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.chainsaw.layout;
19
20 import org.apache.log4j.Logger;
21 import org.apache.log4j.chainsaw.ChainsawConstants;
22 import org.apache.log4j.chainsaw.JTextComponentFormatter;
23 import org.apache.log4j.chainsaw.icons.ChainsawIcons;
24 import org.apache.log4j.spi.LocationInfo;
25 import org.apache.log4j.spi.LoggingEvent;
26 import org.apache.log4j.spi.ThrowableInformation;
27
28 import javax.swing.*;
29 import javax.swing.event.DocumentEvent;
30 import javax.swing.event.DocumentListener;
31 import java.awt.*;
32 import java.awt.event.ActionEvent;
33 import java.awt.event.ActionListener;
34 import java.util.Date;
35 import java.util.Hashtable;
36
37
38
39
40
41
42
43
44 public final class LayoutEditorPane extends JPanel {
45 private final Action copyAction;
46 private final Action cutAction;
47 private final JToolBar editorToolbar = new JToolBar();
48 private final JToolBar okCancelToolbar = new JToolBar();
49 private final JButton okButton = new JButton(" OK ");
50 private final JButton cancelButton = new JButton(" Cancel ");
51
52
53 private final JEditorPane patternEditor = new JEditorPane("text/plain", "");
54 private final JEditorPane previewer =
55 new JEditorPane(ChainsawConstants.DETAIL_CONTENT_TYPE, "");
56 private final JScrollPane patternEditorScroll =
57 new JScrollPane(
58 ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
59 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
60 private final JScrollPane previewEditorScroll =
61 new JScrollPane(
62 ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
63 ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
64 private LoggingEvent event;
65 private EventDetailLayout layout = new EventDetailLayout();
66
67
68
69
70 public LayoutEditorPane() {
71 super();
72 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
73 createEvent();
74 copyAction = createCopyAction();
75 cutAction = createCutAction();
76 initComponents();
77 setupListeners();
78 }
79
80
81
82
83 private Action createCutAction() {
84 final Action action =
85 new AbstractAction("Cut", ChainsawIcons.ICON_CUT) {
86 public void actionPerformed(ActionEvent e) {
87
88 }
89 };
90
91 action.setEnabled(false);
92
93 return action;
94 }
95
96
97
98
99 private Action createCopyAction() {
100 final Action action =
101 new AbstractAction("Copy", ChainsawIcons.ICON_COPY) {
102 public void actionPerformed(ActionEvent e) {
103
104 }
105 };
106
107 action.setEnabled(false);
108
109 return action;
110 }
111
112
113
114
115 private void setupListeners() {
116 patternEditor.getDocument().addDocumentListener(
117 new DocumentListener() {
118 public void changedUpdate(DocumentEvent e) {
119 updatePreview();
120 }
121
122 public void insertUpdate(DocumentEvent e) {
123 updatePreview();
124 }
125
126 public void removeUpdate(DocumentEvent e) {
127 updatePreview();
128 }
129 });
130
131 patternEditor.addCaretListener(
132 e -> updateTextActions(e.getMark() != e.getDot()));
133 }
134
135 private void updatePreview() {
136 String pattern = patternEditor.getText();
137 layout.setConversionPattern(pattern);
138
139 previewer.setText(layout.format(event));
140 }
141
142
143
144
145 private void updateTextActions(boolean enabled) {
146 cutAction.setEnabled(enabled);
147 copyAction.setEnabled(enabled);
148 }
149
150
151
152
153 private void createEvent() {
154 Hashtable<String, String> hashTable = new Hashtable<>();
155 hashTable.put("key1", "val1");
156 hashTable.put("key2", "val2");
157 hashTable.put("key3", "val3");
158
159 LocationInfo li =
160 new LocationInfo(
161 "myfile.java", "com.mycompany.util.MyClass", "myMethod", "321");
162
163 ThrowableInformation tsr = new ThrowableInformation(new Exception());
164
165 event = new LoggingEvent("org.apache.log4j.Logger",
166 Logger.getLogger("com.mycompany.mylogger"),
167 new Date().getTime(),
168 org.apache.log4j.Level.DEBUG,
169 "The quick brown fox jumped over the lazy dog",
170 "Thread-1",
171 tsr,
172 "NDC string",
173 li,
174 hashTable);
175
176 }
177
178
179
180
181 private void initComponents() {
182 editorToolbar.setFloatable(false);
183 okCancelToolbar.setFloatable(false);
184 okButton.setToolTipText("Accepts the current Pattern layout and will apply it to the Log Panel");
185 cancelButton.setToolTipText("Closes this dialog and discards your changes");
186
187 JTextComponentFormatter.applySystemFontAndSize(previewer);
188
189 previewer.setEditable(false);
190 patternEditor.setPreferredSize(new Dimension(240, 240));
191 patternEditor.setMaximumSize(new Dimension(320, 240));
192 previewer.setPreferredSize(new Dimension(360, 240));
193 patternEditorScroll.setViewportView(patternEditor);
194 previewEditorScroll.setViewportView(previewer);
195
196 patternEditor.setToolTipText("Edit the Pattern here");
197 previewer.setToolTipText(
198 "The result of the layout of the pattern is shown here");
199
200 patternEditorScroll.setBorder(
201 BorderFactory.createTitledBorder(
202 BorderFactory.createEtchedBorder(), "Pattern Editor"));
203 previewEditorScroll.setBorder(
204 BorderFactory.createTitledBorder(
205 BorderFactory.createEtchedBorder(), "Pattern Preview"));
206
207
208
209
210 editorToolbar.add(Box.createHorizontalGlue());
211
212 okCancelToolbar.add(Box.createHorizontalGlue());
213 okCancelToolbar.add(okButton);
214 okCancelToolbar.addSeparator();
215 okCancelToolbar.add(cancelButton);
216
217
218
219 add(editorToolbar);
220 add(patternEditorScroll);
221 add(previewEditorScroll);
222 add(okCancelToolbar);
223 }
224
225 public void setConversionPattern(String pattern) {
226 patternEditor.setText(pattern);
227 }
228
229 public String getConversionPattern() {
230 return patternEditor.getText();
231 }
232
233 public void addOkActionListener(ActionListener l) {
234 okButton.addActionListener(l);
235 }
236
237 public void addCancelActionListener(ActionListener l) {
238 cancelButton.addActionListener(l);
239 }
240
241 public static void main(String[] args) {
242 JDialog dialog = new JDialog((Frame) null, "Pattern Editor");
243 dialog.getContentPane().add(new LayoutEditorPane());
244 dialog.setResizable(true);
245 dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
246
247
248 dialog.setSize(new Dimension(640, 480));
249 dialog.setVisible(true);
250 }
251 }