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 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   * An editor Pane that allows a user to Edit a Pattern Layout and preview the output it would
40   * generate with an example LoggingEvent
41   *
42   * @author Paul Smith <psmith@apache.org>
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      //  private final JButton applyButton = new JButton();
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       * @return
82       */
83      private Action createCutAction() {
84          final Action action =
85              new AbstractAction("Cut", ChainsawIcons.ICON_CUT) {
86                  public void actionPerformed(ActionEvent e) {
87                      // TODO Auto-generated method stub
88                  }
89              };
90  
91          action.setEnabled(false);
92  
93          return action;
94      }
95  
96      /**
97       * @return
98       */
99      private Action createCopyAction() {
100         final Action action =
101             new AbstractAction("Copy", ChainsawIcons.ICON_COPY) {
102                 public void actionPerformed(ActionEvent e) {
103                     // TODO Auto-generated method stub
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 //    editorToolbar.add(new JButton(copyAction));
208 //    editorToolbar.add(new JButton(cutAction));
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         //    okCancelToolbar.addSeparator();
218         //    okCancelToolbar.add(applyButton);
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         //    dialog.pack();
248         dialog.setSize(new Dimension(640, 480));
249         dialog.setVisible(true);
250     }
251 }