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