View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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.JTextComponentFormatter;
48  import org.apache.log4j.chainsaw.icons.ChainsawIcons;
49  import org.apache.log4j.spi.LocationInfo;
50  import org.apache.log4j.spi.LoggingEvent;
51  import org.apache.log4j.spi.ThrowableInformation;
52  
53  
54  /**
55   * An editor Pane that allows a user to Edit a Pattern Layout and preview the output it would
56   * generate with an example LoggingEvent
57   * 
58   * @author Paul Smith <psmith@apache.org>
59   *
60   */
61  public final class LayoutEditorPane extends JPanel {
62    private final Action copyAction;
63    private final Action cutAction;
64    private final JToolBar editorToolbar = new JToolBar();
65    private final JToolBar okCancelToolbar = new JToolBar();
66    private final JButton okButton = new JButton(" OK ");
67    private final JButton cancelButton = new JButton(" Cancel ");
68  
69    //  private final JButton applyButton = new JButton();
70    private final JEditorPane patternEditor = new JEditorPane("text/plain", "");
71    private final JEditorPane previewer =
72      new JEditorPane(ChainsawConstants.DETAIL_CONTENT_TYPE, "");
73    private final JScrollPane patternEditorScroll =
74      new JScrollPane(
75        ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
76        ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
77    private final JScrollPane previewEditorScroll =
78      new JScrollPane(
79      		ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
80      		ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
81    private LoggingEvent event;
82    private EventDetailLayout layout = new EventDetailLayout();
83  
84    /**
85     *
86     */
87    public LayoutEditorPane() {
88      super();
89      setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
90      createEvent();
91      copyAction = createCopyAction();
92      cutAction = createCutAction();
93      initComponents();
94      setupListeners();
95    }
96  
97    /**
98    * @return
99    */
100   private Action createCutAction() {
101     final Action action =
102       new AbstractAction("Cut", ChainsawIcons.ICON_CUT) {
103         public void actionPerformed(ActionEvent e) {
104           // TODO Auto-generated method stub
105         }
106       };
107 
108     action.setEnabled(false);
109 
110     return action;
111   }
112 
113   /**
114    * @return
115    */
116   private Action createCopyAction() {
117     final Action action =
118       new AbstractAction("Copy", ChainsawIcons.ICON_COPY) {
119         public void actionPerformed(ActionEvent e) {
120           // TODO Auto-generated method stub
121         }
122       };
123 
124     action.setEnabled(false);
125 
126     return action;
127   }
128 
129   /**
130     *
131     */
132   private void setupListeners() {
133     patternEditor.getDocument().addDocumentListener(
134       new DocumentListener() {
135         public void changedUpdate(DocumentEvent e) {
136           updatePreview();
137         }
138 
139         public void insertUpdate(DocumentEvent e) {
140           updatePreview();
141         }
142 
143         public void removeUpdate(DocumentEvent e) {
144           updatePreview();
145         }
146       });
147 
148     patternEditor.addCaretListener(
149       new CaretListener() {
150         public void caretUpdate(CaretEvent e) {
151           updateTextActions(e.getMark() != e.getDot());
152         }
153       });
154   }
155 
156   private void updatePreview() {
157     String pattern = patternEditor.getText();
158     layout.setConversionPattern(pattern);
159 
160     previewer.setText(layout.format(event));
161   }
162 
163   /**
164   *
165   */
166   private void updateTextActions(boolean enabled) {
167     cutAction.setEnabled(enabled);
168     copyAction.setEnabled(enabled);
169   }
170 
171   /**
172       *
173       */
174   private void createEvent() {
175     Hashtable hashTable = new Hashtable();
176     hashTable.put("key1", "val1");
177     hashTable.put("key2", "val2");
178     hashTable.put("key3", "val3");
179 
180     LocationInfo li =
181       new LocationInfo(
182         "myfile.java", "com.mycompany.util.MyClass", "myMethod", "321");
183 
184     ThrowableInformation tsr = new ThrowableInformation(new Exception());
185 
186     event = new LoggingEvent("org.apache.log4j.Logger",
187 	    Logger.getLogger("com.mycompany.mylogger"),
188 		new Date().getTime(),
189 		org.apache.log4j.Level.DEBUG,
190 		"The quick brown fox jumped over the lazy dog",
191 		"Thread-1",
192 		tsr,
193 		"NDC string",
194 		li,
195 		hashTable);
196     
197   }
198 
199   /**
200      *
201      */
202   private void initComponents() {
203     editorToolbar.setFloatable(false);
204     okCancelToolbar.setFloatable(false);
205     okButton.setToolTipText("Accepts the current Pattern layout and will apply it to the Log Panel");
206     cancelButton.setToolTipText("Closes this dialog and discards your changes");
207 
208     JTextComponentFormatter.applySystemFontAndSize(previewer);
209       
210     previewer.setEditable(false);
211     patternEditor.setPreferredSize(new Dimension(240, 240));
212     patternEditor.setMaximumSize(new Dimension(320, 240));
213     previewer.setPreferredSize(new Dimension(360, 240));
214     patternEditorScroll.setViewportView(patternEditor);
215     previewEditorScroll.setViewportView(previewer);
216 
217     patternEditor.setToolTipText("Edit the Pattern here");
218     previewer.setToolTipText(
219       "The result of the layout of the pattern is shown here");
220 
221     patternEditorScroll.setBorder(
222       BorderFactory.createTitledBorder(
223         BorderFactory.createEtchedBorder(), "Pattern Editor"));
224     previewEditorScroll.setBorder(
225       BorderFactory.createTitledBorder(
226         BorderFactory.createEtchedBorder(), "Pattern Preview"));
227 
228 //    editorToolbar.add(new JButton(copyAction));
229 //    editorToolbar.add(new JButton(cutAction));
230 
231     editorToolbar.add(Box.createHorizontalGlue());
232 
233     okCancelToolbar.add(Box.createHorizontalGlue());
234     okCancelToolbar.add(okButton);
235     okCancelToolbar.addSeparator();
236     okCancelToolbar.add(cancelButton);
237 
238     //    okCancelToolbar.addSeparator();
239     //    okCancelToolbar.add(applyButton);
240     add(editorToolbar);
241     add(patternEditorScroll);
242     add(previewEditorScroll);
243     add(okCancelToolbar);
244   }
245 
246   public void setConversionPattern(String pattern) {
247     patternEditor.setText(pattern);
248   }
249 
250   public String getConversionPattern() {
251     return patternEditor.getText();
252   }
253 
254   public void addOkActionListener(ActionListener l) {
255     okButton.addActionListener(l);
256   }
257 
258   public void addCancelActionListener(ActionListener l) {
259     cancelButton.addActionListener(l);
260   }
261 
262   public static void main(String[] args) {
263     JDialog dialog = new JDialog((Frame) null, "Pattern Editor");
264     dialog.getContentPane().add(new LayoutEditorPane());
265     dialog.setResizable(true);
266     dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
267 
268     //    dialog.pack();
269     dialog.setSize(new Dimension(640, 480));
270     dialog.setVisible(true);
271   }
272 }